Справочник Пользователя для Escali 4.4

Скачать
Страница из 81
Scali MPI Connect Release 4.4 Users Guide 
52
     /* read the image */
     for ( i = 0; i < numpixels; i ++ ) 
       {
 fscanf( infile, "%u", &buffer );
 pixels[i] = (unsigned char)buffer;
       }
     fclose( infile );
      /* calculate number of pixels for each node */
      my_count = numpixels / size;
   }
   /* broadcast to all nodes */
   MPI_Bcast( &my_count, 1, MPI_INT, 0, MPI_COMM_WORLD );
   /* scatter the image */
   MPI_Scatter( pixels, my_count, MPI_UNSIGNED_CHAR, recvbuf,
                my_count, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD );
   /* sum the squares of the pixels in the sub-image */
   my_sum = 0;
   for ( i = 0; i < my_count; i ++ )
      my_sum += recvbuf[i] * recvbuf[i];
   /* find the global sum of the squares */
   MPI_Reduce( &my_sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );
   /* let rank 0 compute the root mean square */
   if ( rank == 0 )
   {
      rms = sqrt( (double)sum / (double)numpixels );
   }
   /* rank 0 broadcasts the RMS to the other nodes */
   MPI_Bcast( &rms, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD );
   /* perform filtering operation (contrast enhancement) */
   for ( i = 0; i < my_count; i ++ )
   {
      val = 2 * recvbuf[i] - rms;
      if ( val < 0 ) recvbuf[i] = 0;
      else if ( val > 255 ) recvbuf[i] = 255;
      else recvbuf[i] = val;
   }
   /* gather back to rank 0 */
   MPI_Gather( recvbuf, my_count, MPI_UNSIGNED_CHAR, pixels,
               my_count, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD );
   /* dump the image from rank 0 */
   if ( rank == 0 )
     {
       outfile = fopen( "try.pgm", "w" );
       if ( ! outfile )
 {
   printf("unable to open try.ppm\n");
 }
       else 
 {
   fprintf( outfile, "%s\n", (unsigned char*)line );
   fprintf( outfile, "%d %d\n", height, width );
   fprintf( outfile, "255\n");
   /*
   numpixels = height * width; */
   for ( i = 0; i < numpixels; i ++ )
     {
       fprintf( outfile, "%d\n", (int)pixels[i] );