Exemple #1
0
/*
 * Juho Gävert & Santeri Hiltunen
 Starting point of calculation. Searches for temperature balance in Array for
 maximum iterations of max_iters.
 */
double calculate_heatconduct(Array* arr, unsigned int max_iters)
{

  if (max_iters == 0 || arr->width < 3 || arr->height < 3)
    return -1;

  Array* temp_arr = new_array(arr->width, arr->height);
  copy_array(arr, temp_arr);

  double prev_mean = -1;
  for (unsigned int i = 0; i < max_iters; ++i) {
    double new_mean = calculate_iteration(arr, temp_arr);

    swap_ptrs((void**) &arr, (void**) &temp_arr);

    if (conf.verbose) {
      printf("Iter: %d Mean: %.15f\n", i + 1, new_mean);
      if (conf.verbose > 1) {
        print_arr(arr);
      }
    }

    if (fabs(new_mean - prev_mean) < 0.0000000000001) {
      printf("Found balance after %d iterations.\n", i);
      del_array(temp_arr);
      return new_mean;
    }

    prev_mean = new_mean;
  }
  del_array(temp_arr);
  printf("Didn't find balance after %d iterations.\n", max_iters);
  return prev_mean;
}
Exemple #2
0
int main(int argc, char *argv[]) {
  XInitThreads();

  pthread_t thread_id[NTHREADS];
  int* from_to;

  int i, j, n, r;

  int q, comm_sz, my_rank;
  my_rank = 0;

  MPI_Init(NULL, NULL);
  MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

  if (my_rank == 0) {

    /* Initialize graphics */
    int gd, gm;
    gd = X11;
    gm = X11_640x480;
    initgraph (&gd, &gm, "");
    setbkcolor(BLACK);
    setcolor(WHITE);
    cleardevice();

    /* Initialize random */
    srand(time(NULL));

    /*  initialize elements of old to 0 or 1 */
    for (i = 1; i <= NI; i++){
      for (j = 1; j <= NJ; j++){
        r = rand() % MAXRAND;
        if (r < 100){
          game.old[i][j] = 1;
        } else {
          game.old[i][j] = 0;
        }
      }
    }

    /*  ITERATION */
    for (n = 0; n < NSTEPS; n++){

      /* corner boundary conditions */
      game.old[0][0] = game.old[NI][NJ];
      game.old[0][NJ + 1] = game.old[NI][1];
      game.old[NI + 1][NJ + 1] = game.old[1][1];
      game.old[NI + 1][0] = game.old[1][NJ];

      /* left-right boundary conditions */
      for(i = 1; i <= NI; i++){
        game.old[i][0] = game.old[i][NJ];
        game.old[i][NJ + 1] = game.old[i][1];
      }

      /* top-bottom boundary conditions */
      for(j = 1; j <= NJ; j++){
        game.old[0][j] = game.old[NI][j];
        game.old[NI + 1][j] = game.old[1][j];
      }

      /* calculate iteration -- ONLY SLAVES */
      //for (q = 1; q < comm_sz; q++) {
      //  MPI_Send(&game,1,game_type,...)
      //}
      calculate_iteration(1, NI);

      cleardevice();

      // THESE SHOULD BE AWESOME WITH GPU
      for(i = 0; i < NTHREADS; i++) {
        from_to = malloc(2 * sizeof(int));
        from_to[0] = (i * (NI / NTHREADS)) + 1;
        from_to[1] = (i + 1) * (NI / NTHREADS);
        pthread_create(
          &thread_id[i],
          NULL,
          print_iteration,
          (void *) from_to
        );
      }
      for(j = 0; j < NTHREADS; ++j)
        pthread_join(thread_id[j], NULL);

      /* copy new state into old state */
      for(i=1; i<=NI; i++){
        for(j=1; j<=NJ; j++){
          game.old[i][j] = game.new[i][j];
        }
      }

      struct timespec tim, tim2;
      tim.tv_sec = 1;
      tim.tv_nsec = 100000000L;
      nanosleep(&tim , &tim2);
    }
    if (getch() == KEY_ESC) {
      closegraph();
      return 0;
    }
  } else {