1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "smpi_comm.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_process.hpp"
10 #include "smpi_request.hpp"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
14 static int getPid(MPI_Comm, int);
15 static int getPid(MPI_Comm comm, int id)
17 simgrid::s4u::ActorPtr actor = comm->group()->actor(id);
18 return (actor == nullptr) ? MPI_UNDEFINED : actor->getPid();
21 /* PMPI User level calls */
22 extern "C" { // Obviously, the C MPI interface should use the C linkage
24 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
29 if (request == nullptr) {
31 } else if (comm == MPI_COMM_NULL) {
32 retval = MPI_ERR_COMM;
33 } else if (not datatype->is_valid()) {
34 retval = MPI_ERR_TYPE;
35 } else if (dst == MPI_PROC_NULL) {
38 *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
42 if (retval != MPI_SUCCESS && request != nullptr)
43 *request = MPI_REQUEST_NULL;
47 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
52 if (request == nullptr) {
54 } else if (comm == MPI_COMM_NULL) {
55 retval = MPI_ERR_COMM;
56 } else if (not datatype->is_valid()) {
57 retval = MPI_ERR_TYPE;
58 } else if (src == MPI_PROC_NULL) {
61 *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
65 if (retval != MPI_SUCCESS && request != nullptr)
66 *request = MPI_REQUEST_NULL;
70 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
75 if (request == nullptr) {
77 } else if (comm == MPI_COMM_NULL) {
78 retval = MPI_ERR_COMM;
79 } else if (not datatype->is_valid()) {
80 retval = MPI_ERR_TYPE;
81 } else if (dst == MPI_PROC_NULL) {
84 *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
88 if (retval != MPI_SUCCESS && request != nullptr)
89 *request = MPI_REQUEST_NULL;
93 int PMPI_Start(MPI_Request * request)
98 if (request == nullptr || *request == MPI_REQUEST_NULL) {
99 retval = MPI_ERR_REQUEST;
102 retval = MPI_SUCCESS;
108 int PMPI_Startall(int count, MPI_Request * requests)
112 if (requests == nullptr) {
113 retval = MPI_ERR_ARG;
115 retval = MPI_SUCCESS;
116 for (int i = 0; i < count; i++) {
117 if(requests[i] == MPI_REQUEST_NULL) {
118 retval = MPI_ERR_REQUEST;
121 if(retval != MPI_ERR_REQUEST) {
122 simgrid::smpi::Request::startall(count, requests);
129 int PMPI_Request_free(MPI_Request * request)
134 if (*request == MPI_REQUEST_NULL) {
135 retval = MPI_ERR_ARG;
137 simgrid::smpi::Request::unref(request);
138 retval = MPI_SUCCESS;
144 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
150 if (request == nullptr) {
151 retval = MPI_ERR_ARG;
152 } else if (comm == MPI_COMM_NULL) {
153 retval = MPI_ERR_COMM;
154 } else if (src == MPI_PROC_NULL) {
155 *request = MPI_REQUEST_NULL;
156 retval = MPI_SUCCESS;
157 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
158 retval = MPI_ERR_RANK;
159 } else if ((count < 0) || (buf==nullptr && count > 0)) {
160 retval = MPI_ERR_COUNT;
161 } else if (not datatype->is_valid()) {
162 retval = MPI_ERR_TYPE;
163 } else if(tag<0 && tag != MPI_ANY_TAG){
164 retval = MPI_ERR_TAG;
167 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
169 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
170 new simgrid::instr::Pt2PtTIData("Irecv", src_traced,
171 datatype->is_replayable() ? count : count * datatype->size(),
172 encode_datatype(datatype)));
174 *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
175 retval = MPI_SUCCESS;
177 TRACE_smpi_comm_out(my_proc_id);
181 if (retval != MPI_SUCCESS && request != nullptr)
182 *request = MPI_REQUEST_NULL;
187 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
192 if (request == nullptr) {
193 retval = MPI_ERR_ARG;
194 } else if (comm == MPI_COMM_NULL) {
195 retval = MPI_ERR_COMM;
196 } else if (dst == MPI_PROC_NULL) {
197 *request = MPI_REQUEST_NULL;
198 retval = MPI_SUCCESS;
199 } else if (dst >= comm->group()->size() || dst <0){
200 retval = MPI_ERR_RANK;
201 } else if ((count < 0) || (buf==nullptr && count > 0)) {
202 retval = MPI_ERR_COUNT;
203 } else if (not datatype->is_valid()) {
204 retval = MPI_ERR_TYPE;
205 } else if(tag<0 && tag != MPI_ANY_TAG){
206 retval = MPI_ERR_TAG;
208 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
209 int trace_dst = getPid(comm, dst);
210 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
211 new simgrid::instr::Pt2PtTIData("Isend", trace_dst,
212 datatype->is_replayable() ? count : count * datatype->size(),
213 encode_datatype(datatype)));
215 TRACE_smpi_send(my_proc_id, my_proc_id, trace_dst, tag, count * datatype->size());
217 *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
218 retval = MPI_SUCCESS;
220 TRACE_smpi_comm_out(my_proc_id);
224 if (retval != MPI_SUCCESS && request!=nullptr)
225 *request = MPI_REQUEST_NULL;
229 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
234 if (request == nullptr) {
235 retval = MPI_ERR_ARG;
236 } else if (comm == MPI_COMM_NULL) {
237 retval = MPI_ERR_COMM;
238 } else if (dst == MPI_PROC_NULL) {
239 *request = MPI_REQUEST_NULL;
240 retval = MPI_SUCCESS;
241 } else if (dst >= comm->group()->size() || dst <0){
242 retval = MPI_ERR_RANK;
243 } else if ((count < 0)|| (buf==nullptr && count > 0)) {
244 retval = MPI_ERR_COUNT;
245 } else if (not datatype->is_valid()) {
246 retval = MPI_ERR_TYPE;
247 } else if(tag<0 && tag != MPI_ANY_TAG){
248 retval = MPI_ERR_TAG;
250 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
251 int trace_dst = getPid(comm, dst);
252 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
253 new simgrid::instr::Pt2PtTIData("ISsend", trace_dst,
254 datatype->is_replayable() ? count : count * datatype->size(),
255 encode_datatype(datatype)));
256 TRACE_smpi_send(my_proc_id, my_proc_id, trace_dst, tag, count * datatype->size());
258 *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
259 retval = MPI_SUCCESS;
261 TRACE_smpi_comm_out(my_proc_id);
265 if (retval != MPI_SUCCESS && request!=nullptr)
266 *request = MPI_REQUEST_NULL;
270 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
275 if (comm == MPI_COMM_NULL) {
276 retval = MPI_ERR_COMM;
277 } else if (src == MPI_PROC_NULL) {
278 simgrid::smpi::Status::empty(status);
279 status->MPI_SOURCE = MPI_PROC_NULL;
280 retval = MPI_SUCCESS;
281 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
282 retval = MPI_ERR_RANK;
283 } else if ((count < 0) || (buf==nullptr && count > 0)) {
284 retval = MPI_ERR_COUNT;
285 } else if (not datatype->is_valid()) {
286 retval = MPI_ERR_TYPE;
287 } else if(tag<0 && tag != MPI_ANY_TAG){
288 retval = MPI_ERR_TAG;
290 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
291 int src_traced = getPid(comm, src);
292 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
293 new simgrid::instr::Pt2PtTIData("recv", src_traced,
294 datatype->is_replayable() ? count : count * datatype->size(),
295 encode_datatype(datatype)));
297 simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
298 retval = MPI_SUCCESS;
300 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
301 if (status != MPI_STATUS_IGNORE) {
302 src_traced = getPid(comm, status->MPI_SOURCE);
303 if (not TRACE_smpi_view_internals()) {
304 TRACE_smpi_recv(src_traced, my_proc_id, tag);
307 TRACE_smpi_comm_out(my_proc_id);
314 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
320 if (comm == MPI_COMM_NULL) {
321 retval = MPI_ERR_COMM;
322 } else if (dst == MPI_PROC_NULL) {
323 retval = MPI_SUCCESS;
324 } else if (dst >= comm->group()->size() || dst <0){
325 retval = MPI_ERR_RANK;
326 } else if ((count < 0) || (buf == nullptr && count > 0)) {
327 retval = MPI_ERR_COUNT;
328 } else if (not datatype->is_valid()) {
329 retval = MPI_ERR_TYPE;
330 } else if(tag < 0 && tag != MPI_ANY_TAG){
331 retval = MPI_ERR_TAG;
333 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
334 int dst_traced = getPid(comm, dst);
335 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
336 new simgrid::instr::Pt2PtTIData("send", dst_traced,
337 datatype->is_replayable() ? count : count * datatype->size(),
338 encode_datatype(datatype)));
339 if (not TRACE_smpi_view_internals()) {
340 TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, tag, count * datatype->size());
343 simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
344 retval = MPI_SUCCESS;
346 TRACE_smpi_comm_out(my_proc_id);
353 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
358 if (comm == MPI_COMM_NULL) {
359 retval = MPI_ERR_COMM;
360 } else if (dst == MPI_PROC_NULL) {
361 retval = MPI_SUCCESS;
362 } else if (dst >= comm->group()->size() || dst <0){
363 retval = MPI_ERR_RANK;
364 } else if ((count < 0) || (buf==nullptr && count > 0)) {
365 retval = MPI_ERR_COUNT;
366 } else if (not datatype->is_valid()) {
367 retval = MPI_ERR_TYPE;
368 } else if(tag<0 && tag != MPI_ANY_TAG){
369 retval = MPI_ERR_TAG;
371 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
372 int dst_traced = getPid(comm, dst);
373 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
374 new simgrid::instr::Pt2PtTIData("Ssend", dst_traced,
375 datatype->is_replayable() ? count : count * datatype->size(),
376 encode_datatype(datatype)));
377 TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, tag, count * datatype->size());
379 simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
380 retval = MPI_SUCCESS;
382 TRACE_smpi_comm_out(my_proc_id);
389 int PMPI_Sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf,
390 int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status)
396 if (comm == MPI_COMM_NULL) {
397 retval = MPI_ERR_COMM;
398 } else if (not sendtype->is_valid() || not recvtype->is_valid()) {
399 retval = MPI_ERR_TYPE;
400 } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
401 simgrid::smpi::Status::empty(status);
402 status->MPI_SOURCE = MPI_PROC_NULL;
403 retval = MPI_SUCCESS;
404 }else if (dst >= comm->group()->size() || dst <0 ||
405 (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
406 retval = MPI_ERR_RANK;
407 } else if ((sendcount < 0 || recvcount<0) ||
408 (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
409 retval = MPI_ERR_COUNT;
410 } else if((sendtag<0 && sendtag != MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
411 retval = MPI_ERR_TAG;
413 int my_proc_id = simgrid::s4u::Actor::self()->getPid();
414 int dst_traced = getPid(comm, dst);
415 int src_traced = getPid(comm, src);
417 // FIXME: Hack the way to trace this one
418 std::vector<int>* dst_hack = new std::vector<int>;
419 std::vector<int>* src_hack = new std::vector<int>;
420 dst_hack->push_back(dst_traced);
421 src_hack->push_back(src_traced);
422 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__,
423 new simgrid::instr::VarCollTIData(
424 "sendRecv", -1, sendtype->is_replayable() ? sendcount : sendcount * sendtype->size(),
425 dst_hack, recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(), src_hack,
426 encode_datatype(sendtype), encode_datatype(recvtype)));
428 TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, sendtag, sendcount * sendtype->size());
430 simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src,
431 recvtag, comm, status);
432 retval = MPI_SUCCESS;
434 TRACE_smpi_recv(src_traced, my_proc_id, recvtag);
435 TRACE_smpi_comm_out(my_proc_id);
442 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
443 MPI_Comm comm, MPI_Status* status)
446 if (not datatype->is_valid()) {
448 } else if (count < 0) {
449 return MPI_ERR_COUNT;
451 int size = datatype->get_extent() * count;
452 void* recvbuf = xbt_new0(char, size);
453 retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
454 if(retval==MPI_SUCCESS){
455 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, buf, count, datatype);
463 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
467 if (request == nullptr || flag == nullptr) {
468 retval = MPI_ERR_ARG;
469 } else if (*request == MPI_REQUEST_NULL) {
471 simgrid::smpi::Status::empty(status);
472 retval = MPI_SUCCESS;
474 int my_proc_id = ((*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
476 TRACE_smpi_testing_in(my_proc_id);
478 *flag = simgrid::smpi::Request::test(request,status);
480 TRACE_smpi_testing_out(my_proc_id);
481 retval = MPI_SUCCESS;
487 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
492 if (index == nullptr || flag == nullptr) {
493 retval = MPI_ERR_ARG;
495 *flag = simgrid::smpi::Request::testany(count, requests, index, status);
496 retval = MPI_SUCCESS;
502 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
507 if (flag == nullptr) {
508 retval = MPI_ERR_ARG;
510 *flag = simgrid::smpi::Request::testall(count, requests, statuses);
511 retval = MPI_SUCCESS;
517 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
521 if (status == nullptr) {
522 retval = MPI_ERR_ARG;
523 } else if (comm == MPI_COMM_NULL) {
524 retval = MPI_ERR_COMM;
525 } else if (source == MPI_PROC_NULL) {
526 simgrid::smpi::Status::empty(status);
527 status->MPI_SOURCE = MPI_PROC_NULL;
528 retval = MPI_SUCCESS;
530 simgrid::smpi::Request::probe(source, tag, comm, status);
531 retval = MPI_SUCCESS;
537 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
541 if (flag == nullptr) {
542 retval = MPI_ERR_ARG;
543 } else if (comm == MPI_COMM_NULL) {
544 retval = MPI_ERR_COMM;
545 } else if (source == MPI_PROC_NULL) {
547 simgrid::smpi::Status::empty(status);
548 status->MPI_SOURCE = MPI_PROC_NULL;
549 retval = MPI_SUCCESS;
551 simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
552 retval = MPI_SUCCESS;
558 // TODO: cheinrich: Move declaration to other file? Rename this function - it's used for PMPI_Wait*?
559 static void trace_smpi_recv_helper(MPI_Request* request, MPI_Status* status);
560 static void trace_smpi_recv_helper(MPI_Request* request, MPI_Status* status)
562 MPI_Request req = *request;
563 if (req != MPI_REQUEST_NULL) { // Received requests become null
564 int src_traced = req->src();
565 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
566 int dst_traced = req->dst();
567 if (req->flags() & RECV) { // Is this request a wait for RECV?
568 if (src_traced == MPI_ANY_SOURCE)
569 src_traced = (status != MPI_STATUSES_IGNORE) ? req->comm()->group()->rank(status->MPI_SOURCE) : req->src();
570 TRACE_smpi_recv(src_traced, dst_traced, req->tag());
575 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
581 simgrid::smpi::Status::empty(status);
583 if (request == nullptr) {
584 retval = MPI_ERR_ARG;
585 } else if (*request == MPI_REQUEST_NULL) {
586 retval = MPI_SUCCESS;
588 int my_proc_id = (*request)->comm() != MPI_COMM_NULL
589 ? simgrid::s4u::Actor::self()->getPid()
590 : -1; // TODO: cheinrich: Check if this correct or if it should be MPI_UNDEFINED
592 TRACE_smpi_comm_in(my_proc_id, __FUNCTION__, new simgrid::instr::NoOpTIData("wait"));
594 simgrid::smpi::Request::wait(request, status);
595 retval = MPI_SUCCESS;
597 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
598 TRACE_smpi_comm_out(my_proc_id);
599 trace_smpi_recv_helper(request, status);
606 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
608 if (index == nullptr)
616 int rank_traced = smpi_process()->index(); // FIXME: In PMPI_Wait, we check if the comm is null?
617 TRACE_smpi_comm_in(rank_traced, __FUNCTION__, new simgrid::instr::CpuTIData("waitAny", static_cast<double>(count)));
619 *index = simgrid::smpi::Request::waitany(count, requests, status);
621 if(*index!=MPI_UNDEFINED){
622 trace_smpi_recv_helper(&requests[*index], status);
623 TRACE_smpi_comm_out(rank_traced);
630 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
634 int rank_traced = smpi_process()->index(); // FIXME: In PMPI_Wait, we check if the comm is null?
635 TRACE_smpi_comm_in(rank_traced, __FUNCTION__, new simgrid::instr::CpuTIData("waitAll", static_cast<double>(count)));
637 int retval = simgrid::smpi::Request::waitall(count, requests, status);
639 for (int i = 0; i < count; i++) {
640 trace_smpi_recv_helper(&requests[i], &status[i]);
642 TRACE_smpi_comm_out(rank_traced);
648 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
653 if (outcount == nullptr) {
654 retval = MPI_ERR_ARG;
656 *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
657 retval = MPI_SUCCESS;
663 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
668 if (outcount == nullptr) {
669 retval = MPI_ERR_ARG;
671 *outcount = simgrid::smpi::Request::testsome(incount, requests, indices, status);
672 retval = MPI_SUCCESS;
678 MPI_Request PMPI_Request_f2c(MPI_Fint request){
679 return static_cast<MPI_Request>(simgrid::smpi::Request::f2c(request));
682 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
683 return request->c2f();