Example #1
0
int
bar (int x, int y, int z)
{
  int a, b[64], i;
  a = 8;
  for (i = 0; i < 64; i++)
    b[i] = i;
  foo (x, y, z, &a, b);
  if (x == 0)
    {
      if (a != 8 + 64 * 32)
	return 1;
      for (i = 0; i < 64; i++)
	if (b[i] != i + 31 * 32 / 2)
	  return 1;
    }
  else if (x == 1)
    {
      int c = omp_get_num_teams ();
      int d = omp_get_team_num ();
      int e = d;
      int f = 0;
      for (i = 0; i < 64; i++)
	if (i == e)
	  {
	    if (b[i] != i + 31 * 32 / 2)
	      return 1;
	    f++;
	    e = e + c;
	  }
	else if (b[i] != i)
	  return 1;
      if (a < 8 || a > 8 + f * 32)
	return 1;
    }
  else if (x == 2)
    {
      if (a != 8 + 32)
	return 1;
      for (i = 0; i < 64; i++)
	if (b[i] != i + (i == y ? 31 * 32 / 2 : 0))
	  return 1;
    }
  else if (x == 3)
    {
      if (a != 8 + 1)
	return 1;
      for (i = 0; i < 64; i++)
	if (b[i] != i + (i == y ? z : 0))
	  return 1;
    }
  return 0;
}
Example #2
0
int32_t
omp_get_num_teams_ (void)
{
  return omp_get_num_teams ();
}
Example #3
0
int main(void) {
  check_offloading();

  double A[N], B[N], C[N], D[N], E[N];
  int fail = 0;
  int expected = 1;
  int success = 0;
  int chunkSize;
  double p = 2.0, q = 4.0;
  int nte, tl, blockSize;

  INIT();

  // **************************
  // Series 1: no dist_schedule
  // **************************

  //
  // Test: #iterations == #teams
  //
  printf("iterations = teams\n");
  #define CLAUSES num_teams(992)
  CODE()
  #undef CLAUSES

  printf("iterations > teams\n");
  #define CLAUSES num_teams(256)
  CODE()
  #undef CLAUSES

  printf("iterations < teams\n");
  #define CLAUSES num_teams(1024)
  CODE()
  #undef CLAUSES

  printf("num_teams(512) dist_schedule(static,1)\n");
  #define CLAUSES num_teams(512) dist_schedule(static, 1)
  CODE()
  #undef CLAUSES

  printf("num_teams(512) dist_schedule(static,512)\n");
  #define CLAUSES num_teams(512) dist_schedule(static, 512)
  CODE()
  #undef CLAUSES

  printf("num_teams(512) dist_schedule(static, chunkSize)\n");
  chunkSize = N / 10;
  #define CLAUSES num_teams(512) dist_schedule(static, chunkSize)
  CODE()
  #undef CLAUSES

  printf("num_teams(1024) dist_schedule(static, chunkSize)\n");
  chunkSize = N / 10;
  #define CLAUSES num_teams(1024) dist_schedule(static, chunkSize)
  CODE()
  #undef CLAUSES

  printf("num_teams(1024) dist_schedule(static, 1)\n");
  #define CLAUSES num_teams(1024) dist_schedule(static, 1)
  CODE()
  #undef CLAUSES

  printf("num_teams(3) dist_schedule(static, 1)\n");
  #define CLAUSES num_teams(3) dist_schedule(static, 1)
  CODE()
  #undef CLAUSES

  printf("num_teams(3) dist_schedule(static, 3)\n");
  #define CLAUSES num_teams(3) dist_schedule(static, 3)
  CODE()
  #undef CLAUSES

  printf("num_teams(10) dist_schedule(static, 99)\n");
  #define CLAUSES num_teams(10) dist_schedule(static, 99)
  CODE()
  #undef CLAUSES

  printf("num_teams(256) dist_schedule(static, 992)\n");
  #define CLAUSES num_teams(256) dist_schedule(static, 992)
  CODE()
  #undef CLAUSES

#if 0
  printf("num_teams(256) private(p,q)\n");
  #define CLAUSES num_teams(256) private(p,q)
  CODE_PRIV()
  #undef CLAUSES
#endif

  //
  // Test: firstprivate
  //

#if 0
  printf("num_teams(64) firstprivate(p, q)\n");
  ZERO(A); ZERO(B);
  p = 2.0, q = 4.0;
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target // implicit firstprivate for p and q, their initial values being 2 and 4 for each target invocation
    #pragma omp teams distribute num_teams(64) firstprivate(p, q)
    for(int i = 0 ; i < 128 ; i++) { // 2 iterations for each team
      p += 3.0;  // p and q are firstprivate to the team, and as such incremented twice (2 iterations per team)
      q += 7.0;
      A[i] += p;
      B[i] += q;
    }
  }
  for(int i = 0 ; i < 128 ; i++) {
    if (i % 2 == 0) {
      if (A[i] != (2.0+3.0)*TRIALS) {
      	printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
      	fail = 1;
      }
      if (B[i] != (4.0+7.0)*TRIALS) {
      	printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0)*TRIALS, B[i]);
      	fail = 1;
      }
    } else {
      if (A[i] != (2.0+3.0*2)*TRIALS) {
      	printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0*2)*TRIALS, A[i]);
      	fail = 1;
      }
      if (B[i] != (4.0+7.0*2)*TRIALS) {
      	printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0*2)*TRIALS, B[i]);
      	fail = 1;
      }
    }
  }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");
