static char rcsid[] = "$Id: afntyschd024.c,v 1.2 2002/04/03 11:51:52 a-hasega Exp $";
/* 
 * $RWC_Release: Omni-1.6 $
 * $RWC_Copyright:
 *  Omni Compiler Software Version 1.5-1.6
 *  Copyright (C) 2002 PC Cluster Consortium
 *  
 *  This software is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version
 *  2.1 published by the Free Software Foundation.
 *  
 *  Omni Compiler Software Version 1.0-1.4
 *  Copyright (C) 1999, 2000, 2001.
 *   Tsukuba Research Center, Real World Computing Partnership, Japan.
 *  
 *  Please check the Copyright and License information in the files named
 *  COPYRIGHT and LICENSE under the top  directory of the Omni Compiler
 *  Software release kit.
 *  
 *  
 *  $
 */
/* 
 * afinity scheduling 024
 * 異なる次元間で cyclic mapping を expr mapping に指定した場合の
 *  affinity scheduling の動作確認
 */

#include "omniConfig.h"
#include <omp.h>
#include "omni.h"


#ifndef __OMNI_SCASH__
main ()
{
  printf ("affinity scheduling is supported at OMNI_SCASH only.\n");
  exit (0);
}
#else


#define SIZE (2*3*4)

#define CHUNK1	1
#define CHUNK2	2
#define CHUNK3	100

int base1[SIZE][SIZE][SIZE];
int base2[SIZE][SIZE][SIZE];
int base3[SIZE][SIZE][SIZE];
int base4[SIZE][SIZE][SIZE];

#pragma omni mapping(base1[*][cyclic][*])
#pragma omni mapping(base2[*][cyclic(1)][*])
#pragma omni mapping(base3[*][cyclic(2)][*])
#pragma omni mapping(base4[*][cyclic(100)][*])

int array11[SIZE][SIZE][SIZE];
int array12[SIZE][SIZE][SIZE];
int array13[SIZE][SIZE][SIZE];
int array14[SIZE][SIZE][SIZE];
int array21[SIZE][SIZE][SIZE];
int array22[SIZE][SIZE][SIZE];
int array23[SIZE][SIZE][SIZE];
int array24[SIZE][SIZE][SIZE];
int array31[SIZE][SIZE][SIZE];
int array32[SIZE][SIZE][SIZE];
int array33[SIZE][SIZE][SIZE];
int array34[SIZE][SIZE][SIZE];

#pragma omni mapping(array11[i][*][*]:base1[*][i][*])
#pragma omni mapping(array12[i][*][*]:base2[*][i][*])
#pragma omni mapping(array13[i][*][*]:base3[*][i][*])
#pragma omni mapping(array14[i][*][*]:base4[*][i][*])

#pragma omni mapping(array21[*][i][*]:base1[*][i][*])
#pragma omni mapping(array22[*][i][*]:base2[*][i][*])
#pragma omni mapping(array23[*][i][*]:base3[*][i][*])
#pragma omni mapping(array24[*][i][*]:base4[*][i][*])

#pragma omni mapping(array31[*][*][i]:base1[*][i][*])
#pragma omni mapping(array32[*][*][i]:base2[*][i][*])
#pragma omni mapping(array33[*][*][i]:base3[*][i][*])
#pragma omni mapping(array34[*][*][i]:base4[*][i][*])


int errors = 0;
int p, s;


check (int thds, int *array, int chunksz, int size, int start, int end, int stride, int offset)
{
  int chunk, i, v, next;

  chunk = chunksz;

  for (i = 0; i < size; i++) {
    if (i < start) {
      if (*(array + offset * i) != 0) {
	errors += 1;
      }

    } else if (i >=end) {
      if (*(array + offset * i) != 0) {
	errors += 1;
      }

    } else {
      v = (i/chunk) % thds + 1;

      if (i == start) {
	if (*(array + offset * i) != v) {
	  errors += 1;
	}
	next = i + stride;
      } else if (i == next) {
	if (*(array + offset * i) != v) {
	  errors += 1;
	}
	next += stride;
      } else {
	if (*(array + offset * i) != 0) {
	  errors += 1;
	}
      }
    }
  }
}


