- return status->count / smpi_datatype_size(datatype);
-}
-
-static void finish_wait(MPI_Request * request, MPI_Status * status)
-{
- MPI_Request req = *request;
- smpi_empty_status(status);
-
- if(!((req->detached != 0) && ((req->flags & SEND) != 0)) && ((req->flags & PREPARED) == 0)){
- if(status != MPI_STATUS_IGNORE) {
- int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
- status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
- status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
- status->MPI_ERROR = req->truncated != 0 ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
- // this handles the case were size in receive differs from size in send
- status->count = req->real_size;
- }
-
- print_request("Finishing", req);
- MPI_Datatype datatype = req->old_type;
-
- if(((req->flags & ACCUMULATE) != 0) || (datatype->sizeof_substruct != 0)){
- if (!smpi_process_get_replaying()){
- if( smpi_privatize_global_variables != 0 && (static_cast<char*>(req->old_buf) >= smpi_start_data_exe)
- && ((char*)req->old_buf < smpi_start_data_exe + smpi_size_data_exe )){
- XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
- smpi_switch_data_segment(smpi_process_index());
- }
- }
-
- if(datatype->sizeof_substruct != 0){
- // This part handles the problem of non-contignous memory the unserialization at the reception
- s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(datatype->substruct);
- if(req->flags & RECV)
- subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) ,
- datatype->substruct, req->op);
- xbt_free(req->buf);
- }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
- int n =req->real_size/smpi_datatype_size(datatype);
- smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
- xbt_free(req->buf);
- }
- }
- }
-
- if (TRACE_smpi_view_internals() && ((req->flags & RECV) != 0)){
- int rank = smpi_process_index();
- int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
- TRACE_smpi_recv(rank, src_traced, rank);
- }
-
- if(req->detached_sender != nullptr){
-
- //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
- double sleeptime = smpi_or(req->real_size);
- if(sleeptime > 0.0){
- simcall_process_sleep(sleeptime);
- XBT_DEBUG("receiving size of %zu : sleep %f ", req->real_size, sleeptime);
- }
- smpi_mpi_request_free(&(req->detached_sender));
- }
- if(req->flags & PERSISTENT)
- req->action = nullptr;
- req->flags |= FINISHED;
-
- smpi_mpi_request_free(request);
-}
-
-int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
- //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
-
- // to avoid deadlocks if used as a break condition, such as
- // while (MPI_Test(request, flag, status) && flag) {
- // }
- // because the time will not normally advance when only calls to MPI_Test are made -> deadlock
- // multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
- static int nsleeps = 1;
- if(smpi_test_sleep > 0)
- simcall_process_sleep(nsleeps*smpi_test_sleep);
-
- smpi_empty_status(status);
- int flag = 1;
- if (((*request)->flags & PREPARED) == 0) {
- if ((*request)->action != nullptr)
- flag = simcall_comm_test((*request)->action);
- if (flag) {
- finish_wait(request, status);
- nsleeps=1;//reset the number of sleeps we will do next time
- if (*request != MPI_REQUEST_NULL && ((*request)->flags & PERSISTENT)==0)
- *request = MPI_REQUEST_NULL;
- }else{
- nsleeps++;
- }
- }
- return flag;
-}
-
-int smpi_mpi_testany(int count, MPI_Request requests[], int *index, MPI_Status * status)
-{
- xbt_dynar_t comms;
- int i;
- int flag = 0;
-
- *index = MPI_UNDEFINED;
- comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
- std::vector<int> map; /** Maps all matching comms back to their location in requests **/
- for(i = 0; i < count; i++) {
- if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action && !(requests[i]->flags & PREPARED)) {
- xbt_dynar_push(comms, &requests[i]->action);
- map.push_back(i);
- }
- }
- if(!map.empty()) {
- //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
- static int nsleeps = 1;
- if(smpi_test_sleep > 0)
- simcall_process_sleep(nsleeps*smpi_test_sleep);
-
- i = simcall_comm_testany(comms); // The i-th element in comms matches!
- if (i != -1) { // -1 is not MPI_UNDEFINED but a SIMIX return code. (nothing matches)
- *index = map[i];
- finish_wait(&requests[*index], status);
- flag = 1;
- nsleeps = 1;
- if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT)) {
- requests[*index] = MPI_REQUEST_NULL;
- }
- } else {
- nsleeps++;
- }
- } else {
- //all requests are null or inactive, return true
- flag = 1;
- smpi_empty_status(status);
- }
- xbt_dynar_free(&comms);
-
- return flag;
-}
-
-int smpi_mpi_testall(int count, MPI_Request requests[], MPI_Status status[])
-{
- MPI_Status stat;
- MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
- int flag=1;
- int i;
- for(i=0; i<count; i++){
- if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
- if (smpi_mpi_test(&requests[i], pstat)!=1){
- flag=0;
- }else{
- requests[i]=MPI_REQUEST_NULL;
- }
- }else{
- smpi_empty_status(pstat);
- }
- if(status != MPI_STATUSES_IGNORE) {
- status[i] = *pstat;
- }
- }
- return flag;
-}
-
-void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
- int flag=0;
- //FIXME find another way to avoid busy waiting ?
- // the issue here is that we have to wait on a nonexistent comm
- while(flag==0){
- smpi_mpi_iprobe(source, tag, comm, &flag, status);
- XBT_DEBUG("Busy Waiting on probing : %d", flag);
- }
-}
-
-void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
-
- MPI_Request request = build_request(nullptr, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE :
- smpi_group_index(smpi_comm_group(comm), source), smpi_comm_rank(comm), tag, comm, PERSISTENT | RECV);
-
- // to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
- // (especially when used as a break condition, such as while(MPI_Iprobe(...)) ... )
- // multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
- static int nsleeps = 1;
- if(smpi_iprobe_sleep > 0)
- simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
- // behave like a receive, but don't do it
- smx_mailbox_t mailbox;
-
- print_request("New iprobe", request);
- // We have to test both mailboxes as we don't know if we will receive one one or another
- if (xbt_cfg_get_int("smpi/async-small-thresh") > 0){
- mailbox = smpi_process_mailbox_small();
- XBT_DEBUG("Trying to probe the perm recv mailbox");
- request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, static_cast<void*>(request));
- }
-
- if (request->action == nullptr){
- mailbox = smpi_process_mailbox();
- XBT_DEBUG("trying to probe the other mailbox");
- request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, static_cast<void*>(request));
- }
-
- if (request->action != nullptr){
- simgrid::simix::Comm *sync_comm = static_cast<simgrid::simix::Comm*>(request->action);
- MPI_Request req = static_cast<MPI_Request>(sync_comm->src_data);
- *flag = 1;
- if(status != MPI_STATUS_IGNORE && (req->flags & PREPARED) == 0) {
- status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
- status->MPI_TAG = req->tag;
- status->MPI_ERROR = MPI_SUCCESS;
- status->count = req->real_size;
- }
- nsleeps = 1;//reset the number of sleeps we will do next time
- }
- else {
- *flag = 0;
- nsleeps++;
- }
- smpi_mpi_request_free(&request);
-
- return;
-}
-
-void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
-{
- print_request("Waiting", *request);
- if ((*request)->flags & PREPARED) {
- smpi_empty_status(status);
- return;
- }
-
- if ((*request)->action != nullptr)
- // this is not a detached send
- simcall_comm_wait((*request)->action, -1.0);
-
- finish_wait(request, status);
- if (*request != MPI_REQUEST_NULL && (((*request)->flags & NON_PERSISTENT)!=0))
- *request = MPI_REQUEST_NULL;
-}
-
-int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
-{
- xbt_dynar_t comms;
- int i;
- int size = 0;
- int index = MPI_UNDEFINED;
- int *map;
-
- if(count > 0) {
- // Wait for a request to complete
- comms = xbt_dynar_new(sizeof(smx_synchro_t), nullptr);
- map = xbt_new(int, count);
- XBT_DEBUG("Wait for one of %d", count);
- for(i = 0; i < count; i++) {
- if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED) && !(requests[i]->flags & FINISHED)) {
- if (requests[i]->action != nullptr) {
- XBT_DEBUG("Waiting any %p ", requests[i]);
- xbt_dynar_push(comms, &requests[i]->action);
- map[size] = i;
- size++;
- }else{
- //This is a finished detached request, let's return this one
- size=0;//so we free the dynar but don't do the waitany call
- index=i;
- finish_wait(&requests[i], status);//cleanup if refcount = 0
- if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
- requests[i]=MPI_REQUEST_NULL;//set to null
- break;
- }
- }
- }
- if(size > 0) {
- i = simcall_comm_waitany(comms, -1);
-
- // not MPI_UNDEFINED, as this is a simix return code
- if (i != -1) {
- index = map[i];
- finish_wait(&requests[index], status);
- if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
- requests[index] = MPI_REQUEST_NULL;
- }
- }
- xbt_free(map);
- xbt_dynar_free(&comms);
- }
-
- if (index==MPI_UNDEFINED)
- smpi_empty_status(status);
-
- return index;
-}
-
-int smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[])
-{
- int index, c;
- MPI_Status stat;
- MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
- int retvalue = MPI_SUCCESS;
- //tag invalid requests in the set
- if (status != MPI_STATUSES_IGNORE) {
- for (c = 0; c < count; c++) {
- if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL || (requests[c]->flags & PREPARED)) {
- smpi_empty_status(&status[c]);
- } else if (requests[c]->src == MPI_PROC_NULL) {
- smpi_empty_status(&status[c]);
- status[c].MPI_SOURCE = MPI_PROC_NULL;
- }
- }
- }
- for(c = 0; c < count; c++) {
-
- if (MC_is_active() || MC_record_replay_is_active()) {
- smpi_mpi_wait(&requests[c], pstat);
- index = c;
- } else {
- index = smpi_mpi_waitany(count, requests, pstat);
- if (index == MPI_UNDEFINED)
- break;
- if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
- requests[index]=MPI_REQUEST_NULL;
- }
- if (status != MPI_STATUSES_IGNORE) {
- status[index] = *pstat;
- if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
- retvalue = MPI_ERR_IN_STATUS;
- }
- }
-
- return retvalue;
-}
-
-int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
-{
- int i;
- int count = 0;
- int index;
- MPI_Status stat;
- MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
-
- for(i = 0; i < incount; i++)
- {
- index=smpi_mpi_waitany(incount, requests, pstat);
- if(index!=MPI_UNDEFINED){
- indices[count] = index;
- count++;
- if(status != MPI_STATUSES_IGNORE) {
- status[index] = *pstat;
- }
- if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
- requests[index]=MPI_REQUEST_NULL;
- }else{
- return MPI_UNDEFINED;
- }
- }
- return count;
-}
-
-int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices, MPI_Status status[])
-{
- int i;
- int count = 0;
- int count_dead = 0;
- MPI_Status stat;
- MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
-
- for(i = 0; i < incount; i++) {
- if((requests[i] != MPI_REQUEST_NULL)) {
- if(smpi_mpi_test(&requests[i], pstat)) {
- indices[i] = 1;
- count++;
- if(status != MPI_STATUSES_IGNORE) {
- status[i] = *pstat;
- }
- if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
- requests[i]=MPI_REQUEST_NULL;
- }
- }else{
- count_dead++;
- }
- }
- if(count_dead==incount)
- return MPI_UNDEFINED;
- else return count;
-}
-
-void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
-{
- smpi_coll_tuned_bcast_binomial_tree(buf, count, datatype, root, comm);
-}
-
-void smpi_mpi_barrier(MPI_Comm comm)
-{
- smpi_coll_tuned_barrier_ompi_basic_linear(comm);
-}
-
-void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
- void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_GATHER;
- int rank, size, src, index;
- MPI_Aint lb = 0, recvext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- if(rank != root) {
- // Send buffer to root
- smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
- } else {
- smpi_datatype_extent(recvtype, &lb, &recvext);
- // Local copy from root
- smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + root * recvcount * recvext, recvcount, recvtype);
- // Receive buffers from senders
- requests = xbt_new(MPI_Request, size - 1);
- index = 0;
- for(src = 0; src < size; src++) {
- if(src != root) {
- requests[index] = smpi_irecv_init(static_cast<char*>(recvbuf) + src * recvcount * recvext, recvcount, recvtype,
- src, system_tag, comm);
- index++;
- }
- }
- // Wait for completion of irecv's.
- smpi_mpi_startall(size - 1, requests);
- smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
- for(src = 0; src < size-1; src++) {
- smpi_mpi_request_free(&requests[src]);
- }
- xbt_free(requests);
- }
-}
-
-void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
- MPI_Comm comm)
-{
- int i, size, count;
- int *displs;
- int rank = smpi_comm_rank(comm);
- void *tmpbuf;
-
- /* arbitrarily choose root as rank 0 */
- size = smpi_comm_size(comm);
- count = 0;
- displs = xbt_new(int, size);
- for (i = 0; i < size; i++) {
- displs[i] = count;
- count += recvcounts[i];
- }
- tmpbuf=static_cast<void*>(smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype)));
-
- mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
- smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
- xbt_free(displs);
- smpi_free_tmp_buffer(tmpbuf);
-}
-
-void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
- MPI_Datatype recvtype, int root, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_GATHERV;
- int rank, size, src, index;
- MPI_Aint lb = 0, recvext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- if(rank != root) {
- // Send buffer to root
- smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
- } else {
- smpi_datatype_extent(recvtype, &lb, &recvext);
- // Local copy from root
- smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char*>(recvbuf) + displs[root] * recvext,
- recvcounts[root], recvtype);
- // Receive buffers from senders
- requests = xbt_new(MPI_Request, size - 1);
- index = 0;
- for(src = 0; src < size; src++) {
- if(src != root) {
- requests[index] = smpi_irecv_init(static_cast<char*>(recvbuf) + displs[src] * recvext,
- recvcounts[src], recvtype, src, system_tag, comm);
- index++;
- }
- }
- // Wait for completion of irecv's.
- smpi_mpi_startall(size - 1, requests);
- smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
- for(src = 0; src < size-1; src++) {
- smpi_mpi_request_free(&requests[src]);
- }
- xbt_free(requests);
- }
-}
-
-void smpi_mpi_allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
- void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_ALLGATHER;
- int rank, size, other, index;
- MPI_Aint lb = 0, recvext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- // FIXME: check for errors
- smpi_datatype_extent(recvtype, &lb, &recvext);
- // Local copy from self
- smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + rank * recvcount * recvext, recvcount, recvtype);
- // Send/Recv buffers to/from others;
- requests = xbt_new(MPI_Request, 2 * (size - 1));
- index = 0;
- for(other = 0; other < size; other++) {
- if(other != rank) {
- requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,comm);
- index++;
- requests[index] = smpi_irecv_init(static_cast<char *>(recvbuf) + other * recvcount * recvext, recvcount, recvtype, other,
- system_tag, comm);
- index++;
- }
- }
- // Wait for completion of all comms.
- smpi_mpi_startall(2 * (size - 1), requests);
- smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
- for(other = 0; other < 2*(size-1); other++) {
- smpi_mpi_request_free(&requests[other]);
- }
- xbt_free(requests);
-}
-
-void smpi_mpi_allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
- int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_ALLGATHERV;
- int rank, size, other, index;
- MPI_Aint lb = 0, recvext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- smpi_datatype_extent(recvtype, &lb, &recvext);
- // Local copy from self
- smpi_datatype_copy(sendbuf, sendcount, sendtype, static_cast<char *>(recvbuf) + displs[rank] * recvext,recvcounts[rank], recvtype);
- // Send buffers to others;
- requests = xbt_new(MPI_Request, 2 * (size - 1));
- index = 0;
- for(other = 0; other < size; other++) {
- if(other != rank) {
- requests[index] =
- smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
- index++;
- requests[index] = smpi_irecv_init(static_cast<char *>(recvbuf) + displs[other] * recvext, recvcounts[other],
- recvtype, other, system_tag, comm);
- index++;
- }
- }
- // Wait for completion of all comms.
- smpi_mpi_startall(2 * (size - 1), requests);
- smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
- for(other = 0; other < 2*(size-1); other++) {
- smpi_mpi_request_free(&requests[other]);
- }
- xbt_free(requests);
-}
-
-void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
- void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_SCATTER;
- int rank, size, dst, index;
- MPI_Aint lb = 0, sendext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- if(rank != root) {
- // Recv buffer from root
- smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
- } else {
- smpi_datatype_extent(sendtype, &lb, &sendext);
- // Local copy from root
- if(recvbuf!=MPI_IN_PLACE){
- smpi_datatype_copy(static_cast<char *>(sendbuf) + root * sendcount * sendext,
- sendcount, sendtype, recvbuf, recvcount, recvtype);
- }
- // Send buffers to receivers
- requests = xbt_new(MPI_Request, size - 1);
- index = 0;
- for(dst = 0; dst < size; dst++) {
- if(dst != root) {
- requests[index] = smpi_isend_init(static_cast<char *>(sendbuf) + dst * sendcount * sendext, sendcount, sendtype, dst,
- system_tag, comm);
- index++;
- }
- }
- // Wait for completion of isend's.
- smpi_mpi_startall(size - 1, requests);
- smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
- for(dst = 0; dst < size-1; dst++) {
- smpi_mpi_request_free(&requests[dst]);
- }
- xbt_free(requests);
- }
-}
-
-void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount,
- MPI_Datatype recvtype, int root, MPI_Comm comm)
-{
- int system_tag = COLL_TAG_SCATTERV;
- int rank, size, dst, index;
- MPI_Aint lb = 0, sendext = 0;
- MPI_Request *requests;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- if(rank != root) {
- // Recv buffer from root
- smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
- } else {
- smpi_datatype_extent(sendtype, &lb, &sendext);
- // Local copy from root
- if(recvbuf!=MPI_IN_PLACE){
- smpi_datatype_copy(static_cast<char *>(sendbuf) + displs[root] * sendext, sendcounts[root],
- sendtype, recvbuf, recvcount, recvtype);
- }
- // Send buffers to receivers
- requests = xbt_new(MPI_Request, size - 1);
- index = 0;
- for(dst = 0; dst < size; dst++) {
- if(dst != root) {
- requests[index] = smpi_isend_init(static_cast<char *>(sendbuf) + displs[dst] * sendext, sendcounts[dst],
- sendtype, dst, system_tag, comm);
- index++;
- }
- }
- // Wait for completion of isend's.
- smpi_mpi_startall(size - 1, requests);
- smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
- for(dst = 0; dst < size-1; dst++) {
- smpi_mpi_request_free(&requests[dst]);
- }
- xbt_free(requests);
- }
-}
-
-void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
- MPI_Comm comm)
-{
- int system_tag = COLL_TAG_REDUCE;
- int rank, size, src, index;
- MPI_Aint lb = 0, dataext = 0;
- MPI_Request *requests;
- void **tmpbufs;
-
- char* sendtmpbuf = static_cast<char *>(sendbuf);
-
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
- //non commutative case, use a working algo from openmpi
- if(!smpi_op_is_commute(op)){
- smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
- return;
- }
-
- if( sendbuf == MPI_IN_PLACE ) {
- sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype)));
- smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
- }
-
- if(rank != root) {
- // Send buffer to root
- smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
- } else {
- smpi_datatype_extent(datatype, &lb, &dataext);
- // Local copy from root
- if (sendtmpbuf != nullptr && recvbuf != nullptr)
- smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
- // Receive buffers from senders
- requests = xbt_new(MPI_Request, size - 1);
- tmpbufs = xbt_new(void *, size - 1);
- index = 0;
- for(src = 0; src < size; src++) {
- if(src != root) {
- if (!smpi_process_get_replaying())
- tmpbufs[index] = xbt_malloc(count * dataext);
- else
- tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
- requests[index] =
- smpi_irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
- index++;
- }
- }
- // Wait for completion of irecv's.
- smpi_mpi_startall(size - 1, requests);
- for(src = 0; src < size - 1; src++) {
- index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
- XBT_DEBUG("finished waiting any request with index %d", index);
- if(index == MPI_UNDEFINED) {
- break;
- }else{
- smpi_mpi_request_free(&requests[index]);
- }
- if(op) /* op can be MPI_OP_NULL that does nothing */
- smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
- }
- for(index = 0; index < size - 1; index++) {
- smpi_free_tmp_buffer(tmpbufs[index]);
- }
- xbt_free(tmpbufs);
- xbt_free(requests);
-
- }
- if( sendbuf == MPI_IN_PLACE ) {
- smpi_free_tmp_buffer(sendtmpbuf);
- }
-}
-
-void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
-{
- smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
- smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
-}
-
-void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
-{
- int system_tag = -888;
- int rank, size, other, index;
- MPI_Aint lb = 0, dataext = 0;
- MPI_Request *requests;
- void **tmpbufs;
-
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
-
- smpi_datatype_extent(datatype, &lb, &dataext);
-
- // Local copy from self
- smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
-
- // Send/Recv buffers to/from others;
- requests = xbt_new(MPI_Request, size - 1);
- tmpbufs = xbt_new(void *, rank);
- index = 0;
- for(other = 0; other < rank; other++) {
- tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
- requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
- index++;
- }
- for(other = rank + 1; other < size; other++) {
- requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
- index++;
- }
- // Wait for completion of all comms.
- smpi_mpi_startall(size - 1, requests);
-
- if(smpi_op_is_commute(op)){
- for(other = 0; other < size - 1; other++) {
- index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
- if(index == MPI_UNDEFINED) {
- break;
- }
- if(index < rank) {
- // #Request is below rank: it's a irecv
- smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
- }
- }
- }else{
- //non commutative case, wait in order
- for(other = 0; other < size - 1; other++) {
- smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
- if(index < rank) {
- smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
- }
- }
- }
- for(index = 0; index < rank; index++) {
- smpi_free_tmp_buffer(tmpbufs[index]);
- }
- for(index = 0; index < size-1; index++) {
- smpi_mpi_request_free(&requests[index]);
- }
- xbt_free(tmpbufs);
- xbt_free(requests);
-}
-
-void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
-{
- int system_tag = -888;
- int rank, size, other, index;
- MPI_Aint lb = 0, dataext = 0;
- MPI_Request *requests;
- void **tmpbufs;
- int recvbuf_is_empty=1;
- rank = smpi_comm_rank(comm);
- size = smpi_comm_size(comm);
-
- smpi_datatype_extent(datatype, &lb, &dataext);
-
- // Send/Recv buffers to/from others;
- requests = xbt_new(MPI_Request, size - 1);
- tmpbufs = xbt_new(void *, rank);
- index = 0;
- for(other = 0; other < rank; other++) {
- tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
- requests[index] =
- smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
- index++;
- }
- for(other = rank + 1; other < size; other++) {
- requests[index] =
- smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
- index++;
- }
- // Wait for completion of all comms.
- smpi_mpi_startall(size - 1, requests);
- if(smpi_op_is_commute(op)){
- for(other = 0; other < size - 1; other++) {
- index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
- if(index == MPI_UNDEFINED) {
- break;
- }
- if(index < rank) {
- if(recvbuf_is_empty){
- smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
- recvbuf_is_empty=0;
- }else
- // #Request is below rank: it's a irecv
- smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
- }
- }
- }else{
- //non commutative case, wait in order
- for(other = 0; other < size - 1; other++) {
- smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
- if(index < rank) {
- if(recvbuf_is_empty){
- smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
- recvbuf_is_empty=0;
- }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
- }
- }
- }
- for(index = 0; index < rank; index++) {
- smpi_free_tmp_buffer(tmpbufs[index]);
- }
- for(index = 0; index < size-1; index++) {
- smpi_mpi_request_free(&requests[index]);
- }
- xbt_free(tmpbufs);
- xbt_free(requests);