Nektar++
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 
48 namespace Gs
49 {
50 using namespace Nektar;
51 
55 
56 typedef struct
57 {
58  void *ptr;
59  size_t n, max;
60 } array;
61 typedef array buffer;
62 #ifdef NEKTAR_USE_MPI
63 typedef MPI_Comm comm_ext;
64 typedef MPI_Request comm_req;
65 #else
66 typedef int comm_ext;
67 typedef int comm_req;
68 #endif
69 
70 struct comm
71 {
72  unsigned int id;
73  unsigned int np;
75 };
76 
77 typedef struct
78 {
79  unsigned int n; /* number of messages */
80  unsigned int *p; /* message source/dest proc */
81  unsigned int *size; /* size of message */
82  unsigned int total; /* sum of message sizes */
83 } pw_comm_data;
84 
85 typedef struct
86 {
88  const unsigned int *map[2];
90  unsigned int buffer_size;
91 } pw_data;
92 
93 typedef struct
94 {
95  const unsigned int *scatter_map, *gather_map;
96  unsigned int size_r, size_r1, size_r2;
97  unsigned int size_sk, size_s, size_total;
98  unsigned int p1, p2;
99  unsigned int nrecvn;
100 } cr_stage;
101 
102 typedef struct
103 {
104  cr_stage *stage[2];
105  unsigned int nstages;
106  unsigned int buffer_size, stage_buffer_size;
107 } cr_data;
108 
109 typedef struct
110 {
111  const unsigned int *map_to_buf[2], *map_from_buf[2];
112  unsigned int buffer_size;
114 
115 typedef void exec_fun(void *data, gs_mode mode, unsigned vn, gs_dom dom,
116  gs_op op, unsigned transpose, const void *execdata,
117  const struct comm *comm, char *buf);
118 typedef void fin_fun(void *data);
119 
120 typedef struct
121 {
122  unsigned int buffer_size, mem_size;
123  void *data;
125  fin_fun *fin;
126 } gs_remote;
127 
128 typedef struct
129 {
130  struct comm comm;
131  const unsigned int *map_local[2]; /* 0=unflagged, 1=all */
132  const unsigned int *flagged_primaries;
134  unsigned int handle_size;
135 } gs_data;
136 
137 typedef enum {
143 
144 extern "C" {
145 void nektar_gs(void *u, gs_dom dom, gs_op op, unsigned transpose, gs_data *gsh,
146  buffer *buf);
147 gs_data *nektar_gs_setup(const long *id, unsigned int n,
148  const struct comm *comm, int unique, gs_method method,
149  int verbose);
151 void nektar_gs_unique(const long *id, unsigned int n, const struct comm *comm);
152 }
153 
154 /**
155  * @brief Initialise Gather-Scatter map.
156  *
157  * On each process an array of IDs for each global degree of freedom is
158  * supplied which corresponds to a unique numbering of universal degrees of
159  * freedom. This is used to initialise the GSLib mapping between process-
160  * boundary degrees of freedom on different processes.
161  * @param pId Array of integers providing universal IDs for each
162  * global DOF on the process.
163  * @param pComm Communication object used for inter-process
164  * communication.
165  * @return GSLib data structure containing mapping information.
166  */
167 static inline gs_data *Init(const Nektar::Array<OneD, long> pId,
168  const LibUtilities::CommSharedPtr &pComm,
169  bool verbose = true)
170 {
171 #ifdef NEKTAR_USE_MPI
172  if (pComm->IsSerial())
173  {
174  return 0;
175  }
177  std::dynamic_pointer_cast<LibUtilities::CommMpi>(pComm);
178  ASSERTL1(vCommMpi, "Failed to cast MPI Comm object.");
179  comm vComm;
180  MPI_Comm_dup(vCommMpi->GetComm(), &vComm.c);
181  vComm.id = vCommMpi->GetRank();
182  vComm.np = vCommMpi->GetSize();
183  gs_data *result = nektar_gs_setup(pId.get(), pId.size(), &vComm, 0,
184  gs_auto, (int)verbose);
185  MPI_Comm_free(&vComm.c);
186  return result;
187 #else
188  boost::ignore_unused(pId, pComm, verbose);
189  return 0;
190 #endif
191 }
192 
193 /**
194  * @brief Updates pId to negate all-but-one references to each universal ID.
195  *
196  * The array of universal IDs corresponding to the process-local DOF are
197  * updated such that the ID of only one instance of each universal ID
198  * remains positive. This allows the consistent formulation of universally
199  * -distributed dot products, for which the contributions of each DOF must
200  * be included only once.
201  */
202 static inline void Unique(const Nektar::Array<OneD, long> pId,
203  const LibUtilities::CommSharedPtr &pComm)
204 {
205 #ifdef NEKTAR_USE_MPI
206  if (pComm->IsSerial())
207  {
208  return;
209  }
211  std::dynamic_pointer_cast<LibUtilities::CommMpi>(pComm);
212  ASSERTL1(vCommMpi, "Failed to cast MPI Comm object.");
213  comm vComm;
214  vComm.c = vCommMpi->GetComm();
215  vComm.id = vCommMpi->GetRank();
216  vComm.np = vCommMpi->GetSize();
217  nektar_gs_unique(pId.get(), pId.size(), &vComm);
218 #else
219  boost::ignore_unused(pId, pComm);
220 #endif
221 }
222 
223 /**
224  * @brief Deallocates the GSLib mapping data.
225  */
226 static inline void Finalise(gs_data *pGsh)
227 {
228 #ifdef NEKTAR_USE_MPI
229  int finalized;
230  MPI_Finalized(&finalized);
231  if (pGsh && !finalized)
232  {
233  nektar_gs_free(pGsh);
234  }
235 #else
236  boost::ignore_unused(pGsh);
237 #endif
238 }
239 
240 /**
241  * @brief Performs a gather-scatter operation of the provided values.
242  *
243  * The
244  */
245 static inline void Gather(
248 {
249 #ifdef NEKTAR_USE_MPI
250  if (!pGsh)
251  {
252  return;
253  }
254  if (pBuffer.size() == 0)
255  {
256  nektar_gs(pU.get(), gs_double, pOp, false, pGsh, 0);
257  }
258  else
259  {
260  array buf;
261  buf.ptr = &pBuffer[0];
262  buf.n = pBuffer.size();
263  nektar_gs(pU.get(), gs_double, pOp, false, pGsh, &buf);
264  }
265 #else
266  boost::ignore_unused(pU, pOp, pGsh, pBuffer);
267 #endif
268 }
269 }
270 
271 #endif
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
Definition: GsLib.hpp:49
gs_dom
Definition: GsLib.hpp:52
@ gs_dom_n
Definition: GsLib.hpp:52
@ gs_int
Definition: GsLib.hpp:52
@ gs_double
Definition: GsLib.hpp:52
@ gs_long
Definition: GsLib.hpp:52
@ gs_float
Definition: GsLib.hpp:52
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:245
int comm_req
Definition: GsLib.hpp:67
gs_op
Definition: GsLib.hpp:53
@ gs_amax
Definition: GsLib.hpp:53
@ gs_add
Definition: GsLib.hpp:53
@ gs_max
Definition: GsLib.hpp:53
@ gs_bpr
Definition: GsLib.hpp:53
@ gs_min
Definition: GsLib.hpp:53
@ gs_op_n
Definition: GsLib.hpp:53
@ gs_mul
Definition: GsLib.hpp:53
void fin_fun(void *data)
Definition: GsLib.hpp:118
static void Finalise(gs_data *pGsh)
Deallocates the GSLib mapping data.
Definition: GsLib.hpp:226
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:61
int comm_ext
Definition: GsLib.hpp:66
gs_method
Definition: GsLib.hpp:137
@ gs_all_reduce
Definition: GsLib.hpp:141
@ gs_pairwise
Definition: GsLib.hpp:139
@ gs_crystal_router
Definition: GsLib.hpp:140
@ gs_auto
Definition: GsLib.hpp:138
static gs_data * Init(const Nektar::Array< OneD, long > pId, const LibUtilities::CommSharedPtr &pComm, bool verbose=true)
Initialise Gather-Scatter map.
Definition: GsLib.hpp:167
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:115
gs_data * nektar_gs_setup(const long *id, unsigned int n, const struct comm *comm, int unique, gs_method method, int verbose)
gs_mode
Definition: GsLib.hpp:54
@ mode_vec
Definition: GsLib.hpp:54
@ mode_plain
Definition: GsLib.hpp:54
@ mode_dry_run
Definition: GsLib.hpp:54
@ mode_many
Definition: GsLib.hpp:54
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:202
void nektar_gs_free(gs_data *gsh)
std::shared_ptr< CommMpi > CommMpiSharedPtr
Pointer to a Communicator object.
Definition: CommMpi.h:55
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static Array< OneD, NekDouble > NullNekDouble1DArray
unsigned int buffer_size
Definition: GsLib.hpp:112
size_t max
Definition: GsLib.hpp:59
void * ptr
Definition: GsLib.hpp:58
comm_ext c
Definition: GsLib.hpp:74
unsigned int np
Definition: GsLib.hpp:73
unsigned int id
Definition: GsLib.hpp:72
unsigned int buffer_size
Definition: GsLib.hpp:106
unsigned int nstages
Definition: GsLib.hpp:105
unsigned int size_s
Definition: GsLib.hpp:97
unsigned int size_r
Definition: GsLib.hpp:96
unsigned int p1
Definition: GsLib.hpp:98
const unsigned int * gather_map
Definition: GsLib.hpp:95
unsigned int nrecvn
Definition: GsLib.hpp:99
unsigned int handle_size
Definition: GsLib.hpp:134
gs_remote r
Definition: GsLib.hpp:133
const unsigned int * flagged_primaries
Definition: GsLib.hpp:132
exec_fun * exec
Definition: GsLib.hpp:124
unsigned int buffer_size
Definition: GsLib.hpp:122
void * data
Definition: GsLib.hpp:123
unsigned int n
Definition: GsLib.hpp:79
unsigned int * size
Definition: GsLib.hpp:81
unsigned int total
Definition: GsLib.hpp:82
unsigned int * p
Definition: GsLib.hpp:80
comm_req * req
Definition: GsLib.hpp:89
unsigned int buffer_size
Definition: GsLib.hpp:90