Yet Another eXchange Tool  0.9.0
xt_exchanger.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 <string.h>
51 #include <stdlib.h>
52 
53 #include "xt/xt_mpi.h"
54 #include "xt_exchanger.h"
59 
61 xt_exchanger_copy(Xt_exchanger exchanger, MPI_Comm new_comm, int new_tag_offset)
62 {
63  return exchanger->vtable->copy(exchanger, new_comm, new_tag_offset);
64 }
65 
67 
68  exchanger->vtable->delete(exchanger);
69 }
70 
71 void xt_exchanger_s_exchange(Xt_exchanger exchanger, const void *src_data, void *dst_data) {
72 
73  exchanger->vtable->s_exchange(exchanger, src_data, dst_data);
74 }
75 
77  const void *src_data, void *dst_data,
78  Xt_request *request) {
79 
80  exchanger->vtable->a_exchange(exchanger, src_data, dst_data, request);
81 }
82 
83 static int compare_exchange_messages (const void * msg_a, const void * msg_b)
84 {
85  int rank_a = *(const int *)msg_a;
86  int rank_b = *(const int *)msg_b;
87  return ( rank_a - rank_b );
88 }
89 
90 void xt_exchanger_internal_optimize(size_t n, void * msgs, size_t msg_type_size,
91  MPI_Comm comm) {
92 
93  int comm_size, comm_rank, is_inter;
94  xt_mpi_call(MPI_Comm_rank(comm, &comm_rank), comm);
95  xt_mpi_call(MPI_Comm_test_inter(comm, &is_inter), comm);
96  int (*get_comm_size)(MPI_Comm, int *)
97  = is_inter ? MPI_Comm_remote_size : MPI_Comm_size;
98  xt_mpi_call(get_comm_size(comm, &comm_size), comm);
99  /* In order to avoid congestion of messages, the order of send and receive
100  * messages is changed. This is done by sorting the messages according to the
101  * rank of the respective message partner. Before the sorting to ranks that
102  * are smaller or equal to the local rank the size of the communicator is
103  * added.
104  * example: process 5 is supposed to communicate with processes: 9, 5, 2, 6, 1
105  * 1. add comm_size(10): 9, 15, 12, 6, 11
106  * 2. sort: 6, 9, 11, 12, 15 -> final order: 6, 9, 1, 2, 5
107  */
108  unsigned char *msgs_ = (unsigned char *)msgs;
109  /* apply offset for ranks <= comm_rank */
110  for (size_t i = 0; i < n; ++i) {
111  int *p = (int *)(void *)(msgs_ + i * msg_type_size),
112  r = *p;
113  if (r <= comm_rank) *p = r + comm_size;
114  }
115  qsort(msgs, n, msg_type_size, compare_exchange_messages);
116  /* undo offset */
117  for (size_t i = 0; i < n; ++i) {
118  int *p = (int *)(void *)(msgs_ + i * msg_type_size),
119  r = *p;
120  if (r >= comm_size) *p = r - comm_size;
121  }
122 }
123 
124 
125 int
127  enum xt_msg_direction direction,
128  int *restrict *ranks)
129 {
130  return exchanger->vtable->get_msg_ranks(exchanger, direction, ranks);
131 }
132 
133 MPI_Datatype
135  int rank, enum xt_msg_direction direction) {
136  return exchanger->vtable->get_MPI_Datatype(exchanger, rank, direction);
137 }
138 
139 /*
140  * Local Variables:
141  * c-basic-offset: 2
142  * coding: utf-8
143  * indent-tabs-mode: nil
144  * show-trailing-whitespace: t
145  * require-trailing-newline: t
146  * End:
147  */
int MPI_Comm
Definition: core.h:64
struct xt_exchanger_vtable * vtable
Definition: xt_exchanger.h:75
int(* get_msg_ranks)(Xt_exchanger, enum xt_msg_direction, int *restrict *)
Definition: xt_exchanger.h:70
Xt_exchanger(* copy)(Xt_exchanger, MPI_Comm, int)
Definition: xt_exchanger.h:66
void(* a_exchange)(Xt_exchanger, const void *, void *, Xt_request *request)
Definition: xt_exchanger.h:69
void(* delete)(Xt_exchanger)
Definition: xt_exchanger.h:67
MPI_Datatype(* get_MPI_Datatype)(Xt_exchanger, int, enum xt_msg_direction)
Definition: xt_exchanger.h:71
void(* s_exchange)(Xt_exchanger, const void *, void *)
Definition: xt_exchanger.h:68
static int compare_exchange_messages(const void *msg_a, const void *msg_b)
Definition: xt_exchanger.c:83
void xt_exchanger_internal_optimize(size_t n, void *msgs, size_t msg_type_size, MPI_Comm comm)
Definition: xt_exchanger.c:90
void xt_exchanger_s_exchange(Xt_exchanger exchanger, const void *src_data, void *dst_data)
Definition: xt_exchanger.c:71
int xt_exchanger_get_msg_ranks(Xt_exchanger exchanger, enum xt_msg_direction direction, int *restrict *ranks)
Definition: xt_exchanger.c:126
void xt_exchanger_a_exchange(Xt_exchanger exchanger, const void *src_data, void *dst_data, Xt_request *request)
Definition: xt_exchanger.c:76
void xt_exchanger_delete(Xt_exchanger exchanger)
Definition: xt_exchanger.c:66
Xt_exchanger xt_exchanger_copy(Xt_exchanger exchanger, MPI_Comm new_comm, int new_tag_offset)
Definition: xt_exchanger.c:61
MPI_Datatype xt_exchanger_get_MPI_Datatype(Xt_exchanger exchanger, int rank, enum xt_msg_direction direction)
Definition: xt_exchanger.c:134
exchanging of data based on information provided by redist's
utility routines for MPI
#define xt_mpi_call(call, comm)
Definition: xt_mpi.h:68
xt_msg_direction