- /* The following reduction is done here instead of after
- the MPIC_Sendrecv_ft or MPIC_Recv_ft above. This is
- because to do it above, in the noncommutative
- case, we would need an extra temp buffer so as not to
- overwrite temp_recvbuf, because temp_recvbuf may have
- to be communicated to other processes in the
- non-power-of-two case. To avoid that extra allocation,
- we do the reduce here. */
- if (received) {
- if (is_commutative || (dst_tree_root < my_tree_root)) {
- {
- if (op != MPI_OP_NULL)
- op->apply(tmp_recvbuf, tmp_results, &blklens[0], datatype);
- if (op != MPI_OP_NULL)
- op->apply(((char*)tmp_recvbuf + dis[1] * extent), ((char*)tmp_results + dis[1] * extent),
- &blklens[1], datatype);
- }
- }
- else {
- {
- if (op != MPI_OP_NULL)
- op->apply(tmp_results, tmp_recvbuf, &blklens[0], datatype);
- if (op != MPI_OP_NULL)
- op->apply(((char*)tmp_results + dis[1] * extent), ((char*)tmp_recvbuf + dis[1] * extent),
- &blklens[1], datatype);
- }
- /* copy result back into tmp_results */
- mpi_errno = Datatype::copy(tmp_recvbuf, 1, recvtype,
- tmp_results, 1, recvtype);
- if (mpi_errno) return(mpi_errno);
- }
- }
+ /* need to allocate another temporary buffer to accumulate
+ results */
+ unsigned char* tmp_results = smpi_get_tmp_sendbuffer(total_count * std::max(true_extent, extent));
+ /* adjust for potential negative lower bound in datatype */
+ tmp_results = tmp_results - true_lb;
+
+ /* copy sendbuf into tmp_results */
+ if (sendbuf != MPI_IN_PLACE)
+ mpi_errno = Datatype::copy(sendbuf, total_count, datatype, tmp_results, total_count, datatype);
+ else
+ mpi_errno = Datatype::copy(recvbuf, total_count, datatype, tmp_results, total_count, datatype);
+
+ if (mpi_errno)
+ return (mpi_errno);
+
+ mask = 0x1;
+ i = 0;
+ while (mask < comm_size) {
+ dst = rank ^ mask;
+
+ dst_tree_root = dst >> i;
+ dst_tree_root <<= i;
+
+ my_tree_root = rank >> i;
+ my_tree_root <<= i;
+
+ /* At step 1, processes exchange (n-n/p) amount of
+ data; at step 2, (n-2n/p) amount of data; at step 3, (n-4n/p)
+ amount of data, and so forth. We use derived datatypes for this.
+
+ At each step, a process does not need to send data
+ indexed from my_tree_root to
+ my_tree_root+mask-1. Similarly, a process won't receive
+ data indexed from dst_tree_root to dst_tree_root+mask-1. */
+
+ /* calculate sendtype */
+ blklens[0] = blklens[1] = 0;
+ for (j = 0; j < my_tree_root; j++)
+ blklens[0] += recvcounts[j];
+ for (j = my_tree_root + mask; j < comm_size; j++)
+ blklens[1] += recvcounts[j];
+
+ dis[0] = 0;
+ dis[1] = blklens[0];
+ for (j = my_tree_root; (j < my_tree_root + mask) && (j < comm_size); j++)
+ dis[1] += recvcounts[j];
+
+ mpi_errno = Datatype::create_indexed(2, blklens, dis, datatype, &sendtype);
+ if (mpi_errno)
+ return (mpi_errno);
+
+ sendtype->commit();
+
+ /* calculate recvtype */
+ blklens[0] = blklens[1] = 0;
+ for (j = 0; j < dst_tree_root && j < comm_size; j++)
+ blklens[0] += recvcounts[j];
+ for (j = dst_tree_root + mask; j < comm_size; j++)
+ blklens[1] += recvcounts[j];
+
+ dis[0] = 0;
+ dis[1] = blklens[0];
+ for (j = dst_tree_root; (j < dst_tree_root + mask) && (j < comm_size); j++)
+ dis[1] += recvcounts[j];
+
+ mpi_errno = Datatype::create_indexed(2, blklens, dis, datatype, &recvtype);
+ if (mpi_errno)
+ return (mpi_errno);
+
+ recvtype->commit();
+
+ received = 0;
+ if (dst < comm_size) {
+ /* tmp_results contains data to be sent in each step. Data is
+ received in tmp_recvbuf and then accumulated into
+ tmp_results. accumulation is done later below. */
+
+ Request::sendrecv(tmp_results, 1, sendtype, dst, COLL_TAG_SCATTER, tmp_recvbuf, 1, recvtype, dst,
+ COLL_TAG_SCATTER, comm, MPI_STATUS_IGNORE);
+ received = 1;
+ }
+
+ /* if some processes in this process's subtree in this step
+ did not have any destination process to communicate with
+ because of non-power-of-two, we need to send them the
+ result. We use a logarithmic recursive-halfing algorithm
+ for this. */
+
+ if (dst_tree_root + mask > comm_size) {
+ nprocs_completed = comm_size - my_tree_root - mask;
+ /* nprocs_completed is the number of processes in this
+ subtree that have all the data. Send data to others
+ in a tree fashion. First find root of current tree
+ that is being divided into two. k is the number of
+ least-significant bits in this process's rank that
+ must be zeroed out to find the rank of the root */
+ j = mask;
+ k = 0;
+ while (j) {
+ j >>= 1;
+ k++;
+ }
+ k--;
+
+ tmp_mask = mask >> 1;
+ while (tmp_mask) {
+ dst = rank ^ tmp_mask;
+
+ tree_root = rank >> k;
+ tree_root <<= k;
+
+ /* send only if this proc has data and destination
+ doesn't have data. at any step, multiple processes
+ can send if they have the data */
+ if ((dst > rank) && (rank < tree_root + nprocs_completed) && (dst >= tree_root + nprocs_completed)) {
+ /* send the current result */
+ Request::send(tmp_recvbuf, 1, recvtype, dst, COLL_TAG_SCATTER, comm);
+ }
+ /* recv only if this proc. doesn't have data and sender
+ has data */
+ else if ((dst < rank) && (dst < tree_root + nprocs_completed) && (rank >= tree_root + nprocs_completed)) {
+ Request::recv(tmp_recvbuf, 1, recvtype, dst, COLL_TAG_SCATTER, comm, MPI_STATUS_IGNORE);
+ received = 1;
+ }
+ tmp_mask >>= 1;
+ k--;
+ }
+ }
+
+ /* The following reduction is done here instead of after
+ the MPIC_Sendrecv_ft or MPIC_Recv_ft above. This is
+ because to do it above, in the noncommutative
+ case, we would need an extra temp buffer so as not to
+ overwrite temp_recvbuf, because temp_recvbuf may have
+ to be communicated to other processes in the
+ non-power-of-two case. To avoid that extra allocation,
+ we do the reduce here. */
+ if (received) {
+ if (is_commutative || (dst_tree_root < my_tree_root)) {
+ {
+ if (op != MPI_OP_NULL)
+ op->apply(tmp_recvbuf, tmp_results, &blklens[0], datatype);
+ if (op != MPI_OP_NULL)
+ op->apply(tmp_recvbuf + dis[1] * extent, tmp_results + dis[1] * extent, &blklens[1], datatype);
+ }
+ } else {
+ {
+ if (op != MPI_OP_NULL)
+ op->apply(tmp_results, tmp_recvbuf, &blklens[0], datatype);
+ if (op != MPI_OP_NULL)
+ op->apply(tmp_results + dis[1] * extent, tmp_recvbuf + dis[1] * extent, &blklens[1], datatype);
+ }
+ /* copy result back into tmp_results */
+ mpi_errno = Datatype::copy(tmp_recvbuf, 1, recvtype, tmp_results, 1, recvtype);
+ if (mpi_errno)
+ return (mpi_errno);
+ }
+ }