schwz  Generated automatically from develop
comm_helpers.hpp (cd61077)
1 /*******************************<SCHWARZ LIB LICENSE>***********************
2 Copyright (c) 2019, the SCHWARZ LIB authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<SCHWARZ LIB LICENSE>*************************/
32 
33 
34 #ifndef comm_helpers_hpp
35 #define comm_helpers_hpp
36 
37 
38 #include <memory>
39 #include <vector>
40 
41 #include <schwarz/config.hpp>
42 
43 #include <communicate.hpp>
44 #include <gather.hpp>
45 #include <scatter.hpp>
46 #include <settings.hpp>
47 
48 
49 namespace schwz {
55 namespace CommHelpers {
56 
57 
58 template <typename ValueType, typename IndexType, typename MixedValueType>
59 void transfer_one_by_one(
60  const Settings &settings,
62  &comm_struct,
63  ValueType *buffer, IndexType **offset, int num_neighbors,
64  IndexType *neighbors)
65 {
66  auto mpi_vtype = schwz::mpi::get_mpi_datatype(buffer[0]);
67  for (auto p = 0; p < num_neighbors; p++) {
68  if ((offset[p])[0] > 0) {
69  if (settings.comm_settings.enable_put) {
70  for (auto i = 0; i < (offset[p])[0]; i++) {
71  MPI_Put(&buffer[(offset[p])[i + 1]], 1, mpi_vtype,
72  neighbors[p], (offset[p])[i + 1], 1, mpi_vtype,
73  comm_struct.window_x);
74  }
75  } else if (settings.comm_settings.enable_get) {
76  for (auto i = 0; i < (offset[p])[0]; i++) {
77  MPI_Get(&buffer[(offset[p])[i + 1]], 1, mpi_vtype,
78  neighbors[p], (offset[p])[i + 1], 1, mpi_vtype,
79  comm_struct.window_x);
80  }
81  }
82  if (settings.comm_settings.enable_flush_all) {
83  MPI_Win_flush(neighbors[p], comm_struct.window_x);
84  } else if (settings.comm_settings.enable_flush_local) {
85  MPI_Win_flush_local(neighbors[p], comm_struct.window_x);
86  }
87  }
88  }
89 }
90 
91 
92 template <typename ValueType, typename IndexType>
93 void pack_buffer(const Settings &settings, const ValueType *buffer,
94  ValueType *send_buffer, IndexType **num_send_elems,
95  IndexType **host_num_send_elems, int offset, int send_subd)
96 {
97  using vec_vtype = gko::matrix::Dense<ValueType>;
98  using arr = gko::Array<IndexType>;
99  using varr = gko::Array<ValueType>;
100  if (settings.executor_string == "cuda" &&
101  !settings.comm_settings.stage_through_host) {
102  auto tmp_send_buf = vec_vtype::create(
103  settings.executor,
104  gko::dim<2>((host_num_send_elems[send_subd])[0], 1));
106  (host_num_send_elems[send_subd])[0],
107  (num_send_elems[send_subd]) + 1, buffer, tmp_send_buf->get_values(),
108  copy));
109  settings.executor->copy((host_num_send_elems[send_subd])[0],
110  tmp_send_buf->get_values(),
111  &(send_buffer[offset]));
112  } else {
113  settings.executor->get_master()->run(
114  Gather<ValueType, IndexType>((host_num_send_elems[send_subd])[0],
115  (host_num_send_elems[send_subd]) + 1,
116  buffer, &(send_buffer[offset]), copy));
117  }
118 }
119 
120 
121 template <typename ValueType, typename IndexType>
122 void transfer_buffer(const Settings &settings, MPI_Win &window,
123  ValueType *target_buffer, IndexType **host_num_elems,
124  int offset, int target_subd, IndexType *neighbors,
125  IndexType *displacements)
126 {
127  auto mpi_vtype = schwz::mpi::get_mpi_datatype(target_buffer[0]);
128  if (settings.comm_settings.enable_lock_local) {
129  MPI_Win_lock(MPI_LOCK_SHARED, neighbors[target_subd], 0, window);
130  }
131  if (settings.comm_settings.enable_put) {
132  MPI_Put(&target_buffer[offset], (host_num_elems[target_subd])[0],
133  mpi_vtype, neighbors[target_subd],
134  displacements[neighbors[target_subd]],
135  (host_num_elems[target_subd])[0], mpi_vtype, window);
136  } else if (settings.comm_settings.enable_get) {
137  MPI_Get(&target_buffer[offset], (host_num_elems[target_subd])[0],
138  mpi_vtype, neighbors[target_subd],
139  displacements[neighbors[target_subd]],
140  (host_num_elems[target_subd])[0], mpi_vtype, window);
141  }
142  if (settings.comm_settings.enable_flush_all) {
143  MPI_Win_flush(neighbors[target_subd], window);
144  } else if (settings.comm_settings.enable_flush_local) {
145  MPI_Win_flush_local(neighbors[target_subd], window);
146  }
147  if (settings.comm_settings.enable_lock_local) {
148  MPI_Win_unlock(neighbors[target_subd], window);
149  }
150 }
151 
152 
153 template <typename ValueType, typename IndexType>
154 void unpack_buffer(const Settings &settings, ValueType *buffer,
155  const ValueType *recv_buffer, IndexType **num_recv_elems,
156  IndexType **host_num_recv_elems, int offset, int recv_subd)
157 {
158  using vec_vtype = gko::matrix::Dense<ValueType>;
159  using arr = gko::Array<IndexType>;
160  using varr = gko::Array<ValueType>;
161  auto num_elems = (host_num_recv_elems[recv_subd])[0];
162  if (settings.executor_string == "cuda" &&
163  !settings.comm_settings.stage_through_host) {
164  auto tmp_recv_buf = vec_vtype::create(
165  settings.executor,
166  gko::dim<2>((host_num_recv_elems[recv_subd])[0], 1));
167  settings.executor->copy(num_elems, &(recv_buffer[offset]),
168  tmp_recv_buf->get_values());
170  num_elems, (num_recv_elems[recv_subd]) + 1,
171  tmp_recv_buf->get_values(), buffer, copy));
172  } else {
173  settings.executor->get_master()->run(Scatter<ValueType, IndexType>(
174  num_elems, (num_recv_elems[recv_subd]) + 1, &(recv_buffer[offset]),
175  buffer, copy));
176  }
177 }
178 
179 
180 } // namespace CommHelpers
181 } // namespace schwz
182 
183 
184 #endif // comm_helpers.hpp
bool enable_lock_local
Use local locks.
Definition: settings.hpp:261
bool enable_flush_local
Use local flush.
Definition: settings.hpp:251
The communication struct used to store the communication data.
Definition: communicate.hpp:67
bool enable_put
Put the data to the window using MPI_Put rather than get.
Definition: settings.hpp:231
bool stage_through_host
Stage the MPI transfers through the host.
Definition: settings.hpp:241
Definition: scatter.hpp:71
Definition: gather.hpp:71
The struct that contains the solver settings and the parameters to be set by the user.
Definition: settings.hpp:77
MPI_Win window_x
The RDMA window for the solution vector.
Definition: communicate.hpp:223
std::string executor_string
The string that contains the ginkgo executor paradigm.
Definition: settings.hpp:81
The Schwarz wrappers namespace.
Definition: comm_helpers.hpp:49
std::shared_ptr< gko::Executor > executor
The ginkgo executor the code is to be executed on.
Definition: settings.hpp:86
bool enable_get
Get the data to the window using MPI_Get rather than put.
Definition: settings.hpp:236
bool enable_flush_all
Use flush all.
Definition: settings.hpp:256