-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI5Comm6.cpp
More file actions
48 lines (40 loc) · 886 Bytes
/
MPI5Comm6.cpp
File metadata and controls
48 lines (40 loc) · 886 Bytes
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
#include "pt4.h"
#include "mpi.h"
void Solve()
{
Task("MPI5Comm6");
int flag;
MPI_Initialized(&flag);
if (flag == 0)
return;
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int k, n;
if (rank == 0)
pt >> k;
else
pt >> n;
MPI_Comm comm;
int color = n == 1 ? 0 : MPI_UNDEFINED;
MPI_Comm_split(MPI_COMM_WORLD, color, rank, &comm);
if (comm == MPI_COMM_NULL)
return;
double* sbuf = new double[k + 1];
int* scounts = new int[k + 1];
int* displs = new int[k + 1];
double x;
if (rank == 0)
{
for (int i = 1; i < k + 1; i++)
pt >> sbuf[i];
scounts[0] = 0;
for (int i = 1; i < k + 1; i++)
scounts[i] = 1;
for (int i = 0; i < k + 1; i++)
displs[i] = i;
}
MPI_Scatterv(sbuf, scounts, displs, MPI_DOUBLE, &x, 1, MPI_DOUBLE, 0, comm);
if (rank != 0)
pt << x;
}