enum_name
Loading...
Searching...
No Matches
mpi_sub_comm.h
1#ifndef DOCTEST_MPI_SUB_COMM_H
2#define DOCTEST_MPI_SUB_COMM_H
3
4#include "mpi.h"
5#include "doctest/doctest.h"
6#include <cassert>
7#include <string>
8
9namespace doctest {
10
11inline
12int mpi_world_nb_procs() {
13 int n;
14 MPI_Comm_size(MPI_COMM_WORLD, &n);
15 return n;
16}
17
19 int nb_procs;
20 int rank;
21 MPI_Comm comm;
22
23 mpi_sub_comm( mpi_sub_comm const& ) = delete;
24 mpi_sub_comm& operator=( mpi_sub_comm const& ) = delete;
25
26 mpi_sub_comm(int nb_prcs) noexcept
27 : nb_procs(nb_prcs)
28 , rank(-1)
29 , comm(MPI_COMM_NULL)
30 {
31 int comm_world_rank;
32 MPI_Comm_rank(MPI_COMM_WORLD, &comm_world_rank);
33 if (nb_procs>mpi_world_nb_procs()) {
34 if (comm_world_rank==0) {
35 MESSAGE(
36 "Unable to run test: need ", std::to_string(nb_procs), " procs",
37 " but program launched with only ", std::to_string(doctest::mpi_world_nb_procs()), "."
38 );
39 CHECK(nb_procs<=mpi_world_nb_procs());
40 }
41 } else {
42 int color = MPI_UNDEFINED;
43 if(comm_world_rank < nb_procs){
44 color = 0;
45 }
46 MPI_Comm_split(MPI_COMM_WORLD, color, comm_world_rank, &comm);
47
48 if(comm != MPI_COMM_NULL){
49 MPI_Comm_rank(comm, &rank);
50 assert(rank==comm_world_rank);
51 }
52 }
53 }
54
55 void destroy_comm() {
56 if(comm != MPI_COMM_NULL){
57 MPI_Comm_free(&comm);
58 }
59 }
60
62 : nb_procs(x.nb_procs)
63 , rank(x.rank)
64 , comm(x.comm)
65 {
66 x.comm = MPI_COMM_NULL;
67 }
68 mpi_sub_comm& operator=(mpi_sub_comm&& x) {
69 destroy_comm();
70 nb_procs = x.nb_procs;
71 rank = x.rank;
72 comm = x.comm;
73 x.comm = MPI_COMM_NULL;
74 return *this;
75 }
76
78 destroy_comm();
79 }
80};
81
82} // doctest
83
84#endif // DOCTEST_SUB_COMM_H
Definition mpi_sub_comm.h:18