Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "mpi.h"
  4.  
  5. main(int argc, char *argv[]) {
  6.    int rank, size;
  7.     int         my_rank;       // rank of process      
  8.     int         p;             // number of processes  
  9.     int         source;        // rank of sender      
  10.     int         dest;          // rank of receiver    
  11.     int         tag = 0;       // tag for messages    
  12.     char        message[30];  // storage for message  
  13.     MPI_Status  status;        // return status for    
  14.                                // receive              
  15.     MPI_Init(&argc, &argv);
  16.  
  17.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  18.  
  19.     MPI_Comm_size(MPI_COMM_WORLD, &p);
  20.  
  21. /* si el rank no es cero, deben enviarle el mensaje al rank 0 */
  22.     if (my_rank != 0) {
  23.         sprintf(message, "Greetings from process %d!",
  24.             my_rank);
  25.         dest = 0;
  26. /* MPI_SEND(datos del mensaje, tamaño del mensaje, tipo de mensaje(char, int, float) , destinatario, tag, MPI_COMM_WORLD) */
  27.         MPI_Send(message, strlen(message)+1, MPI_CHAR,
  28.             dest, tag, MPI_COMM_WORLD);
  29. /* si el rank es 0, entonces recibe los mensajes de los otros rank */
  30.     } else {
  31.         for (source = 1; source < p; source++) {
  32. /*MPI_RECV( mensaje, tamaño del mensaje, tipo de mensaje, MPI_ANY_SOURCE = asincrono MPI_SOURCE: sincrono (lleva orden), tag
  33.         MPI_COM_WORD, estado) */
  34.             MPI_Recv(message, 100, MPI_CHAR, MPI_ANY_SOURCE, tag,
  35.                 MPI_COMM_WORLD, &status);
  36.             printf("Message received from %d\n", status.MPI_SOURCE);
  37.             printf("%s\n", message);
  38.         }
  39.     }
  40.  
  41.     MPI_Finalize();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement