Esempio n. 1
0
 // return prime num
 int init(int n){
     size=n;
     prime_table();
     return tot;
 }
int main ( void )

/******************************************************************************/
/*
  Purpose:

    MAIN is the main program for MULTITASK_OPENMP.

  Discussion:

    This program demonstrates how OpenMP can be used for multitasking, that 
    is, a simple kind of parallel processing in which a certain number of 
    perhaps quite unrelated tasks must be done.

    The OpenMP SECTIONS directive identifies the portion of the program where
    the code for these tasks is given.

    The OpenMP SECTION directive is used repeatedly to divide this area of
    the program into independent tasks.

    The code will get the benefit of parallel processing up to the point where
    there are as many threads as there are tasks.

    The code will get a substantial speedup if the tasks take roughly the
    same amount of time.  However, if one task takes substantially more time
    than the others, this results in a limit to the parallel speedup that is
    possible.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    19 October 2011

  Author:

    John Burkardt

*/
{
  int prime_num;
  int *primes;
  int sine_num;
  double *sines;
  double wtime;
  double wtime1;
  double wtime2;

  timestamp ( );
  printf ( "\n" );
  printf ( "MULTITASK_OPENMP:\n" );
  printf ( "  C/OpenMP version\n" );
  printf ( "  Demonstrate how OpenMP can \"multitask\" by using the\n" );
  printf ( "  SECTIONS directive to carry out several tasks in parallel.\n" );

  prime_num = 20000;
  sine_num = 20000;

  wtime = omp_get_wtime ( );

# pragma omp parallel shared ( prime_num, primes, sine_num, sines )
{
  # pragma omp sections
  {
    # pragma omp section
    {
      wtime1 = omp_get_wtime ( );
      primes = prime_table ( prime_num );
      wtime1 = omp_get_wtime ( ) - wtime1;
    }
    # pragma omp section
    {
      wtime2 = omp_get_wtime ( );
      sines = sine_table ( sine_num );
      wtime2 = omp_get_wtime ( ) - wtime2;
    }
  }
}
  wtime = omp_get_wtime ( ) - wtime;

  printf ( "\n" );
  printf ( "  Number of primes computed was %d\n", prime_num );
  printf ( "  Last prime was %d\n", primes[prime_num-1] );
  printf ( "  Number of sines computed was %d\n", sine_num );
  printf ( "  Last sine computed was %g\n", sines[sine_num-1] );
  printf ( "\n" );
  printf ( "  Elapsed time = %g\n", wtime );
  printf ( "  Task 1 time = %g\n", wtime1 );
  printf ( "  Task 2 time = %g\n", wtime2 );

  free ( primes );
  free ( sines );
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "MULTITASK_OPENMP:\n" );
  printf ( "  Normal end of execution.\n" );
  printf ( "\n" );
  timestamp ( );

  return 0;
}