forked from csc-training/summerschool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.c
45 lines (36 loc) · 1.13 KB
/
exchange.c
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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
int myid, ntasks, nrecv;
int arraysize = 100000;
int msgsize = 100;
int *message;
int *receiveBuffer;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// Allocate buffers
message = (int *)malloc(sizeof(int) * arraysize);
receiveBuffer = (int *)malloc(sizeof(int) * arraysize);
// Initialize message and receive buffer
for (int i = 0; i < arraysize; i++) {
message[i] = myid;
receiveBuffer[i] = -1;
}
// TODO: Implement sending and receiving as defined in the assignment
// Send msgsize elements from the array "message", and receive into
// "receiveBuffer"
if (myid == 0) {
printf("Rank %i received %i elements, first %i\n", myid, nrecv, receiveBuffer[0]);
} else if (myid == 1) {
printf("Rank %i received %i elements, first %i\n", myid, nrecv, receiveBuffer[0]);
}
// Free buffers
free(message);
free(receiveBuffer);
MPI_Finalize();
return 0;
}