-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_test.cpp
34 lines (29 loc) · 1.03 KB
/
mpi_test.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
#include "mpi.h"
#include <bits/stdc++.h>
#define SIZE 4
using namespace std;
int main(int argc, char *argv[]) {
int numtasks, rank, sendcount, recvcount, source;
float sendbuf[SIZE][SIZE] = {
{1.0, 2.0, 3.0, 4.0},
{5.0, 6.0, 7.0, 8.0},
{9.0, 10.0, 11.0, 12.0},
{13.0, 14.0, 15.0, 16.0}};
float recvbuf[SIZE];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
if (numtasks == SIZE) {
// define source task and elements to send/receive, then perform collective scatter
source = 1;
sendcount = SIZE;
recvcount = SIZE;
MPI_Scatter(sendbuf, sendcount, MPI_FLOAT, recvbuf, recvcount,
MPI_FLOAT, source, MPI_COMM_WORLD);
printf("rank= %d Results: %f %f %f %f\n", rank, recvbuf[0],
recvbuf[1], recvbuf[2], recvbuf[3]);
} else
printf("Must specify %d processors. Terminating.\n", SIZE);
MPI_Finalize();
return 0;
}