Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
gs.hpp
Go to the documentation of this file.
1 #ifndef GS_HPP
2 #define GS_HPP
3 
4 #include "moab/MOABConfig.h"
5 #include "moab/TupleList.hpp"
6 #include "moab/Types.hpp"
7 
8 #ifdef MOAB_HAVE_MPI
9 #include "moab_mpi.h"
10 #endif
11 
12 namespace moab
13 {
14 
15 class gs_data
16 {
17  public:
18 #ifdef MOAB_HAVE_MPI
19  class nonlocal_info
20  {
21  public:
22  uint _np; /* number of processors to communicate with */
23  uint* _target; /* int target[np]: array of processor ids to comm w/ */
24  uint* _nshared; /* nshared[i] = number of points shared w/ target[i] */
25  uint* _sh_ind; /* list of shared point indices */
26  slong* _slabels; /* list of signed long labels (not including gid) */
27  Ulong* _ulabels; /* list of unsigned long labels */
28  MPI_Request* _reqs; /* pre-allocated for MPI calls */
29  realType* _buf; /* pre-allocated buffer to receive data */
30  uint _maxv; /* maximum vector size */
31 
32  /**Constructor for nonlocal_info; takes all arguments and initializes
33  * nonlocal_info
34  *
35  * param np number of processors to communicate with
36  * param count number of partner processors
37  * param nlabels number of signed long labels (not including gid)
38  * param nulabels number of unsigned long labels
39  * param maxv maximum vector size
40  */
41  nonlocal_info( uint tmp_np, uint count, uint nlabels, uint nulabels, uint tmp_maxv )
42  {
43  this->initialize( tmp_np, count, nlabels, nulabels, tmp_maxv );
44  }
45 
46  /**Initializes nonlocal_info; see constructor for parameter documentation
47  */
48  void initialize( uint np, uint count, uint nlabels, uint nulabels, uint maxv );
49 
50  ~nonlocal_info()
51  {
52  nlinfo_free();
53  };
54 
55  void nonlocal( realType* u, int op, MPI_Comm comm );
56  void nonlocal_vec( realType* u, uint n, int op, MPI_Comm comm );
57  void nonlocal_many( realType** u, uint n, int op, MPI_Comm comm );
58  void nlinfo_free();
59  };
60 
61  public:
62  /*---------------------------------------------------------------------------
63 
64  Crystal Router
65 
66  Accomplishes all-to-all communication in log P msgs per proc
67  The routine is low-level; the format of the input/output is an
68  array of integers, consisting of a sequence of messages with format:
69 
70  target proc
71  source proc
72  m
73  integer
74  integer
75  ...
76  integer (m integers in total)
77 
78  Before moab_crystal_router is called, the source of each message should be
79  set to this proc id; upon return from moab_crystal_router, the target of each
80  message will be this proc id.
81 
82  Usage:
83 
84  MPI_Comm comm = ... ;
85  moab_crystal_data crystal;
86  //moab_crystal_data crystal(comm); //or this to initialize on
87  //instantiation
88 
89  crystal.initialize(comm); // initialize the data structure
90  // now crystal.id = this proc
91  // and crystal.num = num of procs
92 
93  // allocate space for at least MAX ints
94  buffer_reserve(&crystal->all->buf, MAX*sizeof(uint));
95 
96  // fill up ((uint*)crystal->all->buf.ptr)[0 ... n-1]
97  // and set crystal->all->n
98 
99  crystal.moab_crystal_router();
100 
101  // incoming messages available as
102  // ((uint*)crystal->all->buf.ptr)[0 ... crystal->all->n-1]
103 
104  crystal.reset(); // release acquired memory
105 
106  ---------------------------------------------------------------------------*/
107 
108  class crystal_data
109  {
110  public:
111  // moab_crystal_data member variables & data
112  typedef struct
113  {
114  uint n;
116  } crystal_buf;
117  crystal_buf buffers[3];
118  // crystal_buf provides buffer space for communications
119  crystal_buf *all, *keep, *send;
120  MPI_Comm _comm;
121  uint _num, _id;
122 
123  /**Default constructor (Note: moab_crystal_data must be initialized
124  * before use!)
125  */
126  crystal_data();
127 
128  /**Constructor takes an MPI_Comm and initializes the moab_data_crystal
129  */
130  crystal_data( MPI_Comm cm )
131  {
132  initialize( cm );
133  };
134 
135  ~crystal_data()
136  {
137  reset();
138  };
139 
140  /**Initializes crystal_data members according to MPI_Comm passed in
141  * Note: moab_crystal_data must be initialized before it can be used
142  *
143  * param comm MPI_Comm detailing where to send messages
144  */
145  void initialize( MPI_Comm comm );
146 
147  /**Frees buffers used by moab_crystal_data
148  */
149  void reset();
150 
151  /**Communicates messages with other processors; see class description for
152  * more info and usage
153  */
154  void crystal_router();
155 
156  /**Treats one integer (not long) member of the TupleList as a target proc;
157  * Sends out tuples accordingly, using the crystal router.
158  * Target proc member overwritten with source proc.
159  *
160  * param dynamic non-zero if the TupleList should grow to accomodate
161  * arrivals
162  * param tl the TupleList
163  * param pf which tuple member specifies target proc
164  */
165  ErrorCode gs_transfer( int dynamic, moab::TupleList& tl, unsigned pf );
166 
167  private:
168  // Used by moab_crystal_router: see .cpp for more details
169  void partition( uint cutoff, crystal_buf* lo, crystal_buf* hi );
170 
171  void send_( uint target, int recvn );
172  };
173 #else
174  // If mpi is not used, moab_crystal_data cannot be used
176  {
177  };
178 #endif
179 
180  sint* local_cm; /* local condense map */
181 #ifdef MOAB_HAVE_MPI
182  nonlocal_info* nlinfo;
183  MPI_Comm _comm;
184 #endif
185 
186  /**Constructor for moab_gs_data: takes all arguments and initializes
187  * moab_gs_data. If needs_reset is false after calling constructor,
188  * initialization has failed.
189  *
190  * param n number of tuples in tuple list
191  * param label pointer to signed labels
192  * param ulabel pointer to unsigned labels
193  * param maxv max vector size
194  * param nlabels number of signed long labels (not including gid)
195  * param nulabels number of unsigned long labels
196  * param crystal moab_crystal_data contains MPI_Comm and is used for
197  * message passing
198  */
200  const long* label,
201  const Ulong* ulabel,
202  uint maxv,
203  const unsigned int nlabels,
204  const unsigned int nulabels,
205  crystal_data* crystal,
206  ErrorCode& Init_Result )
207  {
208  Init_Result = this->initialize( n, label, ulabel, maxv, nlabels, nulabels, crystal );
209  };
210 
211  /**Default constructor (Note: moab_gs_data must be initialized
212  * before use!)
213  */
214  gs_data(){};
215 
217  {
218  reset();
219  }
220 
221  /**Sets up the moab_gs_data; see constructor for parameter documentation
222  */
224  const long* label,
225  const Ulong* ulabel,
226  uint maxv,
227  const unsigned int nlabels,
228  const unsigned int nulabels,
229  crystal_data* crystal );
230 
231  void reset();
232 
233  void gs_data_op( realType* u, int op );
234  void gs_data_op_vec( realType* u, uint n, int op );
235  void gs_data_op_many( realType** u, uint n, int op );
236 
237 #define GS_OP_ADD 1
238 #define GS_OP_MUL 2
239 #define GS_OP_MIN 3
240 #define GS_OP_MAX 4
241 #define GS_OP_BPR 5
242 };
243 
244 } // namespace moab
245 #endif