-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMPI.cpp
46 lines (38 loc) · 1.25 KB
/
MyMPI.cpp
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
#ifndef COURSEWORK_HELPER_H
#define COURSEWORK_HELPER_H
#include "mpi.h"
#include "MyMPI.h"
#include <string>
#include <iostream>
using namespace std;
MyMPI::MyMPI(int argc, char* argv[], int Px, int Py) {
Np = Px * Py;
int mpi_init_err = MPI_Init(&argc, &argv);
int mpi_rank_err = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int mpi_size_err = MPI_Comm_size(MPI_COMM_WORLD, &world_size);
if (mpi_init_err != MPI_SUCCESS || mpi_rank_err != MPI_SUCCESS || mpi_size_err != MPI_SUCCESS ) {
cerr << "An error occurred initialising MPI\n";
return;
}
if (world_size < Np) {
if(world_rank == 0) {
fprintf(stderr, "Not enough processors are available to split domain as requested. Exiting now.\n");
}
return;
}
if (world_rank==0) {
printf("Running integration on %i process(es).\n", Np);
}
valid = true;
}
void MyMPI::createSubComm(MPI_Comm* myComm) {
MPI_Group worldGroup, myGroup;
int* group_ranks = new int[Np];
for(int i = 0; i < Np; i++) {
group_ranks[i] = i;
}
MPI_Comm_group(MPI_COMM_WORLD, &worldGroup);
MPI_Group_incl(worldGroup, Np, group_ranks, &myGroup);
MPI_Comm_create(MPI_COMM_WORLD, myGroup, myComm);
}
#endif