Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mpi.h>
  5.  
  6. void Make_numbers(long int [], int, int, int);
  7. void Sequential_sort(long int [], int);
  8. int Get_minpos(long int [], int);
  9.    
  10. main(int argc, char* argv[]) {
  11.  
  12.   long int *big_array, *local_array;
  13.   long int number;
  14.   int n=80, n_bar, p, my_rank, i;
  15.   double start, stop;
  16.  
  17.   MPI_Init(&argc, &argv);
  18.   MPI_Comm_size(MPI_COMM_WORLD, &p);
  19.   MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  20.  
  21.   n = atoi(argv[1]);  
  22.  
  23.   if (my_rank == 0) {
  24.     start = MPI_Wtime();
  25.  
  26.     // Checar se os parametros são válidos
  27.     if (n%p != 0) {
  28.       fprintf(stderr,"The number of processes must evenly divide total elements.\n");
  29.       MPI_Abort( MPI_COMM_WORLD, 2 );
  30.       exit(1);
  31.     }
  32.    
  33.     /* make big array */
  34.     big_array = malloc(n*sizeof(long int));
  35.     if (big_array==NULL) {
  36.       fprintf(stderr, "Big_array malloc failed!!\n");
  37.       MPI_Abort( MPI_COMM_WORLD, 3 );
  38.       exit(0);
  39.     }
  40.     Make_numbers(big_array, n, n/p, p);
  41.   }
  42.  
  43.   n_bar = n/p;  
  44.  
  45.   local_array = malloc(n_bar*sizeof(long int));
  46.   if (local_array==NULL) {
  47.     fprintf(stderr, "local_array malloc failed!!\n");
  48.     MPI_Abort( MPI_COMM_WORLD, 4 );
  49.     exit(0);
  50.   }
  51.  
  52.   /* Can use scatter if numbers are grouped in the big_array  */
  53.   MPI_Scatter(big_array,   n_bar, MPI_LONG, local_array, n_bar, MPI_LONG, 0, MPI_COMM_WORLD);  
  54.  
  55.   Sequential_sort(local_array, n_bar);
  56.  
  57.   MPI_Gather(local_array, n_bar, MPI_LONG, big_array,   n_bar, MPI_LONG, 0, MPI_COMM_WORLD);
  58.  
  59.   stop = MPI_Wtime();
  60.  
  61.   if (my_rank==0) {
  62.     for(i=0; i<n; i++) printf("%7ld %c", big_array[i],
  63.       i%8==7 ? '\n'  : ' ');
  64.     printf("\n\nTime to sort using %d processes = %lf msecs\n",
  65.      p, (stop - start)/0.001);
  66.   }
  67.  
  68.   free(local_array);
  69.   if (my_rank==0)
  70.     free(big_array);
  71.   MPI_Finalize();
  72.  
  73. }
  74.  
  75. void Make_numbers(long int big_array[], int n, int n_bar, int p) {
  76.  
  77.   int i, q;
  78.   MPI_Status status;
  79.  
  80.   for (q = 0; q < p; q++) {
  81.     for (i = 0; i < n_bar; i++) {
  82.       big_array[q*n_bar+i] =  random() % (2*n/p) + (q*2*n/p);
  83.     }
  84.   }
  85.  
  86. }
  87.  
  88. void Sequential_sort(long int array[], int size) {
  89.   /* Use selection sort to sort a list from smallest to largest */
  90.   int      eff_size, minpos;
  91.   long int temp;
  92.  
  93.   for(eff_size = size; eff_size > 1; eff_size--) {
  94.     minpos = Get_minpos(array, eff_size);
  95.     temp = array[minpos];
  96.     array[minpos] = array[eff_size-1];
  97.     array[eff_size-1] = temp;
  98.   }
  99. }
  100.  
  101. /* Return the index of the smallest element left */
  102. int Get_minpos(long int array[], int eff_size)
  103. {
  104.   int i, minpos = 0;
  105.  
  106.   for (i=0; i<eff_size; i++)
  107.     minpos = array[i] > array[minpos] ? i: minpos;
  108.   return minpos;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement