1
/*============================================================================
2
* Base functions for parallelism
3
*============================================================================*/
6
This file is part of Code_Saturne, a general-purpose CFD tool.
8
Copyright (C) 1998-2011 EDF S.A.
10
This program is free software; you can redistribute it and/or modify it under
11
the terms of the GNU General Public License as published by the Free Software
12
Foundation; either version 2 of the License, or (at your option) any later
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20
You should have received a copy of the GNU General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22
Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
/*----------------------------------------------------------------------------*/
27
#if defined(HAVE_CONFIG_H)
28
#include "cs_config.h"
31
/*----------------------------------------------------------------------------
32
* Standard C library headers
33
*----------------------------------------------------------------------------*/
38
/*----------------------------------------------------------------------------
40
*----------------------------------------------------------------------------*/
44
/*----------------------------------------------------------------------------
46
*----------------------------------------------------------------------------*/
48
#include "fvm_config_defs.h"
51
/*----------------------------------------------------------------------------
52
* Header for the current file
53
*----------------------------------------------------------------------------*/
55
#include "fvm_parall.h"
57
/*----------------------------------------------------------------------------*/
62
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
64
#endif /* __cplusplus */
66
/*============================================================================
67
* Local structure definitions
68
*============================================================================*/
70
/*============================================================================
72
*============================================================================*/
76
/* Basic communicator info */
78
static MPI_Comm _fvm_mpi_parall_comm = MPI_COMM_NULL; /* Intra-communicator */
79
static int _fvm_mpi_parall_size = 1;
80
static int _fvm_mpi_parall_rank = 0;
82
/* MPI Datatypes associated with fvm datatypes */
84
MPI_Datatype fvm_datatype_to_mpi[] = {MPI_DATATYPE_NULL,
88
MPI_INT, /* FVM_INT32 */
89
MPI_LONG_INT, /* FVM_INT64 */
90
MPI_UNSIGNED, /* FVM_UINT32 */
91
MPI_UNSIGNED_LONG}; /* FVM_UINT64 */
93
/* Minimum recommended scatter/gather buffer size */
95
static size_t _fvm_parall_min_coll_buf_size = 1024*1024*8;
99
/*=============================================================================
100
* Private function definitions
101
*============================================================================*/
103
/*=============================================================================
104
* Public function definitions
105
*============================================================================*/
107
#if defined(HAVE_MPI)
109
/*----------------------------------------------------------------------------
110
* Return default MPI communicator for FVM library functions.
113
* handle to MPI communicator
114
*----------------------------------------------------------------------------*/
117
fvm_parall_get_mpi_comm(void)
119
return _fvm_mpi_parall_comm;
122
/*----------------------------------------------------------------------------
123
* Set default MPI communicator for FVM library functions.
126
* comm <-- handle to MPI communicator
127
*----------------------------------------------------------------------------*/
130
fvm_parall_set_mpi_comm(const MPI_Comm comm)
134
MPI_Initialized(&mpi_flag);
136
/* Set communicator */
138
_fvm_mpi_parall_comm = comm;
140
if (mpi_flag != 0 && _fvm_mpi_parall_comm != MPI_COMM_NULL) {
141
MPI_Comm_size(_fvm_mpi_parall_comm, &_fvm_mpi_parall_size);
142
MPI_Comm_rank(_fvm_mpi_parall_comm, &_fvm_mpi_parall_rank);
145
_fvm_mpi_parall_size = 1;
146
_fvm_mpi_parall_rank = 0;
149
/* Check (correct) fvm_datatype_to_mpi values */
153
int size_short, size_int, size_long, size_long_long;
155
MPI_Type_size(MPI_SHORT, &size_short);
156
MPI_Type_size(MPI_INT, &size_int);
157
MPI_Type_size(MPI_LONG, &size_long);
159
#if defined(MPI_LONG_LONG)
160
MPI_Type_size(MPI_LONG_LONG, &size_long_long);
166
fvm_datatype_to_mpi[FVM_INT32] = MPI_INT;
167
fvm_datatype_to_mpi[FVM_UINT32] = MPI_UNSIGNED;
169
else if (size_short == 4) {
170
fvm_datatype_to_mpi[FVM_INT32] = MPI_SHORT;
171
fvm_datatype_to_mpi[FVM_UINT32] = MPI_UNSIGNED_SHORT;
173
else if (size_long == 4) {
174
fvm_datatype_to_mpi[FVM_INT32] = MPI_LONG;
175
fvm_datatype_to_mpi[FVM_UINT32] = MPI_UNSIGNED_LONG;
179
fvm_datatype_to_mpi[FVM_INT64] = MPI_INT;
180
fvm_datatype_to_mpi[FVM_UINT64] = MPI_UNSIGNED;
182
else if (size_long == 8) {
183
fvm_datatype_to_mpi[FVM_INT64] = MPI_LONG;
184
fvm_datatype_to_mpi[FVM_UINT64] = MPI_UNSIGNED_LONG;
186
#if defined(MPI_LONG_LONG)
187
else if (size_long_long == 8) {
188
fvm_datatype_to_mpi[FVM_INT64] = MPI_LONG_LONG;
189
#if defined(MPI_UNSIGNED_LONG_LONG)
190
fvm_datatype_to_mpi[FVM_UINT64] = MPI_UNSIGNED_LONG_LONG;
192
fvm_datatype_to_mpi[FVM_UINT64] = MPI_LONG_LONG;
202
/*----------------------------------------------------------------------------
203
* Return rank of current process among associated program processes.
206
* rank of current process in current communicator, or 0 in scalar mode
207
*----------------------------------------------------------------------------*/
210
fvm_parall_get_rank(void)
212
#if defined(HAVE_MPI)
213
return _fvm_mpi_parall_rank;
219
/*----------------------------------------------------------------------------
220
* Return number of processes associated with the current program.
223
* number of processes in current communicator, or 1 in scalar mode
224
*----------------------------------------------------------------------------*/
227
fvm_parall_get_size(void)
229
#if defined(HAVE_MPI)
230
return _fvm_mpi_parall_size;
236
#if defined(HAVE_MPI)
238
/*----------------------------------------------------------------------------
239
* Sum counters on all FVM default communicator processes.
242
* cpt <-> local counter value input, global counter value output (array)
243
* n <-- number of counter array values
244
*----------------------------------------------------------------------------*/
247
fvm_parall_counter(fvm_gnum_t cpt[],
251
if (_fvm_mpi_parall_size > 1) {
258
BFT_MALLOC(sum, n, fvm_gnum_t);
262
MPI_Allreduce(cpt, sum, n, FVM_MPI_GNUM, MPI_SUM,
263
_fvm_mpi_parall_comm);
265
for (i = 0; i < n ; i++)
275
/*----------------------------------------------------------------------------
276
* Maximum values of a counter on all FVM default communicator processes.
279
* cpt <-> local counter value input, global counter value output (array)
280
* n <-- number of counter array values
281
*----------------------------------------------------------------------------*/
284
fvm_parall_counter_max(fvm_lnum_t cpt[],
288
if (_fvm_mpi_parall_size > 1) {
292
fvm_lnum_t _maxval[64];
295
BFT_MALLOC(maxval, n, fvm_lnum_t);
299
MPI_Allreduce(cpt, maxval, n, FVM_MPI_LNUM, MPI_MAX,
300
_fvm_mpi_parall_comm);
302
for (i = 0; i < n ; i++)
305
if (maxval != _maxval)
312
#endif /* defined(HAVE_MPI) */
314
/*----------------------------------------------------------------------------
315
* Return minimum recommended scatter or gather buffer size.
317
* This is used by FVM's internal strided and indexed array scatter/gather
318
* algorithms, for non MPI-IO Input/output.
321
* minimum recommended gather buffer size (in bytes)
322
*----------------------------------------------------------------------------*/
325
fvm_parall_get_min_coll_buf_size(void)
327
#if defined(HAVE_MPI)
328
return _fvm_parall_min_coll_buf_size;
334
/*----------------------------------------------------------------------------
335
* Define minimum recommended gather buffer size.
337
* This is used by FVM's internal strided and indexed array scatter/gather
338
* algorithms, for non MPI-IO Input/output.
341
* minimum recommended gather buffer size (in bytes)
342
*----------------------------------------------------------------------------*/
345
fvm_parall_set_min_coll_buf_size(size_t buffer_size)
347
#if defined(HAVE_MPI)
348
_fvm_parall_min_coll_buf_size = buffer_size;
352
/*----------------------------------------------------------------------------*/
356
#endif /* __cplusplus */