1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
11 "Logging specific to SMPI (base)");
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
15 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
16 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
18 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
19 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
20 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
22 #define EAGER_LIMIT 65536
25 void smpi_process_init(int* argc, char*** argv) {
27 smpi_process_data_t data;
30 proc = SIMIX_process_self();
31 index = atoi((*argv)[1]);
32 data = smpi_process_remote_data(index);
33 SIMIX_process_set_data(proc, data);
36 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
37 (*argv)[(*argc) - 1] = NULL;
40 DEBUG2("<%d> New process in the game: %p", index, proc);
43 void smpi_process_destroy(void) {
44 int index = smpi_process_index();
46 DEBUG1("<%d> Process left the game", index);
49 static MPI_Request build_request(void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags) {
52 request = xbt_new(s_smpi_mpi_request_t, 1);
54 request->size = smpi_datatype_size(datatype) * count;
61 request->complete = 0;
62 request->match = MPI_REQUEST_NULL;
63 request->flags = flags;
64 if(request->size <= EAGER_LIMIT) {
65 request->ack = MPI_REQUEST_NULL;
67 request->ack = xbt_new(s_smpi_mpi_request_t, 1);
68 request->ack->buf = NULL;
69 request->ack->size = 0;
70 request->ack->src = dst;
71 request->ack->dst = src;
72 request->ack->tag = RDV_TAG;
73 request->ack->comm = comm;
74 request->ack->rdv = NULL;
75 request->ack->pair = NULL;
76 request->ack->complete = 0;
77 request->ack->match = MPI_REQUEST_NULL;
78 request->ack->flags = NON_PERSISTENT | ((request->flags & RECV) == RECV ? SEND : RECV);
79 smpi_mpi_start(request->ack);
84 /* MPI Low level calls */
85 MPI_Request smpi_mpi_send_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
86 MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, PERSISTENT | SEND);
91 MPI_Request smpi_mpi_recv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
92 MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, PERSISTENT | RECV);
97 void smpi_mpi_start(MPI_Request request) {
98 xbt_assert0(request->complete == 0, "Cannot start a non-finished communication");
99 if(request->size > EAGER_LIMIT) {
100 print_request("RDV ack", request->ack);
101 smpi_mpi_wait(&request->ack, MPI_STATUS_IGNORE);
103 if((request->flags & RECV) == RECV) {
104 smpi_process_post_recv(request);
105 print_request("New recv", request);
106 request->pair = SIMIX_network_irecv(request->rdv, request->buf, &request->size);
108 smpi_process_post_send(request->comm, request); // FIXME
109 print_request("New send", request);
110 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, request->buf, request->size, NULL);
114 void smpi_mpi_startall(int count, MPI_Request* requests) {
117 for(i = 0; i < count; i++) {
118 smpi_mpi_start(requests[i]);
122 void smpi_mpi_request_free(MPI_Request* request) {
124 *request = MPI_REQUEST_NULL;
127 MPI_Request smpi_isend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
128 MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, NON_PERSISTENT | SEND);
133 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
134 MPI_Request request = smpi_isend_init(buf, count, datatype, dst, tag, comm);
136 smpi_mpi_start(request);
140 MPI_Request smpi_irecv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
141 MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, NON_PERSISTENT | RECV);
146 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
147 MPI_Request request = smpi_irecv_init(buf, count, datatype, src, tag, comm);
149 smpi_mpi_start(request);
153 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
156 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
157 smpi_mpi_wait(&request, status);
160 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
163 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
164 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
167 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) {
168 MPI_Request requests[2];
171 requests[0] = smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
172 requests[1] = smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
173 smpi_mpi_startall(2, requests);
174 smpi_mpi_waitall(2, requests, stats);
175 if(status != MPI_STATUS_IGNORE) {
176 // Copy receive status
177 memcpy(status, &stats[1], sizeof(MPI_Status));
181 static void finish_wait(MPI_Request* request, MPI_Status* status) {
182 if(status != MPI_STATUS_IGNORE) {
183 status->MPI_SOURCE = (*request)->src;
184 status->MPI_TAG = (*request)->tag;
185 status->MPI_ERROR = MPI_SUCCESS;
187 print_request("finishing wait", *request);
188 if((*request)->complete == 1) {
189 SIMIX_rdv_destroy((*request)->rdv);
191 (*request)->match->complete = 1;
192 (*request)->match->match = MPI_REQUEST_NULL;
194 if(((*request)->flags & NON_PERSISTENT) == NON_PERSISTENT) {
195 smpi_mpi_request_free(request);
197 (*request)->rdv = NULL;
198 (*request)->pair = NULL;
202 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
203 int flag = (*request)->complete;
206 SIMIX_communication_destroy((*request)->pair);
207 finish_wait(request, status);
212 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
215 *index = MPI_UNDEFINED;
217 for(i = 0; i < count; i++) {
218 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
219 SIMIX_communication_destroy(requests[i]->pair);
220 finish_wait(&requests[i], status);
229 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
230 print_request("wait", *request);
231 // data is null if receiver waits before sender enters the rdv
232 if((*request)->complete) {
233 SIMIX_communication_destroy((*request)->pair);
235 SIMIX_network_wait((*request)->pair, -1.0);
237 finish_wait(request, status);
240 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
245 index = MPI_UNDEFINED;
247 // First check for already completed requests
248 for(i = 0; i < count; i++) {
249 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
251 SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
255 if(index == MPI_UNDEFINED) {
256 // Otherwise, wait for a request to complete
257 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
258 map = xbt_new(int, count);
260 DEBUG0("Wait for one of");
261 for(i = 0; i < count; i++) {
262 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
263 print_request(" ", requests[i]);
264 xbt_dynar_push(comms, &requests[i]->pair);
270 index = SIMIX_network_waitany(comms);
274 xbt_dynar_free(&comms);
276 if(index != MPI_UNDEFINED) {
277 finish_wait(&requests[index], status);
283 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
288 index = smpi_mpi_waitany(count, requests, &stat);
289 if(index == MPI_UNDEFINED) {
292 if(status != MPI_STATUS_IGNORE) {
293 memcpy(&status[index], &stat, sizeof(stat));
295 // FIXME: check this -v
296 // Move the last request to the found position
297 requests[index] = requests[count - 1];
298 requests[count - 1] = MPI_REQUEST_NULL;
303 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
307 for(i = 0; i < incount; i++) {
308 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
309 SIMIX_communication_destroy(requests[i]->pair);
310 finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
318 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
319 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
320 nary_tree_bcast(buf, count, datatype, root, comm, 4);
323 void smpi_mpi_barrier(MPI_Comm comm) {
324 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
325 nary_tree_barrier(comm, 4);
328 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
329 int system_tag = 666;
330 int rank, size, src, index, sendsize, recvsize;
331 MPI_Request* requests;
333 rank = smpi_comm_rank(comm);
334 size = smpi_comm_size(comm);
336 // Send buffer to root
337 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
339 sendsize = smpi_datatype_size(sendtype);
340 recvsize = smpi_datatype_size(recvtype);
341 // Local copy from root
342 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
343 // Receive buffers from senders
344 requests = xbt_new(MPI_Request, size - 1);
346 for(src = 0; src < size; src++) {
348 requests[index] = smpi_irecv_init(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
352 // Wait for completion of irecv's.
353 smpi_mpi_startall(size - 1, requests);
354 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
359 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) {
360 int system_tag = 666;
361 int rank, size, src, index, sendsize;
362 MPI_Request* requests;
364 rank = smpi_comm_rank(comm);
365 size = smpi_comm_size(comm);
367 // Send buffer to root
368 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
370 sendsize = smpi_datatype_size(sendtype);
371 // Local copy from root
372 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
373 // Receive buffers from senders
374 requests = xbt_new(MPI_Request, size - 1);
376 for(src = 0; src < size; src++) {
378 requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
382 // Wait for completion of irecv's.
383 smpi_mpi_startall(size - 1, requests);
384 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
389 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
390 int system_tag = 666;
391 int rank, size, other, index, sendsize, recvsize;
392 MPI_Request* requests;
394 rank = smpi_comm_rank(comm);
395 size = smpi_comm_size(comm);
396 sendsize = smpi_datatype_size(sendtype);
397 recvsize = smpi_datatype_size(recvtype);
398 // Local copy from self
399 memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
400 // Send/Recv buffers to/from others;
401 requests = xbt_new(MPI_Request, 2 * (size - 1));
403 for(other = 0; other < size; other++) {
405 requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
407 requests[index] = smpi_irecv_init(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
411 // Wait for completion of all comms.
412 smpi_mpi_startall(2 * (size - 1), requests);
413 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
417 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
418 int system_tag = 666;
419 int rank, size, other, index, sendsize, recvsize;
420 MPI_Request* requests;
422 rank = smpi_comm_rank(comm);
423 size = smpi_comm_size(comm);
424 sendsize = smpi_datatype_size(sendtype);
425 recvsize = smpi_datatype_size(recvtype);
426 // Local copy from self
427 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
428 // Send buffers to others;
429 requests = xbt_new(MPI_Request, 2 * (size - 1));
431 for(other = 0; other < size; other++) {
433 requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
435 requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
439 // Wait for completion of all comms.
440 smpi_mpi_startall(2 * (size - 1), requests);
441 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
445 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
446 int system_tag = 666;
447 int rank, size, dst, index, sendsize, recvsize;
448 MPI_Request* requests;
450 rank = smpi_comm_rank(comm);
451 size = smpi_comm_size(comm);
453 // Recv buffer from root
454 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
456 sendsize = smpi_datatype_size(sendtype);
457 recvsize = smpi_datatype_size(recvtype);
458 // Local copy from root
459 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
460 // Send buffers to receivers
461 requests = xbt_new(MPI_Request, size - 1);
463 for(dst = 0; dst < size; dst++) {
465 requests[index] = smpi_isend_init(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
469 // Wait for completion of isend's.
470 smpi_mpi_startall(size - 1, requests);
471 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
476 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) {
477 int system_tag = 666;
478 int rank, size, dst, index, sendsize, recvsize;
479 MPI_Request* requests;
481 rank = smpi_comm_rank(comm);
482 size = smpi_comm_size(comm);
484 // Recv buffer from root
485 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
487 sendsize = smpi_datatype_size(sendtype);
488 recvsize = smpi_datatype_size(recvtype);
489 // Local copy from root
490 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
491 // Send buffers to receivers
492 requests = xbt_new(MPI_Request, size - 1);
494 for(dst = 0; dst < size; dst++) {
496 requests[index] = smpi_isend_init(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
500 // Wait for completion of isend's.
501 smpi_mpi_startall(size - 1, requests);
502 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
507 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
508 int system_tag = 666;
509 int rank, size, src, index, datasize;
510 MPI_Request* requests;
513 rank = smpi_comm_rank(comm);
514 size = smpi_comm_size(comm);
516 // Send buffer to root
517 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
519 datasize = smpi_datatype_size(datatype);
520 // Local copy from root
521 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
522 // Receive buffers from senders
523 //TODO: make a MPI_barrier here ?
524 requests = xbt_new(MPI_Request, size - 1);
525 tmpbufs = xbt_new(void*, size - 1);
527 for(src = 0; src < size; src++) {
529 tmpbufs[index] = xbt_malloc(count * datasize);
530 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
534 // Wait for completion of irecv's.
535 smpi_mpi_startall(size - 1, requests);
536 for(src = 0; src < size - 1; src++) {
537 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
538 if(index == MPI_UNDEFINED) {
541 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
543 for(index = 0; index < size - 1; index++) {
544 xbt_free(tmpbufs[index]);
551 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
552 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
553 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
556 FIXME: buggy implementation
558 int system_tag = 666;
559 int rank, size, other, index, datasize;
560 MPI_Request* requests;
563 rank = smpi_comm_rank(comm);
564 size = smpi_comm_size(comm);
565 datasize = smpi_datatype_size(datatype);
566 // Local copy from self
567 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
568 // Send/Recv buffers to/from others;
569 //TODO: make a MPI_barrier here ?
570 requests = xbt_new(MPI_Request, 2 * (size - 1));
571 tmpbufs = xbt_new(void*, size - 1);
573 for(other = 0; other < size; other++) {
575 tmpbufs[index / 2] = xbt_malloc(count * datasize);
576 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
577 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
581 // Wait for completion of all comms.
582 for(other = 0; other < 2 * (size - 1); other++) {
583 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
584 if(index == MPI_UNDEFINED) {
587 if((index & 1) == 1) {
588 // Request is odd: it's a irecv
589 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
592 for(index = 0; index < size - 1; index++) {
593 xbt_free(tmpbufs[index]);
600 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
601 int system_tag = 666;
602 int rank, size, other, index, datasize;
604 MPI_Request* requests;
607 rank = smpi_comm_rank(comm);
608 size = smpi_comm_size(comm);
609 datasize = smpi_datatype_size(datatype);
610 // Local copy from self
611 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
612 // Send/Recv buffers to/from others;
613 total = rank + (size - (rank + 1));
614 requests = xbt_new(MPI_Request, total);
615 tmpbufs = xbt_new(void*, rank);
617 for(other = 0; other < rank; other++) {
618 tmpbufs[index] = xbt_malloc(count * datasize);
619 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
622 for(other = rank + 1; other < size; other++) {
623 requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
626 // Wait for completion of all comms.
627 smpi_mpi_startall(size - 1, requests);
628 for(other = 0; other < total; other++) {
629 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
630 if(index == MPI_UNDEFINED) {
634 // #Request is below rank: it's a irecv
635 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
638 for(index = 0; index < size - 1; index++) {
639 xbt_free(tmpbufs[index]);