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 void smpi_process_init(int* argc, char*** argv) {
24 smpi_process_data_t data;
27 proc = SIMIX_process_self();
28 index = atoi((*argv)[1]);
29 data = smpi_process_remote_data(index);
30 SIMIX_process_set_data(proc, data);
33 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
34 (*argv)[(*argc) - 1] = NULL;
37 DEBUG2("<%d> New process in the game: %p", index, proc);
40 void smpi_process_destroy(void) {
41 int index = smpi_process_index();
43 DEBUG1("<%d> Process left the game", index);
46 static MPI_Request build_request(void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags) {
49 request = xbt_new(s_smpi_mpi_request_t, 1);
51 request->size = smpi_datatype_size(datatype) * count;
58 request->complete = 0;
59 request->match = MPI_REQUEST_NULL;
60 request->flags = flags;
64 /* MPI Low level calls */
65 MPI_Request smpi_mpi_send_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
66 MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, PERSISTENT | SEND);
71 MPI_Request smpi_mpi_recv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
72 MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, PERSISTENT | RECV);
77 void smpi_mpi_start(MPI_Request request) {
78 xbt_assert0(request->complete == 0, "Cannot start a non-finished communication");
79 if((request->flags & RECV) == RECV) {
80 smpi_process_post_recv(request);
81 print_request("New recv", request);
82 request->pair = SIMIX_network_irecv(request->rdv, request->buf, &request->size);
84 smpi_process_post_send(request->comm, request); // FIXME
85 print_request("New send", request);
86 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, request->buf, request->size, NULL);
90 void smpi_mpi_startall(int count, MPI_Request* requests) {
93 for(i = 0; i < count; i++) {
94 smpi_mpi_start(requests[i]);
98 void smpi_mpi_request_free(MPI_Request* request) {
100 *request = MPI_REQUEST_NULL;
103 MPI_Request smpi_isend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
104 MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, NON_PERSISTENT | SEND);
109 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
110 MPI_Request request = smpi_isend_init(buf, count, datatype, dst, tag, comm);
112 smpi_mpi_start(request);
116 MPI_Request smpi_irecv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
117 MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, NON_PERSISTENT | RECV);
122 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
123 MPI_Request request = smpi_irecv_init(buf, count, datatype, src, tag, comm);
125 smpi_mpi_start(request);
129 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
132 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
133 smpi_mpi_wait(&request, status);
136 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
139 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
140 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
143 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) {
144 MPI_Request requests[2];
147 requests[0] = smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
148 requests[1] = smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
149 smpi_mpi_startall(2, requests);
150 smpi_mpi_waitall(2, requests, stats);
151 if(status != MPI_STATUS_IGNORE) {
152 // Copy receive status
153 memcpy(status, &stats[1], sizeof(MPI_Status));
157 static void finish_wait(MPI_Request* request, MPI_Status* status) {
158 if(status != MPI_STATUS_IGNORE) {
159 status->MPI_SOURCE = (*request)->src;
160 status->MPI_TAG = (*request)->tag;
161 status->MPI_ERROR = MPI_SUCCESS;
163 print_request("finishing wait", *request);
164 if((*request)->complete == 1) {
165 SIMIX_rdv_destroy((*request)->rdv);
167 (*request)->match->complete = 1;
168 (*request)->match->match = MPI_REQUEST_NULL;
170 if(((*request)->flags & NON_PERSISTENT) == NON_PERSISTENT) {
171 smpi_mpi_request_free(request);
173 (*request)->rdv = NULL;
174 (*request)->pair = NULL;
178 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
179 int flag = (*request)->complete;
182 smpi_mpi_wait(request, status);
187 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
190 *index = MPI_UNDEFINED;
192 for(i = 0; i < count; i++) {
193 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
194 smpi_mpi_wait(&requests[i], status);
203 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
204 print_request("wait", *request);
205 SIMIX_network_wait((*request)->pair, -1.0);
206 finish_wait(request, status);
209 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
214 index = MPI_UNDEFINED;
216 // First check for already completed requests
217 for(i = 0; i < count; i++) {
218 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
220 smpi_mpi_wait(&requests[index], status);
224 if(index == MPI_UNDEFINED) {
225 // Otherwise, wait for a request to complete
226 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
227 map = xbt_new(int, count);
229 DEBUG0("Wait for one of");
230 for(i = 0; i < count; i++) {
231 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
232 print_request(" ", requests[i]);
233 xbt_dynar_push(comms, &requests[i]->pair);
239 index = SIMIX_network_waitany(comms);
241 finish_wait(&requests[index], status);
244 xbt_dynar_free(&comms);
250 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
255 index = smpi_mpi_waitany(count, requests, &stat);
256 if(index == MPI_UNDEFINED) {
259 if(status != MPI_STATUS_IGNORE) {
260 memcpy(&status[index], &stat, sizeof(stat));
262 // FIXME: check this -v
263 // Move the last request to the found position
264 requests[index] = requests[count - 1];
265 requests[count - 1] = MPI_REQUEST_NULL;
270 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
274 for(i = 0; i < incount; i++) {
275 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
276 smpi_mpi_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
284 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
285 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
286 nary_tree_bcast(buf, count, datatype, root, comm, 4);
289 void smpi_mpi_barrier(MPI_Comm comm) {
290 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
291 nary_tree_barrier(comm, 4);
294 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
295 int system_tag = 666;
296 int rank, size, src, index, sendsize, recvsize;
297 MPI_Request* requests;
299 rank = smpi_comm_rank(comm);
300 size = smpi_comm_size(comm);
302 // Send buffer to root
303 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
305 sendsize = smpi_datatype_size(sendtype);
306 recvsize = smpi_datatype_size(recvtype);
307 // Local copy from root
308 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
309 // Receive buffers from senders
310 requests = xbt_new(MPI_Request, size - 1);
312 for(src = 0; src < size; src++) {
314 requests[index] = smpi_irecv_init(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
318 // Wait for completion of irecv's.
319 smpi_mpi_startall(size - 1, requests);
320 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
325 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) {
326 int system_tag = 666;
327 int rank, size, src, index, sendsize;
328 MPI_Request* requests;
330 rank = smpi_comm_rank(comm);
331 size = smpi_comm_size(comm);
333 // Send buffer to root
334 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
336 sendsize = smpi_datatype_size(sendtype);
337 // Local copy from root
338 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
339 // Receive buffers from senders
340 requests = xbt_new(MPI_Request, size - 1);
342 for(src = 0; src < size; src++) {
344 requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
348 // Wait for completion of irecv's.
349 smpi_mpi_startall(size - 1, requests);
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_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
373 requests[index] = smpi_irecv_init(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
377 // Wait for completion of all comms.
378 smpi_mpi_startall(2 * (size - 1), requests);
379 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
383 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
384 int system_tag = 666;
385 int rank, size, other, index, sendsize, recvsize;
386 MPI_Request* requests;
388 rank = smpi_comm_rank(comm);
389 size = smpi_comm_size(comm);
390 sendsize = smpi_datatype_size(sendtype);
391 recvsize = smpi_datatype_size(recvtype);
392 // Local copy from self
393 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
394 // Send buffers to others;
395 requests = xbt_new(MPI_Request, 2 * (size - 1));
397 for(other = 0; other < size; other++) {
399 requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
401 requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
405 // Wait for completion of all comms.
406 smpi_mpi_startall(2 * (size - 1), requests);
407 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
411 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
412 int system_tag = 666;
413 int rank, size, dst, index, sendsize, recvsize;
414 MPI_Request* requests;
416 rank = smpi_comm_rank(comm);
417 size = smpi_comm_size(comm);
419 // Recv buffer from root
420 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
422 sendsize = smpi_datatype_size(sendtype);
423 recvsize = smpi_datatype_size(recvtype);
424 // Local copy from root
425 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
426 // Send buffers to receivers
427 requests = xbt_new(MPI_Request, size - 1);
429 for(dst = 0; dst < size; dst++) {
431 requests[index] = smpi_isend_init(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
435 // Wait for completion of isend's.
436 smpi_mpi_startall(size - 1, requests);
437 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
442 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) {
443 int system_tag = 666;
444 int rank, size, dst, index, sendsize, recvsize;
445 MPI_Request* requests;
447 rank = smpi_comm_rank(comm);
448 size = smpi_comm_size(comm);
450 // Recv buffer from root
451 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
453 sendsize = smpi_datatype_size(sendtype);
454 recvsize = smpi_datatype_size(recvtype);
455 // Local copy from root
456 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
457 // Send buffers to receivers
458 requests = xbt_new(MPI_Request, size - 1);
460 for(dst = 0; dst < size; dst++) {
462 requests[index] = smpi_isend_init(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
466 // Wait for completion of isend's.
467 smpi_mpi_startall(size - 1, requests);
468 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
473 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
474 int system_tag = 666;
475 int rank, size, src, index, datasize;
476 MPI_Request* requests;
479 rank = smpi_comm_rank(comm);
480 size = smpi_comm_size(comm);
482 // Send buffer to root
483 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
485 datasize = smpi_datatype_size(datatype);
486 // Local copy from root
487 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
488 // Receive buffers from senders
489 //TODO: make a MPI_barrier here ?
490 requests = xbt_new(MPI_Request, size - 1);
491 tmpbufs = xbt_new(void*, size - 1);
493 for(src = 0; src < size; src++) {
495 tmpbufs[index] = xbt_malloc(count * datasize);
496 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
500 // Wait for completion of irecv's.
501 smpi_mpi_startall(size - 1, requests);
502 for(src = 0; src < size - 1; src++) {
503 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
504 if(index == MPI_UNDEFINED) {
507 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
509 for(index = 0; index < size - 1; index++) {
510 xbt_free(tmpbufs[index]);
517 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
518 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
519 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
522 FIXME: buggy implementation
524 int system_tag = 666;
525 int rank, size, other, index, datasize;
526 MPI_Request* requests;
529 rank = smpi_comm_rank(comm);
530 size = smpi_comm_size(comm);
531 datasize = smpi_datatype_size(datatype);
532 // Local copy from self
533 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
534 // Send/Recv buffers to/from others;
535 //TODO: make a MPI_barrier here ?
536 requests = xbt_new(MPI_Request, 2 * (size - 1));
537 tmpbufs = xbt_new(void*, size - 1);
539 for(other = 0; other < size; other++) {
541 tmpbufs[index / 2] = xbt_malloc(count * datasize);
542 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
543 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
547 // Wait for completion of all comms.
548 for(other = 0; other < 2 * (size - 1); other++) {
549 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
550 if(index == MPI_UNDEFINED) {
553 if((index & 1) == 1) {
554 // Request is odd: it's a irecv
555 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
558 for(index = 0; index < size - 1; index++) {
559 xbt_free(tmpbufs[index]);
566 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
567 int system_tag = 666;
568 int rank, size, other, index, datasize;
570 MPI_Request* requests;
573 rank = smpi_comm_rank(comm);
574 size = smpi_comm_size(comm);
575 datasize = smpi_datatype_size(datatype);
576 // Local copy from self
577 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
578 // Send/Recv buffers to/from others;
579 total = rank + (size - (rank + 1));
580 requests = xbt_new(MPI_Request, total);
581 tmpbufs = xbt_new(void*, rank);
583 for(other = 0; other < rank; other++) {
584 tmpbufs[index] = xbt_malloc(count * datasize);
585 requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
588 for(other = rank + 1; other < size; other++) {
589 requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
592 // Wait for completion of all comms.
593 smpi_mpi_startall(size - 1, requests);
594 for(other = 0; other < total; other++) {
595 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
596 if(index == MPI_UNDEFINED) {
600 // #Request is below rank: it's a irecv
601 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
604 for(index = 0; index < size - 1; index++) {
605 xbt_free(tmpbufs[index]);