The finite element method using deal.II - 2021/2022
mpihello.cc
Go to the documentation of this file.
1 // very simple MPI demo
2 // compile normally and run with
3 // mpirun -n X ./main
4 
5 #include <mpi.h>
6 
7 #include <algorithm>
8 #include <iostream>
9 #include <numeric>
10 #include <thread>
11 #include <vector>
12 
13 int
14 main(int argc, char **argv)
15 {
16  MPI_Init(&argc, &argv);
17 
18  int rank;
19  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
20  int size;
21  MPI_Comm_size(MPI_COMM_WORLD, &size);
22 
23  char processor_name[MPI_MAX_PROCESSOR_NAME];
24  int name_len;
25  MPI_Get_processor_name(processor_name, &name_len);
26 
27  std::cout << "Hi, I am process " << rank << " of " << size
28  << " and I am running on " << processor_name << std::endl;
29 
30 
31 
32  if (rank == 0)
33  {
34  for (int i = 1; i < size; ++i)
35  {
36  double value = 0.0;
37  MPI_Status status;
38 
39  MPI_Recv(&value, // void *buf
40  1, // int count
41  MPI_DOUBLE, // MPI_Datatype datatype,
42  MPI_ANY_SOURCE, // int source
43  0, // int tag
44  MPI_COMM_WORLD,
45  &status); // MPI_Status *status
46 
47  std::cout << "I got value = " << value << " from "
48  << status.MPI_SOURCE << std::endl;
49  }
50  }
51  else
52  {
53  std::this_thread::sleep_for(std::chrono::milliseconds(3000));
54  srand(rank);
55  double my_value = (rand() % 1000) / 1000.0;
56 
57  MPI_Send(&my_value, // void *buf
58  1, // int count
59  MPI_DOUBLE, // MPI_Datatype datatype
60  0, // int dest
61  0, // int tag
62  MPI_COMM_WORLD);
63  }
64 
65  MPI_Finalize();
66 }
MPI_Finalize
****code ** MPI_Finalize()
value
static constexpr bool const value
mpi.h
size
unsigned int size
main
int main(int argc, char **argv)
Definition: mpihello.cc:14