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. */
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/smpi/private.h"
11 #include "src/smpi/smpi_comm.hpp"
12 #include "src/smpi/smpi_coll.hpp"
13 #include "src/smpi/smpi_datatype_derived.hpp"
14 #include "src/smpi/smpi_op.hpp"
15 #include "src/smpi/smpi_process.hpp"
16 #include "src/smpi/smpi_request.hpp"
17 #include "src/smpi/smpi_status.hpp"
18 #include "src/smpi/smpi_win.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi, "Logging specific to SMPI (pmpi)");
22 //this function need to be here because of the calls to smpi_bench
23 void TRACE_smpi_set_category(const char *category)
25 //need to end bench otherwise categories for execution tasks are wrong
27 TRACE_internal_smpi_set_category (category);
28 //begin bench after changing process's category
32 /* PMPI User level calls */
33 extern "C" { // Obviously, the C MPI interface should use the C linkage
35 int PMPI_Init(int *argc, char ***argv)
37 xbt_assert(simgrid::s4u::Engine::isInitialized(),
38 "Your MPI program was not properly initialized. The easiest is to use smpirun to start it.");
39 // PMPI_Init is called only once per SMPI process
41 MPI_Initialized(&already_init);
42 if(already_init == 0){
43 simgrid::smpi::Process::init(argc, argv);
44 smpi_process()->mark_as_initialized();
45 int rank = smpi_process()->index();
46 TRACE_smpi_init(rank);
47 TRACE_smpi_computing_init(rank);
48 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
49 extra->type = TRACING_INIT;
50 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
51 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
63 int rank = smpi_process()->index();
64 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
65 extra->type = TRACING_FINALIZE;
66 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
68 smpi_process()->finalize();
70 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
71 TRACE_smpi_finalize(smpi_process()->index());
75 int PMPI_Finalized(int* flag)
77 *flag=smpi_process()!=nullptr ? smpi_process()->finalized() : 0;
81 int PMPI_Get_version (int *version,int *subversion){
82 *version = MPI_VERSION;
83 *subversion= MPI_SUBVERSION;
87 int PMPI_Get_library_version (char *version,int *len){
89 snprintf(version, MPI_MAX_LIBRARY_VERSION_STRING, "SMPI Version %d.%d. Copyright The Simgrid Team 2007-2017",
90 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR);
91 *len = strlen(version) > MPI_MAX_LIBRARY_VERSION_STRING ? MPI_MAX_LIBRARY_VERSION_STRING : strlen(version);
96 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
98 if (provided != nullptr) {
99 *provided = MPI_THREAD_SINGLE;
101 return MPI_Init(argc, argv);
104 int PMPI_Query_thread(int *provided)
106 if (provided == nullptr) {
109 *provided = MPI_THREAD_SINGLE;
114 int PMPI_Is_thread_main(int *flag)
116 if (flag == nullptr) {
119 *flag = smpi_process()->index() == 0;
124 int PMPI_Abort(MPI_Comm comm, int errorcode)
127 // FIXME: should kill all processes in comm instead
128 simcall_process_kill(SIMIX_process_self());
134 return smpi_mpi_wtime();
137 extern double sg_maxmin_precision;
140 return sg_maxmin_precision;
143 int PMPI_Address(void *location, MPI_Aint * address)
145 if (address==nullptr) {
148 *address = reinterpret_cast<MPI_Aint>(location);
153 int PMPI_Get_address(void *location, MPI_Aint * address)
155 return PMPI_Address(location, address);
158 int PMPI_Type_free(MPI_Datatype * datatype)
160 /* Free a predefined datatype is an error according to the standard, and should be checked for */
161 if (*datatype == MPI_DATATYPE_NULL) {
164 simgrid::smpi::Datatype::unref(*datatype);
169 int PMPI_Type_size(MPI_Datatype datatype, int *size)
171 if (datatype == MPI_DATATYPE_NULL) {
173 } else if (size == nullptr) {
176 *size = static_cast<int>(datatype->size());
181 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
183 if (datatype == MPI_DATATYPE_NULL) {
185 } else if (size == nullptr) {
188 *size = static_cast<MPI_Count>(datatype->size());
193 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
195 if (datatype == MPI_DATATYPE_NULL) {
197 } else if (lb == nullptr || extent == nullptr) {
200 return datatype->extent(lb, extent);
204 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
206 return PMPI_Type_get_extent(datatype, lb, extent);
209 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
211 if (datatype == MPI_DATATYPE_NULL) {
213 } else if (extent == nullptr) {
216 *extent = datatype->get_extent();
221 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
223 if (datatype == MPI_DATATYPE_NULL) {
225 } else if (disp == nullptr) {
228 *disp = datatype->lb();
233 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
235 if (datatype == MPI_DATATYPE_NULL) {
237 } else if (disp == nullptr) {
240 *disp = datatype->ub();
245 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
246 int retval = MPI_SUCCESS;
247 if (datatype == MPI_DATATYPE_NULL) {
250 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
251 //error when duplicating, free the new datatype
252 if(retval!=MPI_SUCCESS){
253 simgrid::smpi::Datatype::unref(*newtype);
254 *newtype = MPI_DATATYPE_NULL;
260 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
262 if (function == nullptr || op == nullptr) {
265 *op = new simgrid::smpi::Op(function, (commute!=0));
270 int PMPI_Op_free(MPI_Op * op)
274 } else if (*op == MPI_OP_NULL) {
283 int PMPI_Op_commutative(MPI_Op op, int* commute){
284 if (op == MPI_OP_NULL) {
286 } else if (commute==nullptr){
289 *commute = op->is_commutative();
294 int PMPI_Group_free(MPI_Group * group)
296 if (group == nullptr) {
299 if(*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_EMPTY)
300 simgrid::smpi::Group::unref(*group);
301 *group = MPI_GROUP_NULL;
306 int PMPI_Group_size(MPI_Group group, int *size)
308 if (group == MPI_GROUP_NULL) {
309 return MPI_ERR_GROUP;
310 } else if (size == nullptr) {
313 *size = group->size();
318 int PMPI_Group_rank(MPI_Group group, int *rank)
320 if (group == MPI_GROUP_NULL) {
321 return MPI_ERR_GROUP;
322 } else if (rank == nullptr) {
325 *rank = group->rank(smpi_process()->index());
330 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1, MPI_Group group2, int *ranks2)
332 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
333 return MPI_ERR_GROUP;
335 for (int i = 0; i < n; i++) {
336 if(ranks1[i]==MPI_PROC_NULL){
337 ranks2[i]=MPI_PROC_NULL;
339 int index = group1->index(ranks1[i]);
340 ranks2[i] = group2->rank(index);
347 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
349 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
350 return MPI_ERR_GROUP;
351 } else if (result == nullptr) {
354 *result = group1->compare(group2);
359 int PMPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
362 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
363 return MPI_ERR_GROUP;
364 } else if (newgroup == nullptr) {
367 return group1->group_union(group2, newgroup);
371 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
374 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
375 return MPI_ERR_GROUP;
376 } else if (newgroup == nullptr) {
379 return group1->intersection(group2,newgroup);
383 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
385 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
386 return MPI_ERR_GROUP;
387 } else if (newgroup == nullptr) {
390 return group1->difference(group2,newgroup);
394 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
396 if (group == MPI_GROUP_NULL) {
397 return MPI_ERR_GROUP;
398 } else if (newgroup == nullptr) {
401 return group->incl(n, ranks, newgroup);
405 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
407 if (group == MPI_GROUP_NULL) {
408 return MPI_ERR_GROUP;
409 } else if (newgroup == nullptr) {
414 if (group != MPI_COMM_WORLD->group()
415 && group != MPI_COMM_SELF->group() && group != MPI_GROUP_EMPTY)
418 } else if (n == group->size()) {
419 *newgroup = MPI_GROUP_EMPTY;
422 return group->excl(n,ranks,newgroup);
427 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
429 if (group == MPI_GROUP_NULL) {
430 return MPI_ERR_GROUP;
431 } else if (newgroup == nullptr) {
435 *newgroup = MPI_GROUP_EMPTY;
438 return group->range_incl(n,ranges,newgroup);
443 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
445 if (group == MPI_GROUP_NULL) {
446 return MPI_ERR_GROUP;
447 } else if (newgroup == nullptr) {
452 if (group != MPI_COMM_WORLD->group() && group != MPI_COMM_SELF->group() &&
453 group != MPI_GROUP_EMPTY)
457 return group->range_excl(n,ranges,newgroup);
462 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
464 if (comm == MPI_COMM_NULL) {
466 } else if (rank == nullptr) {
469 *rank = comm->rank();
474 int PMPI_Comm_size(MPI_Comm comm, int *size)
476 if (comm == MPI_COMM_NULL) {
478 } else if (size == nullptr) {
481 *size = comm->size();
486 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
488 if (comm == MPI_COMM_NULL) {
490 } else if (name == nullptr || len == nullptr) {
493 comm->get_name(name, len);
498 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
500 if (comm == MPI_COMM_NULL) {
502 } else if (group == nullptr) {
505 *group = comm->group();
506 if (*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_NULL && *group != MPI_GROUP_EMPTY)
512 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
514 if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
516 } else if (result == nullptr) {
519 if (comm1 == comm2) { /* Same communicators means same groups */
522 *result = comm1->group()->compare(comm2->group());
523 if (*result == MPI_IDENT) {
524 *result = MPI_CONGRUENT;
531 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
533 if (comm == MPI_COMM_NULL) {
535 } else if (newcomm == nullptr) {
538 return comm->dup(newcomm);
542 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
544 if (comm == MPI_COMM_NULL) {
546 } else if (group == MPI_GROUP_NULL) {
547 return MPI_ERR_GROUP;
548 } else if (newcomm == nullptr) {
550 } else if(group->rank(smpi_process()->index())==MPI_UNDEFINED){
551 *newcomm= MPI_COMM_NULL;
555 *newcomm = new simgrid::smpi::Comm(group, nullptr);
560 int PMPI_Comm_free(MPI_Comm * comm)
562 if (comm == nullptr) {
564 } else if (*comm == MPI_COMM_NULL) {
567 simgrid::smpi::Comm::destroy(*comm);
568 *comm = MPI_COMM_NULL;
573 int PMPI_Comm_disconnect(MPI_Comm * comm)
575 /* TODO: wait until all communication in comm are done */
576 if (comm == nullptr) {
578 } else if (*comm == MPI_COMM_NULL) {
581 simgrid::smpi::Comm::destroy(*comm);
582 *comm = MPI_COMM_NULL;
587 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
592 if (comm_out == nullptr) {
593 retval = MPI_ERR_ARG;
594 } else if (comm == MPI_COMM_NULL) {
595 retval = MPI_ERR_COMM;
597 *comm_out = comm->split(color, key);
598 retval = MPI_SUCCESS;
605 int PMPI_Comm_create_group(MPI_Comm comm, MPI_Group group, int, MPI_Comm* comm_out)
610 if (comm_out == nullptr) {
611 retval = MPI_ERR_ARG;
612 } else if (comm == MPI_COMM_NULL) {
613 retval = MPI_ERR_COMM;
615 retval = MPI_Comm_create(comm, group, comm_out);
622 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
627 if (request == nullptr) {
628 retval = MPI_ERR_ARG;
629 } else if (comm == MPI_COMM_NULL) {
630 retval = MPI_ERR_COMM;
631 } else if (not datatype->is_valid()) {
632 retval = MPI_ERR_TYPE;
633 } else if (dst == MPI_PROC_NULL) {
634 retval = MPI_SUCCESS;
636 *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
637 retval = MPI_SUCCESS;
640 if (retval != MPI_SUCCESS && request != nullptr)
641 *request = MPI_REQUEST_NULL;
645 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
650 if (request == nullptr) {
651 retval = MPI_ERR_ARG;
652 } else if (comm == MPI_COMM_NULL) {
653 retval = MPI_ERR_COMM;
654 } else if (not datatype->is_valid()) {
655 retval = MPI_ERR_TYPE;
656 } else if (src == MPI_PROC_NULL) {
657 retval = MPI_SUCCESS;
659 *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
660 retval = MPI_SUCCESS;
663 if (retval != MPI_SUCCESS && request != nullptr)
664 *request = MPI_REQUEST_NULL;
668 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
673 if (request == nullptr) {
674 retval = MPI_ERR_ARG;
675 } else if (comm == MPI_COMM_NULL) {
676 retval = MPI_ERR_COMM;
677 } else if (not datatype->is_valid()) {
678 retval = MPI_ERR_TYPE;
679 } else if (dst == MPI_PROC_NULL) {
680 retval = MPI_SUCCESS;
682 *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
683 retval = MPI_SUCCESS;
686 if (retval != MPI_SUCCESS && request != nullptr)
687 *request = MPI_REQUEST_NULL;
691 int PMPI_Start(MPI_Request * request)
696 if (request == nullptr || *request == MPI_REQUEST_NULL) {
697 retval = MPI_ERR_REQUEST;
700 retval = MPI_SUCCESS;
706 int PMPI_Startall(int count, MPI_Request * requests)
710 if (requests == nullptr) {
711 retval = MPI_ERR_ARG;
713 retval = MPI_SUCCESS;
714 for (int i = 0; i < count; i++) {
715 if(requests[i] == MPI_REQUEST_NULL) {
716 retval = MPI_ERR_REQUEST;
719 if(retval != MPI_ERR_REQUEST) {
720 simgrid::smpi::Request::startall(count, requests);
727 int PMPI_Request_free(MPI_Request * request)
732 if (*request == MPI_REQUEST_NULL) {
733 retval = MPI_ERR_ARG;
735 simgrid::smpi::Request::unref(request);
736 retval = MPI_SUCCESS;
742 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
748 if (request == nullptr) {
749 retval = MPI_ERR_ARG;
750 } else if (comm == MPI_COMM_NULL) {
751 retval = MPI_ERR_COMM;
752 } else if (src == MPI_PROC_NULL) {
753 *request = MPI_REQUEST_NULL;
754 retval = MPI_SUCCESS;
755 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
756 retval = MPI_ERR_RANK;
757 } else if ((count < 0) || (buf==nullptr && count > 0)) {
758 retval = MPI_ERR_COUNT;
759 } else if (not datatype->is_valid()) {
760 retval = MPI_ERR_TYPE;
761 } else if(tag<0 && tag != MPI_ANY_TAG){
762 retval = MPI_ERR_TAG;
765 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
766 int src_traced = comm->group()->index(src);
768 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
769 extra->type = TRACING_IRECV;
770 extra->src = src_traced;
773 extra->datatype1 = encode_datatype(datatype, &known);
774 int dt_size_send = 1;
776 dt_size_send = datatype->size();
777 extra->send_size = count*dt_size_send;
778 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
780 *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
781 retval = MPI_SUCCESS;
783 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
787 if (retval != MPI_SUCCESS && request != nullptr)
788 *request = MPI_REQUEST_NULL;
793 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
798 if (request == nullptr) {
799 retval = MPI_ERR_ARG;
800 } else if (comm == MPI_COMM_NULL) {
801 retval = MPI_ERR_COMM;
802 } else if (dst == MPI_PROC_NULL) {
803 *request = MPI_REQUEST_NULL;
804 retval = MPI_SUCCESS;
805 } else if (dst >= comm->group()->size() || dst <0){
806 retval = MPI_ERR_RANK;
807 } else if ((count < 0) || (buf==nullptr && count > 0)) {
808 retval = MPI_ERR_COUNT;
809 } else if (not datatype->is_valid()) {
810 retval = MPI_ERR_TYPE;
811 } else if(tag<0 && tag != MPI_ANY_TAG){
812 retval = MPI_ERR_TAG;
814 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
815 int dst_traced = comm->group()->index(dst);
816 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
817 extra->type = TRACING_ISEND;
819 extra->dst = dst_traced;
821 extra->datatype1 = encode_datatype(datatype, &known);
822 int dt_size_send = 1;
824 dt_size_send = datatype->size();
825 extra->send_size = count*dt_size_send;
826 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
827 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
829 *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
830 retval = MPI_SUCCESS;
832 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
836 if (retval != MPI_SUCCESS && request!=nullptr)
837 *request = MPI_REQUEST_NULL;
841 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
846 if (request == nullptr) {
847 retval = MPI_ERR_ARG;
848 } else if (comm == MPI_COMM_NULL) {
849 retval = MPI_ERR_COMM;
850 } else if (dst == MPI_PROC_NULL) {
851 *request = MPI_REQUEST_NULL;
852 retval = MPI_SUCCESS;
853 } else if (dst >= comm->group()->size() || dst <0){
854 retval = MPI_ERR_RANK;
855 } else if ((count < 0)|| (buf==nullptr && count > 0)) {
856 retval = MPI_ERR_COUNT;
857 } else if (not datatype->is_valid()) {
858 retval = MPI_ERR_TYPE;
859 } else if(tag<0 && tag != MPI_ANY_TAG){
860 retval = MPI_ERR_TAG;
862 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
863 int dst_traced = comm->group()->index(dst);
864 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
865 extra->type = TRACING_ISSEND;
867 extra->dst = dst_traced;
869 extra->datatype1 = encode_datatype(datatype, &known);
870 int dt_size_send = 1;
872 dt_size_send = datatype->size();
873 extra->send_size = count*dt_size_send;
874 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
875 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
877 *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
878 retval = MPI_SUCCESS;
880 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
884 if (retval != MPI_SUCCESS && request!=nullptr)
885 *request = MPI_REQUEST_NULL;
889 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
894 if (comm == MPI_COMM_NULL) {
895 retval = MPI_ERR_COMM;
896 } else if (src == MPI_PROC_NULL) {
897 simgrid::smpi::Status::empty(status);
898 status->MPI_SOURCE = MPI_PROC_NULL;
899 retval = MPI_SUCCESS;
900 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
901 retval = MPI_ERR_RANK;
902 } else if ((count < 0) || (buf==nullptr && count > 0)) {
903 retval = MPI_ERR_COUNT;
904 } else if (not datatype->is_valid()) {
905 retval = MPI_ERR_TYPE;
906 } else if(tag<0 && tag != MPI_ANY_TAG){
907 retval = MPI_ERR_TAG;
909 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
910 int src_traced = comm->group()->index(src);
911 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
912 extra->type = TRACING_RECV;
913 extra->src = src_traced;
916 extra->datatype1 = encode_datatype(datatype, &known);
917 int dt_size_send = 1;
919 dt_size_send = datatype->size();
920 extra->send_size = count * dt_size_send;
921 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
923 simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
924 retval = MPI_SUCCESS;
926 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
927 if (status != MPI_STATUS_IGNORE) {
928 src_traced = comm->group()->index(status->MPI_SOURCE);
929 if (not TRACE_smpi_view_internals()) {
930 TRACE_smpi_recv(rank, src_traced, rank, tag);
933 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
940 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
946 if (comm == MPI_COMM_NULL) {
947 retval = MPI_ERR_COMM;
948 } else if (dst == MPI_PROC_NULL) {
949 retval = MPI_SUCCESS;
950 } else if (dst >= comm->group()->size() || dst <0){
951 retval = MPI_ERR_RANK;
952 } else if ((count < 0) || (buf == nullptr && count > 0)) {
953 retval = MPI_ERR_COUNT;
954 } else if (not datatype->is_valid()) {
955 retval = MPI_ERR_TYPE;
956 } else if(tag < 0 && tag != MPI_ANY_TAG){
957 retval = MPI_ERR_TAG;
959 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
960 int dst_traced = comm->group()->index(dst);
961 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
962 extra->type = TRACING_SEND;
964 extra->dst = dst_traced;
966 extra->datatype1 = encode_datatype(datatype, &known);
967 int dt_size_send = 1;
969 dt_size_send = datatype->size();
971 extra->send_size = count*dt_size_send;
972 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
973 if (not TRACE_smpi_view_internals()) {
974 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
977 simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
978 retval = MPI_SUCCESS;
980 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
987 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
992 if (comm == MPI_COMM_NULL) {
993 retval = MPI_ERR_COMM;
994 } else if (dst == MPI_PROC_NULL) {
995 retval = MPI_SUCCESS;
996 } else if (dst >= comm->group()->size() || dst <0){
997 retval = MPI_ERR_RANK;
998 } else if ((count < 0) || (buf==nullptr && count > 0)) {
999 retval = MPI_ERR_COUNT;
1000 } else if (not datatype->is_valid()) {
1001 retval = MPI_ERR_TYPE;
1002 } else if(tag<0 && tag != MPI_ANY_TAG){
1003 retval = MPI_ERR_TAG;
1005 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1006 int dst_traced = comm->group()->index(dst);
1007 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1008 extra->type = TRACING_SSEND;
1010 extra->dst = dst_traced;
1012 extra->datatype1 = encode_datatype(datatype, &known);
1013 int dt_size_send = 1;
1015 dt_size_send = datatype->size();
1017 extra->send_size = count*dt_size_send;
1018 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1019 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
1021 simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
1022 retval = MPI_SUCCESS;
1024 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1031 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void *recvbuf,
1032 int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status)
1038 if (comm == MPI_COMM_NULL) {
1039 retval = MPI_ERR_COMM;
1040 } else if (not sendtype->is_valid() || not recvtype->is_valid()) {
1041 retval = MPI_ERR_TYPE;
1042 } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1043 simgrid::smpi::Status::empty(status);
1044 status->MPI_SOURCE = MPI_PROC_NULL;
1045 retval = MPI_SUCCESS;
1046 }else if (dst >= comm->group()->size() || dst <0 ||
1047 (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
1048 retval = MPI_ERR_RANK;
1049 } else if ((sendcount < 0 || recvcount<0) ||
1050 (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
1051 retval = MPI_ERR_COUNT;
1052 } else if((sendtag<0 && sendtag != MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1053 retval = MPI_ERR_TAG;
1056 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1057 int dst_traced = comm->group()->index(dst);
1058 int src_traced = comm->group()->index(src);
1059 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1060 extra->type = TRACING_SENDRECV;
1061 extra->src = src_traced;
1062 extra->dst = dst_traced;
1064 extra->datatype1 = encode_datatype(sendtype, &known);
1065 int dt_size_send = 1;
1067 dt_size_send = sendtype->size();
1068 extra->send_size = sendcount*dt_size_send;
1069 extra->datatype2 = encode_datatype(recvtype, &known);
1070 int dt_size_recv = 1;
1072 dt_size_recv = recvtype->size();
1073 extra->recv_size = recvcount*dt_size_recv;
1075 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1076 TRACE_smpi_send(rank, rank, dst_traced, sendtag,sendcount*sendtype->size());
1078 simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src, recvtag, comm,
1080 retval = MPI_SUCCESS;
1082 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1083 TRACE_smpi_recv(rank, src_traced, rank, recvtag);
1090 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
1091 MPI_Comm comm, MPI_Status* status)
1094 if (not datatype->is_valid()) {
1095 return MPI_ERR_TYPE;
1096 } else if (count < 0) {
1097 return MPI_ERR_COUNT;
1099 int size = datatype->get_extent() * count;
1100 void* recvbuf = xbt_new0(char, size);
1101 retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
1102 if(retval==MPI_SUCCESS){
1103 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, buf, count, datatype);
1111 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1115 if (request == nullptr || flag == nullptr) {
1116 retval = MPI_ERR_ARG;
1117 } else if (*request == MPI_REQUEST_NULL) {
1119 simgrid::smpi::Status::empty(status);
1120 retval = MPI_SUCCESS;
1122 int rank = ((*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1124 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1125 extra->type = TRACING_TEST;
1126 TRACE_smpi_testing_in(rank, extra);
1128 *flag = simgrid::smpi::Request::test(request,status);
1130 TRACE_smpi_testing_out(rank);
1131 retval = MPI_SUCCESS;
1137 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
1142 if (index == nullptr || flag == nullptr) {
1143 retval = MPI_ERR_ARG;
1145 *flag = simgrid::smpi::Request::testany(count, requests, index, status);
1146 retval = MPI_SUCCESS;
1152 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1157 if (flag == nullptr) {
1158 retval = MPI_ERR_ARG;
1160 *flag = simgrid::smpi::Request::testall(count, requests, statuses);
1161 retval = MPI_SUCCESS;
1167 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1171 if (status == nullptr) {
1172 retval = MPI_ERR_ARG;
1173 } else if (comm == MPI_COMM_NULL) {
1174 retval = MPI_ERR_COMM;
1175 } else if (source == MPI_PROC_NULL) {
1176 simgrid::smpi::Status::empty(status);
1177 status->MPI_SOURCE = MPI_PROC_NULL;
1178 retval = MPI_SUCCESS;
1180 simgrid::smpi::Request::probe(source, tag, comm, status);
1181 retval = MPI_SUCCESS;
1187 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1191 if (flag == nullptr) {
1192 retval = MPI_ERR_ARG;
1193 } else if (comm == MPI_COMM_NULL) {
1194 retval = MPI_ERR_COMM;
1195 } else if (source == MPI_PROC_NULL) {
1197 simgrid::smpi::Status::empty(status);
1198 status->MPI_SOURCE = MPI_PROC_NULL;
1199 retval = MPI_SUCCESS;
1201 simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
1202 retval = MPI_SUCCESS;
1208 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1214 simgrid::smpi::Status::empty(status);
1216 if (request == nullptr) {
1217 retval = MPI_ERR_ARG;
1218 } else if (*request == MPI_REQUEST_NULL) {
1219 retval = MPI_SUCCESS;
1222 int rank = (request!=nullptr && (*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1224 int src_traced = (*request)->src();
1225 int dst_traced = (*request)->dst();
1226 int tag_traced= (*request)->tag();
1227 MPI_Comm comm = (*request)->comm();
1228 int is_wait_for_receive = ((*request)->flags() & RECV);
1229 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1230 extra->type = TRACING_WAIT;
1231 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1233 simgrid::smpi::Request::wait(request, status);
1234 retval = MPI_SUCCESS;
1236 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1237 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1238 if (is_wait_for_receive) {
1239 if(src_traced==MPI_ANY_SOURCE)
1240 src_traced = (status!=MPI_STATUS_IGNORE) ?
1241 comm->group()->rank(status->MPI_SOURCE) :
1243 TRACE_smpi_recv(rank, src_traced, dst_traced, tag_traced);
1251 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1253 if (index == nullptr)
1257 //save requests information for tracing
1265 savedvalstype* savedvals=nullptr;
1267 savedvals = xbt_new0(savedvalstype, count);
1269 for (int i = 0; i < count; i++) {
1270 MPI_Request req = requests[i]; //already received requests are no longer valid
1272 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), req->comm()};
1275 int rank_traced = smpi_process()->index();
1276 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1277 extra->type = TRACING_WAITANY;
1278 extra->send_size=count;
1279 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1281 *index = simgrid::smpi::Request::waitany(count, requests, status);
1283 if(*index!=MPI_UNDEFINED){
1284 int src_traced = savedvals[*index].src;
1285 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1286 int dst_traced = savedvals[*index].dst;
1287 int is_wait_for_receive = savedvals[*index].recv;
1288 if (is_wait_for_receive) {
1289 if(savedvals[*index].src==MPI_ANY_SOURCE)
1290 src_traced = (status != MPI_STATUSES_IGNORE)
1291 ? savedvals[*index].comm->group()->rank(status->MPI_SOURCE)
1292 : savedvals[*index].src;
1293 TRACE_smpi_recv(rank_traced, src_traced, dst_traced, savedvals[*index].tag);
1295 TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1297 xbt_free(savedvals);
1303 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1306 //save information from requests
1315 savedvalstype* savedvals=xbt_new0(savedvalstype, count);
1317 for (int i = 0; i < count; i++) {
1318 MPI_Request req = requests[i];
1319 if(req!=MPI_REQUEST_NULL){
1320 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), 1, req->comm()};
1322 savedvals[i].valid=0;
1325 int rank_traced = smpi_process()->index();
1326 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1327 extra->type = TRACING_WAITALL;
1328 extra->send_size=count;
1329 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1331 int retval = simgrid::smpi::Request::waitall(count, requests, status);
1333 for (int i = 0; i < count; i++) {
1334 if(savedvals[i].valid){
1335 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1336 int src_traced = savedvals[i].src;
1337 int dst_traced = savedvals[i].dst;
1338 int is_wait_for_receive = savedvals[i].recv;
1339 if (is_wait_for_receive) {
1340 if(src_traced==MPI_ANY_SOURCE)
1341 src_traced = (status!=MPI_STATUSES_IGNORE) ?
1342 savedvals[i].comm->group()->rank(status[i].MPI_SOURCE) : savedvals[i].src;
1343 TRACE_smpi_recv(rank_traced, src_traced, dst_traced,savedvals[i].tag);
1347 TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1348 xbt_free(savedvals);
1354 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1359 if (outcount == nullptr) {
1360 retval = MPI_ERR_ARG;
1362 *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
1363 retval = MPI_SUCCESS;
1369 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1374 if (outcount == nullptr) {
1375 retval = MPI_ERR_ARG;
1377 *outcount = simgrid::smpi::Request::testsome(incount, requests, indices, status);
1378 retval = MPI_SUCCESS;
1385 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1391 if (comm == MPI_COMM_NULL) {
1392 retval = MPI_ERR_COMM;
1393 } else if (not datatype->is_valid()) {
1394 retval = MPI_ERR_ARG;
1396 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1397 int root_traced = comm->group()->index(root);
1399 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1400 extra->type = TRACING_BCAST;
1401 extra->root = root_traced;
1403 extra->datatype1 = encode_datatype(datatype, &known);
1404 int dt_size_send = 1;
1406 dt_size_send = datatype->size();
1407 extra->send_size = count * dt_size_send;
1408 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1409 if (comm->size() > 1)
1410 simgrid::smpi::Colls::bcast(buf, count, datatype, root, comm);
1411 retval = MPI_SUCCESS;
1413 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1419 int PMPI_Barrier(MPI_Comm comm)
1425 if (comm == MPI_COMM_NULL) {
1426 retval = MPI_ERR_COMM;
1428 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1429 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1430 extra->type = TRACING_BARRIER;
1431 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1433 simgrid::smpi::Colls::barrier(comm);
1435 //Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
1436 comm->finish_rma_calls();
1438 retval = MPI_SUCCESS;
1440 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1447 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1448 int root, MPI_Comm comm)
1454 if (comm == MPI_COMM_NULL) {
1455 retval = MPI_ERR_COMM;
1456 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1457 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1458 retval = MPI_ERR_TYPE;
1459 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((comm->rank() == root) && (recvcount <0))){
1460 retval = MPI_ERR_COUNT;
1463 char* sendtmpbuf = static_cast<char*>(sendbuf);
1464 int sendtmpcount = sendcount;
1465 MPI_Datatype sendtmptype = sendtype;
1466 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1468 sendtmptype=recvtype;
1470 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1471 int root_traced = comm->group()->index(root);
1472 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1473 extra->type = TRACING_GATHER;
1474 extra->root = root_traced;
1476 extra->datatype1 = encode_datatype(sendtmptype, &known);
1477 int dt_size_send = 1;
1479 dt_size_send = sendtmptype->size();
1480 extra->send_size = sendtmpcount * dt_size_send;
1481 extra->datatype2 = encode_datatype(recvtype, &known);
1482 int dt_size_recv = 1;
1483 if ((comm->rank() == root) && known == 0)
1484 dt_size_recv = recvtype->size();
1485 extra->recv_size = recvcount * dt_size_recv;
1487 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1489 simgrid::smpi::Colls::gather(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1491 retval = MPI_SUCCESS;
1492 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1499 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1500 MPI_Datatype recvtype, int root, MPI_Comm comm)
1506 if (comm == MPI_COMM_NULL) {
1507 retval = MPI_ERR_COMM;
1508 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1509 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1510 retval = MPI_ERR_TYPE;
1511 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1512 retval = MPI_ERR_COUNT;
1513 } else if (recvcounts == nullptr || displs == nullptr) {
1514 retval = MPI_ERR_ARG;
1516 char* sendtmpbuf = static_cast<char*>(sendbuf);
1517 int sendtmpcount = sendcount;
1518 MPI_Datatype sendtmptype = sendtype;
1519 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1521 sendtmptype=recvtype;
1524 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1525 int root_traced = comm->group()->index(root);
1526 int size = comm->size();
1527 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1528 extra->type = TRACING_GATHERV;
1529 extra->num_processes = size;
1530 extra->root = root_traced;
1532 extra->datatype1 = encode_datatype(sendtmptype, &known);
1533 int dt_size_send = 1;
1535 dt_size_send = sendtype->size();
1536 extra->send_size = sendtmpcount * dt_size_send;
1537 extra->datatype2 = encode_datatype(recvtype, &known);
1538 int dt_size_recv = 1;
1540 dt_size_recv = recvtype->size();
1541 if (comm->rank() == root) {
1542 extra->recvcounts = xbt_new(int, size);
1543 for (int i = 0; i < size; i++) // copy data to avoid bad free
1544 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1546 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1548 retval = simgrid::smpi::Colls::gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1549 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1556 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1557 void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1563 if (comm == MPI_COMM_NULL) {
1564 retval = MPI_ERR_COMM;
1565 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1566 (recvtype == MPI_DATATYPE_NULL)){
1567 retval = MPI_ERR_TYPE;
1568 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1570 retval = MPI_ERR_COUNT;
1572 if(sendbuf == MPI_IN_PLACE) {
1573 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*recvcount*comm->rank();
1574 sendcount=recvcount;
1577 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1578 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1579 extra->type = TRACING_ALLGATHER;
1581 extra->datatype1 = encode_datatype(sendtype, &known);
1582 int dt_size_send = 1;
1584 dt_size_send = sendtype->size();
1585 extra->send_size = sendcount * dt_size_send;
1586 extra->datatype2 = encode_datatype(recvtype, &known);
1587 int dt_size_recv = 1;
1589 dt_size_recv = recvtype->size();
1590 extra->recv_size = recvcount * dt_size_recv;
1592 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1594 simgrid::smpi::Colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1595 retval = MPI_SUCCESS;
1596 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1602 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1603 void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1609 if (comm == MPI_COMM_NULL) {
1610 retval = MPI_ERR_COMM;
1611 } else if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL)) {
1612 retval = MPI_ERR_TYPE;
1613 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1614 retval = MPI_ERR_COUNT;
1615 } else if (recvcounts == nullptr || displs == nullptr) {
1616 retval = MPI_ERR_ARG;
1619 if(sendbuf == MPI_IN_PLACE) {
1620 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*displs[comm->rank()];
1621 sendcount=recvcounts[comm->rank()];
1624 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1626 int size = comm->size();
1627 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1628 extra->type = TRACING_ALLGATHERV;
1629 extra->num_processes = size;
1631 extra->datatype1 = encode_datatype(sendtype, &known);
1632 int dt_size_send = 1;
1634 dt_size_send = sendtype->size();
1635 extra->send_size = sendcount * dt_size_send;
1636 extra->datatype2 = encode_datatype(recvtype, &known);
1637 int dt_size_recv = 1;
1639 dt_size_recv = recvtype->size();
1640 extra->recvcounts = xbt_new(int, size);
1641 for (i = 0; i < size; i++) // copy data to avoid bad free
1642 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1644 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1646 simgrid::smpi::Colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1647 retval = MPI_SUCCESS;
1648 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1655 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1656 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1662 if (comm == MPI_COMM_NULL) {
1663 retval = MPI_ERR_COMM;
1664 } else if (((comm->rank() == root) && (not sendtype->is_valid())) ||
1665 ((recvbuf != MPI_IN_PLACE) && (not recvtype->is_valid()))) {
1666 retval = MPI_ERR_TYPE;
1667 } else if ((sendbuf == recvbuf) ||
1668 ((comm->rank()==root) && sendcount>0 && (sendbuf == nullptr))){
1669 retval = MPI_ERR_BUFFER;
1672 if (recvbuf == MPI_IN_PLACE) {
1673 recvtype = sendtype;
1674 recvcount = sendcount;
1676 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1677 int root_traced = comm->group()->index(root);
1678 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1679 extra->type = TRACING_SCATTER;
1680 extra->root = root_traced;
1682 extra->datatype1 = encode_datatype(sendtype, &known);
1683 int dt_size_send = 1;
1684 if ((comm->rank() == root) && known == 0)
1685 dt_size_send = sendtype->size();
1686 extra->send_size = sendcount * dt_size_send;
1687 extra->datatype2 = encode_datatype(recvtype, &known);
1688 int dt_size_recv = 1;
1690 dt_size_recv = recvtype->size();
1691 extra->recv_size = recvcount * dt_size_recv;
1692 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1694 simgrid::smpi::Colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1695 retval = MPI_SUCCESS;
1696 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1703 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1704 MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1710 if (comm == MPI_COMM_NULL) {
1711 retval = MPI_ERR_COMM;
1712 } else if (sendcounts == nullptr || displs == nullptr) {
1713 retval = MPI_ERR_ARG;
1714 } else if (((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL)) ||
1715 ((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1716 retval = MPI_ERR_TYPE;
1718 if (recvbuf == MPI_IN_PLACE) {
1719 recvtype = sendtype;
1720 recvcount = sendcounts[comm->rank()];
1722 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1723 int root_traced = comm->group()->index(root);
1724 int size = comm->size();
1725 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1726 extra->type = TRACING_SCATTERV;
1727 extra->num_processes = size;
1728 extra->root = root_traced;
1730 extra->datatype1 = encode_datatype(sendtype, &known);
1731 int dt_size_send = 1;
1733 dt_size_send = sendtype->size();
1734 if (comm->rank() == root) {
1735 extra->sendcounts = xbt_new(int, size);
1736 for (int i = 0; i < size; i++) // copy data to avoid bad free
1737 extra->sendcounts[i] = sendcounts[i] * dt_size_send;
1739 extra->datatype2 = encode_datatype(recvtype, &known);
1740 int dt_size_recv = 1;
1742 dt_size_recv = recvtype->size();
1743 extra->recv_size = recvcount * dt_size_recv;
1744 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1746 retval = simgrid::smpi::Colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1748 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1755 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1761 if (comm == MPI_COMM_NULL) {
1762 retval = MPI_ERR_COMM;
1763 } else if (not datatype->is_valid() || op == MPI_OP_NULL) {
1764 retval = MPI_ERR_ARG;
1766 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1767 int root_traced = comm->group()->index(root);
1768 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1769 extra->type = TRACING_REDUCE;
1771 extra->datatype1 = encode_datatype(datatype, &known);
1772 int dt_size_send = 1;
1774 dt_size_send = datatype->size();
1775 extra->send_size = count * dt_size_send;
1776 extra->root = root_traced;
1778 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1780 simgrid::smpi::Colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1782 retval = MPI_SUCCESS;
1783 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1790 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
1794 if (not datatype->is_valid() || op == MPI_OP_NULL) {
1795 retval = MPI_ERR_ARG;
1797 op->apply(inbuf, inoutbuf, &count, datatype);
1798 retval = MPI_SUCCESS;
1804 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1810 if (comm == MPI_COMM_NULL) {
1811 retval = MPI_ERR_COMM;
1812 } else if (not datatype->is_valid()) {
1813 retval = MPI_ERR_TYPE;
1814 } else if (op == MPI_OP_NULL) {
1815 retval = MPI_ERR_OP;
1818 char* sendtmpbuf = static_cast<char*>(sendbuf);
1819 if( sendbuf == MPI_IN_PLACE ) {
1820 sendtmpbuf = static_cast<char*>(xbt_malloc(count*datatype->get_extent()));
1821 simgrid::smpi::Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1823 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1824 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1825 extra->type = TRACING_ALLREDUCE;
1827 extra->datatype1 = encode_datatype(datatype, &known);
1828 int dt_size_send = 1;
1830 dt_size_send = datatype->size();
1831 extra->send_size = count * dt_size_send;
1833 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1835 simgrid::smpi::Colls::allreduce(sendtmpbuf, recvbuf, count, datatype, op, comm);
1837 if( sendbuf == MPI_IN_PLACE )
1838 xbt_free(sendtmpbuf);
1840 retval = MPI_SUCCESS;
1841 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1848 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1854 if (comm == MPI_COMM_NULL) {
1855 retval = MPI_ERR_COMM;
1856 } else if (not datatype->is_valid()) {
1857 retval = MPI_ERR_TYPE;
1858 } else if (op == MPI_OP_NULL) {
1859 retval = MPI_ERR_OP;
1861 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1862 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1863 extra->type = TRACING_SCAN;
1865 extra->datatype1 = encode_datatype(datatype, &known);
1866 int dt_size_send = 1;
1868 dt_size_send = datatype->size();
1869 extra->send_size = count * dt_size_send;
1871 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1873 retval = simgrid::smpi::Colls::scan(sendbuf, recvbuf, count, datatype, op, comm);
1875 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1882 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
1887 if (comm == MPI_COMM_NULL) {
1888 retval = MPI_ERR_COMM;
1889 } else if (not datatype->is_valid()) {
1890 retval = MPI_ERR_TYPE;
1891 } else if (op == MPI_OP_NULL) {
1892 retval = MPI_ERR_OP;
1894 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1895 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1896 extra->type = TRACING_EXSCAN;
1898 extra->datatype1 = encode_datatype(datatype, &known);
1899 int dt_size_send = 1;
1901 dt_size_send = datatype->size();
1902 extra->send_size = count * dt_size_send;
1903 void* sendtmpbuf = sendbuf;
1904 if (sendbuf == MPI_IN_PLACE) {
1905 sendtmpbuf = static_cast<void*>(xbt_malloc(count * datatype->size()));
1906 memcpy(sendtmpbuf, recvbuf, count * datatype->size());
1908 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1910 retval = simgrid::smpi::Colls::exscan(sendtmpbuf, recvbuf, count, datatype, op, comm);
1912 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1913 if (sendbuf == MPI_IN_PLACE)
1914 xbt_free(sendtmpbuf);
1921 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1926 if (comm == MPI_COMM_NULL) {
1927 retval = MPI_ERR_COMM;
1928 } else if (not datatype->is_valid()) {
1929 retval = MPI_ERR_TYPE;
1930 } else if (op == MPI_OP_NULL) {
1931 retval = MPI_ERR_OP;
1932 } else if (recvcounts == nullptr) {
1933 retval = MPI_ERR_ARG;
1935 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1937 int size = comm->size();
1938 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1939 extra->type = TRACING_REDUCE_SCATTER;
1940 extra->num_processes = size;
1942 extra->datatype1 = encode_datatype(datatype, &known);
1943 int dt_size_send = 1;
1945 dt_size_send = datatype->size();
1946 extra->send_size = 0;
1947 extra->recvcounts = xbt_new(int, size);
1949 for (i = 0; i < size; i++) { // copy data to avoid bad free
1950 extra->recvcounts[i] = recvcounts[i] * dt_size_send;
1951 totalcount += recvcounts[i];
1953 void* sendtmpbuf = sendbuf;
1954 if (sendbuf == MPI_IN_PLACE) {
1955 sendtmpbuf = static_cast<void*>(xbt_malloc(totalcount * datatype->size()));
1956 memcpy(sendtmpbuf, recvbuf, totalcount * datatype->size());
1959 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1961 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
1962 retval = MPI_SUCCESS;
1963 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1965 if (sendbuf == MPI_IN_PLACE)
1966 xbt_free(sendtmpbuf);
1973 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
1974 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1979 if (comm == MPI_COMM_NULL) {
1980 retval = MPI_ERR_COMM;
1981 } else if (not datatype->is_valid()) {
1982 retval = MPI_ERR_TYPE;
1983 } else if (op == MPI_OP_NULL) {
1984 retval = MPI_ERR_OP;
1985 } else if (recvcount < 0) {
1986 retval = MPI_ERR_ARG;
1988 int count = comm->size();
1990 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1991 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1992 extra->type = TRACING_REDUCE_SCATTER;
1993 extra->num_processes = count;
1995 extra->datatype1 = encode_datatype(datatype, &known);
1996 int dt_size_send = 1;
1998 dt_size_send = datatype->size();
1999 extra->send_size = 0;
2000 extra->recvcounts = xbt_new(int, count);
2001 for (int i = 0; i < count; i++) // copy data to avoid bad free
2002 extra->recvcounts[i] = recvcount * dt_size_send;
2003 void* sendtmpbuf = sendbuf;
2004 if (sendbuf == MPI_IN_PLACE) {
2005 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * count * datatype->size()));
2006 memcpy(sendtmpbuf, recvbuf, recvcount * count * datatype->size());
2009 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2011 int* recvcounts = static_cast<int*>(xbt_malloc(count * sizeof(int)));
2012 for (int i = 0; i < count; i++)
2013 recvcounts[i] = recvcount;
2014 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
2015 xbt_free(recvcounts);
2016 retval = MPI_SUCCESS;
2018 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2020 if (sendbuf == MPI_IN_PLACE)
2021 xbt_free(sendtmpbuf);
2028 int PMPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
2029 MPI_Datatype recvtype, MPI_Comm comm)
2034 if (comm == MPI_COMM_NULL) {
2035 retval = MPI_ERR_COMM;
2036 } else if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL) {
2037 retval = MPI_ERR_TYPE;
2039 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2040 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2041 extra->type = TRACING_ALLTOALL;
2043 void* sendtmpbuf = static_cast<char*>(sendbuf);
2044 int sendtmpcount = sendcount;
2045 MPI_Datatype sendtmptype = sendtype;
2046 if (sendbuf == MPI_IN_PLACE) {
2047 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * comm->size() * recvtype->size()));
2048 memcpy(sendtmpbuf, recvbuf, recvcount * comm->size() * recvtype->size());
2049 sendtmpcount = recvcount;
2050 sendtmptype = recvtype;
2054 extra->datatype1 = encode_datatype(sendtmptype, &known);
2056 extra->send_size = sendtmpcount * sendtmptype->size();
2058 extra->send_size = sendtmpcount;
2059 extra->datatype2 = encode_datatype(recvtype, &known);
2061 extra->recv_size = recvcount * recvtype->size();
2063 extra->recv_size = recvcount;
2065 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2067 retval = simgrid::smpi::Colls::alltoall(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, comm);
2069 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2071 if (sendbuf == MPI_IN_PLACE)
2072 xbt_free(sendtmpbuf);
2079 int PMPI_Alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf,
2080 int* recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2086 if (comm == MPI_COMM_NULL) {
2087 retval = MPI_ERR_COMM;
2088 } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2089 retval = MPI_ERR_TYPE;
2090 } else if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
2091 recvdisps == nullptr) {
2092 retval = MPI_ERR_ARG;
2094 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2096 int size = comm->size();
2097 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2098 extra->type = TRACING_ALLTOALLV;
2099 extra->send_size = 0;
2100 extra->recv_size = 0;
2101 extra->recvcounts = xbt_new(int, size);
2102 extra->sendcounts = xbt_new(int, size);
2104 extra->datatype2 = encode_datatype(recvtype, &known);
2105 int dt_size_recv = recvtype->size();
2107 void* sendtmpbuf = static_cast<char*>(sendbuf);
2108 int* sendtmpcounts = sendcounts;
2109 int* sendtmpdisps = senddisps;
2110 MPI_Datatype sendtmptype = sendtype;
2112 for (i = 0; i < size; i++) { // copy data to avoid bad free
2113 extra->recv_size += recvcounts[i] * dt_size_recv;
2114 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
2115 if (((recvdisps[i] + recvcounts[i]) * dt_size_recv) > maxsize)
2116 maxsize = (recvdisps[i] + recvcounts[i]) * dt_size_recv;
2119 if (sendbuf == MPI_IN_PLACE) {
2120 sendtmpbuf = static_cast<void*>(xbt_malloc(maxsize));
2121 memcpy(sendtmpbuf, recvbuf, maxsize);
2122 sendtmpcounts = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2123 memcpy(sendtmpcounts, recvcounts, size * sizeof(int));
2124 sendtmpdisps = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2125 memcpy(sendtmpdisps, recvdisps, size * sizeof(int));
2126 sendtmptype = recvtype;
2129 extra->datatype1 = encode_datatype(sendtmptype, &known);
2130 int dt_size_send = sendtmptype->size();
2132 for (i = 0; i < size; i++) { // copy data to avoid bad free
2133 extra->send_size += sendtmpcounts[i] * dt_size_send;
2134 extra->sendcounts[i] = sendtmpcounts[i] * dt_size_send;
2136 extra->num_processes = size;
2137 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2138 retval = simgrid::smpi::Colls::alltoallv(sendtmpbuf, sendtmpcounts, sendtmpdisps, sendtmptype, recvbuf, recvcounts,
2139 recvdisps, recvtype, comm);
2140 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2142 if (sendbuf == MPI_IN_PLACE) {
2143 xbt_free(sendtmpbuf);
2144 xbt_free(sendtmpcounts);
2145 xbt_free(sendtmpdisps);
2154 int PMPI_Get_processor_name(char *name, int *resultlen)
2156 strncpy(name, sg_host_self()->getCname(), strlen(sg_host_self()->getCname()) < MPI_MAX_PROCESSOR_NAME - 1
2157 ? strlen(sg_host_self()->getCname()) + 1
2158 : MPI_MAX_PROCESSOR_NAME - 1);
2159 *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2164 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2166 if (status == nullptr || count == nullptr) {
2168 } else if (not datatype->is_valid()) {
2169 return MPI_ERR_TYPE;
2171 size_t size = datatype->size();
2175 } else if (status->count % size != 0) {
2176 return MPI_UNDEFINED;
2178 *count = simgrid::smpi::Status::get_count(status, datatype);
2184 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2185 if (old_type == MPI_DATATYPE_NULL) {
2186 return MPI_ERR_TYPE;
2187 } else if (count<0){
2188 return MPI_ERR_COUNT;
2190 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
2194 int PMPI_Type_commit(MPI_Datatype* datatype) {
2195 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2196 return MPI_ERR_TYPE;
2198 (*datatype)->commit();
2203 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2204 if (old_type == MPI_DATATYPE_NULL) {
2205 return MPI_ERR_TYPE;
2206 } else if (count<0 || blocklen<0){
2207 return MPI_ERR_COUNT;
2209 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
2213 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2214 if (old_type == MPI_DATATYPE_NULL) {
2215 return MPI_ERR_TYPE;
2216 } else if (count<0 || blocklen<0){
2217 return MPI_ERR_COUNT;
2219 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
2223 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2224 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2227 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2228 if (old_type == MPI_DATATYPE_NULL) {
2229 return MPI_ERR_TYPE;
2230 } else if (count<0){
2231 return MPI_ERR_COUNT;
2233 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2237 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2238 if (old_type == MPI_DATATYPE_NULL) {
2239 return MPI_ERR_TYPE;
2240 } else if (count<0){
2241 return MPI_ERR_COUNT;
2243 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2247 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2248 MPI_Datatype* new_type)
2250 if (old_type == MPI_DATATYPE_NULL) {
2251 return MPI_ERR_TYPE;
2252 } else if (count<0){
2253 return MPI_ERR_COUNT;
2255 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
2256 for (int i = 0; i < count; i++)
2257 blocklens[i]=blocklength;
2258 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2259 xbt_free(blocklens);
2264 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
2266 if (old_type == MPI_DATATYPE_NULL) {
2267 return MPI_ERR_TYPE;
2268 } else if (count<0){
2269 return MPI_ERR_COUNT;
2271 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2275 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2276 MPI_Datatype* new_type) {
2277 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2280 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2281 MPI_Datatype* new_type) {
2282 if (old_type == MPI_DATATYPE_NULL) {
2283 return MPI_ERR_TYPE;
2284 } else if (count<0){
2285 return MPI_ERR_COUNT;
2287 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
2288 for (int i = 0; i < count; i++)
2289 blocklens[i] = blocklength;
2290 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2291 xbt_free(blocklens);
2296 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2298 return MPI_ERR_COUNT;
2300 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
2304 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2305 MPI_Datatype* new_type) {
2306 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2309 int PMPI_Error_class(int errorcode, int* errorclass) {
2310 // assume smpi uses only standard mpi error codes
2311 *errorclass=errorcode;
2315 int PMPI_Initialized(int* flag) {
2316 *flag=(smpi_process()!=nullptr && smpi_process()->initialized());
2320 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2321 * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2322 * MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2324 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2325 if (comm_old == MPI_COMM_NULL){
2326 return MPI_ERR_COMM;
2327 } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2330 simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
2331 if(*comm_cart==MPI_COMM_NULL)
2337 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2338 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2339 return MPI_ERR_TOPOLOGY;
2341 if (coords == nullptr) {
2344 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2345 if (topo==nullptr) {
2348 return topo->rank(coords, rank);
2351 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2352 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2353 return MPI_ERR_TOPOLOGY;
2355 if (source == nullptr || dest == nullptr || direction < 0 ) {
2358 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2359 if (topo==nullptr) {
2362 return topo->shift(direction, displ, source, dest);
2365 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2366 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2367 return MPI_ERR_TOPOLOGY;
2369 if (rank < 0 || rank >= comm->size()) {
2370 return MPI_ERR_RANK;
2375 if(coords == nullptr) {
2378 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2379 if (topo==nullptr) {
2382 return topo->coords(rank, maxdims, coords);
2385 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2386 if(comm == nullptr || comm->topo() == nullptr) {
2387 return MPI_ERR_TOPOLOGY;
2389 if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2392 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2393 if (topo==nullptr) {
2396 return topo->get(maxdims, dims, periods, coords);
2399 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2400 if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2401 return MPI_ERR_TOPOLOGY;
2403 if (ndims == nullptr) {
2406 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2407 if (topo==nullptr) {
2410 return topo->dim_get(ndims);
2413 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2414 if(dims == nullptr) {
2417 if (ndims < 1 || nnodes < 1) {
2418 return MPI_ERR_DIMS;
2420 return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
2423 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2424 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2425 return MPI_ERR_TOPOLOGY;
2427 if (comm_new == nullptr) {
2430 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2431 if (topo==nullptr) {
2434 MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
2435 if(*comm_new==MPI_COMM_NULL)
2442 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2443 if (oldtype == MPI_DATATYPE_NULL) {
2444 return MPI_ERR_TYPE;
2446 int blocks[3] = {1, 1, 1};
2447 MPI_Aint disps[3] = {lb, 0, lb + extent};
2448 MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
2450 *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
2452 (*newtype)->addflag(~DT_FLAG_COMMITED);
2456 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2459 if (comm == MPI_COMM_NULL) {
2460 retval= MPI_ERR_COMM;
2461 }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2462 retval= MPI_ERR_OTHER;
2464 *win = new simgrid::smpi::Win( base, size, disp_unit, info, comm);
2465 retval = MPI_SUCCESS;
2471 int PMPI_Win_allocate( MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *base, MPI_Win *win){
2474 if (comm == MPI_COMM_NULL) {
2475 retval= MPI_ERR_COMM;
2476 }else if (disp_unit <= 0 || size < 0 ){
2477 retval= MPI_ERR_OTHER;
2479 void* ptr = xbt_malloc(size);
2481 return MPI_ERR_NO_MEM;
2482 *static_cast<void**>(base) = ptr;
2483 *win = new simgrid::smpi::Win( ptr, size, disp_unit, info, comm,1);
2484 retval = MPI_SUCCESS;
2490 int PMPI_Win_create_dynamic( MPI_Info info, MPI_Comm comm, MPI_Win *win){
2493 if (comm == MPI_COMM_NULL) {
2494 retval= MPI_ERR_COMM;
2496 *win = new simgrid::smpi::Win(info, comm);
2497 retval = MPI_SUCCESS;
2503 int PMPI_Win_attach(MPI_Win win, void *base, MPI_Aint size){
2506 if(win == MPI_WIN_NULL){
2507 retval = MPI_ERR_WIN;
2508 } else if ((base == nullptr && size != 0) || size < 0 ){
2509 retval= MPI_ERR_OTHER;
2511 retval = win->attach(base, size);
2517 int PMPI_Win_detach(MPI_Win win, void *base){
2520 if(win == MPI_WIN_NULL){
2521 retval = MPI_ERR_WIN;
2522 } else if (base == nullptr){
2523 retval= MPI_ERR_OTHER;
2525 retval = win->detach(base);
2532 int PMPI_Win_free( MPI_Win* win){
2535 if (win == nullptr || *win == MPI_WIN_NULL) {
2536 retval = MPI_ERR_WIN;
2545 int PMPI_Win_set_name(MPI_Win win, char * name)
2547 if (win == MPI_WIN_NULL) {
2548 return MPI_ERR_TYPE;
2549 } else if (name == nullptr) {
2552 win->set_name(name);
2557 int PMPI_Win_get_name(MPI_Win win, char * name, int* len)
2559 if (win == MPI_WIN_NULL) {
2561 } else if (name == nullptr) {
2564 win->get_name(name, len);
2569 int PMPI_Win_get_info(MPI_Win win, MPI_Info* info)
2571 if (win == MPI_WIN_NULL) {
2574 *info = win->info();
2579 int PMPI_Win_set_info(MPI_Win win, MPI_Info info)
2581 if (win == MPI_WIN_NULL) {
2582 return MPI_ERR_TYPE;
2584 win->set_info(info);
2589 int PMPI_Win_get_group(MPI_Win win, MPI_Group * group){
2590 if (win == MPI_WIN_NULL) {
2593 win->get_group(group);
2599 int PMPI_Win_fence( int assert, MPI_Win win){
2602 if (win == MPI_WIN_NULL) {
2603 retval = MPI_ERR_WIN;
2605 int rank = smpi_process()->index();
2606 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2607 retval = win->fence(assert);
2608 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2614 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2615 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2618 if (win == MPI_WIN_NULL) {
2619 retval = MPI_ERR_WIN;
2620 } else if (target_rank == MPI_PROC_NULL) {
2621 retval = MPI_SUCCESS;
2622 } else if (target_rank <0){
2623 retval = MPI_ERR_RANK;
2624 } else if (win->dynamic()==0 && target_disp <0){
2625 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2626 retval = MPI_ERR_ARG;
2627 } else if ((origin_count < 0 || target_count < 0) ||
2628 (origin_addr==nullptr && origin_count > 0)){
2629 retval = MPI_ERR_COUNT;
2630 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2631 retval = MPI_ERR_TYPE;
2633 int rank = smpi_process()->index();
2635 win->get_group(&group);
2636 int src_traced = group->index(target_rank);
2637 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2639 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2642 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2648 int PMPI_Rget( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2649 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2652 if (win == MPI_WIN_NULL) {
2653 retval = MPI_ERR_WIN;
2654 } else if (target_rank == MPI_PROC_NULL) {
2655 *request = MPI_REQUEST_NULL;
2656 retval = MPI_SUCCESS;
2657 } else if (target_rank <0){
2658 retval = MPI_ERR_RANK;
2659 } else if (win->dynamic()==0 && target_disp <0){
2660 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2661 retval = MPI_ERR_ARG;
2662 } else if ((origin_count < 0 || target_count < 0) ||
2663 (origin_addr==nullptr && origin_count > 0)){
2664 retval = MPI_ERR_COUNT;
2665 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2666 retval = MPI_ERR_TYPE;
2667 } else if(request == nullptr){
2668 retval = MPI_ERR_REQUEST;
2670 int rank = smpi_process()->index();
2672 win->get_group(&group);
2673 int src_traced = group->index(target_rank);
2674 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2676 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2677 target_datatype, request);
2679 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2685 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2686 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2689 if (win == MPI_WIN_NULL) {
2690 retval = MPI_ERR_WIN;
2691 } else if (target_rank == MPI_PROC_NULL) {
2692 retval = MPI_SUCCESS;
2693 } else if (target_rank <0){
2694 retval = MPI_ERR_RANK;
2695 } else if (win->dynamic()==0 && target_disp <0){
2696 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2697 retval = MPI_ERR_ARG;
2698 } else if ((origin_count < 0 || target_count < 0) ||
2699 (origin_addr==nullptr && origin_count > 0)){
2700 retval = MPI_ERR_COUNT;
2701 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2702 retval = MPI_ERR_TYPE;
2704 int rank = smpi_process()->index();
2706 win->get_group(&group);
2707 int dst_traced = group->index(target_rank);
2708 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2709 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2711 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2714 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2720 int PMPI_Rput( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2721 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2724 if (win == MPI_WIN_NULL) {
2725 retval = MPI_ERR_WIN;
2726 } else if (target_rank == MPI_PROC_NULL) {
2727 *request = MPI_REQUEST_NULL;
2728 retval = MPI_SUCCESS;
2729 } else if (target_rank <0){
2730 retval = MPI_ERR_RANK;
2731 } else if (win->dynamic()==0 && target_disp <0){
2732 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2733 retval = MPI_ERR_ARG;
2734 } else if ((origin_count < 0 || target_count < 0) ||
2735 (origin_addr==nullptr && origin_count > 0)){
2736 retval = MPI_ERR_COUNT;
2737 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2738 retval = MPI_ERR_TYPE;
2739 } else if(request == nullptr){
2740 retval = MPI_ERR_REQUEST;
2742 int rank = smpi_process()->index();
2744 win->get_group(&group);
2745 int dst_traced = group->index(target_rank);
2746 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2747 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2749 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2750 target_datatype, request);
2752 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2758 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2759 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2762 if (win == MPI_WIN_NULL) {
2763 retval = MPI_ERR_WIN;
2764 } else if (target_rank == MPI_PROC_NULL) {
2765 retval = MPI_SUCCESS;
2766 } else if (target_rank <0){
2767 retval = MPI_ERR_RANK;
2768 } else if (win->dynamic()==0 && target_disp <0){
2769 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2770 retval = MPI_ERR_ARG;
2771 } else if ((origin_count < 0 || target_count < 0) ||
2772 (origin_addr==nullptr && origin_count > 0)){
2773 retval = MPI_ERR_COUNT;
2774 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2775 retval = MPI_ERR_TYPE;
2776 } else if (op == MPI_OP_NULL) {
2777 retval = MPI_ERR_OP;
2779 int rank = smpi_process()->index();
2781 win->get_group(&group);
2782 int src_traced = group->index(target_rank);
2783 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2785 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2786 target_datatype, op);
2788 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2794 int PMPI_Raccumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2795 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2798 if (win == MPI_WIN_NULL) {
2799 retval = MPI_ERR_WIN;
2800 } else if (target_rank == MPI_PROC_NULL) {
2801 *request = MPI_REQUEST_NULL;
2802 retval = MPI_SUCCESS;
2803 } else if (target_rank <0){
2804 retval = MPI_ERR_RANK;
2805 } else if (win->dynamic()==0 && target_disp <0){
2806 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2807 retval = MPI_ERR_ARG;
2808 } else if ((origin_count < 0 || target_count < 0) ||
2809 (origin_addr==nullptr && origin_count > 0)){
2810 retval = MPI_ERR_COUNT;
2811 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2812 retval = MPI_ERR_TYPE;
2813 } else if (op == MPI_OP_NULL) {
2814 retval = MPI_ERR_OP;
2815 } else if(request == nullptr){
2816 retval = MPI_ERR_REQUEST;
2818 int rank = smpi_process()->index();
2820 win->get_group(&group);
2821 int src_traced = group->index(target_rank);
2822 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2824 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2825 target_datatype, op, request);
2827 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2833 int PMPI_Get_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2834 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2835 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2838 if (win == MPI_WIN_NULL) {
2839 retval = MPI_ERR_WIN;
2840 } else if (target_rank == MPI_PROC_NULL) {
2841 retval = MPI_SUCCESS;
2842 } else if (target_rank <0){
2843 retval = MPI_ERR_RANK;
2844 } else if (win->dynamic()==0 && target_disp <0){
2845 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2846 retval = MPI_ERR_ARG;
2847 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2848 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2849 (result_addr==nullptr && result_count > 0)){
2850 retval = MPI_ERR_COUNT;
2851 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2852 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2853 retval = MPI_ERR_TYPE;
2854 } else if (op == MPI_OP_NULL) {
2855 retval = MPI_ERR_OP;
2857 int rank = smpi_process()->index();
2859 win->get_group(&group);
2860 int src_traced = group->index(target_rank);
2861 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2863 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2864 result_count, result_datatype, target_rank, target_disp,
2865 target_count, target_datatype, op);
2867 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2874 int PMPI_Rget_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2875 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2876 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2879 if (win == MPI_WIN_NULL) {
2880 retval = MPI_ERR_WIN;
2881 } else if (target_rank == MPI_PROC_NULL) {
2882 *request = MPI_REQUEST_NULL;
2883 retval = MPI_SUCCESS;
2884 } else if (target_rank <0){
2885 retval = MPI_ERR_RANK;
2886 } else if (win->dynamic()==0 && target_disp <0){
2887 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2888 retval = MPI_ERR_ARG;
2889 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2890 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2891 (result_addr==nullptr && result_count > 0)){
2892 retval = MPI_ERR_COUNT;
2893 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2894 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2895 retval = MPI_ERR_TYPE;
2896 } else if (op == MPI_OP_NULL) {
2897 retval = MPI_ERR_OP;
2898 } else if(request == nullptr){
2899 retval = MPI_ERR_REQUEST;
2901 int rank = smpi_process()->index();
2903 win->get_group(&group);
2904 int src_traced = group->index(target_rank);
2905 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2907 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2908 result_count, result_datatype, target_rank, target_disp,
2909 target_count, target_datatype, op, request);
2911 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2917 int PMPI_Fetch_and_op(void *origin_addr, void *result_addr, MPI_Datatype dtype, int target_rank, MPI_Aint target_disp, MPI_Op op, MPI_Win win){
2918 return PMPI_Get_accumulate(origin_addr, origin_addr==nullptr?0:1, dtype, result_addr, 1, dtype, target_rank, target_disp, 1, dtype, op, win);
2921 int PMPI_Compare_and_swap(void *origin_addr, void *compare_addr,
2922 void *result_addr, MPI_Datatype datatype, int target_rank,
2923 MPI_Aint target_disp, MPI_Win win){
2926 if (win == MPI_WIN_NULL) {
2927 retval = MPI_ERR_WIN;
2928 } else if (target_rank == MPI_PROC_NULL) {
2929 retval = MPI_SUCCESS;
2930 } else if (target_rank <0){
2931 retval = MPI_ERR_RANK;
2932 } else if (win->dynamic()==0 && target_disp <0){
2933 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2934 retval = MPI_ERR_ARG;
2935 } else if (origin_addr==nullptr || result_addr==nullptr || compare_addr==nullptr){
2936 retval = MPI_ERR_COUNT;
2937 } else if (not datatype->is_valid()) {
2938 retval = MPI_ERR_TYPE;
2940 int rank = smpi_process()->index();
2942 win->get_group(&group);
2943 int src_traced = group->index(target_rank);
2944 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2946 retval = win->compare_and_swap( origin_addr, compare_addr, result_addr, datatype,
2947 target_rank, target_disp);
2949 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2955 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2958 if (win == MPI_WIN_NULL) {
2959 retval = MPI_ERR_WIN;
2960 } else if (group==MPI_GROUP_NULL){
2961 retval = MPI_ERR_GROUP;
2963 int rank = smpi_process()->index();
2964 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2965 retval = win->post(group,assert);
2966 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2972 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2975 if (win == MPI_WIN_NULL) {
2976 retval = MPI_ERR_WIN;
2977 } else if (group==MPI_GROUP_NULL){
2978 retval = MPI_ERR_GROUP;
2980 int rank = smpi_process()->index();
2981 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2982 retval = win->start(group,assert);
2983 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2989 int PMPI_Win_complete(MPI_Win win){
2992 if (win == MPI_WIN_NULL) {
2993 retval = MPI_ERR_WIN;
2995 int rank = smpi_process()->index();
2996 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2998 retval = win->complete();
3000 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3006 int PMPI_Win_wait(MPI_Win win){
3009 if (win == MPI_WIN_NULL) {
3010 retval = MPI_ERR_WIN;
3012 int rank = smpi_process()->index();
3013 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
3015 retval = win->wait();
3017 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3023 int PMPI_Win_lock(int lock_type, int rank, int assert, MPI_Win win){
3026 if (win == MPI_WIN_NULL) {
3027 retval = MPI_ERR_WIN;
3028 } else if (lock_type != MPI_LOCK_EXCLUSIVE &&
3029 lock_type != MPI_LOCK_SHARED) {
3030 retval = MPI_ERR_LOCKTYPE;
3031 } else if (rank == MPI_PROC_NULL){
3032 retval = MPI_SUCCESS;
3034 int myrank = smpi_process()->index();
3035 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3036 retval = win->lock(lock_type,rank,assert);
3037 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3043 int PMPI_Win_unlock(int rank, MPI_Win win){
3046 if (win == MPI_WIN_NULL) {
3047 retval = MPI_ERR_WIN;
3048 } else if (rank == MPI_PROC_NULL){
3049 retval = MPI_SUCCESS;
3051 int myrank = smpi_process()->index();
3052 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3053 retval = win->unlock(rank);
3054 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3060 int PMPI_Win_lock_all(int assert, MPI_Win win){
3063 if (win == MPI_WIN_NULL) {
3064 retval = MPI_ERR_WIN;
3066 int myrank = smpi_process()->index();
3067 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3068 retval = win->lock_all(assert);
3069 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3075 int PMPI_Win_unlock_all(MPI_Win win){
3078 if (win == MPI_WIN_NULL) {
3079 retval = MPI_ERR_WIN;
3081 int myrank = smpi_process()->index();
3082 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3083 retval = win->unlock_all();
3084 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3090 int PMPI_Win_flush(int rank, MPI_Win win){
3093 if (win == MPI_WIN_NULL) {
3094 retval = MPI_ERR_WIN;
3095 } else if (rank == MPI_PROC_NULL){
3096 retval = MPI_SUCCESS;
3098 int myrank = smpi_process()->index();
3099 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3100 retval = win->flush(rank);
3101 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3107 int PMPI_Win_flush_local(int rank, MPI_Win win){
3110 if (win == MPI_WIN_NULL) {
3111 retval = MPI_ERR_WIN;
3112 } else if (rank == MPI_PROC_NULL){
3113 retval = MPI_SUCCESS;
3115 int myrank = smpi_process()->index();
3116 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3117 retval = win->flush_local(rank);
3118 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3124 int PMPI_Win_flush_all(MPI_Win win){
3127 if (win == MPI_WIN_NULL) {
3128 retval = MPI_ERR_WIN;
3130 int myrank = smpi_process()->index();
3131 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3132 retval = win->flush_all();
3133 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3139 int PMPI_Win_flush_local_all(MPI_Win win){
3142 if (win == MPI_WIN_NULL) {
3143 retval = MPI_ERR_WIN;
3145 int myrank = smpi_process()->index();
3146 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3147 retval = win->flush_local_all();
3148 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3154 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
3155 void *ptr = xbt_malloc(size);
3157 return MPI_ERR_NO_MEM;
3159 *static_cast<void**>(baseptr) = ptr;
3164 int PMPI_Free_mem(void *baseptr){
3169 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
3171 if (datatype == MPI_DATATYPE_NULL) {
3172 return MPI_ERR_TYPE;
3173 } else if (name == nullptr) {
3176 datatype->set_name(name);
3181 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
3183 if (datatype == MPI_DATATYPE_NULL) {
3184 return MPI_ERR_TYPE;
3185 } else if (name == nullptr) {
3188 datatype->get_name(name, len);
3193 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
3194 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
3197 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
3198 return datatype->c2f();
3201 MPI_Group PMPI_Group_f2c(MPI_Fint group){
3202 return simgrid::smpi::Group::f2c(group);
3205 MPI_Fint PMPI_Group_c2f(MPI_Group group){
3206 return group->c2f();
3209 MPI_Request PMPI_Request_f2c(MPI_Fint request){
3210 return static_cast<MPI_Request>(simgrid::smpi::Request::f2c(request));
3213 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
3214 return request->c2f();
3217 MPI_Win PMPI_Win_f2c(MPI_Fint win){
3218 return static_cast<MPI_Win>(simgrid::smpi::Win::f2c(win));
3221 MPI_Fint PMPI_Win_c2f(MPI_Win win){
3225 MPI_Op PMPI_Op_f2c(MPI_Fint op){
3226 return static_cast<MPI_Op>(simgrid::smpi::Op::f2c(op));
3229 MPI_Fint PMPI_Op_c2f(MPI_Op op){
3233 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
3234 return static_cast<MPI_Comm>(simgrid::smpi::Comm::f2c(comm));
3237 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
3241 MPI_Info PMPI_Info_f2c(MPI_Fint info){
3242 return static_cast<MPI_Info>(simgrid::smpi::Info::f2c(info));
3245 MPI_Fint PMPI_Info_c2f(MPI_Info info){
3249 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3250 smpi_copy_fn _copy_fn={copy_fn,nullptr,nullptr};
3251 smpi_delete_fn _delete_fn={delete_fn,nullptr,nullptr};
3252 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Comm>(_copy_fn, _delete_fn, keyval, extra_state);
3255 int PMPI_Keyval_free(int* keyval) {
3256 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Comm>(keyval);
3259 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3260 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3261 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3263 else if (comm==MPI_COMM_NULL)
3264 return MPI_ERR_COMM;
3266 return comm->attr_delete<simgrid::smpi::Comm>(keyval);
3269 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3271 static int zero = 0;
3272 static int tag_ub = INT_MAX;
3273 static int last_used_code = MPI_ERR_LASTCODE;
3275 if (comm==MPI_COMM_NULL){
3277 return MPI_ERR_COMM;
3285 *static_cast<int**>(attr_value) = &zero;
3287 case MPI_UNIVERSE_SIZE:
3289 *static_cast<int**>(attr_value) = &smpi_universe_size;
3291 case MPI_LASTUSEDCODE:
3293 *static_cast<int**>(attr_value) = &last_used_code;
3297 *static_cast<int**>(attr_value) = &tag_ub;
3299 case MPI_WTIME_IS_GLOBAL:
3301 *static_cast<int**>(attr_value) = &one;
3304 return comm->attr_get<simgrid::smpi::Comm>(keyval, attr_value, flag);
3308 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3309 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3310 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3312 else if (comm==MPI_COMM_NULL)
3313 return MPI_ERR_COMM;
3315 return comm->attr_put<simgrid::smpi::Comm>(keyval, attr_value);
3318 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3320 return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3323 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3325 return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3328 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3330 return PMPI_Attr_delete(comm, comm_keyval);
3333 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3336 return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3339 int PMPI_Comm_free_keyval(int* keyval) {
3340 return PMPI_Keyval_free(keyval);
3343 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3345 if (type==MPI_DATATYPE_NULL)
3346 return MPI_ERR_TYPE;
3348 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
3351 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3353 if (type==MPI_DATATYPE_NULL)
3354 return MPI_ERR_TYPE;
3356 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
3359 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3361 if (type==MPI_DATATYPE_NULL)
3362 return MPI_ERR_TYPE;
3364 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
3367 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3370 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
3371 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
3372 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
3375 int PMPI_Type_free_keyval(int* keyval) {
3376 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
3379 int PMPI_Win_get_attr (MPI_Win win, int keyval, void *attribute_val, int* flag)
3381 static MPI_Aint size;
3382 static int disp_unit;
3383 if (win==MPI_WIN_NULL)
3384 return MPI_ERR_TYPE;
3388 *static_cast<void**>(attribute_val) = win->base();
3393 *static_cast<MPI_Aint**>(attribute_val) = &size;
3396 case MPI_WIN_DISP_UNIT :
3397 disp_unit=win->disp_unit();
3398 *static_cast<int**>(attribute_val) = &disp_unit;
3402 return win->attr_get<simgrid::smpi::Win>(keyval, attribute_val, flag);
3408 int PMPI_Win_set_attr (MPI_Win win, int type_keyval, void *attribute_val)
3410 if (win==MPI_WIN_NULL)
3411 return MPI_ERR_TYPE;
3413 return win->attr_put<simgrid::smpi::Win>(type_keyval, attribute_val);
3416 int PMPI_Win_delete_attr (MPI_Win win, int type_keyval)
3418 if (win==MPI_WIN_NULL)
3419 return MPI_ERR_TYPE;
3421 return win->attr_delete<simgrid::smpi::Win>(type_keyval);
3424 int PMPI_Win_create_keyval(MPI_Win_copy_attr_function* copy_fn, MPI_Win_delete_attr_function* delete_fn, int* keyval,
3427 smpi_copy_fn _copy_fn={nullptr, nullptr, copy_fn};
3428 smpi_delete_fn _delete_fn={nullptr, nullptr, delete_fn};
3429 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Win>(_copy_fn, _delete_fn, keyval, extra_state);
3432 int PMPI_Win_free_keyval(int* keyval) {
3433 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Win>(keyval);
3436 int PMPI_Info_create( MPI_Info *info){
3437 if (info == nullptr)
3439 *info = new simgrid::smpi::Info();
3443 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3444 if (info == nullptr || key == nullptr || value == nullptr)
3446 info->set(key, value);
3450 int PMPI_Info_free( MPI_Info *info){
3451 if (info == nullptr || *info==nullptr)
3453 simgrid::smpi::Info::unref(*info);
3454 *info=MPI_INFO_NULL;
3458 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3460 if (info == nullptr || key == nullptr || valuelen <0)
3462 if (value == nullptr)
3463 return MPI_ERR_INFO_VALUE;
3464 return info->get(key, valuelen, value, flag);
3467 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3468 if (info == nullptr || newinfo==nullptr)
3470 *newinfo = new simgrid::smpi::Info(info);
3474 int PMPI_Info_delete(MPI_Info info, char *key){
3475 if (info == nullptr || key==nullptr)
3477 return info->remove(key);
3480 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3481 if (info == nullptr || nkeys==nullptr)
3483 return info->get_nkeys(nkeys);
3486 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3487 if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3489 return info->get_nthkey(n, key);
3492 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3494 if (info == nullptr || key == nullptr || valuelen==nullptr)
3496 return info->get_valuelen(key, valuelen, flag);
3499 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3500 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3502 if (not type->is_valid())
3503 return MPI_ERR_TYPE;
3504 if(comm==MPI_COMM_NULL)
3505 return MPI_ERR_COMM;
3506 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
3509 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3510 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3512 if (not type->is_valid())
3513 return MPI_ERR_TYPE;
3514 if(comm==MPI_COMM_NULL)
3515 return MPI_ERR_COMM;
3516 return type->pack(inbuf, incount, outbuf,outcount,position, comm);
3519 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3522 if (not datatype->is_valid())
3523 return MPI_ERR_TYPE;
3524 if(comm==MPI_COMM_NULL)
3525 return MPI_ERR_COMM;
3527 *size=incount*datatype->size();