-found_request:
-#ifdef DEBUG_STEPH
- print_req( requests[cpt] );
-#endif
- requests[*index]->consumed = 1;
-#ifdef DEBUG_STEPH
- print_req( requests[cpt] );
- printf("...accessing *req[%d]->consumed\n",cpt);
-#endif
- if (NULL != status) {
- status->MPI_SOURCE = requests[*index]->src;
- status->MPI_TAG = requests[*index]->tag;
- status->MPI_ERROR = MPI_SUCCESS;
+void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
+ MPI_Datatype datatype, MPI_Op op, int root,
+ MPI_Comm comm)
+{
+ int system_tag = 666;
+ int rank, size, src, index;
+ MPI_Aint lb = 0, dataext = 0;
+ MPI_Request *requests;
+ void **tmpbufs;
+
+ rank = smpi_comm_rank(comm);
+ size = smpi_comm_size(comm);
+ if(rank != root) {
+ // Send buffer to root
+ smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
+ } else {
+ // FIXME: check for errors
+ smpi_datatype_extent(datatype, &lb, &dataext);
+ // Local copy from root
+ smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
+ // Receive buffers from senders
+ //TODO: make a MPI_barrier here ?
+ requests = xbt_new(MPI_Request, size - 1);
+ tmpbufs = xbt_new(void *, size - 1);
+ index = 0;
+ for(src = 0; src < size; src++) {
+ if(src != root) {
+ // FIXME: possibly overkill we we have contiguous/noncontiguous data
+ // mapping...
+ tmpbufs[index] = xbt_malloc(count * dataext);
+ requests[index] =
+ smpi_irecv_init(tmpbufs[index], count, datatype, src,
+ system_tag, comm);
+ index++;
+ }
+ }
+ // Wait for completion of irecv's.
+ smpi_mpi_startall(size - 1, requests);
+ for(src = 0; src < size - 1; src++) {
+ index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
+ XBT_VERB("finished waiting any request with index %d", index);
+ if(index == MPI_UNDEFINED) {
+ break;
+ }
+ smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
+ }
+ for(index = 0; index < size - 1; index++) {
+ xbt_free(tmpbufs[index]);
+ }
+ xbt_free(tmpbufs);
+ xbt_free(requests);