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 void smpi_process_init(int* argc, char*** argv) {
18 smpi_process_data_t data;
21 proc = SIMIX_process_self();
22 index = atoi((*argv)[1]);
23 data = smpi_process_remote_data(index);
24 SIMIX_process_set_data(proc, data);
27 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
28 (*argv)[(*argc) - 1] = NULL;
31 DEBUG2("<%d> New process in the game: %p", index, proc);
34 void smpi_process_destroy(void) {
35 int index = smpi_process_index();
37 DEBUG1("<%d> Process left the game", index);
40 /* MPI Low level calls */
41 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
44 request = xbt_new(s_smpi_mpi_request_t, 1);
46 request->src = smpi_comm_rank(comm);
49 request->size = smpi_datatype_size(datatype) * count;
50 request->complete = 0;
51 request->data = request;
52 smpi_process_post_send(comm, request);
53 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, NULL);
57 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
60 request = xbt_new(s_smpi_mpi_request_t, 1);
63 request->dst = smpi_comm_rank(comm);
65 request->size = smpi_datatype_size(datatype) * count;
66 request->complete = 0;
67 request->data = MPI_REQUEST_NULL;
68 smpi_process_post_recv(request);
69 request->pair = SIMIX_network_irecv(request->rdv, buf, &request->size);
73 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
76 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
77 smpi_mpi_wait(&request, status);
80 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
83 request = smpi_mpi_isend(buf, count, datatype, src, tag, comm);
84 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
87 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) {
88 MPI_Request requests[2];
91 requests[0] = smpi_mpi_isend(sendbuf, sendcount, sendtype, dst, sendtag, comm);
92 requests[1] = smpi_mpi_irecv(recvbuf, recvcount, recvtype, src, recvtag, comm);
93 smpi_mpi_waitall(2, requests, stats);
94 if(status != MPI_STATUS_IGNORE) {
95 // Copy receive status
96 memcpy(status, &stats[1], sizeof(MPI_Status));
100 static void finish_wait(MPI_Request* request, MPI_Status* status) {
101 MPI_Request data = (*request)->data;
103 xbt_assert0(data != MPI_REQUEST_NULL, "Erroneous situation");
104 if(status != MPI_STATUS_IGNORE) {
105 status->MPI_SOURCE = (*request)->src;
106 status->MPI_TAG = (*request)->tag;
107 status->MPI_ERROR = MPI_SUCCESS;
108 status->_count = (*request)->size; // size in bytes
109 status->_cancelled = 0; // FIXME: cancellation of requests not handled yet
111 DEBUG3("finishing wait for %p [data = %p, complete = %d]", *request, data, data->complete);
112 // data == *request if sender is first to finish its wait
113 // data != *request if receiver is first to finish its wait
114 if(data->complete == 0) {
115 // first arrives here
117 if(data != *request) {
118 // receveiver cleans its part
122 // second arrives here
123 if(data != *request) {
124 // receiver cleans everything
127 SIMIX_rdv_destroy((*request)->rdv);
130 *request = MPI_REQUEST_NULL;
133 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
134 MPI_Request data = (*request)->data;
135 int flag = data && data->complete == 1;
138 SIMIX_communication_destroy((*request)->pair);
139 finish_wait(request, status);
144 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
148 *index = MPI_UNDEFINED;
150 for(i = 0; i < count; i++) {
151 if(requests[i] != MPI_REQUEST_NULL) {
152 data = requests[i]->data;
153 if(data != MPI_REQUEST_NULL && data->complete == 1) {
154 SIMIX_communication_destroy(requests[i]->pair);
155 finish_wait(&requests[i], status);
166 void smpi_mpi_get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
167 int size = smpi_datatype_size(datatype);
168 *count = (int)(status->_count / size);
169 if ( (int)((*count) * size) != status->_count )
170 *count = MPI_UNDEFINED;
174 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
175 MPI_Request data = (*request)->data;
177 DEBUG6("wait for request %p (%p: %p) [src = %d, dst = %d, tag = %d]",
178 *request, (*request)->pair, data, (*request)->src, (*request)->dst, (*request)->tag);
179 // data is null if receiver waits before sender enters the rdv
180 if(data == MPI_REQUEST_NULL || data->complete == 0) {
181 SIMIX_network_wait((*request)->pair, -1.0);
183 SIMIX_communication_destroy((*request)->pair);
185 finish_wait(request, status);
188 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
194 index = MPI_UNDEFINED;
196 // First check for already completed requests
197 for(i = 0; i < count; i++) {
198 if(requests[i] != MPI_REQUEST_NULL) {
199 data = requests[i]->data;
200 if(data != MPI_REQUEST_NULL && data->complete == 1) {
202 SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
207 if(index == MPI_UNDEFINED) {
208 // Otherwise, wait for a request to complete
209 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
210 map = xbt_new(int, count);
212 DEBUG0("Wait for one of");
213 for(i = 0; i < count; i++) {
214 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
215 DEBUG4(" request %p [src = %d, dst = %d, tag = %d]",
216 requests[i], requests[i]->src, requests[i]->dst, requests[i]->tag);
217 xbt_dynar_push(comms, &requests[i]->pair);
223 index = SIMIX_network_waitany(comms);
227 xbt_dynar_free_container(&comms);
229 if(index != MPI_UNDEFINED) {
230 finish_wait(&requests[index], status);
236 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
241 index = smpi_mpi_waitany(count, requests, &stat);
242 if(index == MPI_UNDEFINED) {
245 if(status != MPI_STATUS_IGNORE) {
246 memcpy(&status[index], &stat, sizeof(stat));
248 // Move the last request to the found position
249 requests[index] = requests[count - 1];
250 requests[count - 1] = MPI_REQUEST_NULL;
255 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
260 for(i = 0; i < incount; i++) {
261 if(requests[i] != MPI_REQUEST_NULL) {
262 data = requests[i]->data;
263 if(data != MPI_REQUEST_NULL && data->complete == 1) {
264 SIMIX_communication_destroy(requests[i]->pair);
265 finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
274 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
275 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
276 nary_tree_bcast(buf, count, datatype, root, comm, 4);
279 void smpi_mpi_barrier(MPI_Comm comm) {
280 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
281 nary_tree_barrier(comm, 4);
284 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
285 int system_tag = 666;
286 int rank, size, src, index, sendsize, recvsize;
287 MPI_Request* requests;
289 rank = smpi_comm_rank(comm);
290 size = smpi_comm_size(comm);
292 // Send buffer to root
293 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
295 sendsize = smpi_datatype_size(sendtype);
296 recvsize = smpi_datatype_size(recvtype);
297 // Local copy from root
298 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
299 // Receive buffers from senders
300 requests = xbt_new(MPI_Request, size - 1);
302 for(src = 0; src < size; src++) {
304 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
308 // Wait for completion of irecv's.
309 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
314 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) {
315 int system_tag = 666;
316 int rank, size, src, index, sendsize;
317 MPI_Request* requests;
319 rank = smpi_comm_rank(comm);
320 size = smpi_comm_size(comm);
322 // Send buffer to root
323 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
325 sendsize = smpi_datatype_size(sendtype);
326 // Local copy from root
327 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
328 // Receive buffers from senders
329 requests = xbt_new(MPI_Request, size - 1);
331 for(src = 0; src < size; src++) {
333 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
337 // Wait for completion of irecv's.
338 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
343 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
344 int system_tag = 666;
345 int rank, size, other, index, sendsize, recvsize;
346 MPI_Request* requests;
348 rank = smpi_comm_rank(comm);
349 size = smpi_comm_size(comm);
350 sendsize = smpi_datatype_size(sendtype);
351 recvsize = smpi_datatype_size(recvtype);
352 // Local copy from self
353 memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
354 // Send/Recv buffers to/from others;
355 requests = xbt_new(MPI_Request, 2 * (size - 1));
357 for(other = 0; other < size; other++) {
359 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
361 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
365 // Wait for completion of all comms.
366 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
370 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
371 int system_tag = 666;
372 int rank, size, other, index, sendsize, recvsize;
373 MPI_Request* requests;
375 rank = smpi_comm_rank(comm);
376 size = smpi_comm_size(comm);
377 sendsize = smpi_datatype_size(sendtype);
378 recvsize = smpi_datatype_size(recvtype);
379 // Local copy from self
380 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
381 // Send buffers to others;
382 requests = xbt_new(MPI_Request, 2 * (size - 1));
384 for(other = 0; other < size; other++) {
386 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
388 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
392 // Wait for completion of all comms.
393 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
397 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
398 int system_tag = 666;
399 int rank, size, dst, index, sendsize, recvsize;
400 MPI_Request* requests;
402 rank = smpi_comm_rank(comm);
403 size = smpi_comm_size(comm);
405 // Recv buffer from root
406 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
408 sendsize = smpi_datatype_size(sendtype);
409 recvsize = smpi_datatype_size(recvtype);
410 // Local copy from root
411 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
412 // Send buffers to receivers
413 requests = xbt_new(MPI_Request, size - 1);
415 for(dst = 0; dst < size; dst++) {
417 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
421 // Wait for completion of isend's.
422 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
427 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) {
428 int system_tag = 666;
429 int rank, size, dst, index, sendsize, recvsize;
430 MPI_Request* requests;
432 rank = smpi_comm_rank(comm);
433 size = smpi_comm_size(comm);
435 // Recv buffer from root
436 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
438 sendsize = smpi_datatype_size(sendtype);
439 recvsize = smpi_datatype_size(recvtype);
440 // Local copy from root
441 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
442 // Send buffers to receivers
443 requests = xbt_new(MPI_Request, size - 1);
445 for(dst = 0; dst < size; dst++) {
447 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
451 // Wait for completion of isend's.
452 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
457 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
458 int system_tag = 666;
459 int rank, size, src, index, datasize;
460 MPI_Request* requests;
463 rank = smpi_comm_rank(comm);
464 size = smpi_comm_size(comm);
466 // Send buffer to root
467 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
469 datasize = smpi_datatype_size(datatype);
470 // Local copy from root
471 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
472 // Receive buffers from senders
473 //TODO: make a MPI_barrier here ?
474 requests = xbt_new(MPI_Request, size - 1);
475 tmpbufs = xbt_new(void*, size - 1);
477 for(src = 0; src < size; src++) {
479 tmpbufs[index] = xbt_malloc(count * datasize);
480 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, src, system_tag, comm);
484 // Wait for completion of irecv's.
485 for(src = 0; src < size - 1; src++) {
486 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
487 if(index == MPI_UNDEFINED) {
490 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
492 for(index = 0; index < size - 1; index++) {
493 xbt_free(tmpbufs[index]);
500 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
501 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
502 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
505 FIXME: buggy implementation
507 int system_tag = 666;
508 int rank, size, other, index, datasize;
509 MPI_Request* requests;
512 rank = smpi_comm_rank(comm);
513 size = smpi_comm_size(comm);
514 datasize = smpi_datatype_size(datatype);
515 // Local copy from self
516 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
517 // Send/Recv buffers to/from others;
518 //TODO: make a MPI_barrier here ?
519 requests = xbt_new(MPI_Request, 2 * (size - 1));
520 tmpbufs = xbt_new(void*, size - 1);
522 for(other = 0; other < size; other++) {
524 tmpbufs[index / 2] = xbt_malloc(count * datasize);
525 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
526 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
530 // Wait for completion of all comms.
531 for(other = 0; other < 2 * (size - 1); other++) {
532 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
533 if(index == MPI_UNDEFINED) {
536 if((index & 1) == 1) {
537 // Request is odd: it's a irecv
538 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
541 for(index = 0; index < size - 1; index++) {
542 xbt_free(tmpbufs[index]);
549 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
550 int system_tag = 666;
551 int rank, size, other, index, datasize;
553 MPI_Request* requests;
556 rank = smpi_comm_rank(comm);
557 size = smpi_comm_size(comm);
558 datasize = smpi_datatype_size(datatype);
559 // Local copy from self
560 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
561 // Send/Recv buffers to/from others;
562 total = rank + (size - (rank + 1));
563 requests = xbt_new(MPI_Request, total);
564 tmpbufs = xbt_new(void*, rank);
566 for(other = 0; other < rank; other++) {
567 tmpbufs[index] = xbt_malloc(count * datasize);
568 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, other, system_tag, comm);
571 for(other = rank + 1; other < size; other++) {
572 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
575 // Wait for completion of all comms.
576 for(other = 0; other < total; other++) {
577 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
578 if(index == MPI_UNDEFINED) {
582 // #Request is below rank: it's a irecv
583 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
586 for(index = 0; index < size - 1; index++) {
587 xbt_free(tmpbufs[index]);