Advertisement
Guest User

sscce

a guest
Jul 20th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include "mpi.h"
  5.  
  6. #define MAXPROC 16    /* Max number of procsses */
  7. #define TOTAL_FILES 7
  8.  
  9. int main(int argc, char* argv[]) {
  10.     int i, nprocs, tprocs, me, index;
  11.     const int tag  = 42;    /* Tag value for communication */
  12.  
  13.     MPI_Request recv_req[MAXPROC];  /* Request objects for non-blocking receive */
  14.     MPI_Request send_req[MAXPROC]; /* Request objects for non-blocking send */ 
  15.     MPI_Status status;              /* Status object for non-blocing receive */
  16.    
  17.     char myname[MPI_MAX_PROCESSOR_NAME];             /* Local host name string */
  18.     char hostname[MAXPROC][MPI_MAX_PROCESSOR_NAME];  /* Received host names */
  19.     int namelen;   
  20.    
  21.     MPI_Init(&argc, &argv);                /* Initialize MPI */
  22.     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);    /* Get nr of processes */
  23.     MPI_Comm_rank(MPI_COMM_WORLD, &me);    /* Get own identifier */
  24.    
  25.     MPI_Get_processor_name(myname, &namelen);  /* Get host name */
  26.     myname[namelen++] = (char)0;              /* Terminating null byte */
  27.    
  28.     /* First check that we have at least 2 and at most MAXPROC processes */
  29.     if (nprocs<2 || nprocs>MAXPROC) {
  30.         if (me == 0) {
  31.           printf("You have to use at least 2 and at most %d processes\n", MAXPROC);
  32.         }
  33.         MPI_Finalize(); exit(0);
  34.     }
  35.    
  36.     /* if TOTAL_FILES < nprocs then use only TOTAL_FILES + 1 procs */
  37.     tprocs = (TOTAL_FILES < nprocs) ? TOTAL_FILES + 1 : nprocs;
  38.     int done = -1;
  39.    
  40.     if (me == 0) {    /* Process 0 does this */
  41.        
  42.         int send_counter = 0, received_counter;
  43.    
  44.         for (i=1; i<tprocs; i++) {
  45.             MPI_Isend(&send_counter, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &send_req[i]);
  46.             ++send_counter;
  47.         }
  48.        
  49.         /* Receive a message from all other processes */
  50.         for (i=1; i<tprocs; i++) {
  51.             MPI_Irecv (hostname[i], namelen, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &recv_req[i]);
  52.         }      
  53.        
  54.         for (received_counter = 0; received_counter < TOTAL_FILES; received_counter++){
  55.  
  56.             /* Wait until at least one message has been received from any process other than 0*/
  57.             MPI_Waitany(tprocs-1, &recv_req[1], &index, &status);
  58.            
  59.             if (index == MPI_UNDEFINED) perror("Errorrrrrrr");         
  60.             printf("Received a message from process %d on %s\n", status.MPI_SOURCE, hostname[index+1]);
  61.            
  62.             if (send_counter < TOTAL_FILES){ /* si todavia faltan imagenes por procesar */
  63.                 MPI_Isend(&send_counter, 1, MPI_INT, status.MPI_SOURCE, tag, MPI_COMM_WORLD, &send_req[status.MPI_SOURCE]);
  64.                 ++send_counter;
  65.                 MPI_Irecv (hostname[status.MPI_SOURCE], namelen, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &recv_req[status.MPI_SOURCE]);
  66.             }  
  67.         }
  68.        
  69.         for (i=1; i<tprocs; i++) {
  70.             MPI_Isend(&done, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &send_req[i]);
  71.         }
  72.    
  73.     } else if (me < tprocs) { /* all other processes do this */
  74.    
  75.         int y;     
  76.         MPI_Recv(&y, 1, MPI_INT, 0,tag,MPI_COMM_WORLD,&status);
  77.        
  78.         while (y != -1) {                  
  79.             printf("Process %d: Received image %d\n", me, y);
  80.             sleep(me%3+1);  /* Let the processes sleep for 1-3 seconds */
  81.            
  82.             /* Send own identifier back to process 0 */
  83.             MPI_Send (myname, namelen, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
  84.             MPI_Recv(&y, 1, MPI_INT, 0,tag,MPI_COMM_WORLD,&status);        
  85.         }  
  86.     }
  87.    
  88.     MPI_Finalize();
  89.     exit(0);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement