2014年3月30日 星期日

[ MPI 常見問題 ] MPI multiple dynamic array passing in C

來源自 這裡 
Question: 
I'm trying to ISend() two arrays: arr1,arr2 and an integer n which is the size of arr1,arr2. I understood from this post that sending a struct that contains all three is not an option since n is only known at run time. Obviously, I need n to be received first since otherwise the receiving process wouldn't know how many elements to receive. What's the most efficient way to achieve this without using the blokcing Send() ? 

Answer: 
Sending the size of the array is redundant (and inefficientas MPI provides a way to probe for incoming messages without receiving them, which provides just enough information in order to properly allocate memory. Probing is performed with MPI_PROBE, which looks a lot like MPI_RECV, except that it takes no buffer related arguments. The probe operation returns a status (MPI_Status) object which can then be queried for the number of elements of a given MPI datatype that can be extracted from the content of the message with MPI_GET_COUNT, therefore explicitly sending the number of elements becomes redundant. 

Here is a simple example with two ranks: 
  1. if (rank == 0)  
  2. {  
  3.     MPI_Request req;  
  4.   
  5.     // Send a message to rank 1  
  6.     MPI_Isend(arr1, n, MPI_DOUBLE, 10, MPI_COMM_WORLD, &req);  
  7.     // Do not forget to complete the request!  
  8.     MPI_Wait(&req, MPI_STATUS_IGNORE);  
  9. }  
  10. else if (rank == 1)  
  11. {  
  12.     MPI_Status status;  
  13.   
  14.     // Wait for a message from rank 0 with tag 0  
  15.     MPI_Probe(00, MPI_COMM_WORLD, &status);  
  16.     // Find out the number of elements in the message -> size goes to "n"  
  17.     MPI_Get_count(&status, MPI_DOUBLE, &n);  
  18.     // Allocate memory  
  19.     arr1 = malloc(n*sizeof(double));  
  20.     // Receive the message. ignore the status  
  21.     MPI_Recv(arr1, n, MPI_DOUBLE, 00, MPI_COMM_WORLD, MPI_STATUS_IGNORE);  
  22. }  
MPI_PROBE also accepts the wildcard rank MPI_ANY_SOURCE and the wildcard tag MPI_ANY_TAG. One can then consult the corresponding entry in the status structure in order to find out the actual sender rank and the actual message tag. 

Probing for the message size works as every message carries a header, called envelope. The envelope consists of the sender's rank, the receiver's rank, the message tag and the communicator. It also carries information about the total message size. Envelopes are sent as part of the initial handshake between the two communicating processes.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...