#include #include #include #include #include #include #define MYSERVICE "myfriend" #define MASTER 0 int main(int argc, char* argv[]) { int rank, count; int i; float data[100]; char amsg[64]; char myport[MPI_MAX_PORT_NAME]; char myservice[64]; MPI_Status status; MPI_Comm intercomm; int intercomm_size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); printf("Processor %d (%d, Receiver) initialized\n", rank, getpid()); MPI_Open_port(MPI_INFO_NULL, myport); printf("Processor %d opened port %s\n", rank, myport); sprintf(myservice, "%s-%d", MYSERVICE, rank); printf("Publishing port %s as service %s\n", myport, myservice); MPI_Publish_name(myservice, MPI_INFO_NULL, myport); printf("Processor %d waiting for connections on %s, service %s...\n", rank, myport, myservice); MPI_Comm_accept(myport, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm); printf("Processor %d new connection on port %s\n", rank, myport); printf("Processor %d closing port %s\n", rank, myport); MPI_Close_port(myport); printf("Processor %d unpublishing service %s\n", rank, myservice); MPI_Unpublish_name(myservice, MPI_INFO_NULL, myport); MPI_Comm_remote_size(intercomm, &intercomm_size); printf("Processor %d remote comm size is %d\n", rank, intercomm_size); printf("Processor %d waiting for data from new intercomm...\n", rank); MPI_Recv(data, 100, MPI_FLOAT, MPI_ANY_SOURCE, MPI_ANY_TAG, intercomm, &status); MPI_Get_count(&status, MPI_FLOAT, &count); printf("Processor %d got %d elements from rank %d, tag %d: %f, %f, %f...\n", rank, count, status.MPI_SOURCE, status.MPI_TAG, data[0], data[1], data[2]); printf("Processor %d sending string back...\n", rank); strcpy(amsg, "ciao client"); MPI_Send(amsg, 12, MPI_CHAR, 0, 0, intercomm); printf("Processor %d data sent\n", rank); sleep(10); printf("Processor %d disconnecting communicator\n", rank); MPI_Comm_disconnect(&intercomm); printf("Processor %d finalizing\n", rank); MPI_Finalize(); printf("Processor %d Goodbye!\n", rank); }