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. */
6 #include "simgrid/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/smpi/smpi_comm.hpp"
9 #include "src/smpi/smpi_coll.hpp"
10 #include "src/smpi/smpi_datatype_derived.hpp"
11 #include "src/smpi/smpi_op.hpp"
12 #include "src/smpi/smpi_process.hpp"
13 #include "src/smpi/smpi_request.hpp"
14 #include "src/smpi/smpi_status.hpp"
15 #include "src/smpi/smpi_win.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi, "Logging specific to SMPI (pmpi)");
19 //this function need to be here because of the calls to smpi_bench
20 void TRACE_smpi_set_category(const char *category)
22 //need to end bench otherwise categories for execution tasks are wrong
24 TRACE_internal_smpi_set_category (category);
25 //begin bench after changing process's category
29 /* PMPI User level calls */
30 extern "C" { // Obviously, the C MPI interface should use the C linkage
32 int PMPI_Init(int *argc, char ***argv)
34 xbt_assert(simgrid::s4u::Engine::isInitialized(),
35 "Your MPI program was not properly initialized. The easiest is to use smpirun to start it.");
36 // PMPI_Init is called only once per SMPI process
38 MPI_Initialized(&already_init);
39 if(already_init == 0){
40 simgrid::smpi::Process::init(argc, argv);
41 smpi_process()->mark_as_initialized();
42 int rank = smpi_process()->index();
43 TRACE_smpi_init(rank);
44 TRACE_smpi_computing_init(rank);
45 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
46 extra->type = TRACING_INIT;
47 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
48 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
60 int rank = smpi_process()->index();
61 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
62 extra->type = TRACING_FINALIZE;
63 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
65 smpi_process()->finalize();
67 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
68 TRACE_smpi_finalize(smpi_process()->index());
72 int PMPI_Finalized(int* flag)
74 *flag=smpi_process()!=nullptr ? smpi_process()->finalized() : 0;
78 int PMPI_Get_version (int *version,int *subversion){
79 *version = MPI_VERSION;
80 *subversion= MPI_SUBVERSION;
84 int PMPI_Get_library_version (char *version,int *len){
86 snprintf(version, MPI_MAX_LIBRARY_VERSION_STRING, "SMPI Version %d.%d. Copyright The Simgrid Team 2007-2017",
87 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR);
88 *len = strlen(version) > MPI_MAX_LIBRARY_VERSION_STRING ? MPI_MAX_LIBRARY_VERSION_STRING : strlen(version);
93 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
95 if (provided != nullptr) {
96 *provided = MPI_THREAD_SINGLE;
98 return MPI_Init(argc, argv);
101 int PMPI_Query_thread(int *provided)
103 if (provided == nullptr) {
106 *provided = MPI_THREAD_SINGLE;
111 int PMPI_Is_thread_main(int *flag)
113 if (flag == nullptr) {
116 *flag = smpi_process()->index() == 0;
121 int PMPI_Abort(MPI_Comm comm, int errorcode)
124 // FIXME: should kill all processes in comm instead
125 simcall_process_kill(SIMIX_process_self());
131 return smpi_mpi_wtime();
134 extern double sg_maxmin_precision;
137 return sg_maxmin_precision;
140 int PMPI_Address(void *location, MPI_Aint * address)
142 if (address==nullptr) {
145 *address = reinterpret_cast<MPI_Aint>(location);
150 int PMPI_Get_address(void *location, MPI_Aint * address)
152 return PMPI_Address(location, address);
155 int PMPI_Type_free(MPI_Datatype * datatype)
157 /* Free a predefined datatype is an error according to the standard, and should be checked for */
158 if (*datatype == MPI_DATATYPE_NULL) {
161 simgrid::smpi::Datatype::unref(*datatype);
166 int PMPI_Type_size(MPI_Datatype datatype, int *size)
168 if (datatype == MPI_DATATYPE_NULL) {
170 } else if (size == nullptr) {
173 *size = static_cast<int>(datatype->size());
178 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
180 if (datatype == MPI_DATATYPE_NULL) {
182 } else if (size == nullptr) {
185 *size = static_cast<MPI_Count>(datatype->size());
190 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
192 if (datatype == MPI_DATATYPE_NULL) {
194 } else if (lb == nullptr || extent == nullptr) {
197 return datatype->extent(lb, extent);
201 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
203 return PMPI_Type_get_extent(datatype, lb, extent);
206 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
208 if (datatype == MPI_DATATYPE_NULL) {
210 } else if (extent == nullptr) {
213 *extent = datatype->get_extent();
218 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
220 if (datatype == MPI_DATATYPE_NULL) {
222 } else if (disp == nullptr) {
225 *disp = datatype->lb();
230 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
232 if (datatype == MPI_DATATYPE_NULL) {
234 } else if (disp == nullptr) {
237 *disp = datatype->ub();
242 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
243 int retval = MPI_SUCCESS;
244 if (datatype == MPI_DATATYPE_NULL) {
247 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
248 //error when duplicating, free the new datatype
249 if(retval!=MPI_SUCCESS){
250 simgrid::smpi::Datatype::unref(*newtype);
251 *newtype = MPI_DATATYPE_NULL;
257 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
259 if (function == nullptr || op == nullptr) {
262 *op = new simgrid::smpi::Op(function, (commute!=0));
267 int PMPI_Op_free(MPI_Op * op)
271 } else if (*op == MPI_OP_NULL) {
280 int PMPI_Op_commutative(MPI_Op op, int* commute){
281 if (op == MPI_OP_NULL) {
283 } else if (commute==nullptr){
286 *commute = op->is_commutative();
291 int PMPI_Group_free(MPI_Group * group)
293 if (group == nullptr) {
296 if(*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_EMPTY)
297 simgrid::smpi::Group::unref(*group);
298 *group = MPI_GROUP_NULL;
303 int PMPI_Group_size(MPI_Group group, int *size)
305 if (group == MPI_GROUP_NULL) {
306 return MPI_ERR_GROUP;
307 } else if (size == nullptr) {
310 *size = group->size();
315 int PMPI_Group_rank(MPI_Group group, int *rank)
317 if (group == MPI_GROUP_NULL) {
318 return MPI_ERR_GROUP;
319 } else if (rank == nullptr) {
322 *rank = group->rank(smpi_process()->index());
327 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1, MPI_Group group2, int *ranks2)
329 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
330 return MPI_ERR_GROUP;
332 for (int i = 0; i < n; i++) {
333 if(ranks1[i]==MPI_PROC_NULL){
334 ranks2[i]=MPI_PROC_NULL;
336 int index = group1->index(ranks1[i]);
337 ranks2[i] = group2->rank(index);
344 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
346 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
347 return MPI_ERR_GROUP;
348 } else if (result == nullptr) {
351 *result = group1->compare(group2);
356 int PMPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
359 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
360 return MPI_ERR_GROUP;
361 } else if (newgroup == nullptr) {
364 return group1->group_union(group2, newgroup);
368 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
371 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
372 return MPI_ERR_GROUP;
373 } else if (newgroup == nullptr) {
376 return group1->intersection(group2,newgroup);
380 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
382 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
383 return MPI_ERR_GROUP;
384 } else if (newgroup == nullptr) {
387 return group1->difference(group2,newgroup);
391 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
393 if (group == MPI_GROUP_NULL) {
394 return MPI_ERR_GROUP;
395 } else if (newgroup == nullptr) {
398 return group->incl(n, ranks, newgroup);
402 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
404 if (group == MPI_GROUP_NULL) {
405 return MPI_ERR_GROUP;
406 } else if (newgroup == nullptr) {
411 if (group != MPI_COMM_WORLD->group()
412 && group != MPI_COMM_SELF->group() && group != MPI_GROUP_EMPTY)
415 } else if (n == group->size()) {
416 *newgroup = MPI_GROUP_EMPTY;
419 return group->excl(n,ranks,newgroup);
424 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
426 if (group == MPI_GROUP_NULL) {
427 return MPI_ERR_GROUP;
428 } else if (newgroup == nullptr) {
432 *newgroup = MPI_GROUP_EMPTY;
435 return group->range_incl(n,ranges,newgroup);
440 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
442 if (group == MPI_GROUP_NULL) {
443 return MPI_ERR_GROUP;
444 } else if (newgroup == nullptr) {
449 if (group != MPI_COMM_WORLD->group() && group != MPI_COMM_SELF->group() &&
450 group != MPI_GROUP_EMPTY)
454 return group->range_excl(n,ranges,newgroup);
459 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
461 if (comm == MPI_COMM_NULL) {
463 } else if (rank == nullptr) {
466 *rank = comm->rank();
471 int PMPI_Comm_size(MPI_Comm comm, int *size)
473 if (comm == MPI_COMM_NULL) {
475 } else if (size == nullptr) {
478 *size = comm->size();
483 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
485 if (comm == MPI_COMM_NULL) {
487 } else if (name == nullptr || len == nullptr) {
490 comm->get_name(name, len);
495 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
497 if (comm == MPI_COMM_NULL) {
499 } else if (group == nullptr) {
502 *group = comm->group();
503 if (*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_NULL && *group != MPI_GROUP_EMPTY)
509 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
511 if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
513 } else if (result == nullptr) {
516 if (comm1 == comm2) { /* Same communicators means same groups */
519 *result = comm1->group()->compare(comm2->group());
520 if (*result == MPI_IDENT) {
521 *result = MPI_CONGRUENT;
528 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
530 if (comm == MPI_COMM_NULL) {
532 } else if (newcomm == nullptr) {
535 return comm->dup(newcomm);
539 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
541 if (comm == MPI_COMM_NULL) {
543 } else if (group == MPI_GROUP_NULL) {
544 return MPI_ERR_GROUP;
545 } else if (newcomm == nullptr) {
547 } else if(group->rank(smpi_process()->index())==MPI_UNDEFINED){
548 *newcomm= MPI_COMM_NULL;
552 *newcomm = new simgrid::smpi::Comm(group, nullptr);
557 int PMPI_Comm_free(MPI_Comm * comm)
559 if (comm == nullptr) {
561 } else if (*comm == MPI_COMM_NULL) {
564 simgrid::smpi::Comm::destroy(*comm);
565 *comm = MPI_COMM_NULL;
570 int PMPI_Comm_disconnect(MPI_Comm * comm)
572 /* TODO: wait until all communication in comm are done */
573 if (comm == nullptr) {
575 } else if (*comm == MPI_COMM_NULL) {
578 simgrid::smpi::Comm::destroy(*comm);
579 *comm = MPI_COMM_NULL;
584 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
589 if (comm_out == nullptr) {
590 retval = MPI_ERR_ARG;
591 } else if (comm == MPI_COMM_NULL) {
592 retval = MPI_ERR_COMM;
594 *comm_out = comm->split(color, key);
595 retval = MPI_SUCCESS;
602 int PMPI_Comm_create_group(MPI_Comm comm, MPI_Group group, int, MPI_Comm* comm_out)
607 if (comm_out == nullptr) {
608 retval = MPI_ERR_ARG;
609 } else if (comm == MPI_COMM_NULL) {
610 retval = MPI_ERR_COMM;
612 retval = MPI_Comm_create(comm, group, comm_out);
619 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
624 if (request == nullptr) {
625 retval = MPI_ERR_ARG;
626 } else if (comm == MPI_COMM_NULL) {
627 retval = MPI_ERR_COMM;
628 } else if (!datatype->is_valid()) {
629 retval = MPI_ERR_TYPE;
630 } else if (dst == MPI_PROC_NULL) {
631 retval = MPI_SUCCESS;
633 *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
634 retval = MPI_SUCCESS;
637 if (retval != MPI_SUCCESS && request != nullptr)
638 *request = MPI_REQUEST_NULL;
642 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
647 if (request == nullptr) {
648 retval = MPI_ERR_ARG;
649 } else if (comm == MPI_COMM_NULL) {
650 retval = MPI_ERR_COMM;
651 } else if (!datatype->is_valid()) {
652 retval = MPI_ERR_TYPE;
653 } else if (src == MPI_PROC_NULL) {
654 retval = MPI_SUCCESS;
656 *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
657 retval = MPI_SUCCESS;
660 if (retval != MPI_SUCCESS && request != nullptr)
661 *request = MPI_REQUEST_NULL;
665 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
670 if (request == nullptr) {
671 retval = MPI_ERR_ARG;
672 } else if (comm == MPI_COMM_NULL) {
673 retval = MPI_ERR_COMM;
674 } else if (!datatype->is_valid()) {
675 retval = MPI_ERR_TYPE;
676 } else if (dst == MPI_PROC_NULL) {
677 retval = MPI_SUCCESS;
679 *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
680 retval = MPI_SUCCESS;
683 if (retval != MPI_SUCCESS && request != nullptr)
684 *request = MPI_REQUEST_NULL;
688 int PMPI_Start(MPI_Request * request)
693 if (request == nullptr || *request == MPI_REQUEST_NULL) {
694 retval = MPI_ERR_REQUEST;
697 retval = MPI_SUCCESS;
703 int PMPI_Startall(int count, MPI_Request * requests)
707 if (requests == nullptr) {
708 retval = MPI_ERR_ARG;
710 retval = MPI_SUCCESS;
711 for (int i = 0; i < count; i++) {
712 if(requests[i] == MPI_REQUEST_NULL) {
713 retval = MPI_ERR_REQUEST;
716 if(retval != MPI_ERR_REQUEST) {
717 simgrid::smpi::Request::startall(count, requests);
724 int PMPI_Request_free(MPI_Request * request)
729 if (*request == MPI_REQUEST_NULL) {
730 retval = MPI_ERR_ARG;
732 simgrid::smpi::Request::unref(request);
733 retval = MPI_SUCCESS;
739 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
745 if (request == nullptr) {
746 retval = MPI_ERR_ARG;
747 } else if (comm == MPI_COMM_NULL) {
748 retval = MPI_ERR_COMM;
749 } else if (src == MPI_PROC_NULL) {
750 *request = MPI_REQUEST_NULL;
751 retval = MPI_SUCCESS;
752 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
753 retval = MPI_ERR_RANK;
754 } else if ((count < 0) || (buf==nullptr && count > 0)) {
755 retval = MPI_ERR_COUNT;
756 } else if (!datatype->is_valid()) {
757 retval = MPI_ERR_TYPE;
758 } else if(tag<0 && tag != MPI_ANY_TAG){
759 retval = MPI_ERR_TAG;
762 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
763 int src_traced = comm->group()->index(src);
765 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
766 extra->type = TRACING_IRECV;
767 extra->src = src_traced;
770 extra->datatype1 = encode_datatype(datatype, &known);
771 int dt_size_send = 1;
773 dt_size_send = datatype->size();
774 extra->send_size = count*dt_size_send;
775 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
777 *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
778 retval = MPI_SUCCESS;
780 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
784 if (retval != MPI_SUCCESS && request != nullptr)
785 *request = MPI_REQUEST_NULL;
790 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
795 if (request == nullptr) {
796 retval = MPI_ERR_ARG;
797 } else if (comm == MPI_COMM_NULL) {
798 retval = MPI_ERR_COMM;
799 } else if (dst == MPI_PROC_NULL) {
800 *request = MPI_REQUEST_NULL;
801 retval = MPI_SUCCESS;
802 } else if (dst >= comm->group()->size() || dst <0){
803 retval = MPI_ERR_RANK;
804 } else if ((count < 0) || (buf==nullptr && count > 0)) {
805 retval = MPI_ERR_COUNT;
806 } else if (!datatype->is_valid()) {
807 retval = MPI_ERR_TYPE;
808 } else if(tag<0 && tag != MPI_ANY_TAG){
809 retval = MPI_ERR_TAG;
811 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
812 int dst_traced = comm->group()->index(dst);
813 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
814 extra->type = TRACING_ISEND;
816 extra->dst = dst_traced;
818 extra->datatype1 = encode_datatype(datatype, &known);
819 int dt_size_send = 1;
821 dt_size_send = datatype->size();
822 extra->send_size = count*dt_size_send;
823 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
824 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
826 *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
827 retval = MPI_SUCCESS;
829 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
833 if (retval != MPI_SUCCESS && request!=nullptr)
834 *request = MPI_REQUEST_NULL;
838 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
843 if (request == nullptr) {
844 retval = MPI_ERR_ARG;
845 } else if (comm == MPI_COMM_NULL) {
846 retval = MPI_ERR_COMM;
847 } else if (dst == MPI_PROC_NULL) {
848 *request = MPI_REQUEST_NULL;
849 retval = MPI_SUCCESS;
850 } else if (dst >= comm->group()->size() || dst <0){
851 retval = MPI_ERR_RANK;
852 } else if ((count < 0)|| (buf==nullptr && count > 0)) {
853 retval = MPI_ERR_COUNT;
854 } else if (!datatype->is_valid()) {
855 retval = MPI_ERR_TYPE;
856 } else if(tag<0 && tag != MPI_ANY_TAG){
857 retval = MPI_ERR_TAG;
859 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
860 int dst_traced = comm->group()->index(dst);
861 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
862 extra->type = TRACING_ISSEND;
864 extra->dst = dst_traced;
866 extra->datatype1 = encode_datatype(datatype, &known);
867 int dt_size_send = 1;
869 dt_size_send = datatype->size();
870 extra->send_size = count*dt_size_send;
871 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
872 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
874 *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
875 retval = MPI_SUCCESS;
877 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
881 if (retval != MPI_SUCCESS && request!=nullptr)
882 *request = MPI_REQUEST_NULL;
886 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
891 if (comm == MPI_COMM_NULL) {
892 retval = MPI_ERR_COMM;
893 } else if (src == MPI_PROC_NULL) {
894 simgrid::smpi::Status::empty(status);
895 status->MPI_SOURCE = MPI_PROC_NULL;
896 retval = MPI_SUCCESS;
897 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
898 retval = MPI_ERR_RANK;
899 } else if ((count < 0) || (buf==nullptr && count > 0)) {
900 retval = MPI_ERR_COUNT;
901 } else if (!datatype->is_valid()) {
902 retval = MPI_ERR_TYPE;
903 } else if(tag<0 && tag != MPI_ANY_TAG){
904 retval = MPI_ERR_TAG;
906 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
907 int src_traced = comm->group()->index(src);
908 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
909 extra->type = TRACING_RECV;
910 extra->src = src_traced;
913 extra->datatype1 = encode_datatype(datatype, &known);
914 int dt_size_send = 1;
916 dt_size_send = datatype->size();
917 extra->send_size = count * dt_size_send;
918 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
920 simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
921 retval = MPI_SUCCESS;
923 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
924 if (status != MPI_STATUS_IGNORE) {
925 src_traced = comm->group()->index(status->MPI_SOURCE);
926 if (!TRACE_smpi_view_internals()) {
927 TRACE_smpi_recv(rank, src_traced, rank, tag);
930 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
937 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
943 if (comm == MPI_COMM_NULL) {
944 retval = MPI_ERR_COMM;
945 } else if (dst == MPI_PROC_NULL) {
946 retval = MPI_SUCCESS;
947 } else if (dst >= comm->group()->size() || dst <0){
948 retval = MPI_ERR_RANK;
949 } else if ((count < 0) || (buf == nullptr && count > 0)) {
950 retval = MPI_ERR_COUNT;
951 } else if (!datatype->is_valid()) {
952 retval = MPI_ERR_TYPE;
953 } else if(tag < 0 && tag != MPI_ANY_TAG){
954 retval = MPI_ERR_TAG;
956 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
957 int dst_traced = comm->group()->index(dst);
958 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
959 extra->type = TRACING_SEND;
961 extra->dst = dst_traced;
963 extra->datatype1 = encode_datatype(datatype, &known);
964 int dt_size_send = 1;
966 dt_size_send = datatype->size();
968 extra->send_size = count*dt_size_send;
969 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
970 if (!TRACE_smpi_view_internals()) {
971 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
974 simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
975 retval = MPI_SUCCESS;
977 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
984 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
989 if (comm == MPI_COMM_NULL) {
990 retval = MPI_ERR_COMM;
991 } else if (dst == MPI_PROC_NULL) {
992 retval = MPI_SUCCESS;
993 } else if (dst >= comm->group()->size() || dst <0){
994 retval = MPI_ERR_RANK;
995 } else if ((count < 0) || (buf==nullptr && count > 0)) {
996 retval = MPI_ERR_COUNT;
997 } else if (!datatype->is_valid()){
998 retval = MPI_ERR_TYPE;
999 } else if(tag<0 && tag != MPI_ANY_TAG){
1000 retval = MPI_ERR_TAG;
1002 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1003 int dst_traced = comm->group()->index(dst);
1004 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1005 extra->type = TRACING_SSEND;
1007 extra->dst = dst_traced;
1009 extra->datatype1 = encode_datatype(datatype, &known);
1010 int dt_size_send = 1;
1012 dt_size_send = datatype->size();
1014 extra->send_size = count*dt_size_send;
1015 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1016 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
1018 simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
1019 retval = MPI_SUCCESS;
1021 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1028 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void *recvbuf,
1029 int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status)
1035 if (comm == MPI_COMM_NULL) {
1036 retval = MPI_ERR_COMM;
1037 } else if (!sendtype->is_valid() || !recvtype->is_valid()) {
1038 retval = MPI_ERR_TYPE;
1039 } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1040 simgrid::smpi::Status::empty(status);
1041 status->MPI_SOURCE = MPI_PROC_NULL;
1042 retval = MPI_SUCCESS;
1043 }else if (dst >= comm->group()->size() || dst <0 ||
1044 (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
1045 retval = MPI_ERR_RANK;
1046 } else if ((sendcount < 0 || recvcount<0) ||
1047 (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
1048 retval = MPI_ERR_COUNT;
1049 } else if((sendtag<0 && sendtag != MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1050 retval = MPI_ERR_TAG;
1053 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1054 int dst_traced = comm->group()->index(dst);
1055 int src_traced = comm->group()->index(src);
1056 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1057 extra->type = TRACING_SENDRECV;
1058 extra->src = src_traced;
1059 extra->dst = dst_traced;
1061 extra->datatype1 = encode_datatype(sendtype, &known);
1062 int dt_size_send = 1;
1064 dt_size_send = sendtype->size();
1065 extra->send_size = sendcount*dt_size_send;
1066 extra->datatype2 = encode_datatype(recvtype, &known);
1067 int dt_size_recv = 1;
1069 dt_size_recv = recvtype->size();
1070 extra->recv_size = recvcount*dt_size_recv;
1072 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1073 TRACE_smpi_send(rank, rank, dst_traced, sendtag,sendcount*sendtype->size());
1075 simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src, recvtag, comm,
1077 retval = MPI_SUCCESS;
1079 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1080 TRACE_smpi_recv(rank, src_traced, rank, recvtag);
1087 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
1088 MPI_Comm comm, MPI_Status* status)
1091 if (!datatype->is_valid()) {
1092 return MPI_ERR_TYPE;
1093 } else if (count < 0) {
1094 return MPI_ERR_COUNT;
1096 int size = datatype->get_extent() * count;
1097 void* recvbuf = xbt_new0(char, size);
1098 retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
1099 if(retval==MPI_SUCCESS){
1100 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, buf, count, datatype);
1108 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1112 if (request == nullptr || flag == nullptr) {
1113 retval = MPI_ERR_ARG;
1114 } else if (*request == MPI_REQUEST_NULL) {
1116 simgrid::smpi::Status::empty(status);
1117 retval = MPI_SUCCESS;
1119 int rank = ((*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1121 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1122 extra->type = TRACING_TEST;
1123 TRACE_smpi_testing_in(rank, extra);
1125 *flag = simgrid::smpi::Request::test(request,status);
1127 TRACE_smpi_testing_out(rank);
1128 retval = MPI_SUCCESS;
1134 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
1139 if (index == nullptr || flag == nullptr) {
1140 retval = MPI_ERR_ARG;
1142 *flag = simgrid::smpi::Request::testany(count, requests, index, status);
1143 retval = MPI_SUCCESS;
1149 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1154 if (flag == nullptr) {
1155 retval = MPI_ERR_ARG;
1157 *flag = simgrid::smpi::Request::testall(count, requests, statuses);
1158 retval = MPI_SUCCESS;
1164 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1168 if (status == nullptr) {
1169 retval = MPI_ERR_ARG;
1170 } else if (comm == MPI_COMM_NULL) {
1171 retval = MPI_ERR_COMM;
1172 } else if (source == MPI_PROC_NULL) {
1173 simgrid::smpi::Status::empty(status);
1174 status->MPI_SOURCE = MPI_PROC_NULL;
1175 retval = MPI_SUCCESS;
1177 simgrid::smpi::Request::probe(source, tag, comm, status);
1178 retval = MPI_SUCCESS;
1184 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1188 if (flag == nullptr) {
1189 retval = MPI_ERR_ARG;
1190 } else if (comm == MPI_COMM_NULL) {
1191 retval = MPI_ERR_COMM;
1192 } else if (source == MPI_PROC_NULL) {
1194 simgrid::smpi::Status::empty(status);
1195 status->MPI_SOURCE = MPI_PROC_NULL;
1196 retval = MPI_SUCCESS;
1198 simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
1199 retval = MPI_SUCCESS;
1205 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1211 simgrid::smpi::Status::empty(status);
1213 if (request == nullptr) {
1214 retval = MPI_ERR_ARG;
1215 } else if (*request == MPI_REQUEST_NULL) {
1216 retval = MPI_SUCCESS;
1219 int rank = (request!=nullptr && (*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1221 int src_traced = (*request)->src();
1222 int dst_traced = (*request)->dst();
1223 int tag_traced= (*request)->tag();
1224 MPI_Comm comm = (*request)->comm();
1225 int is_wait_for_receive = ((*request)->flags() & RECV);
1226 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1227 extra->type = TRACING_WAIT;
1228 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1230 simgrid::smpi::Request::wait(request, status);
1231 retval = MPI_SUCCESS;
1233 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1234 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1235 if (is_wait_for_receive) {
1236 if(src_traced==MPI_ANY_SOURCE)
1237 src_traced = (status!=MPI_STATUS_IGNORE) ?
1238 comm->group()->rank(status->MPI_SOURCE) :
1240 TRACE_smpi_recv(rank, src_traced, dst_traced, tag_traced);
1248 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1250 if (index == nullptr)
1254 //save requests information for tracing
1262 savedvalstype* savedvals=nullptr;
1264 savedvals = xbt_new0(savedvalstype, count);
1266 for (int i = 0; i < count; i++) {
1267 MPI_Request req = requests[i]; //already received requests are no longer valid
1269 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), req->comm()};
1272 int rank_traced = smpi_process()->index();
1273 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1274 extra->type = TRACING_WAITANY;
1275 extra->send_size=count;
1276 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1278 *index = simgrid::smpi::Request::waitany(count, requests, status);
1280 if(*index!=MPI_UNDEFINED){
1281 int src_traced = savedvals[*index].src;
1282 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1283 int dst_traced = savedvals[*index].dst;
1284 int is_wait_for_receive = savedvals[*index].recv;
1285 if (is_wait_for_receive) {
1286 if(savedvals[*index].src==MPI_ANY_SOURCE)
1287 src_traced = (status != MPI_STATUSES_IGNORE)
1288 ? savedvals[*index].comm->group()->rank(status->MPI_SOURCE)
1289 : savedvals[*index].src;
1290 TRACE_smpi_recv(rank_traced, src_traced, dst_traced, savedvals[*index].tag);
1292 TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1294 xbt_free(savedvals);
1300 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1303 //save information from requests
1312 savedvalstype* savedvals=xbt_new0(savedvalstype, count);
1314 for (int i = 0; i < count; i++) {
1315 MPI_Request req = requests[i];
1316 if(req!=MPI_REQUEST_NULL){
1317 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), 1, req->comm()};
1319 savedvals[i].valid=0;
1322 int rank_traced = smpi_process()->index();
1323 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1324 extra->type = TRACING_WAITALL;
1325 extra->send_size=count;
1326 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1328 int retval = simgrid::smpi::Request::waitall(count, requests, status);
1330 for (int i = 0; i < count; i++) {
1331 if(savedvals[i].valid){
1332 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1333 int src_traced = savedvals[i].src;
1334 int dst_traced = savedvals[i].dst;
1335 int is_wait_for_receive = savedvals[i].recv;
1336 if (is_wait_for_receive) {
1337 if(src_traced==MPI_ANY_SOURCE)
1338 src_traced = (status!=MPI_STATUSES_IGNORE) ?
1339 savedvals[i].comm->group()->rank(status[i].MPI_SOURCE) : savedvals[i].src;
1340 TRACE_smpi_recv(rank_traced, src_traced, dst_traced,savedvals[i].tag);
1344 TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1345 xbt_free(savedvals);
1351 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1356 if (outcount == nullptr) {
1357 retval = MPI_ERR_ARG;
1359 *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
1360 retval = MPI_SUCCESS;
1366 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1371 if (outcount == nullptr) {
1372 retval = MPI_ERR_ARG;
1374 *outcount = simgrid::smpi::Request::testsome(incount, requests, indices, status);
1375 retval = MPI_SUCCESS;
1382 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1388 if (comm == MPI_COMM_NULL) {
1389 retval = MPI_ERR_COMM;
1390 } else if (!datatype->is_valid()) {
1391 retval = MPI_ERR_ARG;
1393 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1394 int root_traced = comm->group()->index(root);
1396 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1397 extra->type = TRACING_BCAST;
1398 extra->root = root_traced;
1400 extra->datatype1 = encode_datatype(datatype, &known);
1401 int dt_size_send = 1;
1403 dt_size_send = datatype->size();
1404 extra->send_size = count * dt_size_send;
1405 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1406 if (comm->size() > 1)
1407 simgrid::smpi::Colls::bcast(buf, count, datatype, root, comm);
1408 retval = MPI_SUCCESS;
1410 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1416 int PMPI_Barrier(MPI_Comm comm)
1422 if (comm == MPI_COMM_NULL) {
1423 retval = MPI_ERR_COMM;
1425 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1426 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1427 extra->type = TRACING_BARRIER;
1428 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1430 simgrid::smpi::Colls::barrier(comm);
1432 //Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
1433 comm->finish_rma_calls();
1435 retval = MPI_SUCCESS;
1437 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1444 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1445 int root, MPI_Comm comm)
1451 if (comm == MPI_COMM_NULL) {
1452 retval = MPI_ERR_COMM;
1453 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1454 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1455 retval = MPI_ERR_TYPE;
1456 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((comm->rank() == root) && (recvcount <0))){
1457 retval = MPI_ERR_COUNT;
1460 char* sendtmpbuf = static_cast<char*>(sendbuf);
1461 int sendtmpcount = sendcount;
1462 MPI_Datatype sendtmptype = sendtype;
1463 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1465 sendtmptype=recvtype;
1467 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1468 int root_traced = comm->group()->index(root);
1469 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1470 extra->type = TRACING_GATHER;
1471 extra->root = root_traced;
1473 extra->datatype1 = encode_datatype(sendtmptype, &known);
1474 int dt_size_send = 1;
1476 dt_size_send = sendtmptype->size();
1477 extra->send_size = sendtmpcount * dt_size_send;
1478 extra->datatype2 = encode_datatype(recvtype, &known);
1479 int dt_size_recv = 1;
1480 if ((comm->rank() == root) && known == 0)
1481 dt_size_recv = recvtype->size();
1482 extra->recv_size = recvcount * dt_size_recv;
1484 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1486 simgrid::smpi::Colls::gather(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1488 retval = MPI_SUCCESS;
1489 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1496 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1497 MPI_Datatype recvtype, int root, MPI_Comm comm)
1503 if (comm == MPI_COMM_NULL) {
1504 retval = MPI_ERR_COMM;
1505 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1506 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1507 retval = MPI_ERR_TYPE;
1508 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1509 retval = MPI_ERR_COUNT;
1510 } else if (recvcounts == nullptr || displs == nullptr) {
1511 retval = MPI_ERR_ARG;
1513 char* sendtmpbuf = static_cast<char*>(sendbuf);
1514 int sendtmpcount = sendcount;
1515 MPI_Datatype sendtmptype = sendtype;
1516 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1518 sendtmptype=recvtype;
1521 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1522 int root_traced = comm->group()->index(root);
1523 int size = comm->size();
1524 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1525 extra->type = TRACING_GATHERV;
1526 extra->num_processes = size;
1527 extra->root = root_traced;
1529 extra->datatype1 = encode_datatype(sendtmptype, &known);
1530 int dt_size_send = 1;
1532 dt_size_send = sendtype->size();
1533 extra->send_size = sendtmpcount * dt_size_send;
1534 extra->datatype2 = encode_datatype(recvtype, &known);
1535 int dt_size_recv = 1;
1537 dt_size_recv = recvtype->size();
1538 if (comm->rank() == root) {
1539 extra->recvcounts = xbt_new(int, size);
1540 for (int i = 0; i < size; i++) // copy data to avoid bad free
1541 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1543 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1545 retval = simgrid::smpi::Colls::gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1546 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1553 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1554 void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1560 if (comm == MPI_COMM_NULL) {
1561 retval = MPI_ERR_COMM;
1562 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1563 (recvtype == MPI_DATATYPE_NULL)){
1564 retval = MPI_ERR_TYPE;
1565 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1567 retval = MPI_ERR_COUNT;
1569 if(sendbuf == MPI_IN_PLACE) {
1570 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*recvcount*comm->rank();
1571 sendcount=recvcount;
1574 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1575 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1576 extra->type = TRACING_ALLGATHER;
1578 extra->datatype1 = encode_datatype(sendtype, &known);
1579 int dt_size_send = 1;
1581 dt_size_send = sendtype->size();
1582 extra->send_size = sendcount * dt_size_send;
1583 extra->datatype2 = encode_datatype(recvtype, &known);
1584 int dt_size_recv = 1;
1586 dt_size_recv = recvtype->size();
1587 extra->recv_size = recvcount * dt_size_recv;
1589 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1591 simgrid::smpi::Colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1592 retval = MPI_SUCCESS;
1593 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1599 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1600 void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1606 if (comm == MPI_COMM_NULL) {
1607 retval = MPI_ERR_COMM;
1608 } else if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL)) {
1609 retval = MPI_ERR_TYPE;
1610 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1611 retval = MPI_ERR_COUNT;
1612 } else if (recvcounts == nullptr || displs == nullptr) {
1613 retval = MPI_ERR_ARG;
1616 if(sendbuf == MPI_IN_PLACE) {
1617 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*displs[comm->rank()];
1618 sendcount=recvcounts[comm->rank()];
1621 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1623 int size = comm->size();
1624 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1625 extra->type = TRACING_ALLGATHERV;
1626 extra->num_processes = size;
1628 extra->datatype1 = encode_datatype(sendtype, &known);
1629 int dt_size_send = 1;
1631 dt_size_send = sendtype->size();
1632 extra->send_size = sendcount * dt_size_send;
1633 extra->datatype2 = encode_datatype(recvtype, &known);
1634 int dt_size_recv = 1;
1636 dt_size_recv = recvtype->size();
1637 extra->recvcounts = xbt_new(int, size);
1638 for (i = 0; i < size; i++) // copy data to avoid bad free
1639 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1641 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1643 simgrid::smpi::Colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1644 retval = MPI_SUCCESS;
1645 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1652 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1653 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1659 if (comm == MPI_COMM_NULL) {
1660 retval = MPI_ERR_COMM;
1661 } else if (((comm->rank() == root) && (!sendtype->is_valid())) ||
1662 ((recvbuf != MPI_IN_PLACE) && (!recvtype->is_valid()))) {
1663 retval = MPI_ERR_TYPE;
1664 } else if ((sendbuf == recvbuf) ||
1665 ((comm->rank()==root) && sendcount>0 && (sendbuf == nullptr))){
1666 retval = MPI_ERR_BUFFER;
1669 if (recvbuf == MPI_IN_PLACE) {
1670 recvtype = sendtype;
1671 recvcount = sendcount;
1673 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1674 int root_traced = comm->group()->index(root);
1675 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1676 extra->type = TRACING_SCATTER;
1677 extra->root = root_traced;
1679 extra->datatype1 = encode_datatype(sendtype, &known);
1680 int dt_size_send = 1;
1681 if ((comm->rank() == root) && known == 0)
1682 dt_size_send = sendtype->size();
1683 extra->send_size = sendcount * dt_size_send;
1684 extra->datatype2 = encode_datatype(recvtype, &known);
1685 int dt_size_recv = 1;
1687 dt_size_recv = recvtype->size();
1688 extra->recv_size = recvcount * dt_size_recv;
1689 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1691 simgrid::smpi::Colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1692 retval = MPI_SUCCESS;
1693 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1700 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1701 MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1707 if (comm == MPI_COMM_NULL) {
1708 retval = MPI_ERR_COMM;
1709 } else if (sendcounts == nullptr || displs == nullptr) {
1710 retval = MPI_ERR_ARG;
1711 } else if (((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL)) ||
1712 ((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1713 retval = MPI_ERR_TYPE;
1715 if (recvbuf == MPI_IN_PLACE) {
1716 recvtype = sendtype;
1717 recvcount = sendcounts[comm->rank()];
1719 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1720 int root_traced = comm->group()->index(root);
1721 int size = comm->size();
1722 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1723 extra->type = TRACING_SCATTERV;
1724 extra->num_processes = size;
1725 extra->root = root_traced;
1727 extra->datatype1 = encode_datatype(sendtype, &known);
1728 int dt_size_send = 1;
1730 dt_size_send = sendtype->size();
1731 if (comm->rank() == root) {
1732 extra->sendcounts = xbt_new(int, size);
1733 for (int i = 0; i < size; i++) // copy data to avoid bad free
1734 extra->sendcounts[i] = sendcounts[i] * dt_size_send;
1736 extra->datatype2 = encode_datatype(recvtype, &known);
1737 int dt_size_recv = 1;
1739 dt_size_recv = recvtype->size();
1740 extra->recv_size = recvcount * dt_size_recv;
1741 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1743 retval = simgrid::smpi::Colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1745 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1752 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1758 if (comm == MPI_COMM_NULL) {
1759 retval = MPI_ERR_COMM;
1760 } else if (!datatype->is_valid() || op == MPI_OP_NULL) {
1761 retval = MPI_ERR_ARG;
1763 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1764 int root_traced = comm->group()->index(root);
1765 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1766 extra->type = TRACING_REDUCE;
1768 extra->datatype1 = encode_datatype(datatype, &known);
1769 int dt_size_send = 1;
1771 dt_size_send = datatype->size();
1772 extra->send_size = count * dt_size_send;
1773 extra->root = root_traced;
1775 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1777 simgrid::smpi::Colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1779 retval = MPI_SUCCESS;
1780 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1787 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
1791 if (!datatype->is_valid() || op == MPI_OP_NULL) {
1792 retval = MPI_ERR_ARG;
1794 op->apply(inbuf, inoutbuf, &count, datatype);
1795 retval = MPI_SUCCESS;
1801 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1807 if (comm == MPI_COMM_NULL) {
1808 retval = MPI_ERR_COMM;
1809 } else if (!datatype->is_valid()) {
1810 retval = MPI_ERR_TYPE;
1811 } else if (op == MPI_OP_NULL) {
1812 retval = MPI_ERR_OP;
1815 char* sendtmpbuf = static_cast<char*>(sendbuf);
1816 if( sendbuf == MPI_IN_PLACE ) {
1817 sendtmpbuf = static_cast<char*>(xbt_malloc(count*datatype->get_extent()));
1818 simgrid::smpi::Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1820 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1821 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1822 extra->type = TRACING_ALLREDUCE;
1824 extra->datatype1 = encode_datatype(datatype, &known);
1825 int dt_size_send = 1;
1827 dt_size_send = datatype->size();
1828 extra->send_size = count * dt_size_send;
1830 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1832 simgrid::smpi::Colls::allreduce(sendtmpbuf, recvbuf, count, datatype, op, comm);
1834 if( sendbuf == MPI_IN_PLACE )
1835 xbt_free(sendtmpbuf);
1837 retval = MPI_SUCCESS;
1838 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1845 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1851 if (comm == MPI_COMM_NULL) {
1852 retval = MPI_ERR_COMM;
1853 } else if (!datatype->is_valid()) {
1854 retval = MPI_ERR_TYPE;
1855 } else if (op == MPI_OP_NULL) {
1856 retval = MPI_ERR_OP;
1858 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1859 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1860 extra->type = TRACING_SCAN;
1862 extra->datatype1 = encode_datatype(datatype, &known);
1863 int dt_size_send = 1;
1865 dt_size_send = datatype->size();
1866 extra->send_size = count * dt_size_send;
1868 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1870 retval = simgrid::smpi::Colls::scan(sendbuf, recvbuf, count, datatype, op, comm);
1872 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1879 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
1884 if (comm == MPI_COMM_NULL) {
1885 retval = MPI_ERR_COMM;
1886 } else if (!datatype->is_valid()) {
1887 retval = MPI_ERR_TYPE;
1888 } else if (op == MPI_OP_NULL) {
1889 retval = MPI_ERR_OP;
1891 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1892 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1893 extra->type = TRACING_EXSCAN;
1895 extra->datatype1 = encode_datatype(datatype, &known);
1896 int dt_size_send = 1;
1898 dt_size_send = datatype->size();
1899 extra->send_size = count * dt_size_send;
1900 void* sendtmpbuf = sendbuf;
1901 if (sendbuf == MPI_IN_PLACE) {
1902 sendtmpbuf = static_cast<void*>(xbt_malloc(count * datatype->size()));
1903 memcpy(sendtmpbuf, recvbuf, count * datatype->size());
1905 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1907 retval = simgrid::smpi::Colls::exscan(sendtmpbuf, recvbuf, count, datatype, op, comm);
1909 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1910 if (sendbuf == MPI_IN_PLACE)
1911 xbt_free(sendtmpbuf);
1918 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1923 if (comm == MPI_COMM_NULL) {
1924 retval = MPI_ERR_COMM;
1925 } else if (!datatype->is_valid()) {
1926 retval = MPI_ERR_TYPE;
1927 } else if (op == MPI_OP_NULL) {
1928 retval = MPI_ERR_OP;
1929 } else if (recvcounts == nullptr) {
1930 retval = MPI_ERR_ARG;
1932 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1934 int size = comm->size();
1935 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1936 extra->type = TRACING_REDUCE_SCATTER;
1937 extra->num_processes = size;
1939 extra->datatype1 = encode_datatype(datatype, &known);
1940 int dt_size_send = 1;
1942 dt_size_send = datatype->size();
1943 extra->send_size = 0;
1944 extra->recvcounts = xbt_new(int, size);
1946 for (i = 0; i < size; i++) { // copy data to avoid bad free
1947 extra->recvcounts[i] = recvcounts[i] * dt_size_send;
1948 totalcount += recvcounts[i];
1950 void* sendtmpbuf = sendbuf;
1951 if (sendbuf == MPI_IN_PLACE) {
1952 sendtmpbuf = static_cast<void*>(xbt_malloc(totalcount * datatype->size()));
1953 memcpy(sendtmpbuf, recvbuf, totalcount * datatype->size());
1956 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1958 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
1959 retval = MPI_SUCCESS;
1960 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1962 if (sendbuf == MPI_IN_PLACE)
1963 xbt_free(sendtmpbuf);
1970 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
1971 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1976 if (comm == MPI_COMM_NULL) {
1977 retval = MPI_ERR_COMM;
1978 } else if (!datatype->is_valid()) {
1979 retval = MPI_ERR_TYPE;
1980 } else if (op == MPI_OP_NULL) {
1981 retval = MPI_ERR_OP;
1982 } else if (recvcount < 0) {
1983 retval = MPI_ERR_ARG;
1985 int count = comm->size();
1987 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1988 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1989 extra->type = TRACING_REDUCE_SCATTER;
1990 extra->num_processes = count;
1992 extra->datatype1 = encode_datatype(datatype, &known);
1993 int dt_size_send = 1;
1995 dt_size_send = datatype->size();
1996 extra->send_size = 0;
1997 extra->recvcounts = xbt_new(int, count);
1998 for (int i = 0; i < count; i++) // copy data to avoid bad free
1999 extra->recvcounts[i] = recvcount * dt_size_send;
2000 void* sendtmpbuf = sendbuf;
2001 if (sendbuf == MPI_IN_PLACE) {
2002 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * count * datatype->size()));
2003 memcpy(sendtmpbuf, recvbuf, recvcount * count * datatype->size());
2006 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2008 int* recvcounts = static_cast<int*>(xbt_malloc(count * sizeof(int)));
2009 for (int i = 0; i < count; i++)
2010 recvcounts[i] = recvcount;
2011 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
2012 xbt_free(recvcounts);
2013 retval = MPI_SUCCESS;
2015 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2017 if (sendbuf == MPI_IN_PLACE)
2018 xbt_free(sendtmpbuf);
2025 int PMPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
2026 MPI_Datatype recvtype, MPI_Comm comm)
2031 if (comm == MPI_COMM_NULL) {
2032 retval = MPI_ERR_COMM;
2033 } else if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL) {
2034 retval = MPI_ERR_TYPE;
2036 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2037 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2038 extra->type = TRACING_ALLTOALL;
2040 void* sendtmpbuf = static_cast<char*>(sendbuf);
2041 int sendtmpcount = sendcount;
2042 MPI_Datatype sendtmptype = sendtype;
2043 if (sendbuf == MPI_IN_PLACE) {
2044 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * comm->size() * recvtype->size()));
2045 memcpy(sendtmpbuf, recvbuf, recvcount * comm->size() * recvtype->size());
2046 sendtmpcount = recvcount;
2047 sendtmptype = recvtype;
2051 extra->datatype1 = encode_datatype(sendtmptype, &known);
2053 extra->send_size = sendtmpcount * sendtmptype->size();
2055 extra->send_size = sendtmpcount;
2056 extra->datatype2 = encode_datatype(recvtype, &known);
2058 extra->recv_size = recvcount * recvtype->size();
2060 extra->recv_size = recvcount;
2062 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2064 retval = simgrid::smpi::Colls::alltoall(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, comm);
2066 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2068 if (sendbuf == MPI_IN_PLACE)
2069 xbt_free(sendtmpbuf);
2076 int PMPI_Alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf,
2077 int* recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2083 if (comm == MPI_COMM_NULL) {
2084 retval = MPI_ERR_COMM;
2085 } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2086 retval = MPI_ERR_TYPE;
2087 } else if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
2088 recvdisps == nullptr) {
2089 retval = MPI_ERR_ARG;
2091 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2093 int size = comm->size();
2094 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2095 extra->type = TRACING_ALLTOALLV;
2096 extra->send_size = 0;
2097 extra->recv_size = 0;
2098 extra->recvcounts = xbt_new(int, size);
2099 extra->sendcounts = xbt_new(int, size);
2101 int dt_size_recv = 1;
2102 extra->datatype2 = encode_datatype(recvtype, &known);
2103 dt_size_recv = recvtype->size();
2105 void* sendtmpbuf = static_cast<char*>(sendbuf);
2106 int* sendtmpcounts = sendcounts;
2107 int* sendtmpdisps = senddisps;
2108 MPI_Datatype sendtmptype = sendtype;
2110 for (i = 0; i < size; i++) { // copy data to avoid bad free
2111 extra->recv_size += recvcounts[i] * dt_size_recv;
2112 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
2113 if (((recvdisps[i] + recvcounts[i]) * dt_size_recv) > maxsize)
2114 maxsize = (recvdisps[i] + recvcounts[i]) * dt_size_recv;
2117 if (sendbuf == MPI_IN_PLACE) {
2118 sendtmpbuf = static_cast<void*>(xbt_malloc(maxsize));
2119 memcpy(sendtmpbuf, recvbuf, maxsize);
2120 sendtmpcounts = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2121 memcpy(sendtmpcounts, recvcounts, size * sizeof(int));
2122 sendtmpdisps = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2123 memcpy(sendtmpdisps, recvdisps, size * sizeof(int));
2124 sendtmptype = recvtype;
2127 extra->datatype1 = encode_datatype(sendtmptype, &known);
2128 int dt_size_send = 1;
2129 dt_size_send = sendtmptype->size();
2131 for (i = 0; i < size; i++) { // copy data to avoid bad free
2132 extra->send_size += sendtmpcounts[i] * dt_size_send;
2133 extra->sendcounts[i] = sendtmpcounts[i] * dt_size_send;
2135 extra->num_processes = size;
2136 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2137 retval = simgrid::smpi::Colls::alltoallv(sendtmpbuf, sendtmpcounts, sendtmpdisps, sendtmptype, recvbuf, recvcounts,
2138 recvdisps, recvtype, comm);
2139 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2141 if (sendbuf == MPI_IN_PLACE) {
2142 xbt_free(sendtmpbuf);
2143 xbt_free(sendtmpcounts);
2144 xbt_free(sendtmpdisps);
2153 int PMPI_Get_processor_name(char *name, int *resultlen)
2155 strncpy(name, SIMIX_host_self()->cname(), strlen(SIMIX_host_self()->cname()) < MPI_MAX_PROCESSOR_NAME - 1
2156 ? strlen(SIMIX_host_self()->cname()) + 1
2157 : MPI_MAX_PROCESSOR_NAME - 1);
2158 *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2163 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2165 if (status == nullptr || count == nullptr) {
2167 } else if (!datatype->is_valid()) {
2168 return MPI_ERR_TYPE;
2170 size_t size = datatype->size();
2174 } else if (status->count % size != 0) {
2175 return MPI_UNDEFINED;
2177 *count = simgrid::smpi::Status::get_count(status, datatype);
2183 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2184 if (old_type == MPI_DATATYPE_NULL) {
2185 return MPI_ERR_TYPE;
2186 } else if (count<0){
2187 return MPI_ERR_COUNT;
2189 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
2193 int PMPI_Type_commit(MPI_Datatype* datatype) {
2194 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2195 return MPI_ERR_TYPE;
2197 (*datatype)->commit();
2202 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2203 if (old_type == MPI_DATATYPE_NULL) {
2204 return MPI_ERR_TYPE;
2205 } else if (count<0 || blocklen<0){
2206 return MPI_ERR_COUNT;
2208 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
2212 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2213 if (old_type == MPI_DATATYPE_NULL) {
2214 return MPI_ERR_TYPE;
2215 } else if (count<0 || blocklen<0){
2216 return MPI_ERR_COUNT;
2218 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
2222 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2223 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2226 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2227 if (old_type == MPI_DATATYPE_NULL) {
2228 return MPI_ERR_TYPE;
2229 } else if (count<0){
2230 return MPI_ERR_COUNT;
2232 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2236 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2237 if (old_type == MPI_DATATYPE_NULL) {
2238 return MPI_ERR_TYPE;
2239 } else if (count<0){
2240 return MPI_ERR_COUNT;
2242 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2246 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2247 MPI_Datatype* new_type)
2249 if (old_type == MPI_DATATYPE_NULL) {
2250 return MPI_ERR_TYPE;
2251 } else if (count<0){
2252 return MPI_ERR_COUNT;
2254 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
2255 for (int i = 0; i < count; i++)
2256 blocklens[i]=blocklength;
2257 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2258 xbt_free(blocklens);
2263 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
2265 if (old_type == MPI_DATATYPE_NULL) {
2266 return MPI_ERR_TYPE;
2267 } else if (count<0){
2268 return MPI_ERR_COUNT;
2270 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2274 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2275 MPI_Datatype* new_type) {
2276 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2279 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2280 MPI_Datatype* new_type) {
2281 if (old_type == MPI_DATATYPE_NULL) {
2282 return MPI_ERR_TYPE;
2283 } else if (count<0){
2284 return MPI_ERR_COUNT;
2286 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
2287 for (int i = 0; i < count; i++)
2288 blocklens[i] = blocklength;
2289 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2290 xbt_free(blocklens);
2295 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2297 return MPI_ERR_COUNT;
2299 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
2303 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2304 MPI_Datatype* new_type) {
2305 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2308 int PMPI_Error_class(int errorcode, int* errorclass) {
2309 // assume smpi uses only standard mpi error codes
2310 *errorclass=errorcode;
2314 int PMPI_Initialized(int* flag) {
2315 *flag=(smpi_process()!=nullptr && smpi_process()->initialized());
2319 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2320 * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2321 * MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2323 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2324 if (comm_old == MPI_COMM_NULL){
2325 return MPI_ERR_COMM;
2326 } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2329 simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
2330 if(*comm_cart==MPI_COMM_NULL)
2336 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2337 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2338 return MPI_ERR_TOPOLOGY;
2340 if (coords == nullptr) {
2343 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2344 if (topo==nullptr) {
2347 return topo->rank(coords, rank);
2350 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2351 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2352 return MPI_ERR_TOPOLOGY;
2354 if (source == nullptr || dest == nullptr || direction < 0 ) {
2357 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2358 if (topo==nullptr) {
2361 return topo->shift(direction, displ, source, dest);
2364 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2365 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2366 return MPI_ERR_TOPOLOGY;
2368 if (rank < 0 || rank >= comm->size()) {
2369 return MPI_ERR_RANK;
2374 if(coords == nullptr) {
2377 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2378 if (topo==nullptr) {
2381 return topo->coords(rank, maxdims, coords);
2384 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2385 if(comm == nullptr || comm->topo() == nullptr) {
2386 return MPI_ERR_TOPOLOGY;
2388 if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2391 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2392 if (topo==nullptr) {
2395 return topo->get(maxdims, dims, periods, coords);
2398 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2399 if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2400 return MPI_ERR_TOPOLOGY;
2402 if (ndims == nullptr) {
2405 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2406 if (topo==nullptr) {
2409 return topo->dim_get(ndims);
2412 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2413 if(dims == nullptr) {
2416 if (ndims < 1 || nnodes < 1) {
2417 return MPI_ERR_DIMS;
2419 return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
2422 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2423 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2424 return MPI_ERR_TOPOLOGY;
2426 if (comm_new == nullptr) {
2429 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2430 if (topo==nullptr) {
2433 MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
2434 if(*comm_new==MPI_COMM_NULL)
2441 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2442 if (oldtype == MPI_DATATYPE_NULL) {
2443 return MPI_ERR_TYPE;
2445 int blocks[3] = {1, 1, 1};
2446 MPI_Aint disps[3] = {lb, 0, lb + extent};
2447 MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
2449 *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
2451 (*newtype)->addflag(~DT_FLAG_COMMITED);
2455 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2458 if (comm == MPI_COMM_NULL) {
2459 retval= MPI_ERR_COMM;
2460 }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2461 retval= MPI_ERR_OTHER;
2463 *win = new simgrid::smpi::Win( base, size, disp_unit, info, comm);
2464 retval = MPI_SUCCESS;
2470 int PMPI_Win_allocate( MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *base, MPI_Win *win){
2473 if (comm == MPI_COMM_NULL) {
2474 retval= MPI_ERR_COMM;
2475 }else if (disp_unit <= 0 || size < 0 ){
2476 retval= MPI_ERR_OTHER;
2478 void* ptr = xbt_malloc(size);
2480 return MPI_ERR_NO_MEM;
2481 *static_cast<void**>(base) = ptr;
2482 *win = new simgrid::smpi::Win( ptr, size, disp_unit, info, comm,1);
2483 retval = MPI_SUCCESS;
2489 int PMPI_Win_create_dynamic( MPI_Info info, MPI_Comm comm, MPI_Win *win){
2492 if (comm == MPI_COMM_NULL) {
2493 retval= MPI_ERR_COMM;
2495 *win = new simgrid::smpi::Win(info, comm);
2496 retval = MPI_SUCCESS;
2502 int PMPI_Win_attach(MPI_Win win, void *base, MPI_Aint size){
2505 if(win == MPI_WIN_NULL){
2506 retval = MPI_ERR_WIN;
2507 } else if ((base == nullptr && size != 0) || size < 0 ){
2508 retval= MPI_ERR_OTHER;
2510 retval = win->attach(base, size);
2516 int PMPI_Win_detach(MPI_Win win, void *base){
2519 if(win == MPI_WIN_NULL){
2520 retval = MPI_ERR_WIN;
2521 } else if (base == nullptr){
2522 retval= MPI_ERR_OTHER;
2524 retval = win->detach(base);
2531 int PMPI_Win_free( MPI_Win* win){
2534 if (win == nullptr || *win == MPI_WIN_NULL) {
2535 retval = MPI_ERR_WIN;
2544 int PMPI_Win_set_name(MPI_Win win, char * name)
2546 if (win == MPI_WIN_NULL) {
2547 return MPI_ERR_TYPE;
2548 } else if (name == nullptr) {
2551 win->set_name(name);
2556 int PMPI_Win_get_name(MPI_Win win, char * name, int* len)
2558 if (win == MPI_WIN_NULL) {
2560 } else if (name == nullptr) {
2563 win->get_name(name, len);
2568 int PMPI_Win_get_info(MPI_Win win, MPI_Info* info)
2570 if (win == MPI_WIN_NULL) {
2573 *info = win->info();
2578 int PMPI_Win_set_info(MPI_Win win, MPI_Info info)
2580 if (win == MPI_WIN_NULL) {
2581 return MPI_ERR_TYPE;
2583 win->set_info(info);
2588 int PMPI_Win_get_group(MPI_Win win, MPI_Group * group){
2589 if (win == MPI_WIN_NULL) {
2592 win->get_group(group);
2598 int PMPI_Win_fence( int assert, MPI_Win win){
2601 if (win == MPI_WIN_NULL) {
2602 retval = MPI_ERR_WIN;
2604 int rank = smpi_process()->index();
2605 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2606 retval = win->fence(assert);
2607 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2613 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2614 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2617 if (win == MPI_WIN_NULL) {
2618 retval = MPI_ERR_WIN;
2619 } else if (target_rank == MPI_PROC_NULL) {
2620 retval = MPI_SUCCESS;
2621 } else if (target_rank <0){
2622 retval = MPI_ERR_RANK;
2623 } else if (win->dynamic()==0 && target_disp <0){
2624 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2625 retval = MPI_ERR_ARG;
2626 } else if ((origin_count < 0 || target_count < 0) ||
2627 (origin_addr==nullptr && origin_count > 0)){
2628 retval = MPI_ERR_COUNT;
2629 } else if ((!origin_datatype->is_valid()) || (!target_datatype->is_valid())) {
2630 retval = MPI_ERR_TYPE;
2632 int rank = smpi_process()->index();
2634 win->get_group(&group);
2635 int src_traced = group->index(target_rank);
2636 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2638 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2641 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2647 int PMPI_Rget( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2648 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2651 if (win == MPI_WIN_NULL) {
2652 retval = MPI_ERR_WIN;
2653 } else if (target_rank == MPI_PROC_NULL) {
2654 *request = MPI_REQUEST_NULL;
2655 retval = MPI_SUCCESS;
2656 } else if (target_rank <0){
2657 retval = MPI_ERR_RANK;
2658 } else if (win->dynamic()==0 && target_disp <0){
2659 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2660 retval = MPI_ERR_ARG;
2661 } else if ((origin_count < 0 || target_count < 0) ||
2662 (origin_addr==nullptr && origin_count > 0)){
2663 retval = MPI_ERR_COUNT;
2664 } else if ((!origin_datatype->is_valid()) || (!target_datatype->is_valid())) {
2665 retval = MPI_ERR_TYPE;
2666 } else if(request == nullptr){
2667 retval = MPI_ERR_REQUEST;
2669 int rank = smpi_process()->index();
2671 win->get_group(&group);
2672 int src_traced = group->index(target_rank);
2673 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2675 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2676 target_datatype, request);
2678 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2684 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2685 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2688 if (win == MPI_WIN_NULL) {
2689 retval = MPI_ERR_WIN;
2690 } else if (target_rank == MPI_PROC_NULL) {
2691 retval = MPI_SUCCESS;
2692 } else if (target_rank <0){
2693 retval = MPI_ERR_RANK;
2694 } else if (win->dynamic()==0 && target_disp <0){
2695 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2696 retval = MPI_ERR_ARG;
2697 } else if ((origin_count < 0 || target_count < 0) ||
2698 (origin_addr==nullptr && origin_count > 0)){
2699 retval = MPI_ERR_COUNT;
2700 } else if ((!origin_datatype->is_valid()) || (!target_datatype->is_valid())) {
2701 retval = MPI_ERR_TYPE;
2703 int rank = smpi_process()->index();
2705 win->get_group(&group);
2706 int dst_traced = group->index(target_rank);
2707 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2708 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2710 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2713 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2719 int PMPI_Rput( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2720 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2723 if (win == MPI_WIN_NULL) {
2724 retval = MPI_ERR_WIN;
2725 } else if (target_rank == MPI_PROC_NULL) {
2726 *request = MPI_REQUEST_NULL;
2727 retval = MPI_SUCCESS;
2728 } else if (target_rank <0){
2729 retval = MPI_ERR_RANK;
2730 } else if (win->dynamic()==0 && target_disp <0){
2731 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2732 retval = MPI_ERR_ARG;
2733 } else if ((origin_count < 0 || target_count < 0) ||
2734 (origin_addr==nullptr && origin_count > 0)){
2735 retval = MPI_ERR_COUNT;
2736 } else if ((!origin_datatype->is_valid()) || (!target_datatype->is_valid())) {
2737 retval = MPI_ERR_TYPE;
2738 } else if(request == nullptr){
2739 retval = MPI_ERR_REQUEST;
2741 int rank = smpi_process()->index();
2743 win->get_group(&group);
2744 int dst_traced = group->index(target_rank);
2745 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2746 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2748 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2749 target_datatype, request);
2751 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2757 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2758 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2761 if (win == MPI_WIN_NULL) {
2762 retval = MPI_ERR_WIN;
2763 } else if (target_rank == MPI_PROC_NULL) {
2764 retval = MPI_SUCCESS;
2765 } else if (target_rank <0){
2766 retval = MPI_ERR_RANK;
2767 } else if (win->dynamic()==0 && target_disp <0){
2768 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2769 retval = MPI_ERR_ARG;
2770 } else if ((origin_count < 0 || target_count < 0) ||
2771 (origin_addr==nullptr && origin_count > 0)){
2772 retval = MPI_ERR_COUNT;
2773 } else if ((!origin_datatype->is_valid()) ||
2774 (!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 ((!origin_datatype->is_valid()) ||
2812 (!target_datatype->is_valid())) {
2813 retval = MPI_ERR_TYPE;
2814 } else if (op == MPI_OP_NULL) {
2815 retval = MPI_ERR_OP;
2816 } else if(request == nullptr){
2817 retval = MPI_ERR_REQUEST;
2819 int rank = smpi_process()->index();
2821 win->get_group(&group);
2822 int src_traced = group->index(target_rank);
2823 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2825 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2826 target_datatype, op, request);
2828 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2834 int PMPI_Get_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2835 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2836 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2839 if (win == MPI_WIN_NULL) {
2840 retval = MPI_ERR_WIN;
2841 } else if (target_rank == MPI_PROC_NULL) {
2842 retval = MPI_SUCCESS;
2843 } else if (target_rank <0){
2844 retval = MPI_ERR_RANK;
2845 } else if (win->dynamic()==0 && target_disp <0){
2846 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2847 retval = MPI_ERR_ARG;
2848 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2849 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2850 (result_addr==nullptr && result_count > 0)){
2851 retval = MPI_ERR_COUNT;
2852 } else if ((origin_datatype!=MPI_DATATYPE_NULL && !origin_datatype->is_valid()) ||
2853 (!target_datatype->is_valid())||
2854 (!result_datatype->is_valid())) {
2855 retval = MPI_ERR_TYPE;
2856 } else if (op == MPI_OP_NULL) {
2857 retval = MPI_ERR_OP;
2859 int rank = smpi_process()->index();
2861 win->get_group(&group);
2862 int src_traced = group->index(target_rank);
2863 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2865 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2866 result_count, result_datatype, target_rank, target_disp,
2867 target_count, target_datatype, op);
2869 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2876 int PMPI_Rget_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2877 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2878 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2881 if (win == MPI_WIN_NULL) {
2882 retval = MPI_ERR_WIN;
2883 } else if (target_rank == MPI_PROC_NULL) {
2884 *request = MPI_REQUEST_NULL;
2885 retval = MPI_SUCCESS;
2886 } else if (target_rank <0){
2887 retval = MPI_ERR_RANK;
2888 } else if (win->dynamic()==0 && target_disp <0){
2889 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2890 retval = MPI_ERR_ARG;
2891 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2892 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2893 (result_addr==nullptr && result_count > 0)){
2894 retval = MPI_ERR_COUNT;
2895 } else if ((origin_datatype!=MPI_DATATYPE_NULL && !origin_datatype->is_valid()) ||
2896 (!target_datatype->is_valid())||
2897 (!result_datatype->is_valid())) {
2898 retval = MPI_ERR_TYPE;
2899 } else if (op == MPI_OP_NULL) {
2900 retval = MPI_ERR_OP;
2901 } else if(request == nullptr){
2902 retval = MPI_ERR_REQUEST;
2904 int rank = smpi_process()->index();
2906 win->get_group(&group);
2907 int src_traced = group->index(target_rank);
2908 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2910 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2911 result_count, result_datatype, target_rank, target_disp,
2912 target_count, target_datatype, op, request);
2914 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2920 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){
2921 return PMPI_Get_accumulate(origin_addr, origin_addr==nullptr?0:1, dtype, result_addr, 1, dtype, target_rank, target_disp, 1, dtype, op, win);
2924 int PMPI_Compare_and_swap(void *origin_addr, void *compare_addr,
2925 void *result_addr, MPI_Datatype datatype, int target_rank,
2926 MPI_Aint target_disp, MPI_Win win){
2929 if (win == MPI_WIN_NULL) {
2930 retval = MPI_ERR_WIN;
2931 } else if (target_rank == MPI_PROC_NULL) {
2932 retval = MPI_SUCCESS;
2933 } else if (target_rank <0){
2934 retval = MPI_ERR_RANK;
2935 } else if (win->dynamic()==0 && target_disp <0){
2936 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2937 retval = MPI_ERR_ARG;
2938 } else if (origin_addr==nullptr || result_addr==nullptr || compare_addr==nullptr){
2939 retval = MPI_ERR_COUNT;
2940 } else if (!datatype->is_valid()) {
2941 retval = MPI_ERR_TYPE;
2943 int rank = smpi_process()->index();
2945 win->get_group(&group);
2946 int src_traced = group->index(target_rank);
2947 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2949 retval = win->compare_and_swap( origin_addr, compare_addr, result_addr, datatype,
2950 target_rank, target_disp);
2952 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2958 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2961 if (win == MPI_WIN_NULL) {
2962 retval = MPI_ERR_WIN;
2963 } else if (group==MPI_GROUP_NULL){
2964 retval = MPI_ERR_GROUP;
2966 int rank = smpi_process()->index();
2967 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2968 retval = win->post(group,assert);
2969 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2975 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2978 if (win == MPI_WIN_NULL) {
2979 retval = MPI_ERR_WIN;
2980 } else if (group==MPI_GROUP_NULL){
2981 retval = MPI_ERR_GROUP;
2983 int rank = smpi_process()->index();
2984 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2985 retval = win->start(group,assert);
2986 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2992 int PMPI_Win_complete(MPI_Win win){
2995 if (win == MPI_WIN_NULL) {
2996 retval = MPI_ERR_WIN;
2998 int rank = smpi_process()->index();
2999 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
3001 retval = win->complete();
3003 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3009 int PMPI_Win_wait(MPI_Win win){
3012 if (win == MPI_WIN_NULL) {
3013 retval = MPI_ERR_WIN;
3015 int rank = smpi_process()->index();
3016 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
3018 retval = win->wait();
3020 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3026 int PMPI_Win_lock(int lock_type, int rank, int assert, MPI_Win win){
3029 if (win == MPI_WIN_NULL) {
3030 retval = MPI_ERR_WIN;
3031 } else if (lock_type != MPI_LOCK_EXCLUSIVE &&
3032 lock_type != MPI_LOCK_SHARED) {
3033 retval = MPI_ERR_LOCKTYPE;
3034 } else if (rank == MPI_PROC_NULL){
3035 retval = MPI_SUCCESS;
3037 int myrank = smpi_process()->index();
3038 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3039 retval = win->lock(lock_type,rank,assert);
3040 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3046 int PMPI_Win_unlock(int rank, MPI_Win win){
3049 if (win == MPI_WIN_NULL) {
3050 retval = MPI_ERR_WIN;
3051 } else if (rank == MPI_PROC_NULL){
3052 retval = MPI_SUCCESS;
3054 int myrank = smpi_process()->index();
3055 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3056 retval = win->unlock(rank);
3057 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3063 int PMPI_Win_lock_all(int assert, MPI_Win win){
3066 if (win == MPI_WIN_NULL) {
3067 retval = MPI_ERR_WIN;
3069 int myrank = smpi_process()->index();
3070 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3071 retval = win->lock_all(assert);
3072 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3078 int PMPI_Win_unlock_all(MPI_Win win){
3081 if (win == MPI_WIN_NULL) {
3082 retval = MPI_ERR_WIN;
3084 int myrank = smpi_process()->index();
3085 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3086 retval = win->unlock_all();
3087 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3093 int PMPI_Win_flush(int rank, MPI_Win win){
3096 if (win == MPI_WIN_NULL) {
3097 retval = MPI_ERR_WIN;
3098 } else if (rank == MPI_PROC_NULL){
3099 retval = MPI_SUCCESS;
3101 int myrank = smpi_process()->index();
3102 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3103 retval = win->flush(rank);
3104 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3110 int PMPI_Win_flush_local(int rank, MPI_Win win){
3113 if (win == MPI_WIN_NULL) {
3114 retval = MPI_ERR_WIN;
3115 } else if (rank == MPI_PROC_NULL){
3116 retval = MPI_SUCCESS;
3118 int myrank = smpi_process()->index();
3119 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3120 retval = win->flush_local(rank);
3121 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3127 int PMPI_Win_flush_all(MPI_Win win){
3130 if (win == MPI_WIN_NULL) {
3131 retval = MPI_ERR_WIN;
3133 int myrank = smpi_process()->index();
3134 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3135 retval = win->flush_all();
3136 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3142 int PMPI_Win_flush_local_all(MPI_Win win){
3145 if (win == MPI_WIN_NULL) {
3146 retval = MPI_ERR_WIN;
3148 int myrank = smpi_process()->index();
3149 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3150 retval = win->flush_local_all();
3151 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3157 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
3158 void *ptr = xbt_malloc(size);
3160 return MPI_ERR_NO_MEM;
3162 *static_cast<void**>(baseptr) = ptr;
3167 int PMPI_Free_mem(void *baseptr){
3172 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
3174 if (datatype == MPI_DATATYPE_NULL) {
3175 return MPI_ERR_TYPE;
3176 } else if (name == nullptr) {
3179 datatype->set_name(name);
3184 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
3186 if (datatype == MPI_DATATYPE_NULL) {
3187 return MPI_ERR_TYPE;
3188 } else if (name == nullptr) {
3191 datatype->get_name(name, len);
3196 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
3197 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
3200 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
3201 return datatype->c2f();
3204 MPI_Group PMPI_Group_f2c(MPI_Fint group){
3205 return simgrid::smpi::Group::f2c(group);
3208 MPI_Fint PMPI_Group_c2f(MPI_Group group){
3209 return group->c2f();
3212 MPI_Request PMPI_Request_f2c(MPI_Fint request){
3213 return static_cast<MPI_Request>(simgrid::smpi::Request::f2c(request));
3216 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
3217 return request->c2f();
3220 MPI_Win PMPI_Win_f2c(MPI_Fint win){
3221 return static_cast<MPI_Win>(simgrid::smpi::Win::f2c(win));
3224 MPI_Fint PMPI_Win_c2f(MPI_Win win){
3228 MPI_Op PMPI_Op_f2c(MPI_Fint op){
3229 return static_cast<MPI_Op>(simgrid::smpi::Op::f2c(op));
3232 MPI_Fint PMPI_Op_c2f(MPI_Op op){
3236 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
3237 return static_cast<MPI_Comm>(simgrid::smpi::Comm::f2c(comm));
3240 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
3244 MPI_Info PMPI_Info_f2c(MPI_Fint info){
3245 return static_cast<MPI_Info>(simgrid::smpi::Info::f2c(info));
3248 MPI_Fint PMPI_Info_c2f(MPI_Info info){
3252 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3253 smpi_copy_fn _copy_fn={copy_fn,nullptr,nullptr};
3254 smpi_delete_fn _delete_fn={delete_fn,nullptr,nullptr};
3255 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Comm>(_copy_fn, _delete_fn, keyval, extra_state);
3258 int PMPI_Keyval_free(int* keyval) {
3259 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Comm>(keyval);
3262 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3263 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3264 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3266 else if (comm==MPI_COMM_NULL)
3267 return MPI_ERR_COMM;
3269 return comm->attr_delete<simgrid::smpi::Comm>(keyval);
3272 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3274 static int zero = 0;
3275 static int tag_ub = INT_MAX;
3276 static int last_used_code = MPI_ERR_LASTCODE;
3278 if (comm==MPI_COMM_NULL){
3280 return MPI_ERR_COMM;
3288 *static_cast<int**>(attr_value) = &zero;
3290 case MPI_UNIVERSE_SIZE:
3292 *static_cast<int**>(attr_value) = &smpi_universe_size;
3294 case MPI_LASTUSEDCODE:
3296 *static_cast<int**>(attr_value) = &last_used_code;
3300 *static_cast<int**>(attr_value) = &tag_ub;
3302 case MPI_WTIME_IS_GLOBAL:
3304 *static_cast<int**>(attr_value) = &one;
3307 return comm->attr_get<simgrid::smpi::Comm>(keyval, attr_value, flag);
3311 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3312 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3313 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3315 else if (comm==MPI_COMM_NULL)
3316 return MPI_ERR_COMM;
3318 return comm->attr_put<simgrid::smpi::Comm>(keyval, attr_value);
3321 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3323 return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3326 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3328 return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3331 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3333 return PMPI_Attr_delete(comm, comm_keyval);
3336 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3339 return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3342 int PMPI_Comm_free_keyval(int* keyval) {
3343 return PMPI_Keyval_free(keyval);
3346 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3348 if (type==MPI_DATATYPE_NULL)
3349 return MPI_ERR_TYPE;
3351 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
3354 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3356 if (type==MPI_DATATYPE_NULL)
3357 return MPI_ERR_TYPE;
3359 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
3362 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3364 if (type==MPI_DATATYPE_NULL)
3365 return MPI_ERR_TYPE;
3367 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
3370 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3373 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
3374 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
3375 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
3378 int PMPI_Type_free_keyval(int* keyval) {
3379 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
3382 int PMPI_Win_get_attr (MPI_Win win, int keyval, void *attribute_val, int* flag)
3384 static MPI_Aint size;
3385 static int disp_unit;
3386 if (win==MPI_WIN_NULL)
3387 return MPI_ERR_TYPE;
3391 *static_cast<void**>(attribute_val) = win->base();
3396 *static_cast<MPI_Aint**>(attribute_val) = &size;
3399 case MPI_WIN_DISP_UNIT :
3400 disp_unit=win->disp_unit();
3401 *static_cast<int**>(attribute_val) = &disp_unit;
3405 return win->attr_get<simgrid::smpi::Win>(keyval, attribute_val, flag);
3411 int PMPI_Win_set_attr (MPI_Win win, int type_keyval, void *attribute_val)
3413 if (win==MPI_WIN_NULL)
3414 return MPI_ERR_TYPE;
3416 return win->attr_put<simgrid::smpi::Win>(type_keyval, attribute_val);
3419 int PMPI_Win_delete_attr (MPI_Win win, int type_keyval)
3421 if (win==MPI_WIN_NULL)
3422 return MPI_ERR_TYPE;
3424 return win->attr_delete<simgrid::smpi::Win>(type_keyval);
3427 int PMPI_Win_create_keyval(MPI_Win_copy_attr_function* copy_fn, MPI_Win_delete_attr_function* delete_fn, int* keyval,
3430 smpi_copy_fn _copy_fn={nullptr, nullptr, copy_fn};
3431 smpi_delete_fn _delete_fn={nullptr, nullptr, delete_fn};
3432 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Win>(_copy_fn, _delete_fn, keyval, extra_state);
3435 int PMPI_Win_free_keyval(int* keyval) {
3436 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Win>(keyval);
3439 int PMPI_Info_create( MPI_Info *info){
3440 if (info == nullptr)
3442 *info = new simgrid::smpi::Info();
3446 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3447 if (info == nullptr || key == nullptr || value == nullptr)
3449 info->set(key, value);
3453 int PMPI_Info_free( MPI_Info *info){
3454 if (info == nullptr || *info==nullptr)
3456 simgrid::smpi::Info::unref(*info);
3457 *info=MPI_INFO_NULL;
3461 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3463 if (info == nullptr || key == nullptr || valuelen <0)
3465 if (value == nullptr)
3466 return MPI_ERR_INFO_VALUE;
3467 return info->get(key, valuelen, value, flag);
3470 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3471 if (info == nullptr || newinfo==nullptr)
3473 *newinfo = new simgrid::smpi::Info(info);
3477 int PMPI_Info_delete(MPI_Info info, char *key){
3478 if (info == nullptr || key==nullptr)
3480 return info->remove(key);
3483 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3484 if (info == nullptr || nkeys==nullptr)
3486 return info->get_nkeys(nkeys);
3489 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3490 if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3492 return info->get_nthkey(n, key);
3495 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3497 if (info == nullptr || key == nullptr || valuelen==nullptr)
3499 return info->get_valuelen(key, valuelen, flag);
3502 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3503 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3505 if(!type->is_valid())
3506 return MPI_ERR_TYPE;
3507 if(comm==MPI_COMM_NULL)
3508 return MPI_ERR_COMM;
3509 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
3512 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3513 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3515 if(!type->is_valid())
3516 return MPI_ERR_TYPE;
3517 if(comm==MPI_COMM_NULL)
3518 return MPI_ERR_COMM;
3519 return type->pack(inbuf, incount, outbuf,outcount,position, comm);
3522 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3525 if(!datatype->is_valid())
3526 return MPI_ERR_TYPE;
3527 if(comm==MPI_COMM_NULL)
3528 return MPI_ERR_COMM;
3530 *size=incount*datatype->size();