PLE
Parallel Location and Exchange
ple_coupling.h
Go to the documentation of this file.
1 #ifndef __PLE_COUPLING_H__
2 #define __PLE_COUPLING_H__
3 
4 /*============================================================================
5  * Set up communication with coupled codes.
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 Emacs auto-indentation back to column 0 */
49 #endif
50 #endif /* __cplusplus */
51 
52 /*=============================================================================
53  * Macro definitions
54  *============================================================================*/
55 
56 /*
57  * Mask for synchronization flag
58  */
59 
60 /* Command bits */
61 
62 #define PLE_COUPLING_INIT (1 << 0) /* Not yet synchronized */
63 
64 #define PLE_COUPLING_NO_SYNC (1 << 1) /* Not synchronized */
65 #define PLE_COUPLING_STOP (1 << 2) /* Will stop immediately */
66 #define PLE_COUPLING_LAST (1 << 3) /* Last synchronization */
67 
68 /* Time stepping bits */
69 
70 #define PLE_COUPLING_NEW_ITERATION (1 << 4)
71 #define PLE_COUPLING_REDO_ITERATION (1 << 5)
72 
73 /* Time step value handling bits */
74 
75 #define PLE_COUPLING_TS_MIN (1 << 6) /* Use smallest time step */
76 #define PLE_COUPLING_TS_LEADER (1 << 7) /* Prescribe time step for all
77  members of group (only one
78  member may set this flag) */
79 
80 /* Calculation type or state information bits */
81 
82 #define PLE_COUPLING_UNSTEADY (1 << 8)
83 #define PLE_COUPLING_STEADY (1 << 9)
84 #define PLE_COUPLING_CONVERGED (1 << 10)
85 
86 /* Optional user code information bits */
87 
88 #define PLE_COUPLING_USER_1 (1 << 11)
89 #define PLE_COUPLING_USER_2 (1 << 12)
90 #define PLE_COUPLING_USER_3 (1 << 13)
91 #define PLE_COUPLING_USER_4 (1 << 14)
92 
93 /*============================================================================
94  * Type definitions
95  *============================================================================*/
96 
97 #if defined(PLE_HAVE_MPI)
98 
99 /* Opaque code coupling information structure */
100 
101 typedef struct _ple_coupling_mpi_set_t ple_coupling_mpi_set_t;
102 
103 /* Info structure for code coupling */
104 
105 typedef struct {
106 
107  int status; /* Status flag for synchronization info */
108  int root_rank; /* Application root rank in MPI_COMM_WORLD */
109  int n_ranks; /* Number of ranks associated with application */
110  const char *app_type; /* Application type name (may be empty) */
111  const char *app_name; /* Application instance name (may be empty) */
112 
114 
115 #endif /* defined(PLE_HAVE_MPI) */
116 
117 /*=============================================================================
118  * Public function prototypes
119  *============================================================================*/
120 
121 #if defined(PLE_HAVE_MPI)
122 
123 /*----------------------------------------------------------------------------
124  * Build a group id within a communicator based on its name.
125  *
126  * If multiple groups are present, ids are number from 0 to n_groups - 1,
127  * based on the odering of group names. If all processes have the same
128  * group name, the returned value is -1.
129  *
130  * The returned id may typically be used as a "color" argument for
131  * MPI_Comm_split().
132  *
133  * As this function requires communication between applications, it
134  * is a collective function in comm.
135  *
136  * parameters:
137  * comm <-- MPI communicator.
138  * group_name <-- name associated with current group.
139  *
140  * returns:
141  * id associated with local name.
142  *----------------------------------------------------------------------------*/
143 
144 int
145 ple_coupling_mpi_name_to_id(MPI_Comm comm,
146  const char *group_name);
147 
148 /*----------------------------------------------------------------------------
149  * Discover other applications in a set with a common communicator.
150  *
151  * In most cases, the base communicator is MPI_COMM_WORLD, and the local
152  * application communicator app_comm is usually obtained from it using
153  * MPI_Comm_split, but other combinations may be possible using MPI-2
154  * process management functions.
155  *
156  * As this function requires communication between applications, it
157  * is a collective function in base_comm.
158  *
159  * parameters:
160  * sync_flag <-- 1 if application is to be synchronized at each
161  * time step, 0 if independent from others.
162  * app_type <-- name of current application type (software name).
163  * app_name <-- name of current application (data/case name).
164  * base_comm <-- communicator associated with all applications.
165  * app_comm <-- communicator associated with local application.
166  *
167  * returns:
168  * PLE coupling MPI set info structure.
169  *----------------------------------------------------------------------------*/
170 
171 ple_coupling_mpi_set_t *
172 ple_coupling_mpi_set_create(int sync_flag,
173  const char *app_type,
174  const char *app_name,
175  MPI_Comm base_comm,
176  MPI_Comm app_comm);
177 
178 /*----------------------------------------------------------------------------
179  * Free an PLE coupling MPI set info structure.
180  *
181  * parameters:
182  * s <-> pointer to structure that should be freed.
183  *----------------------------------------------------------------------------*/
184 
185 void
186 ple_coupling_mpi_set_destroy(ple_coupling_mpi_set_t **s);
187 
188 /*----------------------------------------------------------------------------
189  * Return the number of applications in a coupled set.
190  *
191  * parameters:
192  * s <-- pointer to PLE coupling MPI set info structure.
193  *
194  * returns:
195  * number of application in set's common communicator.
196  *----------------------------------------------------------------------------*/
197 
198 int
199 ple_coupling_mpi_set_n_apps(const ple_coupling_mpi_set_t *s);
200 
201 /*----------------------------------------------------------------------------
202  * Return the id of the local application in a coupled set.
203  *
204  * parameters:
205  * s <-- pointer to PLE coupling MPI set info structure.
206  *
207  * returns:
208  * id of the local application in set's common communicator.
209  *----------------------------------------------------------------------------*/
210 
211 int
212 ple_coupling_mpi_set_get_app_id(const ple_coupling_mpi_set_t *s);
213 
214 /*----------------------------------------------------------------------------
215  * Return application information in set's common communicator.
216  *
217  * parameters:
218  * s <-- pointer to PLE coupling MPI set info structure.
219  * app_id <-- application id
220  *
221  * returns:
222  * application information structure.
223  *----------------------------------------------------------------------------*/
224 
226 ple_coupling_mpi_set_get_info(const ple_coupling_mpi_set_t *s,
227  int app_id);
228 
229 /*----------------------------------------------------------------------------
230  * Synchronize applications in a set.
231  *
232  * Note that if a member of the set has used a PLE_COUPLING_STOP or
233  * PLE_COUPLING_LAST flag when calling ple_coupling_mpi_set_create() or
234  * or at the previous call to this function, it will not be synchronized
235  * anymore (i.e. the PLE_COUPLING_NO_SYNC flag will be added).
236  *
237  * parameters:
238  * s <-- pointer to PLE coupling MPI set info structure.
239  * sync_flag <-- synchronization info for current application.
240  * time_step <-- time step for current application.
241  *----------------------------------------------------------------------------*/
242 
243 void
244 ple_coupling_mpi_set_synchronize(ple_coupling_mpi_set_t *s,
245  int sync_flag,
246  double time_step);
247 
248 /*----------------------------------------------------------------------------
249  * Get status of applications in a set.
250  *
251  * This function allows access to the status flag of each synchronized
252  * application in the set. It may be used immediately after
253  * ple_coupling_mpi_set_create(), and flags are updated after each
254  * call to ple_coupling_mpi_set_synchronize().
255  *
256  * parameters:
257  * s <-- pointer to PLE coupling MPI set info structure.
258  *
259  * returns:
260  * a pointer to the set's array of status flags
261  *----------------------------------------------------------------------------*/
262 
263 const int *
264 ple_coupling_mpi_set_get_status(const ple_coupling_mpi_set_t *s);
265 
266 /*----------------------------------------------------------------------------
267  * Get time steps in a set.
268  *
269  * This function may be called after ple_coupling_mpi_set_synchronize()
270  * to access the time step values of each synchronized application in the set.
271  *
272  * parameters:
273  * s <-- pointer to PLE coupling MPI set info structure.
274  *
275  * returns:
276  * a pointer to the set's array of time steps
277  *----------------------------------------------------------------------------*/
278 
279 const double *
280 ple_coupling_mpi_set_get_timestep(const ple_coupling_mpi_set_t *s);
281 
282 /*----------------------------------------------------------------------------
283  * Create an intracommunicator from a local and distant communicator
284  * within a base communicator.
285  *
286  * Note that if a member of the set has used a PLE_COUPLING_STOP or
287  * PLE_COUPLING_LAST flag when calling ple_coupling_mpi_set_create() or
288  * or at the previous call to this function, it will not be synchronized
289  * anymore (i.e. the PLE_COUPLING_NO_SYNC flag will be added).
290  *
291  * parameters:
292  * base_comm <-- communicator associated with both applications
293  * app_comm <-- communicator associated with local application
294  * distant_root <-- rank of distant group leader in base_comm
295  * new_comm --> pointer to new communicator
296  * local_range --> first and past-the last ranks of local application
297  * in new communicator
298  * distant_range --> first and past-the last ranks of distant application
299  * in new communicator
300  *----------------------------------------------------------------------------*/
301 
302 void
303 ple_coupling_mpi_intracomm_create(MPI_Comm base_comm,
304  MPI_Comm app_comm,
305  int distant_root,
306  MPI_Comm *new_comm,
307  int local_range[2],
308  int distant_range[2]);
309 
310 /*----------------------------------------------------------------------------
311  * Dump printout of an PLE coupling MPI set info structure.
312  *
313  * parameters:
314  * w <-- pointer to PLE coupling MPI set info structure.
315  *----------------------------------------------------------------------------*/
316 
317 void
318 ple_coupling_mpi_set_dump(const ple_coupling_mpi_set_t *s);
319 
320 #endif /* defined(PLE_HAVE_MPI) */
321 
322 /*----------------------------------------------------------------------------*/
323 
324 #ifdef __cplusplus
325 }
326 #endif /* __cplusplus */
327 
328 #endif /* __PLE_COUPLING_H__ */
const int * ple_coupling_mpi_set_get_status(const ple_coupling_mpi_set_t *s)
Get status of applications in a set.
Definition: ple_coupling.c:846
int ple_coupling_mpi_set_n_apps(const ple_coupling_mpi_set_t *s)
Return the number of applications in a coupled set.
Definition: ple_coupling.c:703
int n_ranks
Definition: ple_coupling.h:109
Definition: ple_coupling.h:105
const double * ple_coupling_mpi_set_get_timestep(const ple_coupling_mpi_set_t *s)
Get time steps in a set.
Definition: ple_coupling.c:871
int ple_coupling_mpi_name_to_id(MPI_Comm comm, const char *group_name)
Build a group id within a communicator based on its name.
Definition: ple_coupling.c:313
const char * app_type
Definition: ple_coupling.h:110
int ple_coupling_mpi_set_get_app_id(const ple_coupling_mpi_set_t *s)
Return the id of the local application in a coupled set.
Definition: ple_coupling.c:724
ple_coupling_mpi_set_info_t ple_coupling_mpi_set_get_info(const ple_coupling_mpi_set_t *s, int app_id)
Return application information in set&#39;s common communicator.
Definition: ple_coupling.c:746
void ple_coupling_mpi_intracomm_create(MPI_Comm base_comm, MPI_Comm app_comm, int distant_root, MPI_Comm *new_comm, int local_range[2], int distant_range[2])
Create an intracommunicator from a local and distant communicator within a base communicator.
Definition: ple_coupling.c:900
ple_coupling_mpi_set_t * ple_coupling_mpi_set_create(int sync_flag, const char *app_type, const char *app_name, MPI_Comm base_comm, MPI_Comm app_comm)
Discover other applications in a set with a common communicator.
Definition: ple_coupling.c:486
void ple_coupling_mpi_set_dump(const ple_coupling_mpi_set_t *s)
Dump printout of an PLE coupling MPI set info structure.
Definition: ple_coupling.c:977
int root_rank
Definition: ple_coupling.h:108
void ple_coupling_mpi_set_destroy(ple_coupling_mpi_set_t **s)
Free an PLE coupling MPI set info structure.
Definition: ple_coupling.c:679
const char * app_name
Definition: ple_coupling.h:111
void ple_coupling_mpi_set_synchronize(ple_coupling_mpi_set_t *s, int sync_flag, double time_step)
Synchronize applications in a set.
Definition: ple_coupling.c:786
int status
Definition: ple_coupling.h:107