Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <mpi.h>
- #include "pgm_IO.h"
- #include "pgm_IO.c"
- #define M 192
- #define N 128
- #define NPROCS 4
- #define MP M/NPROCS
- #define NP N
- #define MAXITER 1500
- #define PRINTFREQ 200
- int main(int argc, char **argv) {
- float old[MP+2][NP+2], new[MP+2][NP+2], edge[MP+2][NP+2];
- float masterbuf[M][N];
- float buf[MP][NP];
- int i, j, iter, maxiter;
- char *filename;
- int my_rank, nproc, next, prev;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &nproc);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- if(nproc != NPROCS) {
- if(my_rank == 0) {
- printf("EROARE: nproc = %d, NPROCS = %d\n", nproc, NPROCS);
- }
- MPI_Finalize();
- exit(1);
- }
- next = my_rank + 1;
- prev = my_rank - 1;
- if(next >= nproc) {
- next = MPI_PROC_NULL;
- }
- if (prev < 0)
- {
- prev = MPI_PROC_NULL;
- }
- if (my_rank == 0)
- {
- printf("In curs de procesare paralelaimaginea PGM %d x %d cu %d procese\n", M, N, NPROCS);
- printf("Numar de iteratii = %d\n", MAXITER);
- filename = "edge_img192x128.pgm";
- printf("\nProcesul %d:- citesc < %s >\n", my_rank, filename);
- pgm_read(filename, masterbuf, M, N);
- printf("\n");
- }
- MPI_Scatter(masterbuf, MP*NP, MPI_FLOAT, buf, MP*NP, MPI_FLOAT, 0, MPI_COMM_WORLD);
- for(i = 1; i < MP+1; i++)
- {
- for(j = 1; j<NP+1; j++)
- {
- edge[i][j] = buf[i+1][j-1];
- }
- }
- for(i=0; i<MP+2; i++)
- {
- for(j=0; j<NP+2;j++)
- {
- old[i][j] = 255.0;
- }
- }
- for(iter = 1; iter <= MAXITER; iter++)
- {
- if(iter%PRINTFREQ==0)
- {
- if(my_rank = 0)
- {
- printf("Iteratia %d\n", iter);
- }
- }
- MPI_Sendrecv(&old[MP][1], NP, MPI_FLOAT, next, 1, &old[0][1], NP, MPI_FLOAT, prev, 1, MPI_COMM_WORLD, &status);
- MPI_Sendrecv(&old[1][1], NP, MPI_FLOAT, prev, 2, &old[MP+1][1], NP, MPI_FLOAT, next, 2, MPI_COMM_WORLD, &status);
- for(i = 1; i < MP+1;i++)
- {
- for(j=1;j<NP+1;j++)
- {
- new[i][j] = 0.25*(old[i-1][j]+old[i+1][j] + old[i][j-1]+old[i][j+1]-edge[i][j]);
- }
- }
- for(i = 1 ;i<MP+1;i++)
- {
- for(j=1;j<NP+1;j++)
- {
- old[i][j]=new[i][j];
- }
- }
- }
- if(my_rank == 0)
- {
- printf("\nAu fost finalizate %d iteratii\n",iter-1);
- }
- for(i=1;i<MP+1;i++)
- {
- for(j=1;j<NP+1;j++)
- {
- buf[i-1][j-1]=old[i][j];
- }
- }
- MPI_Gather(buf,MP*NP,MPI_FLOAT,masterbuf,MP*NP,MPI_FLOAT,0,MPI_COMM_WORLD);
- if(my_rank ==0)
- {
- filename="poza192x128.pgm";
- printf("\nSe scrie <%s>\n",filename);
- pgm_write(filename,masterbuf,M,N);
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement