elsemTim

AlltoallvYkobi2d

May 5th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <mpi.h>
  4.  
  5. #define WIDTH 5
  6. #define HEIGHT 9
  7. #define ITERS 1
  8.  
  9. int main(int argc, char** argv) {
  10.     MPI_Init(&argc, &argv);
  11.    
  12.     int proc_number = 0;            //номер процесса
  13.     int proc_count = 0;         //кол. проц.
  14.     MPI_Comm_rank(MPI_COMM_WORLD, &proc_number);
  15.     MPI_Comm_size(MPI_COMM_WORLD, &proc_count);
  16.    
  17.     if (proc_count > WIDTH) {
  18.         printf("Too many proccess for WIDTH of %d", WIDTH);
  19.         MPI_Finalize();
  20.         return 0;
  21.     }
  22.    
  23.     int width = 0;
  24.     int height = HEIGHT;
  25.     width = WIDTH / proc_count;
  26.     if (proc_number < WIDTH % proc_count) {
  27.         width += 1;
  28.     }
  29.     width += 2;
  30.     height += 2;
  31.    
  32.     //---
  33.    
  34.     double arr[width][height];
  35.     double arrNew[width][height];
  36.    
  37.     int i, j;
  38.     for (i = 0; i < width; i++) {
  39.         for (j = 0; j < height; j++) {
  40.             if ((proc_number == 0 && i == 0) || (j == 0) ||
  41.                 (proc_number == proc_count - 1 && i == width - 1) || (j == height - 1)) {
  42.                 arr[i][j] = 1;
  43.             arrNew[i][j] = 1;
  44.             } else {
  45.                 arr[i][j] = 0;
  46.             arrNew[i][j] = 0;
  47.             }
  48.         }
  49.     }
  50.    
  51.     //---
  52.    
  53.     int iter;
  54.     for (iter = 0; iter < ITERS; iter++) {
  55.         for (i = 1; i < width - 1; i++) {
  56.             for (j = 1; j < height - 1; j++) {
  57.                     arrNew[i][j] = arr[i][j - 1] + arr[i][j + 1];
  58.                     arrNew[i][j] += arr[i - 1][j] + arr[i + 1][j];
  59.                     arrNew[i][j] /= 4.0;
  60.             }
  61.         }
  62.    
  63.        
  64.         //---
  65.         int tt;
  66.         int displs[proc_count];
  67.          if (proc_number > 0) {
  68.         int counts[proc_count];
  69.         for (tt = 0; tt < proc_count; tt++) {
  70.             counts[tt] = 0;
  71.         }
  72.         counts[proc_number - 1] = height - 2;
  73.         for (tt = 0; tt < proc_count; tt++) {
  74.         displs[tt] = 0;
  75.         }
  76.        
  77.         MPI_Alltoallv(&arrNew[1][1], counts, displs, MPI_DOUBLE,
  78.                          &arr[0][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
  79.          }
  80.          
  81.         if (proc_number < proc_count - 1) {
  82.         int counts[proc_count];
  83.         for (tt = 0; tt < proc_count; tt++) {
  84.             counts[tt] = 0;
  85.         }
  86.         counts[proc_number + 1] = height - 2;
  87.         int displs[proc_count];
  88.         for (tt = 0; tt < proc_count; tt++) {
  89.         displs[tt] = 0;
  90.         }
  91.         MPI_Alltoallv(&arrNew[width - 2][1], counts, displs, MPI_DOUBLE,
  92.                          &arr[width - 1][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
  93.         }
  94.        
  95.        
  96.         for (i = 1 ; i < width - 1; i++)
  97.             for (j = 1 ; j < height - 1; j++)
  98.                 arr[i][j]=arrNew[i][j];
  99.        
  100.     }
  101.    
  102.     //---
  103.    
  104.     if (proc_number == 0) {
  105.         fclose(fopen("output.txt", "w"));
  106.     }
  107.    
  108.     int k;
  109.     for (k = 0; k < proc_count; k++) {
  110.         MPI_Barrier(MPI_COMM_WORLD);
  111.        
  112.         if (k == proc_number) {
  113.             FILE* out = fopen("output.txt", "a");
  114.            
  115.             int start = 1;
  116.             if (proc_number == 0) {
  117.                 start = 0;
  118.             }
  119.             int end = width - 1;
  120.             if (proc_number == proc_count - 1) {
  121.                 end = width;
  122.             }
  123.             for (i = start; i < end; i++) {
  124.                 for (j = 0; j < height; j++) {
  125.                     fprintf(out, "%lf ", arr[i][j]);
  126.                 }
  127.                 fprintf(out, "\n");
  128.             }
  129.             //fprintf(out, "\n---\n\n");
  130.            
  131.             fclose(out);
  132.         }
  133.     }
  134.  
  135.     MPI_Finalize();
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment