int main(int argc, char **argv)
{

	int i, n=200, chunk, a[n], suma=0;
	omp_sched_t schedule_type;
	int chunk_value;

	if(argc < 3)
	{
		fprintf(stderr,"\nFalta iteraciones o chunk \n");
		exit(-1);
	}

	n = atoi(argv[1]);
	if (n>200)
		n=200;

	chunk = atoi(argv[2]);

	for (i=0; i<n; i++)
		a[i] = i;

	#pragma omp parallel for firstprivate(suma) lastprivate(suma) \
			schedule(dynamic,chunk)
	for (i=0; i<n; i++)
	{
		suma = suma + a[i];
		printf(" thread %d suma a[%d]=%d suma=%d \n", omp_get_thread_num(),i,a[i],suma);

        if(omp_get_thread_num() == 0)
        {
            printf(" Dentro de 'parallel for':\n");

            printf("   static = 1, dynamic = 2, guided = 3, auto = 4\n");
            omp_get_schedule(&schedule_type, &chunk_value);
            printf("   dyn-var: %d, nthreads-var:%d, thread-limit-var:%d,run-sched-var: %d, chunk: %d\n", \
            		 omp_get_dynamic(), \
            		omp_get_max_threads(), omp_get_thread_limit(), \
            		schedule_type, chunk_value);

            printf(" get_num_threads: %d,get_num_procs: %d,in_parallel():%d \n", \
            		omp_get_num_threads(),omp_get_num_procs(),omp_in_parallel());

        }
	}

	printf("Fuera de 'parallel for' suma=%d\n",suma);

	printf("   static = 1, dynamic = 2, guided = 3, auto = 4\n");
	omp_get_schedule(&schedule_type, &chunk_value);
	printf("   dyn-var: %d, nthreads-var:%d, thread-limit-var:%d,run-sched-var: %d, chunk: %d\n" \
				, omp_get_dynamic(), \
			omp_get_max_threads(), omp_get_thread_limit(), \
			schedule_type, chunk_value);

	printf(" get_num_threads: %d,get_num_procs: %d,in_parallel():%d \n", \
			omp_get_num_threads(),omp_get_num_procs(),omp_in_parallel());
}
Exemple #2
0
// [[Rcpp::export]]
int getOmpThreads() {
  int threads = 0;
#ifdef _OPENMP
  omp_sched_t sched;
  omp_get_schedule(&sched, &threads);
#endif
  return threads;
}
Exemple #3
0
void
omp_get_schedule_8_ (int32_t *kind, int64_t *chunk_size)
{
  omp_sched_t k;
  int cs;
  omp_get_schedule (&k, &cs);
  *kind = k;
  *chunk_size = cs;
}
Exemple #4
0
void
omp_get_schedule_8_ (int32_t *kind, int64_t *modifier)
{
  omp_sched_t k;
  int m;
  omp_get_schedule (&k, &m);
  *kind = k;
  *modifier = m;
}
Exemple #5
0
int main (int argc, char *argv[]) {
    
    int nthreads, tid, i, chunk;
    float a[N], b[N], c[N];
    double inicio, fin;
    omp_sched_t kind;
    int modifier;
    
    //Esta llamada no realiza los cambios enviados, se tiene que realizar antes de ejecutar el ejecutable
    //system("export OMP_SCHEDULE=dynamic"); 
    //sleep(2);

    
    for (i=0; i < N; i++)
        a[i] = b[i] = i * 1.0;
    
    inicio = omp_get_wtime();
    #pragma omp parallel shared(a,b,c,nthreads,kind,modifier) private(i,tid)
    {
        tid = omp_get_thread_num();
        if (tid == 0){
            nthreads = omp_get_num_threads();
            printf("Número de threads = %d\n", nthreads);
            omp_get_schedule (&kind, &modifier);
            
            if (kind == omp_sched_static) printf("Sched estático\n");
            else if (kind == omp_sched_guided) printf("Sched guiado\n");
            else if (kind == omp_sched_dynamic) printf("Sched dinámico\n");
            else if (kind == omp_sched_auto) printf("Sched automático\n");
        }
        printf("Thread %d empezando...\n",tid);
    
        #pragma omp for 
            for (i=0; i<N; i++){
                c[i] = a[i] + b[i];
                //printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
            }
    
    }  
    fin = omp_get_wtime();
    printf("Tiempo = %f\n", fin-inicio);
    
}
int main ( int argc, char *argv[] )

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

    MAIN is the main program for DOT_PRODUCT.

  Discussion:

    This program illustrates how a vector dot product could be set up
    in a C program using OpenMP.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    18 April 2009

  Author:

    John Burkardt
