17 #include <deal.II/base/mpi.h> 18 #include <deal.II/base/utilities.h> 19 #include <deal.II/base/exceptions.h> 20 #include <deal.II/lac/vector_memory.h> 21 #include <deal.II/lac/parallel_vector.h> 22 #include <deal.II/lac/parallel_block_vector.h> 23 #include <deal.II/base/multithread_info.h> 28 #ifdef DEAL_II_WITH_TRILINOS 29 # ifdef DEAL_II_WITH_MPI 30 # include <Epetra_MpiComm.h> 31 # include <deal.II/lac/vector_memory.h> 32 # include <deal.II/lac/trilinos_vector.h> 33 # include <deal.II/lac/trilinos_block_vector.h> 37 #ifdef DEAL_II_WITH_PETSC 38 # ifdef DEAL_II_WITH_MPI 39 # include <petscsys.h> 40 # include <deal.II/lac/petsc_block_vector.h> 41 # include <deal.II/lac/petsc_parallel_block_vector.h> 42 # include <deal.II/lac/petsc_vector.h> 43 # include <deal.II/lac/petsc_parallel_vector.h> 47 #ifdef DEAL_II_WITH_SLEPC 48 # ifdef DEAL_II_WITH_MPI 49 # include <slepcsys.h> 53 DEAL_II_NAMESPACE_OPEN
61 #ifdef DEAL_II_WITH_MPI 98 (void) MPI_Comm_size (mpi_communicator, &n_jobs);
107 (void) MPI_Comm_rank (mpi_communicator, &rank);
115 MPI_Comm new_communicator;
116 MPI_Comm_dup (mpi_communicator, &new_communicator);
117 return new_communicator;
121 std::vector<unsigned int>
123 const std::vector<unsigned int> &destinations)
128 for (
unsigned int i=0; i<destinations.size(); ++i)
130 Assert (destinations[i] < n_procs,
131 ExcIndexRange (destinations[i], 0, n_procs));
132 Assert (destinations[i] != myid,
133 ExcMessage (
"There is no point in communicating with ourselves."));
141 const unsigned int max_n_destinations
144 if (max_n_destinations==0)
146 return std::vector<unsigned int>();
155 std::vector<unsigned int> my_destinations(max_n_destinations,
157 std::copy (destinations.begin(), destinations.end(),
158 my_destinations.begin());
169 std::vector<unsigned int> all_destinations (max_n_destinations * n_procs);
170 MPI_Allgather (&my_destinations[0], max_n_destinations, MPI_UNSIGNED,
171 &all_destinations[0], max_n_destinations, MPI_UNSIGNED,
178 std::vector<unsigned int> origins;
179 for (
unsigned int i=0; i<n_procs; ++i)
180 for (
unsigned int j=0; j<max_n_destinations; ++j)
181 if (all_destinations[i*max_n_destinations + j] == myid)
182 origins.push_back (i);
183 else if (all_destinations[i*max_n_destinations + j] ==
194 void max_reduce (
const void *in_lhs_,
203 Assert(*len==1, ExcInternalError());
205 inout_rhs->sum += in_lhs->sum;
206 if (inout_rhs->min>in_lhs->min)
208 inout_rhs->min = in_lhs->min;
209 inout_rhs->min_index = in_lhs->min_index;
211 else if (inout_rhs->min == in_lhs->min)
214 if (inout_rhs->min_index > in_lhs->min_index)
215 inout_rhs->min_index = in_lhs->min_index;
218 if (inout_rhs->max < in_lhs->max)
220 inout_rhs->max = in_lhs->max;
221 inout_rhs->max_index = in_lhs->max_index;
223 else if (inout_rhs->max == in_lhs->max)
226 if (inout_rhs->max_index > in_lhs->max_index)
227 inout_rhs->max_index = in_lhs->max_index;
236 const MPI_Comm &mpi_communicator)
243 result.sum = my_value;
244 result.avg = my_value;
245 result.min = my_value;
246 result.max = my_value;
247 result.min_index = 0;
248 result.max_index = 0;
255 MinMaxAvg result = { 0., std::numeric_limits<double>::max(),
256 -std::numeric_limits<double>::max(), 0, 0, 0.
259 const unsigned int my_id
260 = ::Utilities::MPI::this_mpi_process(mpi_communicator);
261 const unsigned int numproc
262 = ::Utilities::MPI::n_mpi_processes(mpi_communicator);
265 int ierr = MPI_Op_create((MPI_User_function *)&max_reduce,
true, &op);
266 AssertThrow(ierr == MPI_SUCCESS, ExcInternalError());
269 in.sum = in.min = in.max = my_value;
270 in.min_index = in.max_index = my_id;
273 int lengths[]= {3,2};
274 MPI_Aint displacements[]= {0,offsetof(
MinMaxAvg, min_index)};
275 MPI_Datatype
types[]= {MPI_DOUBLE, MPI_INT};
277 ierr = MPI_Type_struct(2, lengths, displacements, types, &type);
278 AssertThrow(ierr == MPI_SUCCESS, ExcInternalError());
280 ierr = MPI_Type_commit(&type);
281 ierr = MPI_Allreduce (&in, &result, 1, type, op, mpi_communicator);
282 AssertThrow(ierr == MPI_SUCCESS, ExcInternalError());
284 ierr = MPI_Type_free (&type);
285 AssertThrow(ierr == MPI_SUCCESS, ExcInternalError());
287 ierr = MPI_Op_free(&op);
288 AssertThrow(ierr == MPI_SUCCESS, ExcInternalError());
290 result.avg = result.sum / numproc;
312 return mpi_communicator;
323 result.sum = my_value;
324 result.avg = my_value;
325 result.min = my_value;
326 result.max = my_value;
327 result.min_index = 0;
328 result.max_index = 0;
339 const unsigned int max_num_threads)
341 static bool constructor_has_already_run =
false;
342 (void)constructor_has_already_run;
343 Assert (constructor_has_already_run ==
false,
344 ExcMessage (
"You can only create a single object of this class " 345 "in a program since it initializes the MPI system."));
349 #ifdef DEAL_II_WITH_MPI 352 int MPI_has_been_started = 0;
353 MPI_Initialized(&MPI_has_been_started);
355 ExcMessage (
"MPI error. You can only start MPI once!"));
357 int mpi_err, provided;
362 int wanted = MPI_THREAD_SERIALIZED;
363 mpi_err = MPI_Init_thread(&argc, &argv, wanted, &provided);
365 ExcMessage (
"MPI could not be initialized."));
379 #ifdef DEAL_II_WITH_PETSC 380 # ifdef DEAL_II_WITH_SLEPC 382 SlepcInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL);
385 PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL);
389 constructor_has_already_run =
true;
403 #ifdef DEAL_II_WITH_MPI 415 std::vector<char> hostname_array (max_hostname_size);
416 std::copy (hostname.c_str(), hostname.c_str()+hostname.size()+1,
417 hostname_array.begin());
419 std::vector<char> all_hostnames(max_hostname_size *
421 MPI_Allgather (&hostname_array[0], max_hostname_size, MPI_CHAR,
422 &all_hostnames[0], max_hostname_size, MPI_CHAR,
427 unsigned int n_local_processes=0;
428 unsigned int nth_process_on_host = 0;
430 if (std::string (&all_hostnames[0] + i*max_hostname_size) == hostname)
434 ++nth_process_on_host;
436 Assert (nth_process_on_host > 0, ExcInternalError());
445 const unsigned int n_threads
472 #ifdef DEAL_II_WITH_MPI 476 ::release_unused_memory ();
478 ::release_unused_memory ();
480 ::release_unused_memory ();
482 ::release_unused_memory ();
485 # if defined(DEAL_II_WITH_TRILINOS) 496 #ifdef DEAL_II_WITH_PETSC 497 if ((PetscInitializeCalled == PETSC_TRUE)
499 (PetscFinalizeCalled == PETSC_FALSE))
510 # ifdef DEAL_II_WITH_SLEPC 524 #ifdef DEAL_II_WITH_MPI 527 if (std::uncaught_exception())
529 std::cerr <<
"ERROR: Uncaught exception in MPI_InitFinalize on proc " 531 <<
". Skipping MPI_Finalize() to avoid a deadlock." 536 const int mpi_err = MPI_Finalize();
538 ExcMessage (
"An error occurred while calling MPI_Finalize()"));
549 DEAL_II_NAMESPACE_CLOSE
static const unsigned int invalid_unsigned_int
static void release_unused_memory()
::ExceptionBase & ExcMessage(std::string arg1)
static unsigned int n_cores()
MPI_InitFinalize(int &argc, char **&argv, const unsigned int max_num_threads=numbers::invalid_unsigned_int)
#define AssertThrow(cond, exc)
#define Assert(cond, exc)
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
std::string get_hostname()
MPI_Comm duplicate_communicator(const MPI_Comm &mpi_communicator)
static void set_thread_limit(const unsigned int max_threads=numbers::invalid_unsigned_int)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm &mpi_comm, const std::vector< unsigned int > &destinations)
T max(const T &t, const MPI_Comm &mpi_communicator)