Yet Another eXchange Tool 0.11.4
Loading...
Searching...
No Matches
xt_redist_collection_static.c
Go to the documentation of this file.
1
12/*
13 * Keywords:
14 * Maintainer: Jörg Behrens <behrens@dkrz.de>
15 * Moritz Hanke <hanke@dkrz.de>
16 * Thomas Jahns <jahns@dkrz.de>
17 * URL: https://dkrz-sw.gitlab-pages.dkrz.de/yaxt/
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met:
22 *
23 * Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 *
26 * Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * Neither the name of the DKRZ GmbH nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
38 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46#ifdef HAVE_CONFIG_H
47#include <config.h>
48#endif
49
50#include <assert.h>
51#include <limits.h>
52#include <stdbool.h>
53#include <stdlib.h>
54
55#include <mpi.h>
56
57#include "core/core.h"
58#include "core/ppm_xfuncs.h"
59#include "xt/xt_mpi.h"
60#include "xt_mpi_internal.h"
63#include "ensure_array_size.h"
64#include "xt/xt_redist.h"
65#include "xt_redist_internal.h"
66#include "xt_config_internal.h"
67
68
69static void
70generate_msg_infos(size_t nmsg, size_t num_redists,
71 struct Xt_redist_msg *msgs,
72 const MPI_Aint displacements[num_redists],
73 const Xt_redist redists[num_redists],
74 const size_t num_ranks[num_redists],
75 const int *restrict ranks[num_redists],
76 struct Xt_mpiddt_list *ddt_list,
77 MPI_Comm comm,
78 enum xt_msg_direction direction)
79{
80 if (nmsg) {
81 size_t rank_pos[num_redists];
82 for (size_t j = 0; j < num_redists; ++j)
83 rank_pos[j] = 0;
84 MPI_Datatype datatypes[num_redists];
85 int block_lengths[num_redists];
86 for (size_t i = 0; i < num_redists; ++i)
87 block_lengths[i] = 1;
88 for (size_t i = 0; i < nmsg; ++i) {
89 int min_rank = INT_MAX;
90 for (size_t j = 0; j < num_redists; ++j)
91 if (rank_pos[j] < num_ranks[j] && ranks[j][rank_pos[j]] < min_rank)
92 min_rank = ranks[j][rank_pos[j]];
93
94 for (size_t j = 0; j < num_redists; ++j)
95 datatypes[j] =
96 (rank_pos[j] < num_ranks[j] && ranks[j][rank_pos[j]] == min_rank)
97 ? xt_redist_get_MPI_Datatype(redists[j], min_rank, direction, false)
98 : MPI_DATATYPE_NULL;
99
100 msgs[i].rank = min_rank;
101 msgs[i].datatype
102 = xt_create_compound_datatype(num_redists, displacements, datatypes,
103 block_lengths, ddt_list, comm);
104 for (size_t j = 0; j < num_redists; ++j) {
105 rank_pos[j]
106 += (rank_pos[j] < num_ranks[j] && ranks[j][rank_pos[j]] == min_rank);
107 }
108 }
109 }
110}
111
114 const MPI_Aint src_displacements[num_redists],
115 const MPI_Aint dst_displacements[num_redists],
116 MPI_Comm comm)
117{
119 redists, num_redists, src_displacements, dst_displacements, comm,
121}
122
125 Xt_redist * redists, int num_redists,
126 const MPI_Aint src_displacements[num_redists],
127 const MPI_Aint dst_displacements[num_redists],
128 MPI_Comm comm, Xt_config config) {
129 // ensure that yaxt is initialized
130 assert(xt_initialized());
131
132 int tag_offset;
133 MPI_Comm new_comm = xt_mpi_comm_smart_dup(comm, &tag_offset);
134
135 xt_redist_check_comms(redists, num_redists, comm);
136
137 size_t num_redists_ = num_redists >= 0 ? (size_t)num_redists : 0;
138 int *restrict ranks[2][num_redists_];
139 size_t num_ranks[2][num_redists_];
140 size_t nmsg[2];
141 /* get lists of ranks to send/receive message to/from */
142 for (size_t i = 0; i < 2; ++i)
143 nmsg[i] = xt_redist_agg_msg_count(num_redists_, (enum xt_msg_direction)i,
144 redists, num_ranks[i], ranks[i], config);
145 size_t nmsg_sum = nmsg[SEND] + nmsg[RECV];
146 struct Xt_redist_msg *msgs = xmalloc(sizeof (*msgs) * nmsg_sum);
147 struct Xt_mpiddt_list ddt_list = Xt_mpiddt_empty_list;
148 for (size_t i = 0; i < 2; ++i) {
149 size_t ofs = i == 0 ? 0 : nmsg[SEND];
150 const MPI_Aint *disp = i == 0 ? src_displacements : dst_displacements;
151 generate_msg_infos(nmsg[i], num_redists_, msgs+ofs, disp, redists,
152 num_ranks[i],
153 (const int *restrict (*))(intptr_t)ranks[i],
154 &ddt_list, new_comm, (enum xt_msg_direction)i);
155 free(ranks[i][0]);
156 }
157
158 struct Xt_config_ config_ = *config;
159 config_.flags |= exch_no_dt_dup;
160
161 Xt_redist redist_collection =
163 (int)nmsg[SEND], (int)nmsg[RECV], msgs, msgs+nmsg[SEND], new_comm,
164 &config_);
165 Xt_mpi_ddt_cache_free(&ddt_list, new_comm);
166 free(msgs);
167 xt_mpi_comm_smart_dedup(&new_comm, tag_offset);
168 return redist_collection;
169}
170
171/*
172 * Local Variables:
173 * c-basic-offset: 2
174 * coding: utf-8
175 * indent-tabs-mode: nil
176 * show-trailing-whitespace: t
177 * require-trailing-newline: t
178 * End:
179 */
int MPI_Comm
Definition core.h:64
add versions of standard API functions not returning on error
#define xmalloc(size)
Definition ppm_xfuncs.h:70
MPI_Datatype datatype
Definition xt_redist.h:69
struct Xt_config_ xt_default_config
Definition xt_config.c:204
implementation of configuration object
@ exch_no_dt_dup
int xt_initialized(void)
MPI_Comm xt_mpi_comm_smart_dup(MPI_Comm comm, int *tag_offset)
Definition xt_mpi.c:333
void xt_mpi_comm_smart_dedup(MPI_Comm *comm, int tag_offset)
Definition xt_mpi.c:386
utility routines for MPI
void Xt_mpi_ddt_cache_free(struct Xt_mpiddt_list *ddt_list, MPI_Comm comm)
#define Xt_mpiddt_empty_list
MPI_Datatype xt_redist_get_MPI_Datatype(Xt_redist redist, int rank, enum xt_msg_direction direction, bool do_dup)
Definition xt_redist.c:123
MPI_Datatype xt_create_compound_datatype(size_t count, const MPI_Aint displacements[count], const MPI_Datatype datatypes[count], const int block_lengths[count], struct Xt_mpiddt_list *ddt_list, MPI_Comm comm)
Definition xt_redist.c:231
void xt_redist_check_comms(Xt_redist *redists, int num_redists, MPI_Comm comm)
Definition xt_redist.c:143
unsigned xt_redist_agg_msg_count(size_t num_redists, enum xt_msg_direction direction, const Xt_redist redists[num_redists], size_t num_ranks[num_redists], int *restrict ranks[num_redists], Xt_config config)
Definition xt_redist.c:191
redistribution of data
Xt_redist xt_redist_collection_static_new(Xt_redist *redists, int num_redists, const MPI_Aint src_displacements[num_redists], const MPI_Aint dst_displacements[num_redists], MPI_Comm comm)
static void generate_msg_infos(size_t nmsg, size_t num_redists, struct Xt_redist_msg *msgs, const MPI_Aint displacements[num_redists], const Xt_redist redists[num_redists], const size_t num_ranks[num_redists], const int *restrict ranks[num_redists], struct Xt_mpiddt_list *ddt_list, MPI_Comm comm, enum xt_msg_direction direction)
Xt_redist xt_redist_collection_static_custom_new(Xt_redist *redists, int num_redists, const MPI_Aint src_displacements[num_redists], const MPI_Aint dst_displacements[num_redists], MPI_Comm comm, Xt_config config)
redistribution of data, non-public declarations
xt_msg_direction
Xt_redist xt_redist_single_array_base_custom_new(int nsend, int nrecv, const struct Xt_redist_msg send_msgs[], const struct Xt_redist_msg recv_msgs[], MPI_Comm comm, Xt_config config)