*/
{
  double factor;
  int i;
  int n;
  double wtime;
  double *x;
  double xdoty;
  double *y;

  omp_sched_t myschedule;
  char schedstring[20];
  int mychunk;

  printf ( "\n" );
  printf ( "DOT_PRODUCT\n" );
  printf ( "  C/OpenMP version\n" );
  printf ( "\n" );
  printf ( "  A program which computes a vector dot product.\n" );

  printf ( "\n" );
  printf ( "  Number of processors available = %d\n", omp_get_num_procs ( ) );
  printf ( "  Number of threads =              %d\n", omp_get_max_threads ( ) );

  omp_get_schedule ( &myschedule, &mychunk );
  switch (myschedule) {
    case omp_sched_static:
                 strcpy(schedstring, "Static");
                 break;
    case omp_sched_dynamic:
                 strcpy(schedstring, "Dynamic");
                 break;
    case omp_sched_guided:
                 strcpy(schedstring, "Guided");
                 break;
    case omp_sched_auto:
                 strcpy(schedstring, "Auto");
                 break;
  }
  printf ( "  Scheduling Policy =              %s\n", schedstring );
  printf ( "  Chunk Size =                     %d\n", mychunk );

/*
  Set up the vector data.
  N may be increased to get better timing data.

  The value FACTOR is chosen so that the correct value of the dot product 
  of X and Y is N.
*/
  n = 100000000;


//  while ( n < 1000000 )
  {
    n = n * 10;

    x = ( double * ) malloc ( n * sizeof ( double ) );
    y = ( double * ) malloc ( n * sizeof ( double ) );

    factor = ( double ) ( n );
    factor = 1.0 / sqrt ( 2.0 * factor * factor + 3 * factor + 1.0 );

// Chandan: Commenting initialization code
/*
    for ( i = 0; i < n; i++ )
    {
      x[i] = ( i + 1 ) * factor;
    }

    for ( i = 0; i < n; i++ )
    {
      y[i] = ( i + 1 ) * 6 * factor;
    }
*/
    printf ( "\n" );
/*
  Test #1 Sequential Code
*/
/* Chandan commented this block.
    wtime = omp_get_wtime ( );

    xdoty = test01 ( n, x, y );

    wtime = omp_get_wtime ( ) - wtime;

    printf ( "  Sequential  %8d  %14.6e  %15.10f\n", n, xdoty, wtime );
*/
/*
  Test #2
*/
    wtime = omp_get_wtime ( );

    xdoty = test02 ( n, x, y );

    wtime = omp_get_wtime ( ) - wtime;

    printf ( "  Parallel    %8d  %14.6e  %15.10f\n", n, xdoty, wtime );
  
    free ( x );
    free ( y );
  }
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "DOT_PRODUCT\n" );
  printf ( "  Normal end of execution.\n" );

  return 0;
}
Exemple #7
0
int main ()
{
  int a[N], aa[N];
  int b[N], bb[N];
  int c[N], cc[N];
  int d[N], dd[N];
  int e[N], ee[N];
  int i, errors;
  int cond = 0;

  check_offloading();

  // Test: task within target

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    int id = omp_get_thread_num();
    a[id]++;
    #if TRY_TASK
      #pragma omp task firstprivate(id) shared(b) default(none)
      {
        #if TASK_COMPUTE
          PRINT("hi alex from %d\n", id);
          b[id]++;
        #endif
      }
      #pragma omp taskwait
    #endif
  }
  // reproduce
  aa[0]++;
  #if TRY_TASK && TASK_COMPUTE
    bb[0]++;
  #endif
  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: task within parallel

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #if TRY_TASK
        #pragma omp task firstprivate(id) shared(b)
        {
          #if TASK_COMPUTE
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          #endif
        }
      #endif
    }
  }
  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    #if TRY_TASK && TASK_COMPUTE
      bb[i]++;
    #endif
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: multiple nested tasks in parallel region

    // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b)
      {
        PRINT("hi alex from  %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;

        #pragma omp task firstprivate(id) shared(b)
        {
          PRINT("hi alex from  %d\n", id);
          int id = omp_get_thread_num();
          b[id]++;

          #pragma omp task firstprivate(id) shared(b)
          {
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          }
        }
      }
    }
  }

  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: three successive tasks in a parallel region

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
    }
  }
  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: change of context when entering/exiting tasks

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i+1;
    c[i] = cc[i] = 3*i+1;
    d[i] = dd[i] = 4*i+1;
    e[i] = ee[i] = 5*i+1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b, c, d, e)
  {
    omp_set_schedule(omp_sched_static, 1);
    #pragma omp parallel num_threads(64)
    {
      omp_set_schedule(omp_sched_static, 2);
      int id = omp_get_thread_num();
      // task 1
      #pragma omp task firstprivate(id) shared(b, c, d, e)
      {
        omp_set_schedule(omp_sched_static, 3);
        PRINT("hi alex from %d\n", id);
        // task 2
        #pragma omp task firstprivate(id) shared(b, c, d, e)
        {
          omp_set_schedule(omp_sched_static, 4);
          PRINT("hi alex from %d\n", id);
          // task 3
          #pragma omp task firstprivate(id) shared(b, c, d, e)
          {
            omp_set_schedule(omp_sched_static, 5);
            PRINT("hi alex from %d\n", id);
            // task 3
            omp_sched_t s; int chunk;
            omp_get_schedule(&s, &chunk);
            if (s == omp_sched_static && chunk == 5) e[id]++;
          }
          // task 2
          omp_sched_t s; int chunk;
          omp_get_schedule(&s, &chunk);
          if (s == omp_sched_static && chunk == 4) d[id]++;
        }
        // task 1
        omp_sched_t s; int chunk;
        omp_get_schedule(&s, &chunk);
        if (s == omp_sched_static && chunk == 3) c[id]++;
      }
      // par
      omp_sched_t s; int chunk;
      omp_get_schedule(&s, &chunk);
      if (s == omp_sched_static && chunk == 2) b[id]++;
    }
    // team
    omp_sched_t s; int chunk;
    omp_get_schedule(&s, &chunk);
    if (s == omp_sched_static && chunk == 1) a[0]++;
  }
  // reproduce
  aa[0]++;
  for(i=0; i<N; i++) {
    bb[i]++;
    cc[i]++;
    dd[i]++;
    ee[i]++;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
    if (c[i] != cc[i]) printf("%4i: got c %d, expected %d, error %d\n", i, c[i], cc[i], ++errors);
    if (d[i] != dd[i]) printf("%4i: got d %d, expected %d, error %d\n", i, d[i], dd[i], ++errors);
    if (e[i] != ee[i]) printf("%4i: got e %d, expected %d, error %d\n", i, e[i], ee[i], ++errors);
  }

  printf("got %d errors\n", errors);

  // Test: change of context when using if clause

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i+1;
    c[i] = cc[i] = 3*i+1;
    d[i] = dd[i] = 4*i+1;
    e[i] = ee[i] = 5*i+1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b, c, d, e, cond)
  {
    omp_set_schedule(omp_sched_static, 1);
    #pragma omp parallel num_threads(64)
    {
      omp_set_schedule(omp_sched_static, 2);
      int id = omp_get_thread_num();
      // task 1
      #pragma omp task firstprivate(id) shared(b, c, d, e) if(cond)
      {
        omp_set_schedule(omp_sched_static, 3);
        PRINT("hi alex from  %d\n", id);
        // task 2
        #pragma omp task firstprivate(id) shared(b, c, d, e) if(cond)
        {
          omp_set_schedule(omp_sched_static, 4);
          PRINT("hi alex from  %d\n", id);
          // task 3
          #pragma omp task firstprivate(id) shared(b, c, d, e) if(cond)
          {
            omp_set_schedule(omp_sched_static, 5);
            PRINT("hi alex from  %d\n", id);
            // task 3
            omp_sched_t s; int chunk;
            omp_get_schedule(&s, &chunk);
            if (s == omp_sched_static && chunk == 5) e[id]++;
          }
          // task 2
          omp_sched_t s; int chunk;
          omp_get_schedule(&s, &chunk);
          if (s == omp_sched_static && chunk == 4) d[id]++;
        }
        // task 1
        omp_sched_t s; int chunk;
        omp_get_schedule(&s, &chunk);
        if (s == omp_sched_static && chunk == 3) c[id]++;
      }
      // par
      omp_sched_t s; int chunk;
      omp_get_schedule(&s, &chunk);
      if (s == omp_sched_static && chunk == 2) b[id]++;
    }
    // team
    omp_sched_t s; int chunk;
    omp_get_schedule(&s, &chunk);
    if (s == omp_sched_static && chunk == 1) a[0]++;
  }
  // reproduce
  aa[0]++;
  for(i=0; i<N; i++) {
    bb[i]++;
    cc[i]++;
    dd[i]++;
    ee[i]++;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
    if (c[i] != cc[i]) printf("%4i: got c %d, expected %d, error %d\n", i, c[i], cc[i], ++errors);
    if (d[i] != dd[i]) printf("%4i: got d %d, expected %d, error %d\n", i, d[i], dd[i], ++errors);
    if (e[i] != ee[i]) printf("%4i: got e %d, expected %d, error %d\n", i, e[i], ee[i], ++errors);
  }

  printf("got %d errors\n", errors);

  // Test: final

    // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b) final(1)
      {
        PRINT("hi alex from  %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;

        #pragma omp task firstprivate(id) shared(b) final(1)
        {
          PRINT("hi alex from  %d\n", id);
          int id = omp_get_thread_num();
          b[id]++;

          #pragma omp task firstprivate(id) shared(b) final(1)
          {
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          }
        }
      }
    }
  }

  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

