4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
5 "Logging specific to SMPI (base)");
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
16 #define EAGER_LIMIT 65536
19 void smpi_process_init(int* argc, char*** argv) {
21 smpi_process_data_t data;
24 proc = SIMIX_process_self();
25 index = atoi((*argv)[1]);
26 data = smpi_process_remote_data(index);
27 SIMIX_process_set_data(proc, data);
30 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
31 (*argv)[(*argc) - 1] = NULL;
34 DEBUG2("<%d> New process in the game: %p", index, proc);
37 void smpi_process_destroy(void) {
38 int index = smpi_process_index();
40 DEBUG1("<%d> Process left the game", index);
43 /* MPI Low level calls */
44 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
47 size_t size = smpi_datatype_size(datatype) * count;
49 if (size > EAGER_LIMIT) {
50 /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
51 DEBUG1("RDV send to %d", dst);
52 rdv[0] = smpi_mpi_isend(NULL, 0, MPI_BYTE, dst, RDV_TAG, comm);
53 rdv[1] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, dst, RDV_TAG, comm);
54 smpi_mpi_waitall(2, rdv, MPI_STATUS_IGNORE);
56 request = xbt_new(s_smpi_mpi_request_t, 1);
58 request->src = smpi_comm_rank(comm);
62 request->complete = 0;
63 request->data = request;
64 smpi_process_post_send(comm, request);
65 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, NULL);
69 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
72 size_t size = smpi_datatype_size(datatype) * count;
74 if (size > EAGER_LIMIT) {
75 /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
76 DEBUG1("RDV recv from %d", src);
77 rdv[0] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, src, RDV_TAG, comm);
78 rdv[1] = smpi_mpi_isend(NULL, 0, MPI_BYTE, src, RDV_TAG, comm);
79 smpi_mpi_waitall(2, rdv, MPI_STATUS_IGNORE);
81 request = xbt_new(s_smpi_mpi_request_t, 1);
84 request->dst = smpi_comm_rank(comm);
87 request->complete = 0;
88 request->data = MPI_REQUEST_NULL;
89 smpi_process_post_recv(request);
90 request->pair = SIMIX_network_irecv(request->rdv, buf, &request->size);
94 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
97 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
98 smpi_mpi_wait(&request, status);
101 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
104 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
105 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
108 void smpi_mpi_sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status) {
109 MPI_Request requests[2];
112 requests[0] = smpi_mpi_isend(sendbuf, sendcount, sendtype, dst, sendtag, comm);
113 requests[1] = smpi_mpi_irecv(recvbuf, recvcount, recvtype, src, recvtag, comm);
114 smpi_mpi_waitall(2, requests, stats);
115 if(status != MPI_STATUS_IGNORE) {
116 // Copy receive status
117 memcpy(status, &stats[1], sizeof(MPI_Status));
121 static void finish_wait(MPI_Request* request, MPI_Status* status) {
122 MPI_Request data = (*request)->data;
124 xbt_assert0(data != MPI_REQUEST_NULL, "Erroneous situation");
125 if(status != MPI_STATUS_IGNORE) {
126 status->MPI_SOURCE = (*request)->src;
127 status->MPI_TAG = (*request)->tag;
128 status->MPI_ERROR = MPI_SUCCESS;
129 status->_count = (*request)->size; // size in bytes
130 status->_cancelled = 0; // FIXME: cancellation of requests not handled yet
132 DEBUG3("finishing wait for %p [data = %p, complete = %d]", *request, data, data->complete);
133 // data == *request if sender is first to finish its wait
134 // data != *request if receiver is first to finish its wait
135 if(data->complete == 0) {
136 // first arrives here
138 if(data != *request) {
139 // receveiver cleans its part
143 // second arrives here
144 if(data != *request) {
145 // receiver cleans everything
148 SIMIX_rdv_destroy((*request)->rdv);
151 *request = MPI_REQUEST_NULL;
154 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
155 MPI_Request data = (*request)->data;
156 int flag = data && data->complete == 1;
159 SIMIX_communication_destroy((*request)->pair);
160 finish_wait(request, status);
165 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
169 *index = MPI_UNDEFINED;
171 for(i = 0; i < count; i++) {
172 if(requests[i] != MPI_REQUEST_NULL) {
173 data = requests[i]->data;
174 if(data != MPI_REQUEST_NULL && data->complete == 1) {
175 SIMIX_communication_destroy(requests[i]->pair);
176 finish_wait(&requests[i], status);
187 void smpi_mpi_get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
188 int size = smpi_datatype_size(datatype);
189 *count = (int)(status->_count / size);
190 if ( (int)((*count) * size) != status->_count )
191 *count = MPI_UNDEFINED;
195 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
196 MPI_Request data = (*request)->data;
198 DEBUG6("wait for request %p (%p: %p) [src = %d, dst = %d, tag = %d]",
199 *request, (*request)->pair, data, (*request)->src, (*request)->dst, (*request)->tag);
200 // data is null if receiver waits before sender enters the rdv
201 if(data == MPI_REQUEST_NULL || data->complete == 0) {
202 SIMIX_network_wait((*request)->pair, -1.0);
204 SIMIX_communication_destroy((*request)->pair);
206 finish_wait(request, status);
209 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
215 index = MPI_UNDEFINED;
217 // First check for already completed requests
218 for(i = 0; i < count; i++) {
219 if(requests[i] != MPI_REQUEST_NULL) {
220 data = requests[i]->data;
221 if(data != MPI_REQUEST_NULL && data->complete == 1) {
223 SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
228 if(index == MPI_UNDEFINED) {
229 // Otherwise, wait for a request to complete
230 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
231 map = xbt_new(int, count);
233 DEBUG0("Wait for one of");
234 for(i = 0; i < count; i++) {
235 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
236 DEBUG4(" request %p [src = %d, dst = %d, tag = %d]",
237 requests[i], requests[i]->src, requests[i]->dst, requests[i]->tag);
238 xbt_dynar_push(comms, &requests[i]->pair);
244 index = SIMIX_network_waitany(comms);
248 xbt_dynar_free_container(&comms);
250 if(index != MPI_UNDEFINED) {
251 finish_wait(&requests[index], status);
257 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
262 index = smpi_mpi_waitany(count, requests, &stat);
263 if(index == MPI_UNDEFINED) {
266 if(status != MPI_STATUS_IGNORE) {
267 memcpy(&status[index], &stat, sizeof(stat));
269 // Move the last request to the found position
270 requests[index] = requests[count - 1];
271 requests[count - 1] = MPI_REQUEST_NULL;
276 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
281 for(i = 0; i < incount; i++) {
282 if(requests[i] != MPI_REQUEST_NULL) {
283 data = requests[i]->data;
284 if(data != MPI_REQUEST_NULL && data->complete == 1) {
285 SIMIX_communication_destroy(requests[i]->pair);
286 finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
295 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
296 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
297 nary_tree_bcast(buf, count, datatype, root, comm, 4);
300 void smpi_mpi_barrier(MPI_Comm comm) {
301 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
302 nary_tree_barrier(comm, 4);
305 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
306 int system_tag = 666;
307 int rank, size, src, index, sendsize, recvsize;
308 MPI_Request* requests;
310 rank = smpi_comm_rank(comm);
311 size = smpi_comm_size(comm);
313 // Send buffer to root
314 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
316 sendsize = smpi_datatype_size(sendtype);
317 recvsize = smpi_datatype_size(recvtype);
318 // Local copy from root
319 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
320 // Receive buffers from senders
321 requests = xbt_new(MPI_Request, size - 1);
323 for(src = 0; src < size; src++) {
325 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
329 // Wait for completion of irecv's.
330 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
335 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) {
336 int system_tag = 666;
337 int rank, size, src, index, sendsize;
338 MPI_Request* requests;
340 rank = smpi_comm_rank(comm);
341 size = smpi_comm_size(comm);
343 // Send buffer to root
344 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
346 sendsize = smpi_datatype_size(sendtype);
347 // Local copy from root
348 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
349 // Receive buffers from senders
350 requests = xbt_new(MPI_Request, size - 1);
352 for(src = 0; src < size; src++) {
354 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
358 // Wait for completion of irecv's.
359 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
364 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
365 int system_tag = 666;
366 int rank, size, other, index, sendsize, recvsize;
367 MPI_Request* requests;
369 rank = smpi_comm_rank(comm);
370 size = smpi_comm_size(comm);
371 sendsize = smpi_datatype_size(sendtype);
372 recvsize = smpi_datatype_size(recvtype);
373 // Local copy from self
374 memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
375 // Send/Recv buffers to/from others;
376 requests = xbt_new(MPI_Request, 2 * (size - 1));
378 for(other = 0; other < size; other++) {
380 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
382 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
386 // Wait for completion of all comms.
387 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
391 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
392 int system_tag = 666;
393 int rank, size, other, index, sendsize, recvsize;
394 MPI_Request* requests;
396 rank = smpi_comm_rank(comm);
397 size = smpi_comm_size(comm);
398 sendsize = smpi_datatype_size(sendtype);
399 recvsize = smpi_datatype_size(recvtype);
400 // Local copy from self
401 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
402 // Send buffers to others;
403 requests = xbt_new(MPI_Request, 2 * (size - 1));
405 for(other = 0; other < size; other++) {
407 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
409 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
413 // Wait for completion of all comms.
414 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
418 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
419 int system_tag = 666;
420 int rank, size, dst, index, sendsize, recvsize;
421 MPI_Request* requests;
423 rank = smpi_comm_rank(comm);
424 size = smpi_comm_size(comm);
426 // Recv buffer from root
427 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
429 sendsize = smpi_datatype_size(sendtype);
430 recvsize = smpi_datatype_size(recvtype);
431 // Local copy from root
432 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
433 // Send buffers to receivers
434 requests = xbt_new(MPI_Request, size - 1);
436 for(dst = 0; dst < size; dst++) {
438 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
442 // Wait for completion of isend's.
443 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
448 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) {
449 int system_tag = 666;
450 int rank, size, dst, index, sendsize, recvsize;
451 MPI_Request* requests;
453 rank = smpi_comm_rank(comm);
454 size = smpi_comm_size(comm);
456 // Recv buffer from root
457 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
459 sendsize = smpi_datatype_size(sendtype);
460 recvsize = smpi_datatype_size(recvtype);
461 // Local copy from root
462 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
463 // Send buffers to receivers
464 requests = xbt_new(MPI_Request, size - 1);
466 for(dst = 0; dst < size; dst++) {
468 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
472 // Wait for completion of isend's.
473 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
478 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
479 int system_tag = 666;
480 int rank, size, src, index, datasize;
481 MPI_Request* requests;
484 rank = smpi_comm_rank(comm);
485 size = smpi_comm_size(comm);
487 // Send buffer to root
488 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
490 datasize = smpi_datatype_size(datatype);
491 // Local copy from root
492 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
493 // Receive buffers from senders
494 //TODO: make a MPI_barrier here ?
495 requests = xbt_new(MPI_Request, size - 1);
496 tmpbufs = xbt_new(void*, size - 1);
498 for(src = 0; src < size; src++) {
500 tmpbufs[index] = xbt_malloc(count * datasize);
501 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, src, system_tag, comm);
505 // Wait for completion of irecv's.
506 for(src = 0; src < size - 1; src++) {
507 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
508 if(index == MPI_UNDEFINED) {
511 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
513 for(index = 0; index < size - 1; index++) {
514 xbt_free(tmpbufs[index]);
521 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
522 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
523 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
526 FIXME: buggy implementation
528 int system_tag = 666;
529 int rank, size, other, index, datasize;
530 MPI_Request* requests;
533 rank = smpi_comm_rank(comm);
534 size = smpi_comm_size(comm);
535 datasize = smpi_datatype_size(datatype);
536 // Local copy from self
537 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
538 // Send/Recv buffers to/from others;
539 //TODO: make a MPI_barrier here ?
540 requests = xbt_new(MPI_Request, 2 * (size - 1));
541 tmpbufs = xbt_new(void*, size - 1);
543 for(other = 0; other < size; other++) {
545 tmpbufs[index / 2] = xbt_malloc(count * datasize);
546 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
547 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
551 // Wait for completion of all comms.
552 for(other = 0; other < 2 * (size - 1); other++) {
553 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
554 if(index == MPI_UNDEFINED) {
557 if((index & 1) == 1) {
558 // Request is odd: it's a irecv
559 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
562 for(index = 0; index < size - 1; index++) {
563 xbt_free(tmpbufs[index]);
570 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
571 int system_tag = 666;
572 int rank, size, other, index, datasize;
574 MPI_Request* requests;
577 rank = smpi_comm_rank(comm);
578 size = smpi_comm_size(comm);
579 datasize = smpi_datatype_size(datatype);
580 // Local copy from self
581 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
582 // Send/Recv buffers to/from others;
583 total = rank + (size - (rank + 1));
584 requests = xbt_new(MPI_Request, total);
585 tmpbufs = xbt_new(void*, rank);
587 for(other = 0; other < rank; other++) {
588 tmpbufs[index] = xbt_malloc(count * datasize);
589 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, other, system_tag, comm);
592 for(other = rank + 1; other < size; other++) {
593 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
596 // Wait for completion of all comms.
597 for(other = 0; other < total; other++) {
598 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
599 if(index == MPI_UNDEFINED) {
603 // #Request is below rank: it's a irecv
604 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
607 for(index = 0; index < size - 1; index++) {
608 xbt_free(tmpbufs[index]);