[152] | 1 | #include "mpi_manager.hpp" |
---|
| 2 | |
---|
| 3 | #include "fortran/impi_interface.hpp" |
---|
| 4 | |
---|
| 5 | namespace xmlioserver |
---|
| 6 | { |
---|
| 7 | namespace comm |
---|
| 8 | { |
---|
| 9 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 10 | |
---|
| 11 | void CMPIManager::Initialise(int * UNUSED(argc), char *** UNUSED(argv)) |
---|
| 12 | { |
---|
| 13 | int error = 0; |
---|
| 14 | bool flag = false; |
---|
| 15 | |
---|
| 16 | mpi_initialized(&flag, &error); |
---|
| 17 | if (error != mpi_success) |
---|
| 18 | ERROR("CMPIManager::Initialise(arc, argv)", << " MPI Error !"); |
---|
| 19 | if (!flag) |
---|
| 20 | { |
---|
| 21 | mpi_init(&error); |
---|
| 22 | if (error != mpi_success) |
---|
| 23 | ERROR("CMPIManager::Initialise(arc, argv)", << " MPI Error !"); |
---|
| 24 | } |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void CMPIManager::Finalize(void) |
---|
| 28 | { |
---|
| 29 | int error = 0; |
---|
| 30 | mpi_finalize(&error); |
---|
| 31 | if (error != mpi_success) |
---|
| 32 | ERROR("CMPIManager::Initialise(arc, argv)", << " MPI Error !"); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | ///-------------------------------------------------------------- |
---|
| 36 | |
---|
| 37 | int CMPIManager::GetCommRank(MPIComm comm) |
---|
| 38 | { |
---|
| 39 | int rank = 0, error = 0; |
---|
| 40 | mpi_comm_rank(&comm, &rank, &error); |
---|
| 41 | if (error != mpi_success) |
---|
| 42 | ERROR("CMPIManager::GetCommRank(comm)", << " MPI Error !"); |
---|
| 43 | return (rank); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | int CMPIManager::GetCommSize(MPIComm comm) |
---|
| 47 | { |
---|
| 48 | int size = 0, error = 0; |
---|
| 49 | mpi_comm_size(&comm, &size, &error); |
---|
| 50 | if (error != mpi_success) |
---|
| 51 | ERROR("CMPIManager::GetCommSize(comm)", << " MPI Error !"); |
---|
| 52 | return (size); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | MPIComm CMPIManager::GetCommWorld(void) |
---|
| 56 | { return (mpi_comm_world); } |
---|
| 57 | |
---|
| 58 | bool CMPIManager::IsMaster(MPIComm comm) |
---|
| 59 | { return (CMPIManager::GetCommRank(comm) == 0); } |
---|
| 60 | |
---|
| 61 | bool CMPIManager::IsRank(MPIComm comm, int rank) |
---|
| 62 | { return (CMPIManager::GetCommRank(comm) == rank); } |
---|
| 63 | |
---|
| 64 | MPIComm CMPIManager::CreateComm(MPIGroup group, MPIComm pcomm) |
---|
| 65 | { |
---|
| 66 | MPIComm commu = 0; |
---|
| 67 | int error = 0; |
---|
| 68 | mpi_comm_create(&pcomm, &group, &commu, &error); |
---|
| 69 | if (error != mpi_success) |
---|
| 70 | ERROR("CMPIManager::CreateComm(group, pcomm)", << " MPI Error !"); |
---|
| 71 | return (commu); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | //--------------------------------------------------------------- |
---|
| 75 | |
---|
| 76 | void CMPIManager::Barrier(MPIComm comm) |
---|
| 77 | { |
---|
| 78 | int error = 0; |
---|
| 79 | mpi_barrier(&comm, &error); |
---|
| 80 | if (error != mpi_success) |
---|
| 81 | ERROR("CMPIManager::Barrier(comm)", << " MPI Error !"); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | //--------------------------------------------------------------- |
---|
| 85 | |
---|
| 86 | MPIGroup CMPIManager::GetGroupWorld(void) |
---|
| 87 | { |
---|
| 88 | MPIGroup group = 0; |
---|
| 89 | int error = 0; |
---|
| 90 | MPIComm commu = CMPIManager::GetCommWorld(); |
---|
| 91 | mpi_comm_group(&commu, &group, &error); |
---|
| 92 | if (error != mpi_success) |
---|
| 93 | ERROR("CMPIManager::GetGroupWorld()", << " MPI Error !"); |
---|
| 94 | return (group); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | MPIGroup CMPIManager::CreateSubGroup(MPIGroup pgroup, const std::vector<int> & ranks) |
---|
| 98 | { |
---|
| 99 | MPIGroup group = 0; |
---|
| 100 | int size = ranks.size(); |
---|
| 101 | int error = 0; |
---|
| 102 | mpi_group_incl(&pgroup, &size, &(ranks[0]), &group, &error); |
---|
| 103 | if (error != mpi_success) |
---|
| 104 | ERROR("CMPIManager::CreateSubGroup(pgroup, ranks)", << " MPI Error !"); |
---|
| 105 | return (group); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | MPIGroup CMPIManager::CreateSubGroup(MPIGroup pgroup, int min_rank, int max_rank, int intval) |
---|
| 109 | { |
---|
| 110 | std::vector<int> ranks; |
---|
| 111 | for (int i = min_rank; i <= max_rank; i += intval) |
---|
| 112 | ranks.push_back(i); |
---|
| 113 | return (CMPIManager::CreateSubGroup(pgroup, ranks)); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | //--------------------------------------------------------------- |
---|
| 117 | |
---|
| 118 | void CMPIManager::AllocMem(void * data, StdSize size) |
---|
| 119 | { |
---|
| 120 | if (MPI_Alloc_mem(sizeof(char) * size, MPI_INFO_NULL, data) != MPI_SUCCESS) |
---|
| 121 | ERROR("CMPIManager::AllocMem(data, size)", << " MPI Error !"); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void CMPIManager::FreeMem(void * data) |
---|
| 125 | { MPI_Free_mem(data); } |
---|
| 126 | |
---|
| 127 | //-------------------------------------------------------------- |
---|
| 128 | |
---|
| 129 | void CMPIManager::Send (MPIComm comm, int dest_rank, char * data, |
---|
| 130 | StdSize size, MPIRequest & request) |
---|
| 131 | { |
---|
| 132 | MPIDataType type = mpi_char; |
---|
| 133 | int nsize = size; |
---|
| 134 | int tag = 0, error = 0; |
---|
| 135 | mpi_issend(data, &nsize, &type, &dest_rank, &tag, &comm, &request, &error); |
---|
| 136 | if (error != mpi_success) |
---|
| 137 | ERROR("CMPIManager::Send (comm, dest_rank, data, size, request)", << " MPI Error !"); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void CMPIManager::Wait (MPIRequest & request) |
---|
| 141 | { |
---|
| 142 | MPIStatus status = new int[mpi_status_size](); |
---|
| 143 | int error = 0; |
---|
| 144 | mpi_wait(&request, status, &error); |
---|
| 145 | if (error != mpi_success) |
---|
| 146 | ERROR("CMPIManager::Wait (request)", << " MPI Error !"); |
---|
| 147 | delete [] status; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | bool CMPIManager::Test (MPIRequest & request) |
---|
| 151 | { |
---|
| 152 | MPIStatus status = new int[mpi_status_size](); |
---|
| 153 | bool flag = false; |
---|
| 154 | int error = 0; |
---|
| 155 | mpi_test(&request, &flag, status, &error); |
---|
| 156 | if (error != mpi_success) |
---|
| 157 | ERROR("CMPIManager::Test (request)", << " MPI Error !"); |
---|
| 158 | delete [] status; |
---|
| 159 | return (flag); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | bool CMPIManager::HasReceivedData(MPIComm comm, int src_rank) |
---|
| 163 | { |
---|
| 164 | MPIStatus status = new int[mpi_status_size](); |
---|
| 165 | bool flag = false; |
---|
| 166 | int error = 0, tag = mpi_any_tag; |
---|
| 167 | mpi_iprobe(&src_rank, &tag, &comm, &flag, status, &error); |
---|
| 168 | if (error != mpi_success) |
---|
| 169 | ERROR("CMPIManager::hasReceivedData (comm, rank)", << " MPI Error !"); |
---|
| 170 | delete [] status; |
---|
| 171 | return (flag); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | StdSize CMPIManager::GetReceivedDataSize(MPIComm comm, int src_rank) |
---|
| 175 | { |
---|
| 176 | MPIDataType type = mpi_char; |
---|
| 177 | MPIStatus status = new int[mpi_status_size](); |
---|
| 178 | bool flag = false; |
---|
| 179 | int error = 0, size = 0, tag = mpi_any_tag; |
---|
| 180 | |
---|
| 181 | mpi_iprobe(&src_rank, &tag, &comm, &flag, status, &error); |
---|
| 182 | if (error != mpi_success) |
---|
| 183 | ERROR("CMPIManager::getReceivedDataSize (comm, rank)", << " MPI Error !"); |
---|
| 184 | if (flag == false) return (0); |
---|
| 185 | mpi_get_count(status, &type, &size, &error); |
---|
| 186 | if (error != mpi_success) |
---|
| 187 | ERROR("CMPIManager::getReceivedDataSize (comm, rank)", << " MPI Error !"); |
---|
| 188 | delete [] status; |
---|
| 189 | return (size); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void CMPIManager::Receive(MPIComm comm, int src_rank, char * data) |
---|
| 193 | { |
---|
| 194 | MPIRequest req = 0; |
---|
| 195 | MPIDataType type = mpi_char; |
---|
| 196 | int error = 0, tag = mpi_any_tag; |
---|
| 197 | int size = CMPIManager::GetReceivedDataSize(comm, src_rank); |
---|
| 198 | |
---|
| 199 | mpi_irecv(data, &size, &type, &src_rank, &tag, &comm, &req, &error); |
---|
| 200 | if (error != mpi_success) |
---|
| 201 | ERROR("CMPIManager::Receive (comm, src_rank, data)", << " MPI Error !"); |
---|
| 202 | CMPIManager::Wait (req); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | //-------------------------------------------------------------- |
---|
| 206 | |
---|
| 207 | void CMPIManager::SendLinearBuffer |
---|
| 208 | (MPIComm comm, int dest_rank, CLinearBuffer & buff, MPIRequest & request) |
---|
| 209 | { |
---|
| 210 | CMPIManager::Send(comm, dest_rank, buff, buff.getUsedSize(), request); |
---|
| 211 | buff.clear(); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void CMPIManager::ReceiveLinearBuffer(MPIComm comm, int src_rank, CLinearBuffer & buff) |
---|
| 215 | { |
---|
| 216 | CMPIManager::Receive(comm, src_rank, buff); |
---|
| 217 | buff.computeBufferData(); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | boost::shared_ptr<CLinearBuffer> CMPIManager::ReceiveLinearBuffer(MPIComm comm, int src_rank) |
---|
| 221 | { |
---|
| 222 | boost::shared_ptr<CLinearBuffer> buff_ptr |
---|
| 223 | (new CLinearBuffer(CMPIManager::GetReceivedDataSize(comm, src_rank))); |
---|
| 224 | CMPIManager::ReceiveLinearBuffer(comm, src_rank, *buff_ptr); |
---|
| 225 | return (buff_ptr); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void CMPIManager::ReceiveCircularBuffer(MPIComm comm, int src_rank, CCircularBuffer & buff) |
---|
| 229 | { |
---|
| 230 | StdSize data_size = CMPIManager::GetReceivedDataSize(comm, src_rank); |
---|
| 231 | StdSize data_begin = buff.prepareNextDataPosition(data_size); |
---|
| 232 | CMPIManager::Receive(comm, src_rank, buff.getData(data_begin)); |
---|
| 233 | |
---|
| 234 | buff.updateNbRequests(data_begin, data_begin + data_size); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | ///-------------------------------------------------------------- |
---|
| 238 | |
---|
| 239 | } // namespace comm |
---|
| 240 | } // namespace xmlioserver |
---|