+#include "xbt/time.h"
+#include "mc/mc.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
+ "Logging specific to SMPI (base)");
+XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
+XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
+
+static int match_recv(void* a, void* b) {
+ MPI_Request ref = (MPI_Request)a;
+ MPI_Request req = (MPI_Request)b;
+
+ xbt_assert0(ref, "Cannot match recv against null reference");
+ xbt_assert0(req, "Cannot match recv against null request");
+ return req->comm == ref->comm
+ && (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
+ && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
+}
+
+static int match_send(void* a, void* b) {
+ MPI_Request ref = (MPI_Request)a;
+ MPI_Request req = (MPI_Request)b;
+
+ xbt_assert0(ref, "Cannot match send against null reference");
+ xbt_assert0(req, "Cannot match send against null request");
+ return req->comm == ref->comm
+ && (req->src == MPI_ANY_SOURCE || req->src == ref->src)
+ && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
+}
+
+static MPI_Request build_request(void *buf, int count,
+ MPI_Datatype datatype, int src, int dst,
+ int tag, MPI_Comm comm, unsigned flags)
+{
+ MPI_Request request;
+
+ request = xbt_new(s_smpi_mpi_request_t, 1);
+ request->buf = buf;
+ request->size = smpi_datatype_size(datatype) * count;
+ request->src = src;
+ request->dst = dst;
+ request->tag = tag;
+ request->comm = comm;
+ request->action = NULL;
+ request->flags = flags;
+#ifdef HAVE_TRACING
+ request->send = 0;
+ request->recv = 0;
+#endif
+ return request;
+}
+
+/* MPI Low level calls */
+MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
+ int dst, int tag, MPI_Comm comm)
+{
+ MPI_Request request =
+ build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
+ comm, PERSISTENT | SEND);
+
+ return request;
+}
+
+MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
+ int src, int tag, MPI_Comm comm)
+{
+ MPI_Request request =
+ build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
+ comm, PERSISTENT | RECV);
+
+ return request;
+}
+
+void smpi_mpi_start(MPI_Request request)
+{
+ smx_rdv_t mailbox;
+
+ xbt_assert0(!request->action,
+ "Cannot (re)start a non-finished communication");
+ if(request->flags & RECV) {
+ print_request("New recv", request);
+ mailbox = smpi_process_mailbox();
+ request->action = SIMIX_req_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
+ } else {
+ print_request("New send", request);
+ mailbox = smpi_process_remote_mailbox(request->dst);
+ request->action = SIMIX_req_comm_isend(mailbox, request->size, -1.0,
+ request->buf, request->size, &match_send, request, 0);
+#ifdef HAVE_TRACING
+ SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
+#endif
+ }
+}