Example #1
0
File: jacobi.c Project: pousa/minas
void *Worker(void *arg) {
  int myid = (int) arg;
  double maxdiff, temp,tmp=0.0;
  int i, j, iters;
  int first, last;
  double **grid1;
  double **grid2;
  unsigned long mask = 128;//24+myid;
  unsigned long maxnode = 8*sizeof(unsigned long);

  printf("worker %d (pthread id %d) has started\n", myid, pthread_self());

  grid1 = (double**)mai_malloc((stripSize+3)*sizeof(double*));
  grid2 = (double**)mai_malloc((stripSize+3)*sizeof(double*));
  for(i = 0; i <= stripSize; i++) {
	grid1[i] = (double*)mai_malloc((gridSize+3)*sizeof(double));	
	grid2[i] = (double*)mai_malloc((gridSize+3)*sizeof(double));	
  }
  Barrier();
  InitializeGrids(grid1,grid2);

  /* determine first and last rows of my strip of the grids */

  for (iters = 1; iters <= numIters; iters++) {
    /* update my points */
    for (i = 1; i < stripSize; i++) {
      for (j = 1; j <= gridSize; j++) {
        grid2[i][j] = (grid1[i-1][j] + grid1[i+1][j] + 
                       grid1[i][j-1] + grid1[i][j+1]) * 0.25;
      }
    }
    Barrier();
    /* update my points again */
    for (i = 1; i < stripSize; i++) {
      for (j = 1; j <= gridSize; j++) {
        grid1[i][j] = (grid2[i-1][j] + grid2[i+1][j] +
               grid2[i][j-1] + grid2[i][j+1]) * 0.25;
      }
    }
    Barrier();
  }
  /* compute the maximum difference in my strip and set global variable */
  maxdiff = 0.0;
  for (i = 1; i <= stripSize; i++) {
    for (j = 1; j <= gridSize; j++) {
      temp = grid1[i][j]-grid2[i][j];
      if (temp < 0)
        temp = -temp;
      if (maxdiff < temp)
        maxdiff = temp;
    }
  }
  maxDiff[myid] = maxdiff;
}
Example #2
0
File: jacobi.c Project: pousa/minas
int main(int argc, char *argv[]) {
  int i, j;
  double tmp=0,tma=0;

  /* read command line and initialize grids */
  if (argc < 2) {
	printf("jacobi size n_iter\n");
	exit(0);
  }
  gridSize = atoi(argv[1]);
  numIters = atoi(argv[3]);

#ifndef MAI
 tma=0.0;
 tma = myms();
grid1 = (double**)malloc((gridSize+3)*sizeof(double*));
  grid2 = (double**)malloc((gridSize+3)*sizeof(double*));
  for(i = 0; i <= gridSize; i++) {
	grid1[i] = (double*)malloc((gridSize+3)*sizeof(double));	
	grid2[i] = (double*)malloc((gridSize+3)*sizeof(double));	
  }
 tma = myms() - tma;
 printf("Malloc time %f\n",tma/1.e+6);
#endif  

#ifdef MAI  
  mai_init(NULL);
  tma=0.0;
  tma = myms();
  grid1 = mai_alloc_2D(gridSize,gridSize, sizeof(double),DOUBLE);
  grid2 = mai_alloc_2D(gridSize,gridSize, sizeof(double),DOUBLE);
  tma = myms() - tma;
  printf("MAi alloc time: %f\n",tma/1.e+6);
#endif

  mai_bind_rows(grid1);
  mai_bind_rows(grid2);
  InitializeGrids();

  tma=0.0;
  tma = myms();

  jacobi();

#ifdef IRREGULAR
  mai_cyclic(grid1);
  mai_cyclic(grid2);
  irregular(gridSize);
#endif

  tma = myms() - tma;
  printf("Execution time %f\n",tma/1.e+6);
}
Example #3
0
BoundaryCreator::BoundaryCreator(TFile* file)
    : TSelectorInsert(),
      fNDets(4),
      fNStrips(16),
      fGrids(),
      fTheta(0),
      fPhi(0),
      fStripID(0),
      fDetID(0),
      fFile(file)
{
    InitializeGrids();
}
Example #4
0
int main(int argc, char *argv[]) {
  /* thread ids and attributes */
  pthread_t workerid[MAXWORKERS];
  pthread_attr_t attr;
  int i, j;
  double maxdiff = 0.0;
  FILE *results;

  /* set global thread attributes */
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

  /* initialize mutex and condition variable */
  pthread_mutex_init(&barrier, NULL);
  pthread_cond_init(&go, NULL);

  /* read command line and initialize grids */
  gridSize = atoi(argv[1]);
  numWorkers = atoi(argv[2]);
  numIters = atoi(argv[3]);
  stripSize = gridSize/numWorkers;
  InitializeGrids();

  start = times(&buffer);
  /* create the workers, then wait for them to finish */
  for (i = 0; i < numWorkers; i++)
    pthread_create(&workerid[i], &attr, Worker, (void *) i);
  for (i = 0; i < numWorkers; i++)
    pthread_join(workerid[i], NULL);

  finish = times(&buffer);
  /* print the results */
  for (i = 0; i < numWorkers; i++)
    if (maxdiff < maxDiff[i])
      maxdiff = maxDiff[i];
  printf("number of iterations:  %d\nmaximum difference:  %e\n",
          numIters, maxdiff);
  printf("start:  %d   finish:  %d\n", start, finish);
  printf("elapsed time:  %d\n", finish-start);
  results = fopen("results", "w");
  for (i = 1; i <= gridSize; i++) {
    for (j = 1; j <= gridSize; j++) {
      fprintf(results, "%f ", grid2[i][j]);
    }
    fprintf(results, "\n");
  }
}
Example #5
0
File: jacobi.c Project: pousa/minas
void *Worker(void *arg) {
  int myid = (int) arg;
  double maxdiff, temp;
  int i, j, iters;
  int first, last;
  double **grid1;
  double **grid2;
  unsigned long mask = 24+myid;
  unsigned long maxnode = 8*sizeof(unsigned long);
  char msg[215];

  printf("worker %d (pthread id %d) has started\n", myid, pthread_self());
	Barrier();

  grid1 = (double**)malloc((stripSize+3)*sizeof(double*));
  
  grid2 = (double**)malloc((stripSize+3)*sizeof(double*));

  for(i = 0; i <= stripSize; i++) {
	grid1[i] = (double*)malloc((gridSize+3)*sizeof(double));	
	grid2[i] = (double*)malloc((gridSize+3)*sizeof(double));	
  }

  /* attach memory to memtop */
  sprintf(msg,"var grid");
  attach_memory(gettid(),msg,grid1[0],stripSize*(gridSize+3)*sizeof(double));
  sprintf(msg,"var2");
  attach_memory(gettid(),msg,grid2[0],stripSize*(gridSize+3)*sizeof(double));
	

  InitializeGrids(grid1,grid2);

  for (iters = 1; iters <= numIters; iters++) {
    /* update my points */
    for (i = 1; i < stripSize; i++) {
      for (j = 1; j <= gridSize; j++) {
        grid2[i][j] = (grid1[i-1][j] + grid1[i+1][j] + 
                       grid1[i][j-1] + grid1[i][j+1]) * 0.25;
      }
    }
    Barrier();
    /* update my points again */
    for (i = 1; i < stripSize; i++) {
      for (j = 1; j <= gridSize; j++) {
        grid1[i][j] = (grid2[i-1][j] + grid2[i+1][j] +
               grid2[i][j-1] + grid2[i][j+1]) * 0.25;
      }
    }
    Barrier();
  }
  detach_memory(gettid(),msg,grid2[0]);
  /* compute the maximum difference in my strip and set global variable */
  maxdiff = 0.0;
  for (i = 1; i <= stripSize; i++) {
    for (j = 1; j <= gridSize; j++) {
      temp = grid1[i][j]-grid2[i][j];
      if (temp < 0)
        temp = -temp;
      if (maxdiff < temp)
        maxdiff = temp;
    }
  }
  maxDiff[myid] = maxdiff;
}