#endif

  //
  // Test: lastprivate
  //

  printf("num_teams(10) lastprivate(lastpriv)\n");
  success = 0;
  int lastpriv = -1;
  #pragma omp target map(tofrom:lastpriv)
  #pragma omp teams distribute num_teams(10) lastprivate(lastpriv)
  for(int i = 0 ; i < omp_get_num_teams() ; i++)
    lastpriv = omp_get_team_num();

  if(lastpriv != 9) {
    printf("lastpriv value is %d and should have been %d\n", lastpriv, 9);
    fail = 1;
  }

  if(fail) printf("Failed\n");
  else printf("Succeeded\n");


  // // ***************************
  // // Series 4: with parallel for
  // // ***************************

  //
  // Test: simple blocking loop
  //
  printf("num_teams(nte) thread_limit(tl) with parallel for innermost\n");
  success = 0;
  ZERO(A); ZERO(B);
  nte = 32;
  tl = 64;
  blockSize = tl;

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams distribute num_teams(nte) thread_limit(tl)
    for(int j = 0 ; j < 256 ; j += blockSize) {
      #pragma omp parallel for
      for(int i = j ; i < j+blockSize; i++) {
        A[i] += B[i] + C[i];
      }
    }
  }
  for(int i = 0 ; i < 256 ; i++) {
    if (A[i] != TRIALS) {
      printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
      fail = 1;
    }
  }

  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: blocking loop where upper bound is not a multiple of tl*nte
  //

  printf("num_teams(nte) thread_limit(tl) with parallel for innermost\n");
  success = 0;
  ZERO(A); ZERO(B);
  nte = 32;
  tl = 64;
  blockSize = tl;

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams distribute num_teams(nte) thread_limit(tl)
    for(int j = 0 ; j < 510 ; j += blockSize) {
      int ub = (j+blockSize < 510) ? (j+blockSize) : 512;
      #pragma omp parallel for
      for(int i = j ; i < ub; i++) {
        A[i] += B[i] + C[i];
      }
    }
  }
  for(int i = 0 ; i < 256 ; i++) {
    if (A[i] != TRIALS) {
      printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
      fail = 1;
    }
  }

  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  // **************************
  // Series 5: collapse
  // **************************

  //
  // Test: 2 loops
  //

  printf("num_teams(512) collapse(2)\n");
  success = 0;
  double * S = malloc(N*N*sizeof(double));
  double * T = malloc(N*N*sizeof(double));
  double * U = malloc(N*N*sizeof(double));
  for (int i = 0 ; i < N ; i++)
    for (int j = 0 ; j < N ; j++)
    {
      S[i*N+j] = 0.0;
      T[i*N+j] = 1.0;
      U[i*N+j] = 2.0;
    }

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target map(tofrom:S[:N*N]), map(to:T[:N*N],U[:N*N])
    #pragma omp teams distribute num_teams(512) collapse(2)
    for (int i = 0 ; i < N ; i++)
      for (int j = 0 ; j < N ; j++)
        S[i*N+j] += T[i*N+j] + U[i*N+j];  // += 3 at each t
  }
  for (int i = 0 ; i < N ; i++)
    for (int j = 0 ; j < N ; j++)
      if (S[i*N+j] != TRIALS*3.0) {
        printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, S[i*N+j]);
        fail = 1;
      }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: 3 loops
  //

  printf("num_teams(512) collapse(3)\n");
  success = 0;
  int M = N/8;
  double * V = malloc(M*M*M*sizeof(double));
  double * Z = malloc(M*M*M*sizeof(double));
  for (int i = 0 ; i < M ; i++)
    for (int j = 0 ; j < M ; j++)
      for (int k = 0 ; k < M ; k++)
      {
        V[i*M*M+j*M+k] = 2.0;
        Z[i*M*M+j*M+k] = 3.0;
      }

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target map(tofrom:V[:M*M*M]), map(to:Z[:M*M*M])
    #pragma omp teams distribute num_teams(512) collapse(3)
    for (int i = 0 ; i < M ; i++)
      for (int j = 0 ; j < M ; j++)
        for (int k = 0 ; k < M ; k++)
          V[i*M*M+j*M+k] += Z[i*M*M+j*M+k];  // += 3 at each t
  }
  for (int i = 0 ; i < M ; i++)
    for (int j = 0 ; j < M ; j++)
      for (int k = 0 ; k < M ; k++)
        if (V[i*M*M+j*M+k] != 2.0+TRIALS*3.0) {
          printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, V[i*M*M+j*M+k]);
          fail = 1;
        }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  return 0;
}
Example #4
0
int main(void) {
  check_offloading();

  double A[N], B[N], C[N], D[N], E[N];
  int fail = 0;

  INIT();

  // **************************
  // Series 1: no dist_schedule
  // **************************

  //
  // Test: #iterations == #teams
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd
    for (int i = 0 ; i < 512 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 512 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations > #teams
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd
    for (int i = 0 ; i < 500 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 500 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations < #teams
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd
    for (int i = 0 ; i < 123 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 123 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  // ****************************
  // Series 2: with dist_schedule
  // ****************************

  //
  // Test: #iterations == #teams, dist_schedule(1)
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd dist_schedule(static,1)
    for (int i = 0 ; i < 512 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 512 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations == #teams, dist_schedule(#iterations)
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd dist_schedule(static,512)
    for (int i = 0 ; i < 512 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 512 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations == #teams, dist_schedule(#iterations/10), variable chunk size
  //
  ZERO(A);
  int ten = 10;
  int chunkSize = 512/ten;
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd dist_schedule(static,chunkSize)
    for (int i = 0 ; i < 512 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 512 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations > #teams, dist_schedule(1)
  //
    ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd dist_schedule(static,1)
    for (int i = 0 ; i < 500 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 500 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations > #teams, dist_schedule(#iterations)
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd dist_schedule(static,500)
    for (int i = 0 ; i < 500 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 500 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations > #teams, dist_schedule(#iterations/10), variable chunk size
  //
  ZERO(A);
  ten = 10;
  chunkSize = 500/ten;
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd dist_schedule(static,chunkSize)
    for (int i = 0 ; i < 500 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 500 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations < #teams, dist_schedule(1)
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
#pragma omp distribute simd dist_schedule(static,1)
    for (int i = 0 ; i < 123 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 123 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations < #teams, dist_schedule(#iterations)
  //
  ZERO(A);
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
#pragma omp distribute simd dist_schedule(static,123)
    for (int i = 0 ; i < 123 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 123 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: #iterations < #teams, dist_schedule(#iterations)
  //
  ZERO(A);
  ten = 10;
  chunkSize = 123/ten;
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    #pragma omp distribute simd dist_schedule(static,chunkSize)
    for (int i = 0 ; i < 123 ; i++)
    {
      A[i] += C[i]; // += 1 per position
    }
  }
  for (int i = 0 ; i < 123 ; i++)
    if (A[i] != TRIALS) {
      printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  // ****************************
  // Series 3: with ds attributes
  // ****************************

  //
  // Test: private
  //
  ZERO(A); ZERO(B);
  double p = 2.0, q = 4.0;
  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target
    #pragma omp teams num_teams(256)
    {
      #pragma omp distribute simd private(p,q)
      for(int i = 0 ; i < N ; i++) {
	p = 2;
	q = 3;
	A[i] += p;
	B[i] += q;
      }
    }
  }
  for(int i = 0 ; i < N ; i++) {
    if (A[i] != TRIALS*2) {
      printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) TRIALS*2, A[i]);
      fail = 1;
    }
    if (B[i] != TRIALS*3) {
      printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) TRIALS*3, B[i]);
      fail = 1;
    }
  }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: firstprivate
  //
  ZERO(A); ZERO(B);
  p = 2.0, q = 4.0;
  for (int t = 0 ; t < TRIALS ; t++) {
#pragma omp target // implicit firstprivate for p and q, their initial values being 2 and 4 for each target invocation
#pragma omp teams num_teams(64)
    {
      #pragma omp distribute simd firstprivate(p,q)
      for(int i = 0 ; i < 128 ; i++) { // 2 iterations for each team
	p += 3.0;  // p and q are firstprivate to the team, and as such incremented twice (2 iterations per team)
	q += 7.0;
	A[i] += p;
	B[i] += q;
      }
    }
  }
  for(int i = 0 ; i < 128 ; i++) {
    if (i % 2 == 0) {
      if (A[i] != (2.0+3.0)*TRIALS) {
	printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
	fail = 1;
      }
      if (B[i] != (4.0+7.0)*TRIALS) {
	printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0)*TRIALS, B[i]);
	fail = 1;
      }
    } else {
      if (A[i] != (2.0+3.0*2)*TRIALS) {
	printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0*2)*TRIALS, A[i]);
	fail = 1;
      }
      if (B[i] != (4.0+7.0*2)*TRIALS) {
	printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0*2)*TRIALS, B[i]);
	fail = 1;
      }
    }
  }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: lastprivate
  //

  int lastpriv = -1;
#pragma omp target map(tofrom:lastpriv)
#pragma omp teams num_teams(10)
#pragma omp distribute simd lastprivate(lastpriv)
  for(int i = 0 ; i < omp_get_num_teams() ; i++)
    lastpriv = omp_get_team_num();

  if(lastpriv != 9) {
    printf("lastpriv value is %d and should have been %d\n", lastpriv, 9);
    fail = 1;
  }

  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  // **************************
  // Series 4: collapse
  // **************************

  //
  // Test: 2 loops
  //
  double * S = malloc(N*N*sizeof(double));
  double * T = malloc(N*N*sizeof(double));
  double * U = malloc(N*N*sizeof(double));
  for (int i = 0 ; i < N ; i++)
    for (int j = 0 ; j < N ; j++)
    {
      S[i*N+j] = 0.0;
      T[i*N+j] = 1.0;
      U[i*N+j] = 2.0;
    }

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target map(tofrom:S[:N*N]), map(to:T[:N*N],U[:N*N])
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd collapse(2)
    for (int i = 0 ; i < N ; i++)
      for (int j = 0 ; j < N ; j++)
	S[i*N+j] += T[i*N+j] + U[i*N+j];  // += 3 at each t
  }
  for (int i = 0 ; i < N ; i++)
    for (int j = 0 ; j < N ; j++)
    if (S[i*N+j] != TRIALS*3.0) {
      printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, S[i*N+j]);
      fail = 1;
    }
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  //
  // Test: 3 loops
  //
  int M = N/8;
  double * V = malloc(M*M*M*sizeof(double));
  double * Z = malloc(M*M*M*sizeof(double));
  for (int i = 0 ; i < M ; i++)
    for (int j = 0 ; j < M ; j++)
      for (int k = 0 ; k < M ; k++)
      {
	V[i*M*M+j*M+k] = 2.0;
	Z[i*M*M+j*M+k] = 3.0;
      }

  for (int t = 0 ; t < TRIALS ; t++) {
    #pragma omp target map(tofrom:V[:M*M*M]), map(to:Z[:M*M*M])
    #pragma omp teams num_teams(512)
    #pragma omp distribute simd collapse(3)
    for (int i = 0 ; i < M ; i++)
      for (int j = 0 ; j < M ; j++)
	for (int k = 0 ; k < M ; k++)
	  V[i*M*M+j*M+k] += Z[i*M*M+j*M+k];  // += 3 at each t
  }
  for (int i = 0 ; i < M ; i++)
    for (int j = 0 ; j < M ; j++)
      for (int k = 0 ; k < M ; k++)
	if (V[i*M*M+j*M+k] != 2.0+TRIALS*3.0) {
	  printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, V[i*M*M+j*M+k]);
	  fail = 1;
	}
  if(fail) printf("Failed\n");
  else printf("Succeeded\n");

  return 0;
}
Example #5
0
int main(void) {
    check_offloading();

    double A[N], B[N], C[N], D[N], E[N];
    int fail = 0;

    INIT();

    // **************************
    // Series 1: no dist_schedule
    // **************************

    //
    // Test: #iterations == #teams
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute num_teams(512)
        for (int i = 0 ; i < 512 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 512 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");


    //
    // Test: #iterations > #teams
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute num_teams(256)
        for (int i = 0 ; i < 500 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 500 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations < #teams
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute num_teams(256)
        for (int i = 0 ; i < 123 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 123 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    // ****************************
    // Series 2: with dist_schedule
    // ****************************

    //
    // Test: #iterations == #teams, dist_schedule(1)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,1) num_teams(512)
        for (int i = 0 ; i < 512 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 512 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations == #teams, dist_schedule(#iterations)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,512) num_teams(512)
        for (int i = 0 ; i < 512 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 512 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations == #teams, dist_schedule(#iterations/10), variable chunk size
    //
    ZERO(A);
    int ten = 10;
    int chunkSize = 512/ten;
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,chunkSize) num_teams(512)
        for (int i = 0 ; i < 512 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 512 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations > #teams, dist_schedule(1)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,1) num_teams(256)
        for (int i = 0 ; i < 500 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 500 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations > #teams, dist_schedule(#iterations)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,500) num_teams(256)
        for (int i = 0 ; i < 500 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 500 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations > #teams, dist_schedule(#iterations/10), variable chunk size
    //
    ZERO(A);
    ten = 10;
    chunkSize = 500/ten;
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,chunkSize) num_teams(256)
        for (int i = 0 ; i < 500 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 500 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations < #teams, dist_schedule(1)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,1) num_teams(256)
        for (int i = 0 ; i < 123 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 123 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations < #teams, dist_schedule(#iterations)
    //
    ZERO(A);
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,123) num_teams(256)
        for (int i = 0 ; i < 123 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 123 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: #iterations < #teams, dist_schedule(#iterations)
    //
    ZERO(A);
    ten = 10;
    chunkSize = 123/ten;
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute dist_schedule(static,chunkSize) num_teams(256)
        for (int i = 0 ; i < 123 ; i++)
        {
            A[i] += C[i]; // += 1 per position
        }
    }
    for (int i = 0 ; i < 123 ; i++)
        if (A[i] != TRIALS) {
            printf("Error at %d, h = %lf, d = %lf\n", i, (double) TRIALS, A[i]);
            fail = 1;
        }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    // ****************************
    // Series 3: with ds attributes
    // ****************************
    // DS currently failing in the compiler with asserts (bug #T158)
#if 0
    //
    // Test: private
    //
    ZERO(A);
    ZERO(B);
    double p = 2.0, q = 4.0;
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute private(p,q) num_teams(256)
        for(int i = 0 ; i < N ; i++) {
            p = 2;
            q = 3;
            A[i] += p;
            B[i] += q;
        }
    }
    for(int i = 0 ; i < N ; i++) {
        if (A[i] != TRIALS*2) {
            printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) TRIALS*2, A[i]);
            fail = 1;
        }
        if (B[i] != TRIALS*3) {
            printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) TRIALS*3, B[i]);
            fail = 1;
        }
    }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: firstprivate
    //
    ZERO(A);
    ZERO(B);
    p = 2.0, q = 4.0;
    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute firstprivate(p,q) num_teams(64)
        for(int i = 0 ; i < 128 ; i++) { // 2 iterations for each team
            p += 3.0;  // p and q are firstprivate to the team, and as such incremented twice (2 iterations per team)
            q += 7.0;
            A[i] += p;
            B[i] += q;
        }
    }
    for(int i = 0 ; i < 128 ; i++) {
        if (i % 2 == 0) {
            if (A[i] != (2.0+3.0)*TRIALS) {
                printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
                fail = 1;
            }
            if (B[i] != (4.0+7.0)*TRIALS) {
                printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0)*TRIALS, B[i]);
                fail = 1;
            }
        } else {
            if (A[i] != (2.0+3.0*2)*TRIALS) {
                printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0*2)*TRIALS, A[i]);
                fail = 1;
            }
            if (B[i] != (4.0+7.0*2)*TRIALS) {
                printf("Error at B[%d], h = %lf, d = %lf\n", i, (double) (4.0+7.0*2)*TRIALS, B[i]);
                fail = 1;
            }
        }
    }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");
