Yet Another eXchange Tool  0.9.0
xt_redist_repeat.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://doc.redmine.dkrz.de/yaxt/html/
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 <stdlib.h>
53 
54 #include <mpi.h>
55 
56 #include "core/core.h"
57 #include "core/ppm_xfuncs.h"
58 #include "xt/xt_mpi.h"
59 #include "xt_mpi_internal.h"
60 #include "xt/xt_redist_repeat.h"
62 #include "ensure_array_size.h"
63 #include "xt/xt_redist.h"
64 #include "xt_redist_internal.h"
65 #include "xt_config_internal.h"
66 
67 static void
68 generate_msg_infos(struct Xt_redist_msg **msgs, int *nmsgs,
69  MPI_Aint extent, const int *displacements, Xt_redist redist,
70  int num_repetitions, MPI_Comm comm,
71  enum xt_msg_direction direction)
72 {
73  assert(*nmsgs >= 0);
74  size_t num_messages = (size_t)*nmsgs;
75  int *restrict ranks = NULL;
76  size_t num_ranks
77  = (size_t)xt_redist_get_msg_ranks(redist, direction, &ranks);
78  struct Xt_redist_msg *restrict p
79  = xrealloc(*msgs, sizeof (*p) * (num_messages + num_ranks));
80  for (size_t i = 0; i < num_ranks; ++i) {
81  MPI_Datatype datatype
82  = xt_redist_get_MPI_Datatype(redist, ranks[i], direction);
83  MPI_Aint curr_lb, curr_extent;
84  MPI_Datatype datatype_with_extent;
85 
86  // adjust extent of datatype to match the displacements
87  xt_mpi_call(MPI_Type_get_extent(datatype, &curr_lb, &curr_extent), comm);
88  xt_mpi_call(MPI_Type_create_resized(datatype, curr_lb, extent,
89  &datatype_with_extent), comm);
90 
91  p[num_messages + i].rank = ranks[i];
92  p[num_messages + i].datatype
93  = xt_mpi_generate_datatype(displacements, num_repetitions,
94  datatype_with_extent, comm);
95  MPI_Type_free(&datatype_with_extent);
96  MPI_Type_free(&datatype);
97  }
98  free(ranks);
99  *msgs = p;
100  *nmsgs = (int)(num_messages + num_ranks);
101 }
102 
103 Xt_redist
104 xt_redist_repeat_asym_new(Xt_redist redist, MPI_Aint src_extent,
105  MPI_Aint dst_extent, int num_repetitions,
106  const int src_displacements[num_repetitions],
107  const int dst_displacements[num_repetitions])
108 {
110  redist, src_extent, dst_extent, num_repetitions,
111  src_displacements, dst_displacements, (Xt_config)&xt_default_config);
112 }
113 
114 Xt_redist
115 xt_redist_repeat_asym_custom_new(Xt_redist redist, MPI_Aint src_extent,
116  MPI_Aint dst_extent, int num_repetitions,
117  const int src_displacements[num_repetitions],
118  const int dst_displacements[num_repetitions],
119  Xt_config config)
120 {
121  // ensure that yaxt is initialized
122  assert(xt_initialized());
123 
124  int nsend = 0, nrecv = 0;
125  struct Xt_redist_msg *send_msgs = NULL, *recv_msgs = NULL;
126  int tag_offset;
127  MPI_Comm comm
128  = xt_mpi_comm_smart_dup(xt_redist_get_MPI_Comm(redist), &tag_offset);
129 
130  if (num_repetitions < 1)
131  Xt_abort(comm, "ERROR: invalid number of repetitions (Xt_redist_repeat)\n",
132  __FILE__, __LINE__);
133 
134 
135  generate_msg_infos(&send_msgs, &nsend, src_extent,
136  src_displacements, redist, num_repetitions, comm, SEND);
137 
138  generate_msg_infos(&recv_msgs, &nrecv, dst_extent,
139  dst_displacements, redist, num_repetitions, comm, RECV);
140 
142  nsend, nrecv, send_msgs, recv_msgs, comm, config);
143  xt_redist_msgs_free((size_t)nsend, send_msgs, comm);
144  xt_redist_msgs_free((size_t)nrecv, recv_msgs, comm);
145  xt_mpi_comm_smart_dedup(&comm, tag_offset);
146  return result;
147 }
148 
149 Xt_redist xt_redist_repeat_new(Xt_redist redist, MPI_Aint src_extent,
150  MPI_Aint dst_extent, int num_repetitions,
151  const int displacements[num_repetitions]) {
153  redist, src_extent, dst_extent, num_repetitions, displacements,
154  displacements, (Xt_config)&xt_default_config);
155 }
156 
157 Xt_redist xt_redist_repeat_custom_new(Xt_redist redist, MPI_Aint src_extent,
158  MPI_Aint dst_extent, int num_repetitions,
159  const int displacements[num_repetitions],
160  Xt_config config)
161 {
163  redist, src_extent, dst_extent, num_repetitions, displacements,
164  displacements, config);
165 }
166 
167 /*
168  * Local Variables:
169  * c-basic-offset: 2
170  * coding: utf-8
171  * indent-tabs-mode: nil
172  * show-trailing-whitespace: t
173  * require-trailing-newline: t
174  * End:
175  */
int MPI_Comm
Definition: core.h:64
add versions of standard API functions not returning on error
#define xrealloc(ptr, size)
Definition: ppm_xfuncs.h:71
MPI_Datatype datatype
Definition: xt_redist.h:69
struct Xt_config_ xt_default_config
Definition: xt_config.c:65
implementation of configuration object
int xt_initialized(void)
Definition: xt_init.c:107
MPI_Comm xt_mpi_comm_smart_dup(MPI_Comm comm, int *tag_offset)
Definition: xt_mpi.c:813
void xt_mpi_comm_smart_dedup(MPI_Comm *comm, int tag_offset)
Definition: xt_mpi.c:864
utility routines for MPI
MPI_Datatype xt_mpi_generate_datatype(int const *displacements, int count, MPI_Datatype old_type, MPI_Comm comm)
Definition: xt_mpi.c:264
#define xt_mpi_call(call, comm)
Definition: xt_mpi.h:68
MPI_Datatype xt_redist_get_MPI_Datatype(Xt_redist redist, int rank, enum xt_msg_direction direction)
Definition: xt_redist.c:117
int xt_redist_get_msg_ranks(Xt_redist redist, enum xt_msg_direction direction, int *restrict *ranks)
Definition: xt_redist.c:128
redistribution of data
MPI_Comm xt_redist_get_MPI_Comm(Xt_redist redist)
Definition: xt_redist.c:123
redistribution of data, non-public declarations
static void xt_redist_msgs_free(size_t n, struct Xt_redist_msg *msgs, MPI_Comm comm)
xt_msg_direction
@ RECV
@ SEND
Xt_redist xt_redist_repeat_custom_new(Xt_redist redist, MPI_Aint src_extent, MPI_Aint dst_extent, int num_repetitions, const int displacements[num_repetitions], Xt_config config)
Xt_redist xt_redist_repeat_asym_custom_new(Xt_redist redist, MPI_Aint src_extent, MPI_Aint dst_extent, int num_repetitions, const int src_displacements[num_repetitions], const int dst_displacements[num_repetitions], Xt_config config)
Xt_redist xt_redist_repeat_new(Xt_redist redist, MPI_Aint src_extent, MPI_Aint dst_extent, int num_repetitions, const int displacements[num_repetitions])
Xt_redist xt_redist_repeat_asym_new(Xt_redist redist, MPI_Aint src_extent, MPI_Aint dst_extent, int num_repetitions, const int src_displacements[num_repetitions], const int dst_displacements[num_repetitions])
static void generate_msg_infos(struct Xt_redist_msg **msgs, int *nmsgs, MPI_Aint extent, const int *displacements, Xt_redist redist, int num_repetitions, MPI_Comm comm, enum xt_msg_direction 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)