-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab3_benchmark.c
More file actions
70 lines (66 loc) · 2.16 KB
/
lab3_benchmark.c
File metadata and controls
70 lines (66 loc) · 2.16 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <mpi.h>
#define VECSIZE 8
#define ITERATIONS 10000
double When()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return ((double) tp.tv_sec + (double) tp.tv_usec * 1e-6);
}
main(int argc, char *argv[])
{
int iproc, nproc,i, iter;
char host[255], message[55];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &iproc);
gethostname(host,253);
printf("I am proc %d of %d running on %s\n", iproc, nproc,host);
// each process has an array of VECSIZE double: ain[VECSIZE]
double ain[VECSIZE], aout[VECSIZE];
int ind[VECSIZE];
struct {
double val;
int rank;
} in[VECSIZE], out[VECSIZE];
int myrank, root = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
// Start time here
srand(myrank+5);
double start = When();
for(iter = 0; iter < ITERATIONS; iter++) {
for(i = 0; i < VECSIZE; i++) {
ain[i] = rand();
// printf("init proc %d [%d]=%f\n",myrank,i,ain[i]);
}
for (i=0; i<VECSIZE; ++i) {
in[i].val = ain[i];
in[i].rank = myrank;
}
MPI_Reduce( in, out, VECSIZE, MPI_DOUBLE_INT, MPI_MAXLOC, root, MPI_COMM_WORLD);
// At this point, the answer resides on process root
if (myrank == root) {
/* read ranks out
*/
for (i=0; i<VECSIZE; ++i) {
// printf("root out[%d] = %f from %d\n",i,out[i].val,out[i].rank);
aout[i] = out[i].val;
ind[i] = out[i].rank;
}
}
// Now broadcast this max vector to everyone else.
MPI_Bcast(out, VECSIZE, MPI_DOUBLE_INT, root, MPI_COMM_WORLD);
for(i = 0; i < VECSIZE; i++) {
// printf("final proc %d [%d]=%f from %d\n",myrank,i,out[i].val,out[i].rank);
}
}
MPI_Finalize();
double end = When();
if(myrank == root) {
printf("Time %f\n",end-start);
}
}