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
18 void smpi_process_init(int* argc, char*** argv) {
20 smpi_process_data_t data;
23 proc = SIMIX_process_self();
24 index = atoi((*argv)[1]);
25 data = smpi_process_remote_data(index);
26 SIMIX_process_set_data(proc, data);
29 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
30 (*argv)[(*argc) - 1] = NULL;
33 DEBUG2("<%d> New process in the game: %p", index, proc);
36 void smpi_process_destroy(void) {
37 int index = smpi_process_index();
39 DEBUG1("<%d> Process left the game", index);
42 /* MPI Low level calls */
43 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
45 size_t size = smpi_datatype_size(datatype) * count;
47 if (size > EAGER_LIMIT) {
48 /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
49 smpi_mpi_send (NULL, 0, MPI_BYTE, dst, tag, comm);
51 request = xbt_new(s_smpi_mpi_request_t, 1);
53 request->src = smpi_comm_rank(comm);
57 request->complete = 0;
58 request->data = request;
59 smpi_process_post_send(comm, request);
60 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, NULL);
64 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
66 size_t size = smpi_datatype_size(datatype) * count;
68 if (size > EAGER_LIMIT) {
69 /* Warning: this (zero-length synchronous) call will come back here with size == 0 */
70 smpi_mpi_recv (NULL, 0, MPI_BYTE, src, tag, comm, MPI_STATUS_IGNORE);
72 request = xbt_new(s_smpi_mpi_request_t, 1);
75 request->dst = smpi_comm_rank(comm);
78 request->complete = 0;
79 request->data = MPI_REQUEST_NULL;
80 smpi_process_post_recv(request);
81 request->pair = SIMIX_network_irecv(request->rdv, buf, &request->size);
85 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
88 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
89 smpi_mpi_wait(&request, status);
92 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
95 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
96 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
99 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) {
100 MPI_Request requests[2];
103 requests[0] = smpi_mpi_isend(sendbuf, sendcount, sendtype, dst, sendtag, comm);
104 requests[1] = smpi_mpi_irecv(recvbuf, recvcount, recvtype, src, recvtag, comm);
105 smpi_mpi_waitall(2, requests, stats);
106 if(status != MPI_STATUS_IGNORE) {
107 // Copy receive status
108 memcpy(status, &stats[1], sizeof(MPI_Status));
112 static void finish_wait(MPI_Request* request, MPI_Status* status) {
113 MPI_Request data = (*request)->data;
115 xbt_assert0(data != MPI_REQUEST_NULL, "Erroneous situation");
116 if(status != MPI_STATUS_IGNORE) {
117 status->MPI_SOURCE = (*request)->src;
118 status->MPI_TAG = (*request)->tag;
119 status->MPI_ERROR = MPI_SUCCESS;
120 status->_count = (*request)->size; // size in bytes
121 status->_cancelled = 0; // FIXME: cancellation of requests not handled yet
123 DEBUG3("finishing wait for %p [data = %p, complete = %d]", *request, data, data->complete);
124 // data == *request if sender is first to finish its wait
125 // data != *request if receiver is first to finish its wait
126 if(data->complete == 0) {
127 // first arrives here
129 if(data != *request) {
130 // receveiver cleans its part
134 // second arrives here
135 if(data != *request) {
136 // receiver cleans everything
139 SIMIX_rdv_destroy((*request)->rdv);
142 *request = MPI_REQUEST_NULL;
145 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
146 MPI_Request data = (*request)->data;
147 int flag = data && data->complete == 1;
150 SIMIX_communication_destroy((*request)->pair);
151 finish_wait(request, status);
156 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
160 *index = MPI_UNDEFINED;
162 for(i = 0; i < count; i++) {
163 if(requests[i] != MPI_REQUEST_NULL) {
164 data = requests[i]->data;
165 if(data != MPI_REQUEST_NULL && data->complete == 1) {
166 SIMIX_communication_destroy(requests[i]->pair);
167 finish_wait(&requests[i], status);
178 void smpi_mpi_get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
179 int size = smpi_datatype_size(datatype);
180 *count = (int)(status->_count / size);
181 if ( (int)((*count) * size) != status->_count )
182 *count = MPI_UNDEFINED;
186 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
187 MPI_Request data = (*request)->data;
189 DEBUG6("wait for request %p (%p: %p) [src = %d, dst = %d, tag = %d]",
190 *request, (*request)->pair, data, (*request)->src, (*request)->dst, (*request)->tag);
191 // data is null if receiver waits before sender enters the rdv
192 if(data == MPI_REQUEST_NULL || data->complete == 0) {
193 SIMIX_network_wait((*request)->pair, -1.0);
195 SIMIX_communication_destroy((*request)->pair);
197 finish_wait(request, status);
200 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
206 index = MPI_UNDEFINED;
208 // First check for already completed requests
209 for(i = 0; i < count; i++) {
210 if(requests[i] != MPI_REQUEST_NULL) {
211 data = requests[i]->data;
212 if(data != MPI_REQUEST_NULL && data->complete == 1) {
214 SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
219 if(index == MPI_UNDEFINED) {
220 // Otherwise, wait for a request to complete
221 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
222 map = xbt_new(int, count);
224 DEBUG0("Wait for one of");
225 for(i = 0; i < count; i++) {
226 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
227 DEBUG4(" request %p [src = %d, dst = %d, tag = %d]",
228 requests[i], requests[i]->src, requests[i]->dst, requests[i]->tag);
229 xbt_dynar_push(comms, &requests[i]->pair);
235 index = SIMIX_network_waitany(comms);
239 xbt_dynar_free_container(&comms);
241 if(index != MPI_UNDEFINED) {
242 finish_wait(&requests[index], status);
248 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
253 index = smpi_mpi_waitany(count, requests, &stat);
254 if(index == MPI_UNDEFINED) {
257 if(status != MPI_STATUS_IGNORE) {
258 memcpy(&status[index], &stat, sizeof(stat));
260 // Move the last request to the found position
261 requests[index] = requests[count - 1];
262 requests[count - 1] = MPI_REQUEST_NULL;
267 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
272 for(i = 0; i < incount; i++) {
273 if(requests[i] != MPI_REQUEST_NULL) {
274 data = requests[i]->data;
275 if(data != MPI_REQUEST_NULL && data->complete == 1) {
276 SIMIX_communication_destroy(requests[i]->pair);
277 finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
286 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
287 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
288 nary_tree_bcast(buf, count, datatype, root, comm, 4);
291 void smpi_mpi_barrier(MPI_Comm comm) {
292 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
293 nary_tree_barrier(comm, 4);
296 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
297 int system_tag = 666;
298 int rank, size, src, index, sendsize, recvsize;
299 MPI_Request* requests;
301 rank = smpi_comm_rank(comm);
302 size = smpi_comm_size(comm);
304 // Send buffer to root
305 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
307 sendsize = smpi_datatype_size(sendtype);
308 recvsize = smpi_datatype_size(recvtype);
309 // Local copy from root
310 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
311 // Receive buffers from senders
312 requests = xbt_new(MPI_Request, size - 1);
314 for(src = 0; src < size; src++) {
316 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
320 // Wait for completion of irecv's.
321 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
326 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) {
327 int system_tag = 666;
328 int rank, size, src, index, sendsize;
329 MPI_Request* requests;
331 rank = smpi_comm_rank(comm);
332 size = smpi_comm_size(comm);
334 // Send buffer to root
335 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
337 sendsize = smpi_datatype_size(sendtype);
338 // Local copy from root
339 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
340 // Receive buffers from senders
341 requests = xbt_new(MPI_Request, size - 1);
343 for(src = 0; src < size; src++) {
345 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
349 // Wait for completion of irecv's.
350 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
355 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
356 int system_tag = 666;
357 int rank, size, other, index, sendsize, recvsize;
358 MPI_Request* requests;
360 rank = smpi_comm_rank(comm);
361 size = smpi_comm_size(comm);
362 sendsize = smpi_datatype_size(sendtype);
363 recvsize = smpi_datatype_size(recvtype);
364 // Local copy from self
365 memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
366 // Send/Recv buffers to/from others;
367 requests = xbt_new(MPI_Request, 2 * (size - 1));
369 for(other = 0; other < size; other++) {
371 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
373 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
377 // Wait for completion of all comms.
378 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
382 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
383 int system_tag = 666;
384 int rank, size, other, index, sendsize, recvsize;
385 MPI_Request* requests;
387 rank = smpi_comm_rank(comm);
388 size = smpi_comm_size(comm);
389 sendsize = smpi_datatype_size(sendtype);
390 recvsize = smpi_datatype_size(recvtype);
391 // Local copy from self
392 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
393 // Send buffers to others;
394 requests = xbt_new(MPI_Request, 2 * (size - 1));
396 for(other = 0; other < size; other++) {
398 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
400 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
404 // Wait for completion of all comms.
405 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
409 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
410 int system_tag = 666;
411 int rank, size, dst, index, sendsize, recvsize;
412 MPI_Request* requests;
414 rank = smpi_comm_rank(comm);
415 size = smpi_comm_size(comm);
417 // Recv buffer from root
418 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
420 sendsize = smpi_datatype_size(sendtype);
421 recvsize = smpi_datatype_size(recvtype);
422 // Local copy from root
423 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
424 // Send buffers to receivers
425 requests = xbt_new(MPI_Request, size - 1);
427 for(dst = 0; dst < size; dst++) {
429 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
433 // Wait for completion of isend's.
434 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
439 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) {
440 int system_tag = 666;
441 int rank, size, dst, index, sendsize, recvsize;
442 MPI_Request* requests;
444 rank = smpi_comm_rank(comm);
445 size = smpi_comm_size(comm);
447 // Recv buffer from root
448 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
450 sendsize = smpi_datatype_size(sendtype);
451 recvsize = smpi_datatype_size(recvtype);
452 // Local copy from root
453 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
454 // Send buffers to receivers
455 requests = xbt_new(MPI_Request, size - 1);
457 for(dst = 0; dst < size; dst++) {
459 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
463 // Wait for completion of isend's.
464 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
469 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
470 int system_tag = 666;
471 int rank, size, src, index, datasize;
472 MPI_Request* requests;
475 rank = smpi_comm_rank(comm);
476 size = smpi_comm_size(comm);
478 // Send buffer to root
479 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
481 datasize = smpi_datatype_size(datatype);
482 // Local copy from root
483 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
484 // Receive buffers from senders
485 //TODO: make a MPI_barrier here ?
486 requests = xbt_new(MPI_Request, size - 1);
487 tmpbufs = xbt_new(void*, size - 1);
489 for(src = 0; src < size; src++) {
491 tmpbufs[index] = xbt_malloc(count * datasize);
492 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, src, system_tag, comm);
496 // Wait for completion of irecv's.
497 for(src = 0; src < size - 1; src++) {
498 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
499 if(index == MPI_UNDEFINED) {
502 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
504 for(index = 0; index < size - 1; index++) {
505 xbt_free(tmpbufs[index]);
512 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
513 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
514 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
517 FIXME: buggy implementation
519 int system_tag = 666;
520 int rank, size, other, index, datasize;
521 MPI_Request* requests;
524 rank = smpi_comm_rank(comm);
525 size = smpi_comm_size(comm);
526 datasize = smpi_datatype_size(datatype);
527 // Local copy from self
528 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
529 // Send/Recv buffers to/from others;
530 //TODO: make a MPI_barrier here ?
531 requests = xbt_new(MPI_Request, 2 * (size - 1));
532 tmpbufs = xbt_new(void*, size - 1);
534 for(other = 0; other < size; other++) {
536 tmpbufs[index / 2] = xbt_malloc(count * datasize);
537 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
538 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
542 // Wait for completion of all comms.
543 for(other = 0; other < 2 * (size - 1); other++) {
544 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
545 if(index == MPI_UNDEFINED) {
548 if((index & 1) == 1) {
549 // Request is odd: it's a irecv
550 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
553 for(index = 0; index < size - 1; index++) {
554 xbt_free(tmpbufs[index]);
561 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
562 int system_tag = 666;
563 int rank, size, other, index, datasize;
565 MPI_Request* requests;
568 rank = smpi_comm_rank(comm);
569 size = smpi_comm_size(comm);
570 datasize = smpi_datatype_size(datatype);
571 // Local copy from self
572 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
573 // Send/Recv buffers to/from others;
574 total = rank + (size - (rank + 1));
575 requests = xbt_new(MPI_Request, total);
576 tmpbufs = xbt_new(void*, rank);
578 for(other = 0; other < rank; other++) {
579 tmpbufs[index] = xbt_malloc(count * datasize);
580 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, other, system_tag, comm);
583 for(other = rank + 1; other < size; other++) {
584 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
587 // Wait for completion of all comms.
588 for(other = 0; other < total; other++) {
589 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
590 if(index == MPI_UNDEFINED) {
594 // #Request is below rank: it's a irecv
595 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
598 for(index = 0; index < size - 1; index++) {
599 xbt_free(tmpbufs[index]);