main ()
{
  int	thds, i;


  thds = omp_get_max_threads();
  if (thds == 1) {
    printf ("shuld be run this program on multi threads\n");
    exit (0);
  }

  for (p=0; p<SIZE; p++) {
    for (s=1; s<SIZE; s++) {
      memset (array11, 0, sizeof(array11));
      #pragma omp parallel for schedule(affinity, array11[i][*][*])
      for (i=p; i<SIZE; i+=s) {
	array11[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array11, CHUNK1, SIZE, p, SIZE, s, SIZE*SIZE);

      memset (array11, 0, sizeof(array11));
      #pragma omp parallel for schedule(affinity, array11[i][*][*])
      for (i=0; i<SIZE-p; i+=s) {
	array11[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array11, CHUNK1, SIZE, 0, SIZE-p, s, SIZE*SIZE);

      memset (array12, 0, sizeof(array12));
      #pragma omp parallel for schedule(affinity, array12[i][*][*])
      for (i=p; i<SIZE; i+=s) {
	array12[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array12, CHUNK1, SIZE, p, SIZE, s, SIZE*SIZE);

      memset (array12, 0, sizeof(array12));
      #pragma omp parallel for schedule(affinity, array12[i][*][*])
      for (i=0; i<SIZE-p; i+=s) {
	array12[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array12, CHUNK1, SIZE, 0, SIZE-p, s, SIZE*SIZE);

      memset (array13, 0, sizeof(array13));
      #pragma omp parallel for schedule(affinity, array13[i][*][*])
      for (i=p; i<SIZE; i+=s) {
	array13[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array13, CHUNK2, SIZE, p, SIZE, s, SIZE*SIZE);

      memset (array13, 0, sizeof(array13));
      #pragma omp parallel for schedule(affinity, array13[i][*][*])
      for (i=0; i<SIZE-p; i+=s) {
	array13[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array13, CHUNK2, SIZE, 0, SIZE-p, s, SIZE*SIZE);

      memset (array14, 0, sizeof(array14));
      #pragma omp parallel for schedule(affinity, array14[i][*][*])
      for (i=p; i<SIZE; i+=s) {
	array14[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array14, CHUNK3, SIZE, p, SIZE, s, SIZE*SIZE);

      memset (array14, 0, sizeof(array14));
      #pragma omp parallel for schedule(affinity, array14[i][*][*])
      for (i=0; i<SIZE-p; i+=s) {
	array14[i][0][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array14, CHUNK3, SIZE, 0, SIZE-p, s, SIZE*SIZE);


      memset (array21, 0, sizeof(array21));
      #pragma omp parallel for schedule(affinity, array21[*][i][*])
      for (i=p; i<SIZE; i+=s) {
	array21[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array21, CHUNK1, SIZE, p, SIZE, s, SIZE);

      memset (array21, 0, sizeof(array21));
      #pragma omp parallel for schedule(affinity, array21[*][i][*])
      for (i=0; i<SIZE-p; i+=s) {
	array21[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array21, CHUNK1, SIZE, 0, SIZE-p, s, SIZE);

      memset (array22, 0, sizeof(array22));
      #pragma omp parallel for schedule(affinity, array22[*][i][*])
      for (i=p; i<SIZE; i+=s) {
	array22[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array22, CHUNK1, SIZE, p, SIZE, s, SIZE);

      memset (array22, 0, sizeof(array22));
      #pragma omp parallel for schedule(affinity, array22[*][i][*])
      for (i=0; i<SIZE-p; i+=s) {
	array22[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array22, CHUNK1, SIZE, 0, SIZE-p, s, SIZE);

      memset (array23, 0, sizeof(array23));
      #pragma omp parallel for schedule(affinity, array23[*][i][*])
      for (i=p; i<SIZE; i+=s) {
	array23[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array23, CHUNK2, SIZE, p, SIZE, s, SIZE);

      memset (array23, 0, sizeof(array23));
      #pragma omp parallel for schedule(affinity, array23[*][i][*])
      for (i=0; i<SIZE-p; i+=s) {
	array23[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array23, CHUNK2, SIZE, 0, SIZE-p, s, SIZE);

      memset (array24, 0, sizeof(array24));
      #pragma omp parallel for schedule(affinity, array24[*][i][*])
      for (i=p; i<SIZE; i+=s) {
	array24[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array24, CHUNK3, SIZE, p, SIZE, s, SIZE);

      memset (array24, 0, sizeof(array24));
      #pragma omp parallel for schedule(affinity, array24[*][i][*])
      for (i=0; i<SIZE-p; i+=s) {
	array24[0][i][0] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array24, CHUNK3, SIZE, 0, SIZE-p, s, SIZE);


      memset (array31, 0, sizeof(array31));
      #pragma omp parallel for schedule(affinity, array31[*][*][i])
      for (i=p; i<SIZE; i+=s) {
	array31[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array31, CHUNK1, SIZE, p, SIZE, s, 1);

      memset (array31, 0, sizeof(array31));
      #pragma omp parallel for schedule(affinity, array31[*][*][i])
      for (i=0; i<SIZE-p; i+=s) {
	array31[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array31, CHUNK1, SIZE, 0, SIZE-p, s, 1);

      memset (array32, 0, sizeof(array32));
      #pragma omp parallel for schedule(affinity, array32[*][*][i])
      for (i=p; i<SIZE; i+=s) {
	array32[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array32, CHUNK1, SIZE, p, SIZE, s, 1);

      memset (array32, 0, sizeof(array32));
      #pragma omp parallel for schedule(affinity, array32[*][*][i])
      for (i=0; i<SIZE-p; i+=s) {
	array32[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array32, CHUNK1, SIZE, 0, SIZE-p, s, 1);

      memset (array33, 0, sizeof(array33));
      #pragma omp parallel for schedule(affinity, array33[*][*][i])
      for (i=p; i<SIZE; i+=s) {
	array33[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array33, CHUNK2, SIZE, p, SIZE, s, 1);

      memset (array33, 0, sizeof(array33));
      #pragma omp parallel for schedule(affinity, array33[*][*][i])
      for (i=0; i<SIZE-p; i+=s) {
	array33[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array33, CHUNK2, SIZE, 0, SIZE-p, s, 1);

      memset (array34, 0, sizeof(array34));
      #pragma omp parallel for schedule(affinity, array34[*][*][i])
      for (i=p; i<SIZE; i+=s) {
	array34[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array34, CHUNK3, SIZE, p, SIZE, s, 1);

      memset (array34, 0, sizeof(array34));
      #pragma omp parallel for schedule(affinity, array34[*][*][i])
      for (i=0; i<SIZE-p; i+=s) {
	array34[0][0][i] = omp_get_thread_num () + 1;
      }
      check (thds, (int *)array34, CHUNK3, SIZE, 0, SIZE-p, s, 1);
    }
  }


  if (errors == 0) {
    printf ("affinity scheduling 024 : SUCCESS\n");
    return 0;
  } else {
    printf ("affinity scheduling 024 : FAILED\n");
    return 1;
  }
}


#endif
