Yet Another eXchange Tool  DO_NOT_EDIT_HERE
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 
66 static void
67 generate_msg_infos(struct Xt_redist_msg **msgs, int *nmsgs,
68  MPI_Aint extent, const int *displacements, Xt_redist redist,
69  int num_repetitions, MPI_Comm comm,
70  enum xt_msg_direction direction,
71  MPI_Datatype (*get_MPI_datatype)(Xt_redist,int)) {
72 
73  int comm_size;
74  xt_mpi_call(MPI_Comm_size(comm, &comm_size), comm);
75 
76  assert(*nmsgs >= 0);
77  size_t num_messages = (size_t)*nmsgs;
78  int *restrict ranks = NULL;
79  size_t num_ranks
80  = (size_t)xt_redist_get_msg_ranks(redist, direction, (int **)&ranks);
81  struct Xt_redist_msg *restrict p
82  = xrealloc(*msgs, sizeof (*p) * (num_messages + num_ranks));
83  for (size_t i = 0; i < num_ranks; ++i) {
84  MPI_Datatype datatype = get_MPI_datatype(redist, ranks[i]);
85  MPI_Aint curr_lb, curr_extent;
86  MPI_Datatype datatype_with_extent;
87 
88  // adjust extent of datatype to match the displacements
89  xt_mpi_call(MPI_Type_get_extent(datatype, &curr_lb, &curr_extent), comm);
90  xt_mpi_call(MPI_Type_create_resized(datatype, curr_lb, extent,
91  &datatype_with_extent), comm);
92 
93  p[num_messages + i].rank = ranks[i];
94  p[num_messages + i].datatype
95  = xt_mpi_generate_datatype(displacements, num_repetitions,
96  datatype_with_extent, comm);
97  MPI_Type_free(&datatype_with_extent);
98  MPI_Type_free(&datatype);
99  }
100  free(ranks);
101  *msgs = p;
102  *nmsgs = (int)(num_messages + num_ranks);
103 }
104 
105 Xt_redist xt_redist_repeat_asym_new(Xt_redist redist, MPI_Aint src_extent,
106  MPI_Aint dst_extent, int num_repetitions,
107  const int src_displacements[num_repetitions],
108  const int dst_displacements[num_repetitions]) {
109 
110  int nsend = 0, nrecv = 0;
111  struct Xt_redist_msg *send_msgs = NULL, *recv_msgs = NULL;
112  int tag_offset;
113  MPI_Comm comm
114  = xt_mpi_comm_smart_dup(xt_redist_get_MPI_Comm(redist), &tag_offset);
115 
116  if (num_repetitions < 1)
117  Xt_abort(comm, "ERROR: invalid number of repetitions (Xt_redist_repeat)\n",
118  __FILE__, __LINE__);
119 
120 
121  generate_msg_infos(&send_msgs, &nsend, src_extent,
122  src_displacements, redist, num_repetitions, comm,
124 
125  generate_msg_infos(&recv_msgs, &nrecv, dst_extent,
126  dst_displacements, redist, num_repetitions, comm,
128 
129  Xt_redist result
130  = xt_redist_single_array_base_new(nsend, nrecv, send_msgs, recv_msgs, comm);
131  xt_mpi_comm_smart_dedup(&comm, tag_offset);
132  return result;
133 }
134 
135 Xt_redist xt_redist_repeat_new(Xt_redist redist, MPI_Aint src_extent,
136  MPI_Aint dst_extent, int num_repetitions,
137  const int displacements[num_repetitions]) {
138 
139  return xt_redist_repeat_asym_new(redist, src_extent,
140  dst_extent, num_repetitions,
141  displacements,
142  displacements);
143 }
144 
145 /*
146  * Local Variables:
147  * c-basic-offset: 2
148  * coding: utf-8
149  * indent-tabs-mode: nil
150  * show-trailing-whitespace: t
151  * require-trailing-newline: t
152  * End:
153  */
xt_msg_direction
redistribution of data, non-public declarations
add versions of standard API functions not returning on error
MPI_Datatype xt_redist_get_send_MPI_Datatype(Xt_redist redist, int rank)
Definition: xt_redist.c:82
MPI_Datatype xt_redist_get_recv_MPI_Datatype(Xt_redist redist, int rank)
Definition: xt_redist.c:87
#define xrealloc(ptr, size)
Definition: ppm_xfuncs.h:67
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])
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, MPI_Datatype(*get_MPI_datatype)(Xt_redist, int))
Xt_redist xt_redist_single_array_base_new(int nsend, int nrecv, struct Xt_redist_msg *send_msgs, struct Xt_redist_msg *recv_msgs, MPI_Comm comm)
redistribution of data
MPI_Datatype datatype
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])
MPI_Datatype xt_mpi_generate_datatype(int const *displacements, int count, MPI_Datatype old_type, MPI_Comm comm)
Definition: xt_mpi.c:263
MPI_Comm xt_mpi_comm_smart_dup(MPI_Comm comm, int *tag_offset)
Definition: xt_mpi.c:850
void xt_mpi_comm_smart_dedup(MPI_Comm *comm, int tag_offset)
Definition: xt_mpi.c:901
int xt_redist_get_msg_ranks(Xt_redist redist, enum xt_msg_direction direction, int **ranks)
Definition: xt_redist.c:97
#define xt_mpi_call(call, comm)
Definition: xt_mpi.h:68
MPI_Comm xt_redist_get_MPI_Comm(Xt_redist redist)
Definition: xt_redist.c:92
int MPI_Comm
Definition: core.h:64
utility routines for MPI