2 /* Copyright (c) 2007-2015. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_rma, smpi, "Logging specific to SMPI (RMA operations)");
15 xbt_bar_t creation_bar = nullptr;
17 typedef struct s_smpi_mpi_win{
24 std::vector<MPI_Request> *requests;
26 MPI_Win* connected_wins;
33 MPI_Win smpi_mpi_win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm){
36 int comm_size = smpi_comm_size(comm);
37 int rank=smpi_comm_rank(comm);
38 XBT_DEBUG("Creating window");
40 win = xbt_new(s_smpi_mpi_win_t, 1);
43 win->disp_unit = disp_unit;
46 if(info!=MPI_INFO_NULL)
51 win->group = MPI_GROUP_NULL;
52 win->requests = new std::vector<MPI_Request>();
53 win->connected_wins = xbt_new0(MPI_Win, comm_size);
54 win->connected_wins[rank] = win;
57 win->bar=xbt_barrier_init(comm_size);
59 mpi_coll_allgather_fun(&(win->connected_wins[rank]), sizeof(MPI_Win), MPI_BYTE, win->connected_wins, sizeof(MPI_Win),
62 mpi_coll_bcast_fun( &(win->bar), sizeof(xbt_bar_t), MPI_BYTE, 0, comm);
64 mpi_coll_barrier_fun(comm);
69 int smpi_mpi_win_free( MPI_Win* win){
70 //As per the standard, perform a barrier to ensure every async comm is finished
71 xbt_barrier_wait((*win)->bar);
72 delete (*win)->requests;
73 xbt_free((*win)->connected_wins);
74 if ((*win)->name != nullptr){
75 xbt_free((*win)->name);
77 if((*win)->info!=MPI_INFO_NULL){
78 MPI_Info_free(&(*win)->info);
81 mpi_coll_barrier_fun((*win)->comm);
82 int rank=smpi_comm_rank((*win)->comm);
84 xbt_barrier_destroy((*win)->bar);
90 void smpi_mpi_win_get_name(MPI_Win win, char* name, int* length){
91 if(win->name==nullptr){
96 *length = strlen(win->name);
97 strncpy(name, win->name, *length+1);
100 void smpi_mpi_win_get_group(MPI_Win win, MPI_Group* group){
101 if(win->comm != MPI_COMM_NULL){
102 *group = smpi_comm_group(win->comm);
106 void smpi_mpi_win_set_name(MPI_Win win, char* name){
107 win->name = xbt_strdup(name);
110 int smpi_mpi_win_fence( int assert, MPI_Win win){
111 XBT_DEBUG("Entering fence");
114 if(assert != MPI_MODE_NOPRECEDE){
115 xbt_barrier_wait(win->bar);
117 std::vector<MPI_Request> *reqs = win->requests;
118 int size = static_cast<int>(reqs->size());
119 // start all requests that have been prepared by another process
120 for(auto req: *reqs){
121 if (req->flags & PREPARED)
125 MPI_Request* treqs = &(*reqs)[0];
126 smpi_mpi_waitall(size,treqs,MPI_STATUSES_IGNORE);
128 win->assert = assert;
130 xbt_barrier_wait(win->bar);
131 XBT_DEBUG("Leaving fence ");
136 int smpi_mpi_put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
137 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win)
139 if(win->opened==0)//check that post/start has been done
141 //get receiver pointer
142 MPI_Win recv_win = win->connected_wins[target_rank];
144 void* recv_addr = static_cast<void*> ( static_cast<char*>(recv_win->base) + target_disp * recv_win->disp_unit);
145 XBT_DEBUG("Entering MPI_Put to %d", target_rank);
147 if(target_rank != smpi_comm_rank(win->comm)){
148 //prepare send_request
149 MPI_Request sreq = smpi_rma_send_init(origin_addr, origin_count, origin_datatype, smpi_process_index(),
150 smpi_group_index(smpi_comm_group(win->comm),target_rank), RMA_TAG+1, win->comm, MPI_OP_NULL);
152 //prepare receiver request
153 MPI_Request rreq = smpi_rma_recv_init(recv_addr, target_count, target_datatype, smpi_process_index(),
154 smpi_group_index(smpi_comm_group(win->comm),target_rank), RMA_TAG+1, recv_win->comm, MPI_OP_NULL);
156 //push request to receiver's win
157 recv_win->requests->push_back(rreq);
160 smpi_mpi_start(sreq);
162 //push request to sender's win
163 win->requests->push_back(sreq);
165 smpi_datatype_copy(origin_addr, origin_count, origin_datatype, recv_addr, target_count, target_datatype);
171 int smpi_mpi_get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
172 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win)
174 if(win->opened==0)//check that post/start has been done
177 MPI_Win send_win = win->connected_wins[target_rank];
179 void* send_addr = static_cast<void*>(static_cast<char*>(send_win->base) + target_disp * send_win->disp_unit);
180 XBT_DEBUG("Entering MPI_Get from %d", target_rank);
182 if(target_rank != smpi_comm_rank(win->comm)){
183 //prepare send_request
184 MPI_Request sreq = smpi_rma_send_init(send_addr, target_count, target_datatype,
185 smpi_group_index(smpi_comm_group(win->comm),target_rank), smpi_process_index(), RMA_TAG+2, send_win->comm,
188 //prepare receiver request
189 MPI_Request rreq = smpi_rma_recv_init(origin_addr, origin_count, origin_datatype,
190 smpi_group_index(smpi_comm_group(win->comm),target_rank), smpi_process_index(), RMA_TAG+2, win->comm,
193 //start the send, with another process than us as sender.
194 smpi_mpi_start(sreq);
196 //push request to receiver's win
197 send_win->requests->push_back(sreq);
200 smpi_mpi_start(rreq);
202 //push request to sender's win
203 win->requests->push_back(rreq);
205 smpi_datatype_copy(send_addr, target_count, target_datatype, origin_addr, origin_count, origin_datatype);
212 int smpi_mpi_accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
213 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win)
215 if(win->opened==0)//check that post/start has been done
217 //FIXME: local version
218 //get receiver pointer
219 MPI_Win recv_win = win->connected_wins[target_rank];
221 void* recv_addr = static_cast<void*>(static_cast<char*>(recv_win->base) + target_disp * recv_win->disp_unit);
222 XBT_DEBUG("Entering MPI_Accumulate to %d", target_rank);
224 //prepare send_request
225 MPI_Request sreq = smpi_rma_send_init(origin_addr, origin_count, origin_datatype,
226 smpi_process_index(), smpi_group_index(smpi_comm_group(win->comm),target_rank), RMA_TAG+3, win->comm, op);
228 //prepare receiver request
229 MPI_Request rreq = smpi_rma_recv_init(recv_addr, target_count, target_datatype,
230 smpi_process_index(), smpi_group_index(smpi_comm_group(win->comm),target_rank), RMA_TAG+3, recv_win->comm, op);
231 //push request to receiver's win
232 recv_win->requests->push_back(rreq);
234 smpi_mpi_start(sreq);
236 //push request to sender's win
237 win->requests->push_back(sreq);
242 int smpi_mpi_win_start(MPI_Group group, int assert, MPI_Win win){
243 /* From MPI forum advices
244 The call to MPI_WIN_COMPLETE does not return until the put call has completed at the origin; and the target window
245 will be accessed by the put operation only after the call to MPI_WIN_START has matched a call to MPI_WIN_POST by
246 the target process. This still leaves much choice to implementors. The call to MPI_WIN_START can block until the
247 matching call to MPI_WIN_POST occurs at all target processes. One can also have implementations where the call to
248 MPI_WIN_START is nonblocking, but the call to MPI_PUT blocks until the matching call to MPI_WIN_POST occurred; or
249 implementations where the first two calls are nonblocking, but the call to MPI_WIN_COMPLETE blocks until the call
250 to MPI_WIN_POST occurred; or even implementations where all three calls can complete before any target process
251 called MPI_WIN_POST --- the data put must be buffered, in this last case, so as to allow the put to complete at the
252 origin ahead of its completion at the target. However, once the call to MPI_WIN_POST is issued, the sequence above
253 must complete, without further dependencies. */
255 //naive, blocking implementation.
257 int size = smpi_group_size(group);
258 MPI_Request* reqs = xbt_new0(MPI_Request, size);
261 int src=smpi_group_index(group,j);
262 if(src!=smpi_process_index()){
263 reqs[i]=smpi_irecv_init(nullptr, 0, MPI_CHAR, src,RMA_TAG+4, MPI_COMM_WORLD);
269 smpi_mpi_startall(size, reqs);
270 smpi_mpi_waitall(size, reqs, MPI_STATUSES_IGNORE);
272 smpi_mpi_request_free(&reqs[i]);
275 win->opened++; //we're open for business !
277 smpi_group_use(group);
281 int smpi_mpi_win_post(MPI_Group group, int assert, MPI_Win win){
282 //let's make a synchronous send here
284 int size = smpi_group_size(group);
285 MPI_Request* reqs = xbt_new0(MPI_Request, size);
288 int dst=smpi_group_index(group,j);
289 if(dst!=smpi_process_index()){
290 reqs[i]=smpi_mpi_send_init(nullptr, 0, MPI_CHAR, dst, RMA_TAG+4, MPI_COMM_WORLD);
297 smpi_mpi_startall(size, reqs);
298 smpi_mpi_waitall(size, reqs, MPI_STATUSES_IGNORE);
300 smpi_mpi_request_free(&reqs[i]);
303 win->opened++; //we're open for business !
305 smpi_group_use(group);
309 int smpi_mpi_win_complete(MPI_Win win){
311 xbt_die("Complete called on already opened MPI_Win");
313 XBT_DEBUG("Entering MPI_Win_Complete");
315 int size = smpi_group_size(win->group);
316 MPI_Request* reqs = xbt_new0(MPI_Request, size);
319 int dst=smpi_group_index(win->group,j);
320 if(dst!=smpi_process_index()){
321 reqs[i]=smpi_mpi_send_init(nullptr, 0, MPI_CHAR, dst, RMA_TAG+5, MPI_COMM_WORLD);
327 XBT_DEBUG("Win_complete - Sending sync messages to %d processes", size);
328 smpi_mpi_startall(size, reqs);
329 smpi_mpi_waitall(size, reqs, MPI_STATUSES_IGNORE);
332 smpi_mpi_request_free(&reqs[i]);
336 //now we can finish RMA calls
338 std::vector<MPI_Request> *reqqs = win->requests;
339 size = static_cast<int>(reqqs->size());
341 XBT_DEBUG("Win_complete - Finishing %d RMA calls", size);
342 // start all requests that have been prepared by another process
343 for (auto req: *reqqs){
344 if (req->flags & PREPARED)
348 MPI_Request* treqs = &(*reqqs)[0];
349 smpi_mpi_waitall(size,treqs,MPI_STATUSES_IGNORE);
351 smpi_group_unuse(win->group);
352 win->opened--; //we're closed for business !
356 int smpi_mpi_win_wait(MPI_Win win){
357 //naive, blocking implementation.
358 XBT_DEBUG("Entering MPI_Win_Wait");
360 int size = smpi_group_size(win->group);
361 MPI_Request* reqs = xbt_new0(MPI_Request, size);
364 int src=smpi_group_index(win->group,j);
365 if(src!=smpi_process_index()){
366 reqs[i]=smpi_irecv_init(nullptr, 0, MPI_CHAR, src,RMA_TAG+5, MPI_COMM_WORLD);
372 XBT_DEBUG("Win_wait - Receiving sync messages from %d processes", size);
373 smpi_mpi_startall(size, reqs);
374 smpi_mpi_waitall(size, reqs, MPI_STATUSES_IGNORE);
376 smpi_mpi_request_free(&reqs[i]);
380 std::vector<MPI_Request> *reqqs = win->requests;
381 size = static_cast<int>(reqqs->size());
383 XBT_DEBUG("Win_complete - Finishing %d RMA calls", size);
385 // start all requests that have been prepared by another process
386 for(auto req: *reqqs){
387 if (req->flags & PREPARED)
391 MPI_Request* treqs = &(*reqqs)[0];
392 smpi_mpi_waitall(size,treqs,MPI_STATUSES_IGNORE);
394 smpi_group_unuse(win->group);
395 win->opened--; //we're opened for business !