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