Exemple #1
0
int main(int argc, char **argv)
 {
  FILE *fout, *fin;
  int board[8];
  int lv;

  if ((fin = fopen("msquare.in", "r")) == NULL)
   {
    perror ("fopen fin");
    exit(1);
   }
  if ((fout = fopen("msquare.out", "w")) == NULL)
   {
    perror ("fopen fout");
    exit(1);
   }

  for (lv = 0; lv < 8; lv++) 
   {
    fscanf (fin, "%d", &board[lv]);
    board[lv]--; /* use 0-based instead of 1-based */
   }

  /* calculate the distance from each board to the ending board */
  do_dist(board);

  for (lv = 0; lv < 8; lv++) board[lv] = lv;

  /* output the distance from and the path from the initial configuration */
  fprintf (fout, "%d\n", dist[encode(board)]-1);
  walk(fout);

  return 0;
 }
static void do_output(int test_type, long long *array, int bins, int show_std_dev, int show_dist) {
   int s[10];
   long long min, max;
   double average, std;

   std = do_stats(array, &min, &max, &average);

   print_stats(test_type, min, max, average, std);

   if (show_std_dev) {
      do_std_dev(array, s, std, average);
      print_std_dev(s);
   }

   if (show_dist) {
      int *d;
      d = malloc(bins*sizeof(int));
      do_dist(array, min, max, bins, d);
      print_dist(min, max, bins, d);
      free(d);
   }
}
Exemple #3
0
int main ( int argc, char *argv[] ) {
    int id;
    int ierr;
    int p=3;
    double wtime = 0, total;


    int n,m, bad_edges=0, oriented=0, i, diameter, leftover, lines;
    int **tab;
    int print = 0;
    char FILENAME[100];

    int *process_lines, *process_start;

    /******************** MPI Initialisation *************************/
    ierr = MPI_Init ( &argc, &argv );
    if(ierr != MPI_SUCCESS) {
            perror("Error with initializing MPI");
            exit(1);
    }

    // Get the number of processes.
    ierr = MPI_Comm_size ( MPI_COMM_WORLD, &p );
    // Get the individual process ID.
    ierr = MPI_Comm_rank ( MPI_COMM_WORLD, &id );


    /******************** Problem Initialisation *************************/
    // Process 0 reads data + prints welcome
    n = 0;
    if (id==0) {
        for(i=1; i<argc; i++) {
            if(!strcmp(argv[i], "-print")) {
                        print = 1;
            } else if (!strcmp(argv[i], "-read")) {
                strcpy(FILENAME, argv[i+1]); i++;
            } else if (!strcmp(argv[i], "-random")) {
                n = atoi(argv[i+1]);
                oriented = atoi(argv[i+2]); i+=2;
            } else {
                n = 4000;
                oriented = 1;
            }
        }

        if (n>0)
            init_tab(n,&m,&tab,oriented); // last one = oriented or not ... 
        else
            bad_edges = read_tab(FILENAME, &n, &m, &tab, &oriented);

        fprintf(stderr, "Running program with %d rows and %d edges (%d are bad)\n",
        n, m, bad_edges);

        fprintf(stderr, "The number of processes is %d.\n", p);
    }

    // Distribute the problem size to all machines
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

    // Malloc the table, which was not yet done for the other processes
    if(id != 0)
        malloc_tab(n, &tab);

    // Distribute the table to all machines.
    for(i = 0; i < n; i++)
        MPI_Bcast(tab[i], n, MPI_INT, 0, MPI_COMM_WORLD);

    // Determine the lines and start per process
    process_lines = malloc(p * sizeof(int));
    process_start = malloc(p * sizeof(int));
    leftover = n % p;
    lines = n / p;

    for(i = 0; i < p; i++) {
        process_lines[i] = i < leftover ? lines + 1 : lines;
        process_start[i] = i * lines + (i < leftover ? i : leftover);
    }

    printf("%d - Pointers: %d & %d\n", id, (int)process_lines, (int)process_start);

    /******************** Distance calculation *************************/
    MPI_Barrier(MPI_COMM_WORLD);
    if(id == 0)
        wtime = MPI_Wtime();
    MPI_Barrier(MPI_COMM_WORLD);

    printf("%d - Pointers: %d & %d\n", id, (int)process_lines, (int)process_start);
    total = do_dist(tab, n, id, p, process_start, process_lines);
    printf("%d - Pointers: %d & %d\n", id, (int)process_lines, (int)process_start);

    if(id == 0) {
        wtime = MPI_Wtime() - wtime;
        fprintf (stderr, "Distance took %10.3f seconds.\n", wtime);
    }

    /******************** ASP *************************/
    MPI_Barrier(MPI_COMM_WORLD);
    if(id == 0)
        wtime = MPI_Wtime();

    MPI_Barrier(MPI_COMM_WORLD);
    //do_asp(tab, n, id, p, process_start, process_lines);
    do_asp_lin(tab, n, id, p, process_start, process_lines);

    if(id == 0) {
        wtime = MPI_Wtime() - wtime;
        fprintf (stderr, "ASP took %10.3f seconds.\n", wtime);
    }

    /******************** Diameter *************************/
    MPI_Barrier(MPI_COMM_WORLD);
    if(id == 0)
        wtime = MPI_Wtime();

    MPI_Barrier(MPI_COMM_WORLD);
    diameter = do_diam(tab, n, id, p, process_start, process_lines);

    if(id == 0) {
        wtime = MPI_Wtime() - wtime;
        fprintf (stderr, "Diameter took %10.3f seconds.\n", wtime);
    }

    if(id == 0 && print == 1) {
        print_tab(tab, n);
        printf("Total distance: %.0lf\n", total);
        printf("Diameter: %d\n", diameter);
    }

    // Clean up
    free_tab(tab, n);
    free(process_start);
    free(process_lines);

    // Shut down MPI.
    ierr = MPI_Finalize();

    return 0;
}