// use MPI_Bsend to send a single message in buffered mode // note that a buffered mode send operation can be started // whether or not a matching receive has been posted // in this case, the MPI_Bsend completes before a matching receive is posted #include // for input/output #include //for mpi routines #define BUFSIZE 2048 //The size of the messege being passed main( int argc, char** argv) { int rank; //the rank of this process int tag = 0; int bufsize, abufsize; float *buf, *abuf, *message; MPI_Status status; //not important here bufsize = (BUFSIZE * sizeof(float) + MPI_BSEND_OVERHEAD); buf = (float *)malloc(bufsize); message = (float *)malloc(sizeof(float) * BUFSIZE); MPI_Init(&argc, &argv); //Initializing mpi MPI_Comm_rank(MPI_COMM_WORLD, &rank); //Getting my rank if( rank == 0 ) { printf("Hello world! I am proc 0, sending to proc 1..\n"); MPI_Buffer_attach(buf, bufsize); /* a buffer can now be used by MPI_Bsend */ //send to the proc 1 MPI_Bsend(message, BUFSIZE, MPI_FLOAT, 1, tag, MPI_COMM_WORLD); MPI_Buffer_detach(&abuf, &abufsize); /* Buffer size reduced to zero */ free(abuf); } else if( rank == 1) { //sleep for 3 sec sleep(3); printf("Hello world! I am proc 1, just wake up!!\n"); //recieve from the proc 0 MPI_Recv(message, BUFSIZE, MPI_FLOAT, 0, tag, MPI_COMM_WORLD, &status); printf("Hello world! Received 1 message from proc 1!!\n"); } printf("Proc %d finished!!\n", rank); MPI_Finalize(); return 0; }