//#endif

    //
    // Test: lastprivate
    //

    int lastpriv = -1;
    // map(tofrom:lastpriv)
    #pragma omp target teams distribute lastprivate(lastpriv) num_teams(10)
    for(int i = 0 ; i < omp_get_num_teams() ; i++)
        lastpriv = omp_get_team_num();

    if(lastpriv != 9) {
        printf("lastpriv value is %d and should have been %d\n", lastpriv, 9);
        fail = 1;
    }

    if(fail) printf("Failed\n");
    else printf("Succeeded\n");


    // ***************************
    // Series 4: with parallel for
    // ***************************

    //
    // Test: simple blocking loop
    //
    ZERO(A);
    ZERO(B);
    int nte = 32;
    int tl = 64;
    int blockSize = tl;

    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute num_teams(nte) thread_limit(tl)
        for(int j = 0 ; j < 256 ; j += blockSize) {
            #pragma omp parallel for
            for(int i = j ; i < j+blockSize; i++) {
                A[i] += B[i] + C[i];
            }
        }
    }
    for(int i = 0 ; i < 256 ; i++) {
        if (A[i] != TRIALS) {
            printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
            fail = 1;
        }
    }

    if(fail) printf("Failed\n");
    else printf("Succeeded\n");
#endif

    //
    // Test: blocking loop where upper bound is not a multiple of tl*nte
    //
    ZERO(A);
    ZERO(B);
    int nte = 32;
    int tl = 64;
    int blockSize = tl;

    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute num_teams(nte) thread_limit(tl)
        for(int j = 0 ; j < 510 ; j += blockSize) {
            int ub = (j+blockSize < 510) ? (j+blockSize) : 512;
            #pragma omp parallel for
            for(int i = j ; i < ub; i++) {
                A[i] += B[i] + C[i];
            }
        }
    }
    for(int i = 0 ; i < 256 ; i++) {
        if (A[i] != TRIALS) {
            printf("Error at A[%d], h = %lf, d = %lf\n", i, (double) (2.0+3.0)*TRIALS, A[i]);
            fail = 1;
        }
    }

    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    // **************************
    // Series 5: collapse
    // **************************

    //
    // Test: 2 loops
    //
    double * S = malloc(N*N*sizeof(double));
    double * T = malloc(N*N*sizeof(double));
    double * U = malloc(N*N*sizeof(double));
    for (int i = 0 ; i < N ; i++)
        for (int j = 0 ; j < N ; j++)
        {
            S[i*N+j] = 0.0;
            T[i*N+j] = 1.0;
            U[i*N+j] = 2.0;
        }

    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute collapse(2) map(tofrom:S[:N*N]), map(to:T[:N*N],U[:N*N]) num_teams(512)
        for (int i = 0 ; i < N ; i++)
            for (int j = 0 ; j < N ; j++)
                S[i*N+j] += T[i*N+j] + U[i*N+j];  // += 3 at each t
    }
    for (int i = 0 ; i < N ; i++)
        for (int j = 0 ; j < N ; j++)
            if (S[i*N+j] != TRIALS*3.0) {
                printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, S[i*N+j]);
                fail = 1;
            }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    //
    // Test: 3 loops
    //
    int M = N/8;
    double * V = malloc(M*M*M*sizeof(double));
    double * Z = malloc(M*M*M*sizeof(double));
    for (int i = 0 ; i < M ; i++)
        for (int j = 0 ; j < M ; j++)
            for (int k = 0 ; k < M ; k++)
            {
                V[i*M*M+j*M+k] = 2.0;
                Z[i*M*M+j*M+k] = 3.0;
            }

    for (int t = 0 ; t < TRIALS ; t++) {
        #pragma omp target teams distribute collapse(3) map(tofrom:V[:M*M*M]), map(to:Z[:M*M*M]) num_teams(512)
        for (int i = 0 ; i < M ; i++)
            for (int j = 0 ; j < M ; j++)
                for (int k = 0 ; k < M ; k++)
                    V[i*M*M+j*M+k] += Z[i*M*M+j*M+k];  // += 3 at each t
    }
    for (int i = 0 ; i < M ; i++)
        for (int j = 0 ; j < M ; j++)
            for (int k = 0 ; k < M ; k++)
                if (V[i*M*M+j*M+k] != 2.0+TRIALS*3.0) {
                    printf("Error at (%d,%d), h = %lf, d = %lf\n", i, j, (double) TRIALS*3.0, V[i*M*M+j*M+k]);
                    fail = 1;
                }
    if(fail) printf("Failed\n");
    else printf("Succeeded\n");

    return 0;
}