PLE
Parallel Location and Exchange
ple_locator.h
Go to the documentation of this file.
1 #ifndef __PLE_LOCATOR_H__
2 #define __PLE_LOCATOR_H__
3 
4 /*============================================================================
5  * Locate points in a nodal representation associated with a mesh
6  *============================================================================*/
7 
8 /*
9  This file is part of the "Parallel Location and Exchange" library,
10  intended to provide mesh or particle-based code coupling services.
11 
12  Copyright (C) 2005-2020 EDF S.A.
13 
14  This library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU Lesser General Public
16  License as published by the Free Software Foundation; either
17  version 2.1 of the License, or (at your option) any later version.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28 
29 /*----------------------------------------------------------------------------*/
30 
31 #include "ple_config.h"
32 
33 #if defined(PLE_HAVE_MPI)
34 #include <mpi.h>
35 #endif
36 
37 /*----------------------------------------------------------------------------
38  * Local headers
39  *----------------------------------------------------------------------------*/
40 
41 #include "ple_defs.h"
42 
43 /*----------------------------------------------------------------------------*/
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #if 0
48 } /* Fake brace to force back Emacs auto-indentation back to column 0 */
49 #endif
50 #endif /* __cplusplus */
51 
52 /*=============================================================================
53  * Macro definitions
54  *============================================================================*/
55 
56 /*============================================================================
57  * Type definitions
58  *============================================================================*/
59 
60 /*============================================================================
61  * Type definitions
62  *============================================================================*/
63 
64 /* PLE option types */
65 
66 typedef enum {
67 
70 
72 
73 /*----------------------------------------------------------------------------
74  * Query number of extents and compute extents of a mesh representation.
75  *
76  * For future optimizations, computation of extents should not be limited
77  * to mesh extents, but to 1 to n extents, allowing different extent
78  * refinements, from global mesh to individual element extents.
79  *
80  * The minimum required functionality for this function is to compute
81  * whole mesh extents, but it could also return extents of individual
82  * elements, or intermediate extents of mesh subdivisions or coarsened
83  * elements. As such, it takes an argument indicating the maximum
84  * local number of extents it should compute (based on the size of
85  * the extents array argument), but returns the number of extents
86  * really computed, which may be lower (usually 1 for mesh extents,
87  * possibly even 0 if the local mesh is empty). If n_max_extents = 1,
88  * the whole mesh extents should be computed.
89  *
90  * If n_max_extents is set to a negative value (-1), no extents are computed,
91  * but the function returns the maximum number of extents it may compute.
92  * This query mode allows for the caller to allocate the correct amount
93  * of memory for a subsequent call.
94  *
95  * parameters:
96  * mesh <-- pointer to mesh representation structure
97  * n_max_extents <-- maximum number of sub-extents (such as element extents)
98  * to compute, or -1 to query
99  * tolerance <-- addition to local extents of each element:
100  * extent = base_extent * (1 + tolerance)
101  * extents <-> extents associated with the mesh or elements (or even
102  * aggregated elements in case of coarser representation):
103  * x_min_0, y_min_0, ..., x_max_i, y_max_i, ...
104  * (size: 2*dim*n_max_extents), ignored in query mode
105  * returns:
106  * the number of extents computed
107  *----------------------------------------------------------------------------*/
108 
109 typedef ple_lnum_t
110 (ple_mesh_extents_t) (const void *mesh,
111  ple_lnum_t n_max_extents,
112  double tolerance,
113  double extents[]);
114 
115 /*----------------------------------------------------------------------------
116  * Find elements in a given local mesh containing points: updates the
117  * location[] and distance[] arrays associated with a set of points
118  * for points that are in an element of this mesh, or closer to one
119  * than to previously encountered elements.
120  *
121  * parameters:
122  * this_nodal <-- pointer to nodal mesh representation structure
123  * tolerance_base <-- associated base tolerance (used for bounding
124  * box check only, not for location test)
125  * tolerance_fraction <-- associated fraction of element bounding boxes
126  * added to tolerance
127  * n_points <-- number of points to locate
128  * point_coords <-- point coordinates (interleaved)
129  * point_tag <-- optional point tag (size: n_points)
130  * location <-> number of element containing or closest to each
131  * point (size: n_points)
132  * distance <-> distance from point to element indicated by
133  * location[]: < 0 if unlocated, 0 - 1 if inside,
134  * and > 1 if outside a volume element, or absolute
135  * distance to a surface element (size: n_points)
136  *----------------------------------------------------------------------------*/
137 
138 typedef void
139 (ple_mesh_elements_locate_t) (const void *mesh,
140  float tolerance_base,
141  float tolerance_fraction,
142  ple_lnum_t n_points,
143  const ple_coord_t point_coords[],
144  const int point_tag[],
145  ple_lnum_t location[],
146  float distance[]);
147 
148 /*----------------------------------------------------------------------------
149  * Function pointer type for user definable logging/profiling type functions
150  *----------------------------------------------------------------------------*/
151 
152 typedef int
153 (ple_locator_log_t) (int event,
154  int data,
155  const char *string);
156 
157 /*----------------------------------------------------------------------------
158  * Structure defining a locator
159  *----------------------------------------------------------------------------*/
160 
161 typedef struct _ple_locator_t ple_locator_t;
162 
163 /*=============================================================================
164  * Static global variables
165  *============================================================================*/
166 
167 /*=============================================================================
168  * Public function prototypes
169  *============================================================================*/
170 
171 /*----------------------------------------------------------------------------
172  * Creation of a locator structure.
173  *
174  * Note that depending on the choice of ranks of the associated communicator,
175  * distant ranks may in fact be truly distant or not. If n_ranks = 1 and
176  * start_rank is equal to the current rank in the communicator, the locator
177  * will work only locally.
178  *
179  * parameters:
180  * comm <-- associated MPI communicator
181  * n_ranks <-- number of MPI ranks associated with distant location
182  * start_rank <-- first MPI rank associated with distant location
183  *
184  * returns:
185  * pointer to locator
186  *----------------------------------------------------------------------------*/
187 
188 #if defined(PLE_HAVE_MPI)
189 
190 ple_locator_t *
191 ple_locator_create(MPI_Comm comm,
192  int n_ranks,
193  int start_rank);
194 
195 #else
196 
197 ple_locator_t *
198 ple_locator_create(void);
199 
200 #endif
201 
202 /*----------------------------------------------------------------------------
203  * Destruction of a locator structure.
204  *
205  * parameters:
206  * this_locator <-> locator to destroy
207  *
208  * returns:
209  * NULL pointer
210  *----------------------------------------------------------------------------*/
211 
212 ple_locator_t *
213 ple_locator_destroy(ple_locator_t * this_locator);
214 
215 /*----------------------------------------------------------------------------
216  * Prepare locator for use with a given mesh representation.
217  *
218  * parameters:
219  * this_locator <-> pointer to locator structure
220  * mesh <-- pointer to mesh representation structure
221  * options <-- options array (size PLE_LOCATOR_N_OPTIONS),
222  * or NULL
223  * tolerance_base <-- associated base tolerance (used for bounding
224  * box check only, not for location test)
225  * tolerance_fraction <-- associated fraction of element bounding boxes
226  * added to tolerance
227  * dim <-- spatial dimension of mesh and points to locate
228  * n_points <-- number of points to locate
229  * point_list <-- optional indirection array to point_coords
230  * point_tag <-- optional point tag (size: n_points)
231  * point_coords <-- coordinates of points to locate
232  * (dimension: dim * n_points)
233  * distance --> optional distance from point to matching element:
234  * < 0 if unlocated; 0 - 1 if inside and > 1 if
235  * outside a volume element, or absolute distance
236  * to a surface element (size: n_points)
237  * mesh_extents_f <-- pointer to function computing mesh extents
238  * locate_f <-- pointer to function wich updates the location[]
239  * and distance[] arrays associated with a set of
240  * points for points that are in an element of this
241  * mesh, or closer to one than to previously
242  * encountered elements.
243  *----------------------------------------------------------------------------*/
244 
245 void
246 ple_locator_set_mesh(ple_locator_t *this_locator,
247  const void *mesh,
248  const int *options,
249  float tolerance_base,
250  float tolerance_fraction,
251  int dim,
252  ple_lnum_t n_points,
253  const ple_lnum_t point_list[],
254  const int point_tag[],
255  const ple_coord_t point_coords[],
256  float distance[],
257  ple_mesh_extents_t *mesh_extents_f,
258  ple_mesh_elements_locate_t *mesh_elements_locate_f);
259 
260 /*----------------------------------------------------------------------------
261  * Extend search for a locator for which set_mesh has already been called.
262  *
263  * parameters:
264  * this_locator <-> pointer to locator structure
265  * mesh <-- pointer to mesh representation structure
266  * options <-- options array (size PLE_LOCATOR_N_OPTIONS),
267  * or NULL
268  * tolerance_base <-- associated base tolerance (used for bounding
269  * box check only, not for location test)
270  * tolerance_fraction <-- associated fraction of element bounding boxes
271  * added to tolerance
272  * n_points <-- number of points to locate
273  * point_list <-- optional indirection array to point_coords
274  * point_tag <-- optional point tag (size: n_points)
275  * point_coords <-- coordinates of points to locate
276  * (dimension: dim * n_points)
277  * distance --> optional distance from point to matching element:
278  * < 0 if unlocated; 0 - 1 if inside and > 1 if
279  * outside a volume element, or absolute distance
280  * to a surface element (size: n_points)
281  * mesh_extents_f <-- pointer to function computing mesh extents
282  * locate_f <-- pointer to function wich updates the location[]
283  * and distance[] arrays associated with a set of
284  * points for points that are in an element of this
285  * mesh, or closer to one than to previously
286  * encountered elements.
287  */
288 /*----------------------------------------------------------------------------*/
289 
290 void
291 ple_locator_extend_search(ple_locator_t *this_locator,
292  const void *mesh,
293  const int *options,
294  float tolerance_base,
295  float tolerance_fraction,
296  ple_lnum_t n_points,
297  const ple_lnum_t point_list[],
298  const int point_tag[],
299  const ple_coord_t point_coords[],
300  float distance[],
301  ple_mesh_extents_t *mesh_extents_f,
302  ple_mesh_elements_locate_t *mesh_locate_f);
303 
304 /*----------------------------------------------------------------------------
305  * Shift location ids for located points after locator initialization.
306  *
307  * This is useful mainly to switch between 0-based to 1-based numberings.
308  *
309  * parameters:
310  * this_locator <-> pointer to locator structure
311  * location_shift <-- shift value
312  *----------------------------------------------------------------------------*/
313 
314 void
315 ple_locator_shift_locations(ple_locator_t *this_locator,
316  ple_lnum_t location_shift);
317 
318 /*----------------------------------------------------------------------------
319  * Return number of distant points after locator initialization.
320  *
321  * parameters:
322  * this_locator <-- pointer to locator structure
323  *
324  * returns:
325  * number of distant points.
326  *----------------------------------------------------------------------------*/
327 
329 ple_locator_get_n_dist_points(const ple_locator_t *this_locator);
330 
331 /*----------------------------------------------------------------------------
332  * Return an array of local element numbers containing (or nearest to)
333  * each distant point after locator initialization.
334  *
335  * parameters:
336  * this_locator <-- pointer to locator structure
337  *
338  * returns:
339  * local element numbers associated with distant points.
340  *----------------------------------------------------------------------------*/
341 
342 const ple_lnum_t *
343 ple_locator_get_dist_locations(const ple_locator_t *this_locator);
344 
345 /*----------------------------------------------------------------------------
346  * Return an array of coordinates of each distant point after
347  * locator initialization.
348  *
349  * parameters:
350  * this_locator <-- pointer to locator structure
351  *
352  * returns:
353  * coordinate array associated with distant points (interlaced).
354  *----------------------------------------------------------------------------*/
355 
356 const ple_coord_t *
357 ple_locator_get_dist_coords(const ple_locator_t *this_locator);
358 
359 /*----------------------------------------------------------------------------
360  * Return number of points located after locator initialization.
361  *
362  * parameters:
363  * this_locator <-- pointer to locator structure
364  *
365  * returns:
366  * number of points located.
367  *----------------------------------------------------------------------------*/
368 
370 ple_locator_get_n_interior(const ple_locator_t *this_locator);
371 
372 /*----------------------------------------------------------------------------
373  * Return list of points located after locator initialization.
374  * This list defines a subset of the point set used at initialization.
375  *
376  * parameters:
377  * this_locator <-- pointer to locator structure
378  *
379  * returns:
380  * list of points located.
381  *----------------------------------------------------------------------------*/
382 
383 const ple_lnum_t *
384 ple_locator_get_interior_list(const ple_locator_t *this_locator);
385 
386 /*----------------------------------------------------------------------------
387  * Return number of points not located after locator initialization.
388  *
389  * parameters:
390  * this_locator <-- pointer to locator structure
391  *
392  * returns:
393  * number of points not located.
394  *----------------------------------------------------------------------------*/
395 
397 ple_locator_get_n_exterior(const ple_locator_t *this_locator);
398 
399 /*----------------------------------------------------------------------------
400  * Return list of points not located after locator initialization.
401  * This list defines a subset of the point set used at initialization.
402  *
403  * parameters:
404  * this_locator <-- pointer to locator structure
405  *
406  * returns:
407  * list of points not located.
408  *----------------------------------------------------------------------------*/
409 
410 const ple_lnum_t *
411 ple_locator_get_exterior_list(const ple_locator_t *this_locator);
412 
413 /*----------------------------------------------------------------------------
414  * Discard list of points not located after locator initialization.
415  * This list defines a subset of the point set used at initialization.
416  *
417  * parameters:
418  * this_locator <-- pointer to locator structure
419  *----------------------------------------------------------------------------*/
420 
421 void
422 ple_locator_discard_exterior(ple_locator_t *this_locator);
423 
424 /*----------------------------------------------------------------------------
425  * Distribute variable defined on distant points to processes owning
426  * the original points (i.e. distant processes).
427  *
428  * The exchange is symmetric if both variables are defined, receive
429  * only if distant_var is NULL, or send only if local_var is NULL.
430  *
431  * The caller should have defined the values of distant_var[] for the
432  * distant points, whose coordinates are given by
433  * ple_locator_get_dist_coords(), and which are located in the elements
434  * whose numbers are given by ple_locator_get_dist_locations().
435  *
436  * The local_var[] is defined at the located points (those whose
437  * numbers are returned by ple_locator_get_interior_list().
438  *
439  * If the optional local_list indirection is used, it is assumed to use
440  * the same base numbering as that defined by the options for the previous
441  * call to ple_locator_set_mesh() or ple_locator_extend_search().
442  *
443  * parameters:
444  * this_locator <-- pointer to locator structure
445  * distant_var <-> variable defined on distant points (ready to send)
446  * size: n_dist_points*stride
447  * local_var <-> variable defined on located local points (received)
448  * size: n_interior*stride
449  * local_list <-- optional indirection list for local_var
450  * type_size <-- sizeof (float or double) variable type
451  * stride <-- dimension (1 for scalar, 3 for interlaced vector)
452  * reverse <-- if nonzero, exchange is reversed
453  * (receive values associated with distant points
454  * from the processes owning the original points)
455  *----------------------------------------------------------------------------*/
456 
457 void
458 ple_locator_exchange_point_var(ple_locator_t *this_locator,
459  void *distant_var,
460  void *local_var,
461  const ple_lnum_t *local_list,
462  size_t type_size,
463  size_t stride,
464  int reverse);
465 
466 /*----------------------------------------------------------------------------
467  * Return timing information.
468  *
469  * In parallel mode, this includes communication time.
470  *
471  * parameters:
472  * this_locator <-- pointer to locator structure
473  * location_wtime --> Location Wall-clock time (or NULL)
474  * location_cpu_time --> Location CPU time (or NULL)
475  * exchange_wtime --> Variable exchange Wall-clock time (or NULL)
476  * exchange_cpu_time --> Variable exchange CPU time (or NULL)
477  *----------------------------------------------------------------------------*/
478 
479 void
480 ple_locator_get_times(const ple_locator_t *this_locator,
481  double *location_wtime,
482  double *location_cpu_time,
483  double *exchange_wtime,
484  double *exchange_cpu_time);
485 
486 /*----------------------------------------------------------------------------
487  * Return communication timing information.
488  *
489  * In serial mode, returned times are always zero..
490  *
491  * parameters:
492  * this_locator <-- pointer to locator structure
493  * location_wtime --> Location Wall-clock time (or NULL)
494  * location_cpu_time --> Location CPU time (or NULL)
495  * exchange_wtime --> Variable exchange Wall-clock time (or NULL)
496  * exchange_cpu_time --> Variable exchange CPU time (or NULL)
497  *----------------------------------------------------------------------------*/
498 
499 void
500 ple_locator_get_comm_times(const ple_locator_t *this_locator,
501  double *location_wtime,
502  double *location_cpu_time,
503  double *exchange_wtime,
504  double *exchange_cpu_time);
505 
506 /*----------------------------------------------------------------------------
507  * Dump printout of a locator structure.
508  *
509  * parameters:
510  * this_locator <-- pointer to structure that should be dumped
511  *----------------------------------------------------------------------------*/
512 
513 void
514 ple_locator_dump(const ple_locator_t *this_locator);
515 
516 /*----------------------------------------------------------------------------
517  * Get the maximum number of exchanging ranks for which we use asynchronous
518  * MPI sends and receives instead of MPI_SendRecv.
519  *
520  * returns:
521  * the maximum number of ranks allowing asynchronous exchanges
522  *----------------------------------------------------------------------------*/
523 
524 #if defined(PLE_HAVE_MPI)
525 
526 int
528 
529 #endif /* defined(PLE_HAVE_MPI) */
530 
531 /*----------------------------------------------------------------------------
532  * Set the maximum number of exchanging ranks for which we use asynchronous
533  * MPI sends and receives instead of MPI_SendRecv.
534  *
535  * parameters:
536  * threshold <-- maximum number of ranks allowing asynchronous exchanges
537  *----------------------------------------------------------------------------*/
538 
539 #if defined(PLE_HAVE_MPI)
540 
541 void
542 ple_locator_set_async_threshold(int threshold);
543 
544 #endif /* defined(PLE_HAVE_MPI) */
545 
546 /*----------------------------------------------------------------------------
547  * Register communication logging functions for locator instrumentation.
548  *
549  * By default, locators are not instrumented.
550  *
551  * Functions using MPE may be defined and used, but other similar systems
552  * may be used.
553  *
554  * parameters:
555  * log_function <-- pointer to logging function
556  * start_p_comm <-- point to point communication start event number
557  * end_p_comm <-- point to point communication end event number
558  * start_g_comm <-- global communication start event number
559  * end_g_comm <-- global communication end event number
560  *----------------------------------------------------------------------------*/
561 
562 #if defined(PLE_HAVE_MPI)
563 
564 void
566  int start_p_comm,
567  int end_p_comm,
568  int start_g_comm,
569  int end_g_comm);
570 
571 #endif /* defined(PLE_HAVE_MPI) */
572 
573 /*----------------------------------------------------------------------------*/
574 
575 #ifdef __cplusplus
576 }
577 #endif /* __cplusplus */
578 
579 #endif /* __PLE_LOCATOR_H__ */
ple_lnum_t ple_locator_get_n_dist_points(const ple_locator_t *this_locator)
Return number of distant points after locator initialization.
Definition: ple_locator.c:3297
int ple_locator_get_async_threshold(void)
Get the maximum number of exchanging ranks for which we use asynchronous MPI sends and receives inste...
Definition: ple_locator.c:3805
const ple_lnum_t * ple_locator_get_exterior_list(const ple_locator_t *this_locator)
Return list of points not located after locator initialization. This list defines a subset of the poi...
Definition: ple_locator.c:3423
void ple_locator_get_times(const ple_locator_t *this_locator, double *location_wtime, double *location_cpu_time, double *exchange_wtime, double *exchange_cpu_time)
Return timing information.
Definition: ple_locator.c:3580
Definition: ple_locator.h:69
const ple_coord_t * ple_locator_get_dist_coords(const ple_locator_t *this_locator)
Return an array of coordinates of each distant point after locator initialization.
Definition: ple_locator.c:3345
ple_locator_t * ple_locator_destroy(ple_locator_t *this_locator)
Destruction of a locator structure.
Definition: ple_locator.c:2857
void ple_locator_set_mesh(ple_locator_t *this_locator, const void *mesh, const int *options, float tolerance_base, float tolerance_fraction, int dim, ple_lnum_t n_points, const ple_lnum_t point_list[], const int point_tag[], const ple_coord_t point_coords[], float distance[], ple_mesh_extents_t *mesh_extents_f, ple_mesh_elements_locate_t *mesh_elements_locate_f)
Prepare locator for use with a given mesh representation.
Definition: ple_locator.c:2917
Definition: ple_locator.h:68
ple_lnum_t() ple_mesh_extents_t(const void *mesh, ple_lnum_t n_max_extents, double tolerance, double extents[])
Definition: ple_locator.h:110
void ple_locator_exchange_point_var(ple_locator_t *this_locator, void *distant_var, void *local_var, const ple_lnum_t *local_list, size_t type_size, size_t stride, int reverse)
Distribute variable defined on distant points to processes owning the original points (i...
Definition: ple_locator.c:3480
void ple_locator_discard_exterior(ple_locator_t *this_locator)
Discard list of points not located after locator initialization. This list defines a subset of the po...
Definition: ple_locator.c:3438
ple_lnum_t ple_locator_get_n_exterior(const ple_locator_t *this_locator)
Return number of points not located after locator initialization.
Definition: ple_locator.c:3406
int ple_lnum_t
Definition: ple_defs.h:146
ple_locator_option_t
Definition: ple_locator.h:66
void ple_locator_set_comm_log(ple_locator_log_t *log_function, int start_p_comm, int end_p_comm, int start_g_comm, int end_g_comm)
Register communication logging functions for locator instrumentation.
Definition: ple_locator.c:3843
double ple_coord_t
Definition: ple_defs.h:149
void ple_locator_get_comm_times(const ple_locator_t *this_locator, double *location_wtime, double *location_cpu_time, double *exchange_wtime, double *exchange_cpu_time)
Return communication timing information.
Definition: ple_locator.c:3612
void ple_locator_shift_locations(ple_locator_t *this_locator, ple_lnum_t location_shift)
Shift location ids for located points after locator initialization.
Definition: ple_locator.c:3271
void ple_locator_set_async_threshold(int threshold)
Set the maximum number of exchanging ranks for which we use asynchronous MPI sends and receives inste...
Definition: ple_locator.c:3820
int() ple_locator_log_t(int event, int data, const char *string)
Definition: ple_locator.h:153
void() ple_mesh_elements_locate_t(const void *mesh, float tolerance_base, float tolerance_fraction, ple_lnum_t n_points, const ple_coord_t point_coords[], const int point_tag[], ple_lnum_t location[], float distance[])
Definition: ple_locator.h:139
const ple_lnum_t * ple_locator_get_interior_list(const ple_locator_t *this_locator)
Return list of points located after locator initialization. This list defines a subset of the point s...
Definition: ple_locator.c:3390
ple_locator_t * ple_locator_create(MPI_Comm comm, int n_ranks, int start_rank)
Creation of a locator structure.
Definition: ple_locator.c:2785
const ple_lnum_t * ple_locator_get_dist_locations(const ple_locator_t *this_locator)
Return an array of local element numbers containing (or nearest to) each distant point after locator ...
Definition: ple_locator.c:3321
ple_lnum_t ple_locator_get_n_interior(const ple_locator_t *this_locator)
Return number of points located after locator initialization.
Definition: ple_locator.c:3368
void ple_locator_dump(const ple_locator_t *this_locator)
Dump printout of a locator structure.
Definition: ple_locator.c:3633
void ple_locator_extend_search(ple_locator_t *this_locator, const void *mesh, const int *options, float tolerance_base, float tolerance_fraction, ple_lnum_t n_points, const ple_lnum_t point_list[], const int point_tag[], const ple_coord_t point_coords[], float distance[], ple_mesh_extents_t *mesh_extents_f, ple_mesh_elements_locate_t *mesh_locate_f)
Extend search for a locator for which set_mesh has already been called.
Definition: ple_locator.c:3007