1 / 27

MPI Send and Receive: Foundation and Operation

Learn about the foundational concepts and operation of MPI's send and receive functions, including how to package and route messages, specify message IDs, and use sending and receiving prototypes.

Download Presentation

MPI Send and Receive: Foundation and Operation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4

  2. MPI Send and Receive • Sending and receiving are two foundational concepts of MPI • Almost every single function in MPI can be implemented with basic send and receive calls

  3. Overview of Sending and Receiving • MPI’s send and receive calls operate in the following manner - Process 0 tries to send a message to process 1 - Process 0 packages the data into a buffer - Route the message to the right location -The location is defined by the process’s rank - Process 1 needs to acknowledge to want to receive 0’s data - Once process 1 acknowledges this, the data has been transmitted - Process 0 may go back to work

  4. Overview of Sending and Receiving • If Process 0 has to send different types of messages to processor 1 - MPI allows senders and receivers to specify message IDs with the message (tags) - Process 1 can request a message with a certain tag number - Messages with different tag numbers will be buffered until Processor is ready to receive them

  5. Overview of Sending and Receiving • Prototypes for MPI sending and receiving functions MPI_Send(void* data, int count, MPI_Datatype datatype, int destination, int tag, MPI_Comm communicator) MPI_Recv(void* data, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm communicator, MPI_Status* status)

  6. Overview of Sending and Receiving • The first argument is the data buffer • The second and third arguments describe the count and type of elements that reside in the buffer • MPI_Send sends the exact count of elements • MPI_Recv will receive at most the count of elements • The fourth and fifth arguments specify the rank of the sending/receiving process and the tag of the message • The sixth argument specifies the communicator • The last argument (for MPI_Recv only) provides information about the received message

  7. MPI Data Types

  8. Example 1 // Find out rank, size int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size);   int number; if (world_rank == 0) { number = -1; MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); } else if (world_rank == 1) { MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process 1 received number %d from process 0\n", number); }

  9. Example 2 int ping_pong_count = 0; int partner_rank = (world_rank + 1) % 2; while (ping_pong_count < PING_PONG_LIMIT) { if (world_rank == ping_pong_count % 2) { // Increment the ping pong count before you send it ping_pong_count++; MPI_Send(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD); printf("%d sent and incremented ping_pong_count " "%d to %d\n", world_rank, ping_pong_count, partner_rank); } else { MPI_Recv(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("%d received ping_pong_count %d from %d\n", world_rank, ping_pong_count, partner_rank); } }

  10. Example 2 0 sent and incremented ping_pong_count 1 to 1 0 received ping_pong_count 2 from 1 0 sent and incremented ping_pong_count 3 to 1 0 received ping_pong_count 4 from 1 0 sent and incremented ping_pong_count 5 to 1 0 received ping_pong_count 6 from 1 0 sent and incremented ping_pong_count 7 to 1 0 received ping_pong_count 8 from 1 0 sent and incremented ping_pong_count 9 to 1 0 received ping_pong_count 10 from 1 1 sent and incremented ping_pong_count 1 to 0 1 received ping_pong_count 2 from 0 1 sent and incremented ping_pong_count 3 to 0 1 received ping_pong_count 4 from 0 1 sent and incremented ping_pong_count 5 to 0 1 received ping_pong_count 6 from 0 1 sent and incremented ping_pong_count 7 to 0 1 received ping_pong_count 8 from 0 1 sent and incremented ping_pong_count 9 to 0 1 received ping_pong_count 10 from 0

  11. Example 3 int token; if (world_rank != 0) { MPI_Recv(&token, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process %d received token %d from process %d\n", world_rank, token, world_rank - 1); } else { // Set the token's value if you are process 0 token = -1; } MPI_Send(&token, 1, MPI_INT, (world_rank + 1) % world_size, 0, MPI_COMM_WORLD); // Now process 0 can receive from the last process. if (world_rank == 0) { MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process %d received token %d from process %d\n", world_rank, token, world_size - 1); }

  12. Example 3 • Initialize a value from process 0, and the value is passed around every single process • Terminates when process zero receives the value from the last process • Extra care is taken to assure that it doesn’t deadlock • Process 0 makes sure that it has completed its first send before it tries to receive the value from the last process • All of the other processes simply call MPI_Recv (receiving from their neighboring lower process) and then MPI_Send (sending the value to their neighboring higher process) to pass the value along the ring

  13. Example 3 Process 1 received token -1 from process 0 Process 2 received token -1 from process 1 Process 3 received token -1 from process 2 Process 4 received token -1 from process 3 Process 0 received token -1 from process 4

  14. File Input in C • Use fopen() to create a new file or open an existing file • Initialize an object of the type FILE • FILE contains all the information necessary to control the stream • Its prototype FILE *fopen( const char * filename, const char * mode );

  15. File Input in C

  16. File Input in C • Close a file using fclose() • Its prototype int fclose( FILE *fp ); • Return 0 on access, EOF if there is an error in closing the file • EOF is a constant defined in the header file stdio.h.

  17. File Input in C • Reading a file using fgetc(), fgets(), and fscanf() • fgetc() reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF • fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. It encounters a newline character '\n' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including new line character • fscanf() function to read strings from a file but it stops reading after the first space character encounters.

  18. Example 4 #include <stdio.h> main(int argc, char **argv) { FILE *fp; char buff[255]; fp = fopen(argv[1], "r"); fscanf(fp, "%s", buff); printf("1 : %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("2: %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("3: %s\n", buff ); fclose(fp); }

  19. Example 4 • Assume the contents of the input file is This is a test program. It is used to test file input functions. • When executing the program, its output is as follows. ./file input 1 : This 2: is a test program. 3: It is used to test file input functions.

  20. Dynamic Memory Allocation in C • The library stdlib.h should be included • malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form • Use free() explicitly to release space

  21. Example 5 #include <stdio.h> #include <stdlib.h> main(int argc, char **argv){ FILE *fp; int n,i,*ptr,sum=0; fp=fopen(argv[1], "r"); fscanf(fp, "%d", &n); printf("The number of elements are %d.\n", n); ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); }

  22. Example 5 for (i = 0; i < n; ++i) { if( fscanf(fp, "%d", ptr + i) != 1) { printf("wrong format: no enough numbers are given\n"); } } printf("The elements of the array is: \n"); for(i=0;i<n;++i) { printf("%d ",*(ptr+i)); sum+=*(ptr+i); } printf("\n"); printf("Sum=%d\n",sum); free(ptr); }

  23. Example 5 • If the input file input1 is as follows cat input1 5 1 2 3 4 5 • The output should be ./file1 input1 The number of elements are 5. The elements of the array is: 1 2 3 4 5 Sum=15

  24. MPI_Bcast() • A broadcast is one of the standard collective communication techniques • During a broadcast, one process sends the same data to all processes in a communicator • One of the main uses of broadcasting is to send out user input to a parallel program, or send out configuration parameters to all processes

  25. MPI_Bcast() • Its prototype MPI_Bcast(void* data, int count, MPI_Datatype datatype, int root, MPI_Comm communicator) • Each process has only one outgoing/incoming network link. The function is only using one network link from process 0 to send all the data • More efficient implementation is a tree-based communication algorithm that can use more of the available network links at once

  26. Example 6 #include <stdio.h> #include <mpi.h> int main(int argc, char **argv){ int i,myid, numprocs, count; int buffer[4]; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); count=4; if(myid == 0) { for(i=0;i<count;i++) buffer[i]=i; } MPI_Bcast(buffer,count,MPI_INT,source,MPI_COMM_WORLD); for(i=0;i<count;i++) printf("%d ",buffer[i]); printf("This is from processor %d\n", myid); MPI_Finalize(); }

  27. Example 6 • The output should be 0 1 2 3 This is from processor 0 0 1 2 3 This is from processor 2 0 1 2 3 This is from processor 1 0 1 2 3 This is from processor 3

More Related