Example #1
0
int main (int argc, char ** argv) {

  int error = 0;
  int* displacements;
  int* sendCounts;
  pixel* workData;
  MPI_Datatype pixelType;
  int workDataSize;

  /* MPI INIT*/
  int ierr = MPI_Init(&argc, &argv);
  int myId, numberProc;
  
  MPI_Comm_size(MPI_COMM_WORLD, &numberProc);
  MPI_Comm_rank(MPI_COMM_WORLD, &myId);
  
  int xsize, ysize, colmax;
  pixel* src = (pixel*) malloc(MAX_PIXELS*sizeof(pixel));
  double stime, etime;

  if (myId == 0)
        printf("MPI thres Threads: %d, file: %s\n", numberProc, argv[1]);

  /* Take care of the arguments */

  if (argc != 3) {
    fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
    MPI_Finalize();
    exit(1);
  }

  /* read file */
  if(myId == 0)
  {
    if(read_ppm (argv[1], &xsize, &ysize, &colmax, (char *) src) != 0)
  	{
  	  error = 1;
  	}

    if (colmax > 255) {
      error = 1;
    }
  }

  //start time measure
  if(myId == 0)  
    stime = MPI_Wtime();

  MPI_Bcast(&error, 1, MPI_INT, 0, MPI_COMM_WORLD);
  MPI_Bcast(&xsize, 1, MPI_INT, 0, MPI_COMM_WORLD);  
  MPI_Bcast(&ysize, 1, MPI_INT, 0, MPI_COMM_WORLD);

  if (error == 1) {
    fprintf(stderr, "Too large maximum color-component value\n");
    MPI_Finalize();
    exit(1);
  }

  /* Scatter the data */
  workDataSize = ysize * xsize / numberProc + 1;
  displacements = (int*) malloc(numberProc * sizeof(int));
  sendCounts = (int*) malloc(numberProc * sizeof(int));
  workData = (pixel*) malloc(workDataSize * sizeof(pixel));
  memset(workData, 0, workDataSize * sizeof(pixel));
  
  calcDispls(xsize, ysize, numberProc, displacements, sendCounts);
  constructPixelDataType(&pixelType);

  MPI_Scatterv(src, sendCounts, displacements, pixelType, workData, sendCounts[myId], pixelType, 0, MPI_COMM_WORLD);

  thresfilter(sendCounts[myId], workData, numberProc, myId, xsize * ysize);

  MPI_Gatherv(workData, sendCounts[myId], pixelType, src, sendCounts, displacements, pixelType, 0, MPI_COMM_WORLD);

  if(myId == 0){
    etime = MPI_Wtime();
    printf("Total time: %.6f\n", etime - stime);
  
    /* write result */
    printf("Writing output file\n");
    
    if(write_ppm (argv[2], xsize, ysize, (char *)src) != 0)
      {
	MPI_Finalize();
	exit(1);
      }
  }

  free(displacements);
  free(sendCounts);
  free(src);
  free(workData);

  MPI_Finalize();
  return(0);
}
Example #2
0
int main (int argc, char ** argv) {
  int xsize, ysize, colmax, workload, work2, i, offset, np, me;
  pixel src[MAX_PIXELS], recvbuff[MAX_PIXELS];

  struct timespec stime, etime, rootstime, rootetime;

  /* Take care of the arguments */

  if (argc != 3) {
    fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
    exit(1);
  }
    
  MPI_Comm com = MPI_COMM_WORLD;
  MPI_Init( &argc, &argv );
  MPI_Comm_size( com, &np );
  MPI_Comm_rank( com, &me );

  int scounts[np], displs[np];


  MPI_Aint offsets[1] = {0};
  MPI_Datatype oldtypes[1] = {MPI_CHAR};
  int blockcounts[1] = {3};
  MPI_Datatype MPI_PIXEL;

  MPI_Type_struct(1, blockcounts,offsets, oldtypes,&MPI_PIXEL);
  MPI_Type_commit(&MPI_PIXEL);




  if(me==0)
    {		      
      clock_gettime(CLOCK_REALTIME, &rootstime);

      read_ppm(argv[1], &xsize, &ysize, &colmax, (char*)src);
      workload = (ysize/np)*xsize;
      work2= ysize%np;
      int offset = 0;
      for(i=0; i<np; i++)
	{
	  displs[i]=offset;
	  if(i<work2)
	    {
	      scounts[i] = workload+xsize;	
	    }
	  else{
	    scounts[i] = workload;
	  }
	  offset += scounts[i];
	}
      if (colmax > 255) {
	fprintf(stderr, "Too large maximum color-component value\n");
	exit(1);
      }
      clock_gettime(CLOCK_REALTIME, &rootetime);
	
      printf("Root reading and calculating work took: %2g secs\n", (rootetime.tv_sec  - rootstime.tv_sec) +
	     1e-9*(rootetime.tv_nsec  - rootstime.tv_nsec)) ;
    }


  clock_gettime(CLOCK_REALTIME, &stime);

  MPI_Bcast(&ysize, 1, MPI_INT, 0, com);
  MPI_Bcast(&xsize, 1, MPI_INT, 0, com);
  MPI_Bcast(scounts, np, MPI_INT, 0, com);
  MPI_Bcast(displs, np, MPI_INT, 0, com);
  // printf("Processor %d has scounts: %d and displs %d \n", me, scounts[me], displs[me]);


  
  MPI_Scatterv( src, scounts, displs, MPI_PIXEL, recvbuff, scounts[me], MPI_PIXEL, 0, com);  

  
  uint sum;
  for(i = 0, sum = 0; i < scounts[me]; i++) {
    sum += (uint)recvbuff[i].r + (uint)recvbuff[i].g + (uint)recvbuff[i].b;
  }
  sum /= scounts[me];

  uint globalsum;

  //printf("Processor %d has sum %d\n", me, sum);
  MPI_Reduce(&sum, &globalsum, 1, MPI_UNSIGNED , MPI_SUM, 0, com); 
  

    
    

  if(me==0)
    { 
      globalsum /= np;
      // printf("Processor %d has reduced sum %d\n", me, globalsum);
      
      // 
     
    }
  MPI_Bcast(&globalsum, 1, MPI_UNSIGNED, 0, com);

  thresfilter(xsize, scounts[me]/xsize, recvbuff, globalsum);
  
  MPI_Gatherv(recvbuff, scounts[me], MPI_PIXEL , src, scounts, displs, MPI_PIXEL, 0, com);
  clock_gettime(CLOCK_REALTIME, &etime);


  if(me == 0){
    write_ppm (argv[2], xsize, ysize, (char *)src);
  
  }


  
  printf("Filtering for process %d took: %2g secs\n", me ,(etime.tv_sec  - stime.tv_sec) +
	 1e-9*(etime.tv_nsec  - stime.tv_nsec)) ;


  MPI_Finalize();
  






}