xt_redist_repeat.c 6.33 KB
Newer Older
Moritz Hanke's avatar
Moritz Hanke committed
1
2
3
/**
 * @file xt_redist_repeat.c
 *
4
5
 * @copyright Copyright  (C)  2016 Jörg Behrens <behrens@dkrz.de>
 *                                 Moritz Hanke <hanke@dkrz.de>
Moritz Hanke's avatar
Moritz Hanke committed
6
7
 *                                 Thomas Jahns <jahns@dkrz.de>
 *
8
9
 * @author Jörg Behrens <behrens@dkrz.de>
 *         Moritz Hanke <hanke@dkrz.de>
Moritz Hanke's avatar
Moritz Hanke committed
10
11
12
13
 *         Thomas Jahns <jahns@dkrz.de>
 */
/*
 * Keywords:
14
15
 * Maintainer: Jörg Behrens <behrens@dkrz.de>
 *             Moritz Hanke <hanke@dkrz.de>
Moritz Hanke's avatar
Moritz Hanke committed
16
 *             Thomas Jahns <jahns@dkrz.de>
Moritz Hanke's avatar
Moritz Hanke committed
17
 * URL: https://doc.redmine.dkrz.de/yaxt/html/
Moritz Hanke's avatar
Moritz Hanke committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are  permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of the DKRZ GmbH nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
Thomas Jahns's avatar
Thomas Jahns committed
46
47
48
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Moritz Hanke's avatar
Moritz Hanke committed
49
50

#include <assert.h>
Thomas Jahns's avatar
Thomas Jahns committed
51
#include <limits.h>
Moritz Hanke's avatar
Moritz Hanke committed
52
53
54
55
56
57
58
#include <stdlib.h>

#include <mpi.h>

#include "core/core.h"
#include "core/ppm_xfuncs.h"
#include "xt/xt_mpi.h"
59
#include "xt_mpi_internal.h"
Moritz Hanke's avatar
Moritz Hanke committed
60
#include "xt/xt_redist_repeat.h"
61
#include "xt/xt_redist_single_array_base.h"
Moritz Hanke's avatar
Moritz Hanke committed
62
63
64
#include "ensure_array_size.h"
#include "xt/xt_redist.h"
#include "xt_redist_internal.h"
Thomas Jahns's avatar
Thomas Jahns committed
65
#include "xt_config_internal.h"
Moritz Hanke's avatar
Moritz Hanke committed
66
67

static void
Thomas Jahns's avatar
Thomas Jahns committed
68
generate_msg_infos(struct Xt_redist_msg *restrict msgs,
69
                   MPI_Aint extent, const int *displacements, Xt_redist redist,
70
                   int num_repetitions, MPI_Comm comm,
71
72
                   enum xt_msg_direction direction)
{
73
74
  int *restrict ranks = NULL;
  size_t num_ranks
75
    = (size_t)xt_redist_get_msg_ranks(redist, direction, &ranks);
76
  for (size_t i = 0; i < num_ranks; ++i) {
77
78
    MPI_Datatype datatype
      = xt_redist_get_MPI_Datatype(redist, ranks[i], direction);
79
    MPI_Aint curr_lb, curr_extent;
80
81
82
83
84
85
86
    MPI_Datatype datatype_with_extent;

    // adjust extent of datatype to match the displacements
    xt_mpi_call(MPI_Type_get_extent(datatype, &curr_lb, &curr_extent), comm);
    xt_mpi_call(MPI_Type_create_resized(datatype, curr_lb, extent,
                                        &datatype_with_extent), comm);

Thomas Jahns's avatar
Thomas Jahns committed
87
88
    msgs[i].rank = ranks[i];
    msgs[i].datatype
89
      = xt_mpi_generate_datatype(displacements, num_repetitions,
Moritz Hanke's avatar
Moritz Hanke committed
90
                                 datatype_with_extent, comm);
91
92
    MPI_Type_free(&datatype_with_extent);
    MPI_Type_free(&datatype);
Moritz Hanke's avatar
Moritz Hanke committed
93
  }
94
  free(ranks);
Moritz Hanke's avatar
Moritz Hanke committed
95
96
}

Thomas Jahns's avatar
Thomas Jahns committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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])
{
  return xt_redist_repeat_asym_custom_new(
    redist, src_extent, dst_extent, num_repetitions,
    src_displacements, dst_displacements, (Xt_config)&xt_default_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)
{
115
116
  // ensure that yaxt is initialized
  assert(xt_initialized());
Moritz Hanke's avatar
Moritz Hanke committed
117

118
  int tag_offset;
119
120
  MPI_Comm comm
    = xt_mpi_comm_smart_dup(xt_redist_get_MPI_Comm(redist), &tag_offset);
Moritz Hanke's avatar
Moritz Hanke committed
121

122
  if (num_repetitions < 1)
Moritz Hanke's avatar
Moritz Hanke committed
123
124
125
    Xt_abort(comm, "ERROR: invalid number of repetitions (Xt_redist_repeat)\n",
             __FILE__, __LINE__);

Thomas Jahns's avatar
Thomas Jahns committed
126
127
128
129
130
  int nmsg[2];
  for (int i = 0; i < 2; ++i)
    nmsg[i] = redist->vtable->get_num_msg(redist, (enum xt_msg_direction)i);
  size_t num_messages = (size_t)nmsg[SEND] + (size_t)nmsg[RECV];
  struct Xt_redist_msg *msgs = xmalloc(sizeof (*msgs) * num_messages);
Moritz Hanke's avatar
Moritz Hanke committed
131

Thomas Jahns's avatar
Thomas Jahns committed
132
133
  generate_msg_infos(msgs, src_extent, src_displacements,
                     redist, num_repetitions, comm, SEND);
Moritz Hanke's avatar
Moritz Hanke committed
134

Thomas Jahns's avatar
Thomas Jahns committed
135
136
  generate_msg_infos(msgs+nmsg[0], dst_extent, dst_displacements,
                     redist, num_repetitions, comm, RECV);
Moritz Hanke's avatar
Moritz Hanke committed
137

Thomas Jahns's avatar
Thomas Jahns committed
138
  Xt_redist result = xt_redist_single_array_base_custom_new(
Thomas Jahns's avatar
Thomas Jahns committed
139
140
    nmsg[SEND], nmsg[RECV], msgs, msgs+nmsg[SEND], comm, config);
  xt_redist_msgs_free(num_messages, msgs, comm);
141
142
  xt_mpi_comm_smart_dedup(&comm, tag_offset);
  return result;
Moritz Hanke's avatar
Moritz Hanke committed
143
}
144

145
146
147
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]) {
Thomas Jahns's avatar
Thomas Jahns committed
148
149
150
151
  return xt_redist_repeat_asym_custom_new(
    redist, src_extent, dst_extent, num_repetitions, displacements,
    displacements, (Xt_config)&xt_default_config);
}
152

Thomas Jahns's avatar
Thomas Jahns committed
153
154
155
156
157
158
159
160
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)
{
  return xt_redist_repeat_asym_custom_new(
    redist, src_extent, dst_extent, num_repetitions, displacements,
    displacements, config);
161
162
}

163
164
165
166
167
168
169
170
171
/*
 * Local Variables:
 * c-basic-offset: 2
 * coding: utf-8
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */