如何在Open MPI 3.0.0的MPI_Wait包装器中区分收发请求?
Great question! The standard MPI API doesn't expose a direct way to check if an MPI_Request is tied to a send or receive operation—but since you're working with Open MPI 3.0.0, you can leverage the implementation's internal structures to get this information. Here's how to do it:
Key Background
Open MPI stores all request metadata in an internal struct called ompi_request_t. This struct includes a req_type field that identifies the type of operation the request corresponds to. Send requests use the constant OMPI_REQUEST_SEND, while receive requests use OMPI_REQUEST_RECV.
Modified Wrapper Code
First, include Open MPI's internal request header to access these definitions. Then cast the MPI_Request to the internal struct type and check the req_type field:
/* ================== C Wrappers for MPI_Wait ================== */ #include <stdio.h> #include <ompi/request/request.h> // Required for Open MPI's request internals _EXTERN_C_ int PMPI_Wait(MPI_Request *request, MPI_Status *status); _EXTERN_C_ int MPI_Wait(MPI_Request *request, MPI_Status *status) { int _wrap_py_return_val = 0; // Guard against null requests to avoid crashes if (*request == MPI_REQUEST_NULL) { _wrap_py_return_val = PMPI_Wait(request, status); return _wrap_py_return_val; } // Cast the standard MPI_Request to Open MPI's internal request struct ompi_request_t *ompi_req = (ompi_request_t *)*request; // Check if it's a send or receive request if (ompi_req->req_type == OMPI_REQUEST_SEND) { printf("send\n"); } else if (ompi_req->req_type == OMPI_REQUEST_RECV) { printf("recv\n"); } else { // Handle other request types (e.g., collective operations) if needed printf("other request type\n"); } _wrap_py_return_val = PMPI_Wait(request, status); return _wrap_py_return_val; }
Important Notes
- Non-portability: This approach is specific to Open MPI 3.0.0. Internal structures and constants can change between Open MPI versions, and this won't work with other MPI implementations like MPICH.
- Compilation: When building your wrapper, ensure the compiler can find Open MPI's internal headers. Use
mpiccwith the include path fromompi_info --path sysincludedir, e.g.:mpicc -I$(ompi_info --path sysincludedir) your_wrapper.c -o your_wrapper - Request Validity: Always check for
MPI_REQUEST_NULLbefore casting to avoid segmentation faults, as shown in the code above.
内容的提问来源于stack exchange,提问作者JACK M

