source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_gather.cpp @ 1359

Last change on this file since 1359 was 1295, checked in by yushan, 7 years ago

EP update all

File size: 4.0 KB
RevLine 
[1134]1/*!
2   \file ep_gather.cpp
3   \since 2 may 2016
4
5   \brief Definitions of MPI collective function: MPI_Gather, MPI_Allgather
6 */
7
8#include "ep_lib.hpp"
9#include <mpi.h>
10#include "ep_declaration.hpp"
[1287]11#include "ep_mpi.hpp"
[1134]12
13using namespace std;
14
15namespace ep_lib
16{
[1295]17
[1287]18  int MPI_Gather_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, int local_root, MPI_Comm comm)
[1134]19  {
[1287]20    assert(valid_type(datatype));
[1134]21
[1287]22    ::MPI_Aint datasize, lb;
23    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize);
[1134]24
[1287]25    int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first;
26    int num_ep = comm.ep_comm_ptr->size_rank_info[1].second;
[1134]27
[1287]28    #pragma omp critical (_gather)
29    comm.my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf);
[1134]30
[1287]31    MPI_Barrier_local(comm);
[1134]32
[1287]33    if(ep_rank_loc == local_root)
[1134]34    {
[1287]35      for(int i=0; i<num_ep; i++)
36        memcpy(recvbuf + datasize * i * count, comm.my_buffer->void_buffer[i], datasize * count);
[1134]37
[1287]38      //printf("local_recvbuf = %d %d \n", static_cast<int*>(recvbuf)[0], static_cast<int*>(recvbuf)[1] );
[1134]39    }
40
[1287]41    MPI_Barrier_local(comm);
[1134]42  }
43
44  int MPI_Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
45  {
[1287]46    if(!comm.is_ep)
[1134]47    {
[1287]48      return ::MPI_Gather(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, recvcount, to_mpi_type(recvtype),
49                   root, to_mpi_comm(comm.mpi_comm));
[1134]50    }
51
[1287]52    assert(sendcount == recvcount && sendtype == recvtype);
[1134]53
[1287]54    int ep_rank = comm.ep_comm_ptr->size_rank_info[0].first;
55    int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first;
56    int mpi_rank = comm.ep_comm_ptr->size_rank_info[2].first;
57    int ep_size = comm.ep_comm_ptr->size_rank_info[0].second;
58    int num_ep = comm.ep_comm_ptr->size_rank_info[1].second;
59    int mpi_size = comm.ep_comm_ptr->size_rank_info[2].second;
[1134]60
61    int root_mpi_rank = comm.rank_map->at(root).second;
62    int root_ep_loc = comm.rank_map->at(root).first;
63
64    ::MPI_Aint datasize, lb;
[1287]65    ::MPI_Type_get_extent(to_mpi_type(sendtype), &lb, &datasize);
[1134]66
[1287]67    bool is_master = (ep_rank_loc==0 && mpi_rank != root_mpi_rank ) || ep_rank == root;
68    bool is_root = ep_rank == root;
[1134]69
[1287]70    void* local_recvbuf;
[1134]71
[1287]72    if(is_master)
[1134]73    {
[1287]74      local_recvbuf = new void*[datasize * num_ep * sendcount];
[1134]75    }
76
[1287]77    void* tmp_recvbuf;
78    if(is_root) tmp_recvbuf = new void*[datasize * recvcount * ep_size];
[1134]79
80
[1287]81    if(mpi_rank == root_mpi_rank) MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, root_ep_loc, comm);
82    else                          MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, 0, comm);
[1134]83
[1287]84    std::vector<int> recvcounts(mpi_size, 0);
85    std::vector<int> displs(mpi_size, 0);
[1134]86
87
[1287]88    if(is_master)
89    {
90      for(int i=0; i<ep_size; i++)
[1134]91      {
[1287]92        recvcounts[comm.rank_map->at(i).second]+=sendcount;
[1134]93      }
94
[1287]95      for(int i=1; i<mpi_size; i++)
96        displs[i] = displs[i-1] + recvcounts[i-1];
[1134]97
[1287]98      ::MPI_Gatherv(local_recvbuf, sendcount*num_ep, sendtype, tmp_recvbuf, recvcounts.data(), displs.data(), recvtype, root_mpi_rank, to_mpi_comm(comm.mpi_comm));
99    }   
[1134]100
101
[1287]102    // reorder data
103    if(is_root)
[1134]104    {
[1287]105      // printf("tmp_recvbuf = %d %d %d %d %d %d %d %d\n", static_cast<int*>(tmp_recvbuf)[0], static_cast<int*>(tmp_recvbuf)[1],
106      //                                                   static_cast<int*>(tmp_recvbuf)[2], static_cast<int*>(tmp_recvbuf)[3],
107      //                                                   static_cast<int*>(tmp_recvbuf)[4], static_cast<int*>(tmp_recvbuf)[5],
108      //                                                   static_cast<int*>(tmp_recvbuf)[6], static_cast<int*>(tmp_recvbuf)[7] );
[1134]109
[1287]110      int offset;
111      for(int i=0; i<ep_size; i++)
[1134]112      {
[1287]113        offset = displs[comm.rank_map->at(i).second] + comm.rank_map->at(i).first * sendcount; 
114        memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize);
[1134]115
116
117      }
118
119    }
120
121
[1287]122    if(is_master)
[1134]123    {
[1287]124      delete[] local_recvbuf;
[1134]125    }
[1287]126    if(is_root) delete[] tmp_recvbuf;
127   
[1134]128  }
129
130}
Note: See TracBrowser for help on using the repository browser.