#if 0
  // Test: untied

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b) untied
      {
        PRINT("hi alex from  %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;

        #pragma omp task firstprivate(id) shared(b) untied
        {
          PRINT("hi alex from  %d\n", id);
          int id = omp_get_thread_num();
          b[id]++;

          #pragma omp task firstprivate(id) shared(b) untied
          {
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          }
        }
      }
    }
  }

  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);
#endif

  // Test: mergeaeble

    // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b)
      {
        PRINT("hi alex from  %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;

        #pragma omp task firstprivate(id) shared(b) mergeable
        {
          PRINT("hi alex from  %d\n", id);
          int id = omp_get_thread_num();
          b[id]++;

          #pragma omp task firstprivate(id) shared(b) mergeable
          {
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          }
        }
      }
    }
  }

  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: private

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #if TRY_TASK
        #pragma omp task private(id) shared(b)
        {
          #if TASK_COMPUTE
            PRINT("hi alex from  %d\n", id);
            int id = omp_get_thread_num();
            b[id]++;
          #endif
        }
      #endif
    }
  }
  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    #if TRY_TASK && TASK_COMPUTE
      bb[i]++;
    #endif
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: depend

  // init
  int x;
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b) depend(out:x)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b) depend(inout:x)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b) depend(in:x)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
    }
  }
  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);

  // Test: inverted priority

  // init
  for(i=0; i<N; i++) {
    a[i] = aa[i] = i+1;
    b[i] = bb[i] = 2*i +1;
  }

  // target starts 1 team and many threads in it
  #pragma omp target map(tofrom: a, b)
  {
    #pragma omp parallel num_threads(64)
    {
      int id = omp_get_thread_num();
      a[id]++;
      #pragma omp task firstprivate(id) shared(b) priority(0)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b) priority(10)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
      #pragma omp task firstprivate(id) shared(b) priority(20)
      {
        PRINT("hi alex from %d\n", id);
        int id = omp_get_thread_num();
        b[id]++;
      }
    }
  }
  // reproduce
  for(i=0; i<N; i++) {
    aa[i]++;
    bb[i]+=3;
  }

  // verify
  errors = 0;
  for(i=0; i<N; i++) {
    if (a[i] != aa[i]) printf("%4i: got a %d, expected %d, error %d\n", i, a[i], aa[i], ++errors);
    if (b[i] != bb[i]) printf("%4i: got b %d, expected %d, error %d\n", i, b[i], bb[i], ++errors);
  }
  printf("got %d errors\n", errors);


  return 0;
}
Exemple #8
0
int main(int argc, char** argv) {
	/* omp.h:
	typedef enum omp_sched_t {
		omp_sched_static  = 1,
		omp_sched_dynamic = 2,
		omp_sched_guided  = 3,
		omp_sched_auto    = 4
	} omp_sched_t;
	*/
	omp_sched_t omp_sched_kind = omp_sched_static;
	int omp_sched_modifier = 0; // value <= 0 sets to default

	long array_size = 1000;

	int opt;
	while ((opt = getopt(argc, argv, "k:m:s:")) != -1) {
		switch (opt) {
		case 'k':
			omp_sched_kind = strtol(optarg, NULL, 10);
			break;
		case 'm':
			omp_sched_modifier = strtol(optarg, NULL, 10);
			break;
		case 's':
			array_size = strtol(optarg, NULL, 10);
			break;
		default:
			printf("usage: %s -k <omp_sched_kind> -m <omp_sched_modifier> -s <array_size>\n", argv[0]);
		}
	}

	int* array = make_array(array_size);
	if (array == NULL) {
		exit(EXIT_FAILURE);
	}

	omp_sched_print(omp_sched_kind, omp_sched_modifier);
	omp_set_schedule(omp_sched_kind, omp_sched_modifier);

	// reset to test if omp_get_schedule() works
	omp_sched_modifier = -1;
	omp_sched_kind = -1;

	double t = omp_get_wtime();
	#pragma omp parallel
	{
		#pragma omp single
		{
			omp_get_schedule(&omp_sched_kind, &omp_sched_modifier);
			omp_sched_print(omp_sched_kind, omp_sched_modifier);
		}
		#pragma omp for schedule(runtime) // "runtime" means set by omp_set_schedule()
		for (int i = 0; i < array_size; i++) {
			process(array[i]);
		}
	}
	printf("%d %d %lf\n", omp_sched_kind, omp_sched_modifier, omp_get_wtime() - t);

	free(array);
	exit(EXIT_SUCCESS);
}
Exemple #9
0
int main(int argc, char ** argv){
	int **M;
	int	*v1, *v2;
	int	i, k, a, N;
	double cgt1, cgt2, ncgt; 	//para tiempo de ejecución
	time_t t;

	// Semilla de rand()
   	srand((unsigned) time(&t));

	// Obtenemos el numero de filas x columnas de la matriz cuadrada
	if(argc < 4){
		fprintf(stderr,"Error: %s <N_filas> <Chunk (0...I)> <Sched (static, dynamic, guided)>\n", argv[0]);
		exit(-1);
	}
	N = atoi(argv[1]);

	// == Directivas de OpenMP
	// ====================================================>
	int chunk = 0;
	omp_sched_t kind;
	if(strcmp(argv[2], "default") == 0)
		omp_get_schedule(&kind, &chunk);
	else
		chunk = atoi(argv[2]);

	// Modificar OMP_SCHEDULE
		 if(strcmp(argv[3], "static") == 0)		omp_set_schedule(1, chunk);
	else if(strcmp(argv[3], "dynamic") == 0)	omp_set_schedule(2, chunk);
	else if(strcmp(argv[3], "guided") == 0)		omp_set_schedule(3, chunk);
	else {
		printf("Error en el metodo de asignacion de trabajo a las hebras (static, dynamic, guided)\n");
		exit(-1);
	}

	// El numero de hebras que se vayan a usar debe ser el mismo que el numero de procesadores disponibles
	omp_set_num_threads(omp_get_num_procs());

	// == Reserva de Memoria
	// ====================================================>
	v1 = (int *) malloc(N*sizeof(int));
	v2 = (int *) malloc(N*sizeof(int));
	if ( v1 == NULL || v2 == NULL ){
		printf("Error en la reserva de espacio para los vectores\n");
		exit(-2);
	}

	M = (int**) malloc (N*sizeof(int*));
	// i como private en un for establece que cada hebra tendra una copia de i, pero en parallel for tendra cada una i como sigue
	// i = 0, i = 3, i = 6	para un bucle de N = 9
	#pragma omp parallel for shared(M,N) private(i) default(none) schedule(runtime)
	for(i = 0; i<N; i++){
		M[i] = (int*) malloc (N*sizeof(int));
		if( M[i] == NULL ){
			printf("Error en la reserva de espacio para los vectores\n");
			exit(-2);
		}
	}
	// == Inicializacion
	// ====================================================>
	// M, v1, v2, N, i compartidas
	// Cada hebra se encargará de una parte del bucle usando i

	// k es privada
	// Para que cada hebra que este calculando la parte iesima del bucle y tenga una copia de k = 0 propia, parte k es secuencial
	#pragma omp parallel for shared(N,M) private(i,k,a) default(none) schedule(runtime)
	for(i = 0; i<N; i++){
		if(i>0){
			for(a = 0; a<i; a++)
				M[i][a] = 0;
			for(k = a; k<N; k++)
				M[i][k] = rand() % 8;
		}
		else {
			for(k = 0; k<N; k++){
				M[i][k] = rand() % 8;
			}
		}
	}

	#pragma omp parallel for shared(v1,v2,N) private(i) default(none) schedule(runtime)
	for(i = 0; i<N; i++){
		v1[i] = rand() % 6;
		v2[i] = 0;
	}

	// == Calculo
	// ====================================================>
	cgt1 = omp_get_wtime();
	#pragma omp parallel for shared(v1,v2,M,N) private(i,k) default(none) schedule(runtime)
	for(i = 0; i<N; i++){
		for(k = i; k<N; k++)
			v2[i] += M[i][k] * v1[k];
		
	}
	cgt2 = omp_get_wtime();

	ncgt = (double)(cgt2 - cgt1);

	// == Imprimir Mensajes
	// ====================================================>	
	printf("Tiempo(seg.):%11.9f\n", ncgt);
	printf("Tamaño de los vectores: %u\n", N);
	printf("\tv1 = %uElem -> %lu bytes\n\tv2 = %uElem -> %lu bytes\n", N, N*sizeof(int), N, N*sizeof(int));
	printf("Tamaño de la matriz: %ux%u -> %lu bytes\n", N, N, N*N*sizeof(int));
	// Imprimir el primer y último componente del resultado evita que las optimizaciones del compilador 
	// eliminen el código de la suma.
	printf("v2[0] = %u ... v2[N-1] = %u \n", v2[0], v2[N-1]);

	// Para tamaños pequeños de N < 15 mostrar los valores calculados
	if(N < 15){
		printf("\n----------- Matriz M ----------- \n");
		for(i = 0; i<N; i++){
			for(k = 0; k<N; k++)
				printf("%u\t", M[i][k]);
			printf("\n");
		}
		printf("\n----------- Vector V1 ----------- \n");
		for(i = 0; i<N; i++)
			printf("%u\t", v1[i]);
		printf("\n");

		printf("\n----------- Vector V2----------- \n");
		for(i = 0; i<N; i++)
			printf("%u\t", v2[i]);
		printf("\n");
	}

	// == Liberar Memoria
	// ====================================================>
	free(v1);
	free(v2);
	#pragma omp parallel for shared(M,N) private(i) default(none) schedule(runtime)
	for(i = 0; i<N; i++)
		free(M[i]);
	free(M);
}
int main ( void )

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

    MAIN is the main program for MXV_OPENMP.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    17 April 2009

  Author:

    John Burkardt
