Parellel Computing - MPI, Image
Processor |
|
| Parellel Computing - MPI, Image Processor (PDF) Rahul Agarwal Images – Sample Output Original Image:
Sample Translation (parameter 54)
Sample Zoom r>1 (parameter 65 65 2)
Sample Zoom r<1 (parameters 65 65 0.5)
Source Code #include "mpi.h" #include <stdio.h> #include <math.h> #define BUFLEN 100 void main(int argc, char *argv[]) { int myid, numprocs; int data[640][480],tdata[640][480],data1[640][480],tdata1[1820][1440],tdata2[640][480],i, j, r,g,b, width, height, maxColor,k,l,temp; int ii,jj,dx,dy,scaler,angle,slot,row,wi,or,oc,nr,nc,sx,sy,teta; int buf[BUFLEN],buff[BUFLEN]; int wz,hz,th,tw,tcounter; float fbuf[1]; float zoomr; FILE *fin, *fout; char c1,c2; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid == 0){ fin = fopen("in.ppm","r"); fscanf(fin,"%c%c",&c1,&c2); fscanf(fin,"%d",&width); fscanf(fin,"%d",&height); fscanf(fin,"%d",&maxColor); maxColor++; for(i=0; i<height; i++){ for(j=0;j<width;j++){ if(!feof(fin)){ fscanf(fin,"%d %d %d",&r,&g,&b); data[i][j]=r*maxColor*maxColor+g*maxColor+b; data1[i][j]=r*maxColor*maxColor+g*maxColor+b; }else { data[i][j]=0; data1[i][j]=0; } } } slot=height/(numprocs-1); printf("a slot has %d lines\n",slot); //######################################################### //Translation //######################################################## printf("Shift parameters dx: "); scanf("%d",&dx); for(i=1;i<numprocs;i++){ buf[0]=(i-1)*slot;buf[1]=slot;buf[2]=dx%width;buf[3]=width; MPI_Send(buf,4,MPI_INT,i,1,MPI_COMM_WORLD); }; for(i=0;i<height;i++){ for(j=0;j<width;j++){ tdata[i][j]=0; } } for(i=0;i<(numprocs-1)*slot;i++){ for(j=0;j<width;j++){ MPI_Recv(buff,4,MPI_INT,MPI_ANY_SOURCE,99,MPI_COMM_WORLD,&status); or=buff[0];oc=buff[1];nc=buff[2]; if((0<=nc)&&(nc<width)){ tdata[i][nc]=data[i][oc]; }; }; } for(i=0; i<height; i++){ for(j=0;j<width;j++){ data[i][j]=tdata[i][j]; } } //######################################################## // Zooming //######################################################## printf("Zooming parameters x y r (max r=3 for max 640x480 image): "); scanf("%d %d %f",&sx,&sy,&zoomr); for(i=1;i<numprocs;i++){ buf[0]=(i-1)*slot;buf[1]=slot;buf[2]=(width/2)-sx;buf[3]=(height/2)-sy;buf[4]=width; MPI_Send(buf,5,MPI_INT,i,2,MPI_COMM_WORLD); fbuf[0]=zoomr; MPI_Send(fbuf,1,MPI_FLOAT,i,3,MPI_COMM_WORLD); }; wz=width*zoomr; hz=height*zoomr; for(i=0;i<hz;i++){ for(j=0;j<wz;j++){ tdata1[i][j]=0; } } for(i=0;i<(numprocs-1)*slot;i++){ for(j=0;j<width;j++){ MPI_Recv(buff,4,MPI_INT,MPI_ANY_SOURCE,98,MPI_COMM_WORLD,&status); or=buff[0];oc=buff[1];nr=buff[2];nc=buff[3]; tdata1[nr][nc]=data1[or][oc]; }; } for(i=0;i<height;i++){ for(j=0;j<width;j++){ tdata2[i][j]=0; } } for(i=0;i<height;i++){ for(j=0;j<width;j++){ th=i+(int)(height/zoomr); tw=i+(int)(width/zoomr); tdata2[i][j]=tdata1[i+(int)(height/zoomr)][j+(int)(width/zoomr)]; if(tdata2[i][j]==0){ for(k=i-1;k<=i+1;k++){ for(l=j-1;l<=j+1;l++){ if(tdata2[k][l]>0){ tdata2[i+temp][j+temp]=tdata2[k][l]; break; }}} } if(tdata2[i][j]==0){ for(k=i-2;k<=i+2;k++){ for(l=j-2;l<=j+2;l++){ if(tdata2[k][l]>0){ tdata2[i][j]=tdata2[k][l]; break; }}} } if(tdata2[i][j]==0){ for(k=i-3;k<=i+3;k++){ for(l=j-3;l<=j+3;l++){ if(tdata2[k][l]>0){ tdata2[i][j]=tdata2[k][l]; break; }}} } } } };//end master if(myid>0){ //slave translation MPI_Recv(buf,4,MPI_INT,0,1,MPI_COMM_WORLD,&status); row=buf[0];slot=buf[1];dx=buf[2];wi=buf[3]; for(or=row;or<row+slot;or++){ for(oc=0;oc<wi;oc++){ if(dx>=0){ nc=(oc+dx)%wi; } else{ if((oc+dx)<0){ nc=wi+oc+dx; } else{ nc=oc+dx; } } buf[0]=or;buf[1]=oc;buf[2]=nc; MPI_Send(buf,3,MPI_INT,0,99,MPI_COMM_WORLD); }; } //slave zooming MPI_Recv(buf,5,MPI_INT,0,2,MPI_COMM_WORLD,&status); row=buf[0];slot=buf[1];sx=buf[2];sy=buf[3];wi=buf[4]; MPI_Recv(fbuf,1,MPI_FLOAT,0,3,MPI_COMM_WORLD,&status); zoomr=fbuf[0]; for(or=row;or<row+slot;or++){ for(oc=0;oc<wi;oc++){ nc=(oc+sx)*zoomr; nr=(or+sy)*zoomr; buf[0]=or;buf[1]=oc;buf[2]=nr;buf[3]=nc; MPI_Send(buf,4,MPI_INT,0,98,MPI_COMM_WORLD); }; } };//end slaves if (myid == 0){ fout = fopen("translate.ppm","w"); fprintf(fout,"%c%c \n%d %d \n%d \n",c1,c2,width,height,maxColor); for(i=0; i<height; i++){ for(j=0;j<width;j++) { b=data[i][j]%maxColor; g=((data[i][j]-b)/maxColor)%maxColor; r=((data[i][j]-g*maxColor-b)/(maxColor*maxColor))%maxColor; fprintf(fout,"%d %d %d ",r,g,b); if(j%5==4) fprintf(fout,"\n"); }; } fclose(fout); fout = fopen("zoom.ppm","w"); fprintf(fout,"%c%c \n%d %d \n%d \n",c1,c2,width,height,maxColor); for(i=0; i<height; i++){ for(j=0;j<width;j++) { if(zoomr>=1){ b=tdata2[i][j]%maxColor; g=((tdata2[i][j]-b)/maxColor)%maxColor; r=((tdata2[i][j]-g*maxColor-b)/(maxColor*maxColor))%maxColor; fprintf(fout,"%d %d %d ",r,g,b); if(j%5==4) fprintf(fout,"\n"); } else{ b=tdata1[i][j]%maxColor; g=((tdata1[i][j]-b)/maxColor)%maxColor; r=((tdata1[i][j]-g*maxColor-b)/(maxColor*maxColor))%maxColor; fprintf(fout,"%d %d %d ",r,g,b); if(j%5==4) fprintf(fout,"\n"); } }; } fclose(fout); }; MPI_Finalize(); } |
|