utilities  0.0
mpi.h
Go to the documentation of this file.
1 #ifndef UTILITIES_SRC_MOLPRO_MPI_H_
2 #define UTILITIES_SRC_MOLPRO_MPI_H_
3 
4 #include <cstdint>
5 
6 #ifndef HAVE_MPI_H
7 #if defined(OMPI_MPI_H) || defined(MPI_INCLUDED)
8 #define HAVE_MPI_H
9 #endif
10 #if defined __has_include
11 #if __has_include(<mpi.h>)
12 #endif
13 #define HAVE_MPI_H
14 #endif
15 #endif
16 
17 #ifdef HAVE_MPI_H
18 #include <mpi.h>
19 #else
21 #define MPI_Comm_c2f(x) x
22 using MPI_Comm = int64_t;
23 #define MPI_COMM_NULL 0
24 #define MPI_WIN_NULL 0
25 #define MPI_INFO_NULL 0
26 using MPI_Win = int64_t;
27 inline void _MPI_nullfunction(const void* x) {}
28 #define MPI_Barrier(x) _MPI_nullfunction(&x)
29 #define MPI_Win_free(x) _MPI_nullfunction(x)
30 #define MPI_Win_allocate(x1,x2,x3,x4,x5,x6) _MPI_nullfunction(x5)
31 #define MPI_Win_lock(x1,x2,x3,x4) _MPI_nullfunction(&x4)
32 #define MPI_Win_unlock(x1,x2) _MPI_nullfunction(&x2)
34 #endif
35 
36 #ifdef HAVE_GA_H
37 #include <ga.h>
38 #include <ga-mpi.h>
39 #endif
40 
41 #ifdef HAVE_PPIDD_H
42 #include <ppidd.h>
43 #endif
44 
45 namespace molpro {
46 namespace mpi {
47 
52 inline MPI_Comm comm_self() {
53 #ifdef HAVE_MPI_H
54  int flag;
55  MPI_Initialized(&flag);
56  if (!flag)
57  return MPI_COMM_NULL;
58  return MPI_COMM_SELF;
59 #else
60  return 0;
61 #endif
62 }
63 
64 #ifdef MOLPRO
65 extern "C" int molpro_mppx();
66 #endif
67 
80 inline MPI_Comm comm_global() {
81 #ifdef MOLPRO
82  if (molpro_mppx()) return comm_self();
83 #endif
84 #ifdef HAVE_MPI_H
85  int flag;
86  MPI_Initialized(&flag);
87  if (!flag) {
88  MPI_Init(0, nullptr);
89  return MPI_COMM_WORLD;
90  }
91 #else
92  return comm_self();
93 #endif
94 #ifdef __PPIDD_H__
95  {
96  if (PPIDD_Size() > 0)
97  return MPI_Comm_f2c(PPIDD_Worker_comm());
98  }
99 #else
100 #ifdef HAVE_GA_H
101  if (GA_MPI_Comm() != 0 && GA_MPI_Comm() != MPI_COMM_NULL) {
102  return GA_MPI_Comm();
103  }
104 #endif
105 #endif
106 #ifdef HAVE_MPI_H
107  return MPI_COMM_WORLD;
108 #endif
109  return comm_self();
110 }
111 
116 inline int size_global() {
117  int size = 1;
118 #ifdef HAVE_MPI_H
119  MPI_Comm_size(comm_global(), &size);
120 #endif
121  return size;
122 }
123 
128 inline int rank_global() {
129  int rank = 0;
130 #ifdef HAVE_MPI_H
131  MPI_Comm_rank(comm_global(), &rank);
132 #endif
133  return rank;
134 }
135 
139 inline int init() {
140 #ifdef HAVE_MPI_H
141  return MPI_Init(0, nullptr);
142 #else
143  return 0;
144 #endif
145 }
146 
150 inline int finalize() {
151 #ifdef HAVE_MPI_H
152  return MPI_Finalize();
153 #else
154  return 0;
155 #endif
156 }
157 
158 
159 } // namespace mpi
160 } // namespace molpro
161 
165 extern "C" inline int64_t mpicomm_global() { return (int64_t)MPI_Comm_c2f(molpro::mpi::comm_global()); }
166 
170 extern "C" inline int64_t mpicomm_self() { return (int64_t)MPI_Comm_c2f(molpro::mpi::comm_self()); }
171 
175 extern "C" inline int64_t mpisize_global() { return molpro::mpi::size_global(); }
176 
180 extern "C" inline int64_t mpirank_global() { return molpro::mpi::rank_global(); }
181 
185 extern "C" inline int mpi_init() { return molpro::mpi::init(); }
186 
190 extern "C" inline int mpi_finalize() { return molpro::mpi::finalize(); }
191 #endif // UTILITIES_SRC_MOLPRO_MPI_H_
int64_t mpicomm_global()
C binding of mpi::comm_global(), suitable for calling from Fortran.
Definition: mpi.h:165
int mpi_init()
C binding of mpi::init(), suitable for calling from Fortran.
Definition: mpi.h:185
int64_t mpicomm_self()
C binding of mpi::comm_self(), suitable for calling from Fortran.
Definition: mpi.h:170
int64_t mpisize_global()
C binding of mpi::size_global(), suitable for calling from Fortran.
Definition: mpi.h:175
int64_t mpirank_global()
C binding of mpi::rank_global(), suitable for calling from Fortran.
Definition: mpi.h:180
int mpi_finalize()
C binding of mpi::finalize(), suitable for calling from Fortran.
Definition: mpi.h:190
int finalize()
In MPI environment finalize; otherwise do nothing.
Definition: mpi.h:150
MPI_Comm comm_self()
Return MPI_COMM_SELF in an MPI program, or an appropriate dummy if not.
Definition: mpi.h:52
int rank_global()
Query the rank in the global MPI communicator.
Definition: mpi.h:128
MPI_Comm comm_global()
Return the MPI communicator containing all processes available for participation in computation....
Definition: mpi.h:80
int init()
In MPI environment initialize; otherwise do nothing. Intended to support MPI-agnostic programs.
Definition: mpi.h:139
int size_global()
Query the size of the global MPI communicator.
Definition: mpi.h:116
Class that manages input options.
Definition: iostream.h:14