*/
{
  int i;
  int m;
  int n;
  omp_sched_t myschedule;
  char schedstring[20];
  int mychunk;

  timestamp ( );

  printf ( "\n" );
  printf ( "MXV_OPENMP:\n" );
  printf ( "  C/OpenMP version\n" );
  printf ( "  Compute matrix vector products y = A*x.\n" );

  printf ( "\n" );
  printf ( "  Number of processors available = %d\n", omp_get_num_procs ( ) );
  printf ( "  Number of threads =              %d\n", omp_get_max_threads ( ) );

  omp_get_schedule ( &myschedule, &mychunk );
  switch (myschedule) {
    case omp_sched_static:
                 strcpy(schedstring, "Static");
                 break;
    case omp_sched_dynamic:
                 strcpy(schedstring, "Dynamic");
                 break;
    case omp_sched_guided:
                 strcpy(schedstring, "Guided");
                 break;
    case omp_sched_auto:
                 strcpy(schedstring, "Auto");
                 break;
  }
  printf ( "  Scheduling Policy =              %s\n", schedstring );
  printf ( "  Chunk Size =                     %d\n", mychunk );

//  printf ( "  Compare various algorithms:\n" );
//  printf ( "\n" );
//  printf ( "  MXV_PLAIN          - plain MxV coding.\n" );
//  printf ( "  MXV_PLAIN_OPENMP  - plain MxV coding + OpenMP.\n" );
  printf ( "\n" );
  printf ( "  Algorithm                  M         N      Seconds\n" );


  m = 10000;
  n = 10000;
  test01 ( m, n );
/*
  N = M
*/
/*
  m = 10;

  for ( i = 1; i <= 3; i++ )
  {
    printf ( "\n" );

    n = m;
    test01 ( m, n );

    m = m * 10;
  }
*/
/*
  N = 10 * M
*/
/*
  m = 1;

  for ( i = 1; i <= 4; i++ )
  {
    printf ( "\n" );

    n = 10 * m;
    test01 ( m, n );

    m = m * 10;
  }
*/
/*
  M = 10 * N
*/
/*
  n = 1;

  for ( i = 1; i <= 4; i++ )
  {
    printf ( "\n" );

    m = 10 * n;
    test01 ( m, n );

    n = n * 10;
  }
*/
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "MXV_OPENMP:\n" );
  printf ( "  Normal end of execution.\n" );

  printf ( "\n" );
  timestamp ( );

  return 0;
}
Exemple #11
0
int
main ()
{
  int d_o = omp_get_dynamic ();
  int n_o = omp_get_nested ();
  omp_sched_t s_o;
  int c_o;
  omp_get_schedule (&s_o, &c_o);
  int m_o = omp_get_max_threads ();
  omp_set_dynamic (1);
  omp_set_nested (1);
  omp_set_schedule (omp_sched_static, 2);
  omp_set_num_threads (4);
  int d = omp_get_dynamic ();
  int n = omp_get_nested ();
  omp_sched_t s;
  int c;
  omp_get_schedule (&s, &c);
  int m = omp_get_max_threads ();
  if (!omp_is_initial_device ())
    abort ();
  #pragma omp target if (0)
  {
    omp_sched_t s_c;
    int c_c;
    omp_get_schedule (&s_c, &c_c);
    if (d_o != omp_get_dynamic ()
	|| n_o != omp_get_nested ()
	|| s_o != s_c
	|| c_o != c_c
	|| m_o != omp_get_max_threads ())
      abort ();
    omp_set_dynamic (0);
    omp_set_nested (0);
    omp_set_schedule (omp_sched_dynamic, 4);
    omp_set_num_threads (2);
    if (!omp_is_initial_device ())
      abort ();
  }
  if (!omp_is_initial_device ())
    abort ();
  omp_sched_t s_c;
  int c_c;
  omp_get_schedule (&s_c, &c_c);
  if (d != omp_get_dynamic ()
      || n != omp_get_nested ()
      || s != s_c
      || c != c_c
      || m != omp_get_max_threads ())
    abort ();
  #pragma omp target if (0)
  #pragma omp teams
  {
    omp_sched_t s_c;
    int c_c;
    omp_get_schedule (&s_c, &c_c);
    if (d_o != omp_get_dynamic ()
	|| n_o != omp_get_nested ()
	|| s_o != s_c
	|| c_o != c_c
	|| m_o != omp_get_max_threads ())
      abort ();
    omp_set_dynamic (0);
    omp_set_nested (0);
    omp_set_schedule (omp_sched_dynamic, 4);
    omp_set_num_threads (2);
    if (!omp_is_initial_device ())
      abort ();
  }
  if (!omp_is_initial_device ())
    abort ();
  omp_get_schedule (&s_c, &c_c);
  if (d != omp_get_dynamic ()
      || n != omp_get_nested ()
      || s != s_c
      || c != c_c
      || m != omp_get_max_threads ())
    abort ();
  return 0;
}