Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GsLib.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GsLib.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: wrapper of functions around GSLib routines
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIB_UTILITIES_COMMUNICATION_GSLIB_HPP
36#define NEKTAR_LIB_UTILITIES_COMMUNICATION_GSLIB_HPP
37
38#include <iostream>
39
40#include <boost/core/ignore_unused.hpp>
41
44#ifdef NEKTAR_USE_MPI
46#endif
47
48namespace Gs
49{
50using namespace Nektar;
51
52typedef enum
53{
60typedef enum
61{
70typedef enum
71{
77
78typedef struct
79{
80 void *ptr;
81 size_t n, max;
82} array;
83typedef array buffer;
84#ifdef NEKTAR_USE_MPI
85typedef MPI_Comm comm_ext;
86typedef MPI_Request comm_req;
87#else
88typedef int comm_ext;
89typedef int comm_req;
90#endif
91
92struct comm
93{
94 unsigned int id;
95 unsigned int np;
97};
98
99typedef struct
100{
101 unsigned int n; /* number of messages */
102 unsigned int *p; /* message source/dest proc */
103 unsigned int *size; /* size of message */
104 unsigned int total; /* sum of message sizes */
106
107typedef struct
108{
110 const unsigned int *map[2];
112 unsigned int buffer_size;
113} pw_data;
114
115typedef struct
116{
117 const unsigned int *scatter_map, *gather_map;
118 unsigned int size_r, size_r1, size_r2;
119 unsigned int size_sk, size_s, size_total;
120 unsigned int p1, p2;
121 unsigned int nrecvn;
122} cr_stage;
123
124typedef struct
125{
126 cr_stage *stage[2];
127 unsigned int nstages;
128 unsigned int buffer_size, stage_buffer_size;
129} cr_data;
130
131typedef struct
132{
133 const unsigned int *map_to_buf[2], *map_from_buf[2];
134 unsigned int buffer_size;
136
137typedef void exec_fun(void *data, gs_mode mode, unsigned vn, gs_dom dom,
138 gs_op op, unsigned transpose, const void *execdata,
139 const struct comm *comm, char *buf);
140typedef void fin_fun(void *data);
141
142typedef struct
143{
144 unsigned int buffer_size, mem_size;
145 void *data;
148} gs_remote;
149
150typedef struct
151{
152 struct comm comm;
153 const unsigned int *map_local[2]; /* 0=unflagged, 1=all */
154 const unsigned int *flagged_primaries;
156 unsigned int handle_size;
157} gs_data;
158
159typedef enum
160{
166
167extern "C"
168{
169 void nektar_gs(void *u, gs_dom dom, gs_op op, unsigned transpose,
170 gs_data *gsh, buffer *buf);
171 gs_data *nektar_gs_setup(const long *id, unsigned int n,
172 const struct comm *comm, int unique,
173 gs_method method, int verbose);
175 void nektar_gs_unique(const long *id, unsigned int n,
176 const struct comm *comm);
177}
178
179/**
180 * @brief Initialise Gather-Scatter map.
181 *
182 * On each process an array of IDs for each global degree of freedom is
183 * supplied which corresponds to a unique numbering of universal degrees of
184 * freedom. This is used to initialise the GSLib mapping between process-
185 * boundary degrees of freedom on different processes.
186 * @param pId Array of integers providing universal IDs for each
187 * global DOF on the process.
188 * @param pComm Communication object used for inter-process
189 * communication.
190 * @return GSLib data structure containing mapping information.
191 */
192static inline gs_data *Init(const Nektar::Array<OneD, long> pId,
193 const LibUtilities::CommSharedPtr &pComm,
194 bool verbose = true)
195{
196#ifdef NEKTAR_USE_MPI
197 if (pComm->IsSerial())
198 {
199 return 0;
200 }
202 std::dynamic_pointer_cast<LibUtilities::CommMpi>(pComm);
203 ASSERTL1(vCommMpi, "Failed to cast MPI Comm object.");
204 comm vComm;
205 MPI_Comm_dup(vCommMpi->GetComm(), &vComm.c);
206 vComm.id = vCommMpi->GetRank();
207 vComm.np = vCommMpi->GetSize();
208 gs_data *result = nektar_gs_setup(pId.get(), pId.size(), &vComm, 0, gs_auto,
209 (int)verbose);
210 MPI_Comm_free(&vComm.c);
211 return result;
212#else
213 boost::ignore_unused(pId, pComm, verbose);
214 return 0;
215#endif
216}
217
218/**
219 * @brief Updates pId to negate all-but-one references to each universal ID.
220 *
221 * The array of universal IDs corresponding to the process-local DOF are
222 * updated such that the ID of only one instance of each universal ID
223 * remains positive. This allows the consistent formulation of universally
224 * -distributed dot products, for which the contributions of each DOF must
225 * be included only once.
226 */
227static inline void Unique(const Nektar::Array<OneD, long> pId,
228 const LibUtilities::CommSharedPtr &pComm)
229{
230#ifdef NEKTAR_USE_MPI
231 if (pComm->IsSerial())
232 {
233 return;
234 }
236 std::dynamic_pointer_cast<LibUtilities::CommMpi>(pComm);
237 ASSERTL1(vCommMpi, "Failed to cast MPI Comm object.");
238 comm vComm;
239 vComm.c = vCommMpi->GetComm();
240 vComm.id = vCommMpi->GetRank();
241 vComm.np = vCommMpi->GetSize();
242 nektar_gs_unique(pId.get(), pId.size(), &vComm);
243#else
244 boost::ignore_unused(pId, pComm);
245#endif
246}
247
248/**
249 * @brief Deallocates the GSLib mapping data.
250 */
251static inline void Finalise(gs_data *pGsh)
252{
253#ifdef NEKTAR_USE_MPI
254 int finalized;
255 MPI_Finalized(&finalized);
256 if (pGsh && !finalized)
257 {
258 nektar_gs_free(pGsh);
259 }
260#else
261 boost::ignore_unused(pGsh);
262#endif
263}
264
265/**
266 * @brief Performs a gather-scatter operation of the provided values.
267 *
268 * The
269 */
270static inline void Gather(
273{
274#ifdef NEKTAR_USE_MPI
275 if (!pGsh)
276 {
277 return;
278 }
279 if (pBuffer.size() == 0)
280 {
281 nektar_gs(pU.get(), gs_double, pOp, false, pGsh, 0);
282 }
283 else
284 {
285 array buf;
286 buf.ptr = &pBuffer[0];
287 buf.n = pBuffer.size();
288 nektar_gs(pU.get(), gs_double, pOp, false, pGsh, &buf);
289 }
290#else
291 boost::ignore_unused(pU, pOp, pGsh, pBuffer);
292#endif
293}
294} // namespace Gs
295
296#endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
Definition: GsLib.hpp:49
gs_dom
Definition: GsLib.hpp:53
@ gs_dom_n
Definition: GsLib.hpp:58
@ gs_int
Definition: GsLib.hpp:56
@ gs_double
Definition: GsLib.hpp:54
@ gs_long
Definition: GsLib.hpp:57
@ gs_float
Definition: GsLib.hpp:55
gs_data * nektar_gs_setup(const long *id, unsigned int n, const struct comm *comm, int unique, gs_method method, int verbose)
static void Gather(Nektar::Array< OneD, NekDouble > pU, gs_op pOp, gs_data *pGsh, Nektar::Array< OneD, NekDouble > pBuffer=NullNekDouble1DArray)
Performs a gather-scatter operation of the provided values.
Definition: GsLib.hpp:270
int comm_req
Definition: GsLib.hpp:89
static gs_data * Init(const Nektar::Array< OneD, long > pId, const LibUtilities::CommSharedPtr &pComm, bool verbose=true)
Initialise Gather-Scatter map.
Definition: GsLib.hpp:192
gs_op
Definition: GsLib.hpp:61
@ gs_amax
Definition: GsLib.hpp:66
@ gs_add
Definition: GsLib.hpp:62
@ gs_max
Definition: GsLib.hpp:65
@ gs_bpr
Definition: GsLib.hpp:67
@ gs_min
Definition: GsLib.hpp:64
@ gs_op_n
Definition: GsLib.hpp:68
@ gs_mul
Definition: GsLib.hpp:63
void fin_fun(void *data)
Definition: GsLib.hpp:140
static void Finalise(gs_data *pGsh)
Deallocates the GSLib mapping data.
Definition: GsLib.hpp:251
void nektar_gs_unique(const long *id, unsigned int n, const struct comm *comm)
void nektar_gs(void *u, gs_dom dom, gs_op op, unsigned transpose, gs_data *gsh, buffer *buf)
array buffer
Definition: GsLib.hpp:83
int comm_ext
Definition: GsLib.hpp:88
gs_method
Definition: GsLib.hpp:160
@ gs_all_reduce
Definition: GsLib.hpp:164
@ gs_pairwise
Definition: GsLib.hpp:162
@ gs_crystal_router
Definition: GsLib.hpp:163
@ gs_auto
Definition: GsLib.hpp:161
void exec_fun(void *data, gs_mode mode, unsigned vn, gs_dom dom, gs_op op, unsigned transpose, const void *execdata, const struct comm *comm, char *buf)
Definition: GsLib.hpp:137
gs_mode
Definition: GsLib.hpp:71
@ mode_vec
Definition: GsLib.hpp:73
@ mode_plain
Definition: GsLib.hpp:72
@ mode_dry_run
Definition: GsLib.hpp:75
@ mode_many
Definition: GsLib.hpp:74
static void Unique(const Nektar::Array< OneD, long > pId, const LibUtilities::CommSharedPtr &pComm)
Updates pId to negate all-but-one references to each universal ID.
Definition: GsLib.hpp:227
void nektar_gs_free(gs_data *gsh)
std::shared_ptr< CommMpi > CommMpiSharedPtr
Pointer to a Communicator object.
Definition: CommMpi.h:58
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:57
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
static Array< OneD, NekDouble > NullNekDouble1DArray
unsigned int buffer_size
Definition: GsLib.hpp:134
size_t max
Definition: GsLib.hpp:81
void * ptr
Definition: GsLib.hpp:80
comm_ext c
Definition: GsLib.hpp:96
unsigned int np
Definition: GsLib.hpp:95
unsigned int id
Definition: GsLib.hpp:94
unsigned int buffer_size
Definition: GsLib.hpp:128
unsigned int nstages
Definition: GsLib.hpp:127
unsigned int size_s
Definition: GsLib.hpp:119
unsigned int size_r
Definition: GsLib.hpp:118
unsigned int p1
Definition: GsLib.hpp:120
const unsigned int * gather_map
Definition: GsLib.hpp:117
unsigned int nrecvn
Definition: GsLib.hpp:121
unsigned int handle_size
Definition: GsLib.hpp:156
gs_remote r
Definition: GsLib.hpp:155
const unsigned int * flagged_primaries
Definition: GsLib.hpp:154
exec_fun * exec
Definition: GsLib.hpp:146
unsigned int buffer_size
Definition: GsLib.hpp:144
void * data
Definition: GsLib.hpp:145
fin_fun * fin
Definition: GsLib.hpp:147
unsigned int n
Definition: GsLib.hpp:101
unsigned int * size
Definition: GsLib.hpp:103
unsigned int total
Definition: GsLib.hpp:104
unsigned int * p
Definition: GsLib.hpp:102
comm_req * req
Definition: GsLib.hpp:111
unsigned int buffer_size
Definition: GsLib.hpp:112