Advertisement
Guest User

monstru

a guest
Apr 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <mpi.h>
  5.  
  6. #include "pgm_IO.h"
  7.  
  8. #include "pgm_IO.c"
  9.  
  10. #define M 192
  11. #define N 128
  12.  
  13. #define NPROCS 4
  14.  
  15. #define MP M/NPROCS
  16. #define NP N
  17.  
  18. #define MAXITER 1500
  19. #define PRINTFREQ 200
  20.  
  21. int main(int argc, char **argv) {
  22.     float old[MP+2][NP+2], new[MP+2][NP+2], edge[MP+2][NP+2];
  23.    
  24.     float masterbuf[M][N];
  25.     float buf[MP][NP];
  26.    
  27.     int i, j, iter, maxiter;
  28.     char *filename;
  29.    
  30.     int my_rank, nproc, next, prev;
  31.     MPI_Status status;
  32.    
  33.     MPI_Init(&argc, &argv);
  34.    
  35.     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
  36.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  37.    
  38.     if(nproc != NPROCS) {
  39.         if(my_rank == 0) {
  40.             printf("EROARE: nproc = %d, NPROCS = %d\n", nproc, NPROCS);
  41.         }
  42.         MPI_Finalize();
  43.         exit(1);
  44.     }
  45.     next = my_rank + 1;
  46.     prev = my_rank - 1;
  47.    
  48.     if(next >= nproc) {
  49.         next = MPI_PROC_NULL;
  50.     }
  51. if (prev < 0)
  52. {
  53.   prev = MPI_PROC_NULL;
  54. }
  55. if (my_rank == 0)
  56. {
  57.   printf("In curs de procesare paralelaimaginea PGM %d x %d cu %d procese\n", M, N, NPROCS);
  58.   printf("Numar de iteratii = %d\n", MAXITER);
  59.   filename = "edge_img192x128.pgm";
  60.   printf("\nProcesul %d:- citesc < %s >\n", my_rank, filename);
  61.   pgm_read(filename, masterbuf, M, N);
  62.   printf("\n");
  63. }
  64.  
  65. MPI_Scatter(masterbuf, MP*NP, MPI_FLOAT, buf, MP*NP, MPI_FLOAT, 0, MPI_COMM_WORLD);
  66.  
  67. for(i = 1; i < MP+1; i++)
  68. {
  69.   for(j = 1; j<NP+1; j++)
  70.   {
  71.     edge[i][j] = buf[i+1][j-1];
  72.   }
  73. }
  74.  
  75. for(i=0; i<MP+2; i++)
  76. {
  77.   for(j=0; j<NP+2;j++)
  78.   {
  79.     old[i][j] = 255.0;
  80.   }
  81. }
  82.  
  83. for(iter = 1; iter <= MAXITER; iter++)
  84. {
  85.   if(iter%PRINTFREQ==0)
  86.   {
  87.     if(my_rank = 0)
  88.     {
  89.       printf("Iteratia %d\n", iter);
  90.     }
  91.   }
  92.   MPI_Sendrecv(&old[MP][1], NP, MPI_FLOAT, next, 1, &old[0][1], NP, MPI_FLOAT, prev, 1, MPI_COMM_WORLD, &status);
  93.   MPI_Sendrecv(&old[1][1], NP, MPI_FLOAT, prev, 2, &old[MP+1][1], NP, MPI_FLOAT, next, 2, MPI_COMM_WORLD, &status);
  94.  
  95.   for(i = 1; i < MP+1;i++)
  96.   {
  97.     for(j=1;j<NP+1;j++)
  98.     {
  99.       new[i][j] = 0.25*(old[i-1][j]+old[i+1][j] + old[i][j-1]+old[i][j+1]-edge[i][j]);
  100.     }
  101.   }
  102.  
  103. for(i = 1 ;i<MP+1;i++)
  104.   {
  105.     for(j=1;j<NP+1;j++)
  106.     {
  107.       old[i][j]=new[i][j];
  108.     }
  109.   }
  110. }
  111. if(my_rank == 0)
  112. {
  113.   printf("\nAu fost finalizate %d iteratii\n",iter-1);
  114. }
  115. for(i=1;i<MP+1;i++)
  116. {
  117.   for(j=1;j<NP+1;j++)
  118.   {
  119.     buf[i-1][j-1]=old[i][j];
  120.   }
  121. }
  122. MPI_Gather(buf,MP*NP,MPI_FLOAT,masterbuf,MP*NP,MPI_FLOAT,0,MPI_COMM_WORLD);
  123.  
  124. if(my_rank ==0)
  125. {
  126.   filename="poza192x128.pgm";
  127.   printf("\nSe scrie <%s>\n",filename);
  128.   pgm_write(filename,masterbuf,M,N);
  129. }
  130. MPI_Finalize();
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement