1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
5 
6 #include "mpiimpl.h"
7 
8 /* Local reduce remote send
9  *
10  * Remote group does a local intracommunicator reduce to rank 0. Rank
11  * 0 then sends data to root.
12  */
13 
MPIR_Ireduce_inter_sched_local_reduce_remote_send(const void * sendbuf,void * recvbuf,int count,MPI_Datatype datatype,MPI_Op op,int root,MPIR_Comm * comm_ptr,MPIR_Sched_t s)14 int MPIR_Ireduce_inter_sched_local_reduce_remote_send(const void *sendbuf, void *recvbuf, int count,
15                                                       MPI_Datatype datatype, MPI_Op op, int root,
16                                                       MPIR_Comm * comm_ptr, MPIR_Sched_t s)
17 {
18     int mpi_errno = MPI_SUCCESS;
19     int rank;
20     MPI_Aint true_lb, true_extent, extent;
21     void *tmp_buf = NULL;
22     MPIR_SCHED_CHKPMEM_DECL(1);
23 
24     MPIR_Assert(comm_ptr->comm_kind == MPIR_COMM_KIND__INTERCOMM);
25 
26     if (root == MPI_PROC_NULL) {
27         /* local processes other than root do nothing */
28         return MPI_SUCCESS;
29     }
30 
31     if (root == MPI_ROOT) {
32         /* root receives data from rank 0 on remote group */
33         mpi_errno = MPIR_Sched_recv(recvbuf, count, datatype, 0, comm_ptr, s);
34         MPIR_ERR_CHECK(mpi_errno);
35         mpi_errno = MPIR_Sched_barrier(s);
36         MPIR_ERR_CHECK(mpi_errno);
37     } else {
38         /* remote group. Rank 0 allocates temporary buffer, does
39          * local intracommunicator reduce, and then sends the data
40          * to root. */
41         rank = comm_ptr->rank;
42 
43         if (rank == 0) {
44             MPIR_Type_get_true_extent_impl(datatype, &true_lb, &true_extent);
45 
46             MPIR_Datatype_get_extent_macro(datatype, extent);
47             MPIR_SCHED_CHKPMEM_MALLOC(tmp_buf, void *, count * (MPL_MAX(extent, true_extent)),
48                                       mpi_errno, "temporary buffer", MPL_MEM_BUFFER);
49             /* adjust for potential negative lower bound in datatype */
50             tmp_buf = (void *) ((char *) tmp_buf - true_lb);
51         }
52 
53         if (!comm_ptr->local_comm) {
54             mpi_errno = MPII_Setup_intercomm_localcomm(comm_ptr);
55             MPIR_ERR_CHECK(mpi_errno);
56         }
57 
58         mpi_errno =
59             MPIR_Ireduce_sched_auto(sendbuf, tmp_buf, count, datatype, op, 0, comm_ptr->local_comm,
60                                     s);
61         MPIR_ERR_CHECK(mpi_errno);
62         mpi_errno = MPIR_Sched_barrier(s);
63         MPIR_ERR_CHECK(mpi_errno);
64 
65         if (rank == 0) {
66             mpi_errno = MPIR_Sched_send(tmp_buf, count, datatype, root, comm_ptr, s);
67             MPIR_ERR_CHECK(mpi_errno);
68             mpi_errno = MPIR_Sched_barrier(s);
69             MPIR_ERR_CHECK(mpi_errno);
70         }
71     }
72 
73     MPIR_SCHED_CHKPMEM_COMMIT(s);
74   fn_exit:
75     return mpi_errno;
76   fn_fail:
77     MPIR_SCHED_CHKPMEM_REAP(s);
78     goto fn_exit;
79 }
80