+int smpi_mpi_waitany(int count, MPI_Request requests[],
+ MPI_Status * status)
+{
+ xbt_dynar_t comms;
+ int i, size, index;
+ int *map;
+
+ index = MPI_UNDEFINED;
+ if(count > 0) {
+ // Wait for a request to complete
+ comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
+ map = xbt_new(int, count);
+ size = 0;
+ XBT_DEBUG("Wait for one of");
+ for(i = 0; i < count; i++) {
+ if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
+ print_request(" ", requests[i]);
+ xbt_dynar_push(comms, &requests[i]->action);
+ map[size] = i;
+ size++;
+ }
+ }
+ if(size > 0) {
+ i = SIMIX_req_comm_waitany(comms);
+ // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
+ if (i != MPI_UNDEFINED) {
+ index = map[i];
+ finish_wait(&requests[index], status);
+ }
+ }
+ xbt_free(map);
+ xbt_dynar_free(&comms);
+ }
+ return index;
+}
+
+void smpi_mpi_waitall(int count, MPI_Request requests[],
+ MPI_Status status[])
+{
+ int index, c;
+ MPI_Status stat;
+ MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
+
+ for(c = 0; c < count; c++) {
+ if(MC_IS_ENABLED) {
+ smpi_mpi_wait(&requests[c], pstat);
+ index = c;
+ } else {
+ index = smpi_mpi_waitany(count, requests, pstat);
+ if(index == MPI_UNDEFINED) {
+ break;
+ }
+ }
+ if(status != MPI_STATUS_IGNORE) {
+ memcpy(&status[index], pstat, sizeof(*pstat));
+ }
+ }
+}
+
+int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
+ MPI_Status status[])
+{
+ int i, count, index;
+
+ count = 0;
+ for(i = 0; i < incount; i++) {
+ if(smpi_mpi_testany(incount, requests, &index, status)) {
+ indices[count] = index;
+ count++;
+ }
+ }
+ return count;
+}
+
+void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
+ MPI_Comm comm)
+{
+ // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
+ nary_tree_bcast(buf, count, datatype, root, comm, 4);
+}
+
+void smpi_mpi_barrier(MPI_Comm comm)
+{
+ // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
+ nary_tree_barrier(comm, 4);
+}
+
+void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
+ void *recvbuf, int recvcount, MPI_Datatype recvtype,
+ int root, MPI_Comm comm)
+{
+ int system_tag = 666;
+ int rank, size, src, index;
+ MPI_Aint lb = 0, recvext = 0;
+ MPI_Request *requests;
+
+ rank = smpi_comm_rank(comm);
+ size = smpi_comm_size(comm);
+ if(rank != root) {
+ // Send buffer to root
+ smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
+ } else {
+ // FIXME: check for errors
+ smpi_datatype_extent(recvtype, &lb, &recvext);
+ // Local copy from root
+ smpi_datatype_copy(sendbuf, sendcount, sendtype,
+ (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
+ // Receive buffers from senders
+ requests = xbt_new(MPI_Request, size - 1);
+ index = 0;
+ for(src = 0; src < size; src++) {
+ if(src != root) {
+ requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
+ recvcount, recvtype,
+ src, system_tag, comm);
+ index++;
+ }
+ }
+ // Wait for completion of irecv's.
+ smpi_mpi_startall(size - 1, requests);
+ smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
+ xbt_free(requests);
+ }
+}