-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmpi_lib.cpp
More file actions
85 lines (74 loc) · 1.97 KB
/
Copy pathmpi_lib.cpp
File metadata and controls
85 lines (74 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifdef _OPENMP
#include <omp.h>
#endif
#include <mpi.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <stdio.h>
namespace py = pybind11;
using pymod = pybind11::module;
class Distributed
{
public:
Distributed() : comm_global(MPI_COMM_WORLD) {}
~Distributed() {}
void say_hi() {
int world_size;
MPI_Comm_size(comm_global, &world_size);
int world_rank;
MPI_Comm_rank(comm_global, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME] = "localhost";
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("[C++] Hello from machine %s, MPI rank %d out of %d\n",
processor_name,
world_rank,
world_size);
if (world_rank == 0) {
int thread_level;
MPI_Query_thread( &thread_level );
switch (thread_level) {
case MPI_THREAD_SINGLE:
printf("Detected thread level MPI_THREAD_SINGLE\n");
fflush(stdout);
break;
case MPI_THREAD_FUNNELED:
printf("Detected thread level MPI_THREAD_FUNNELED\n");
fflush(stdout);
break;
case MPI_THREAD_SERIALIZED:
printf("Detected thread level MPI_THREAD_SERIALIZED\n");
fflush(stdout);
break;
case MPI_THREAD_MULTIPLE:
printf("Detected thread level MPI_THREAD_MULTIPLE\n");
fflush(stdout);
break;
}
int nthreads, tid;
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0 )
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
}
}
private:
MPI_Comm comm_global;
};
PYBIND11_MODULE(mpi_lib, mmod)
{
constexpr auto MODULE_DESCRIPTION = "Just testing out mpi with python.";
mmod.doc() = MODULE_DESCRIPTION;
py::class_<Distributed>(mmod, "Distributed")
// .def(py::init<py::object &>())
.def(py::init<>())
.def("say_hi", &Distributed::say_hi, "Each process will say hi");
}