-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.c
81 lines (67 loc) · 2.01 KB
/
hello.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
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
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <mpi.h>
#include <time.h>
int main(int argc, char **argv)
{
int me, npes, nid, cpu, x, y, z;
char hostname[64];
char my_string[128];
char timebuf [26];
char *all_strings;
time_t timer;
struct tm* tm_info;
int i;
MPI_Init(&argc, &argv);
// Get my pe and num pes
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &npes);
// Print time and nodes for first rank
if (!me) {
time(&timer);
tm_info = localtime(&timer);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s: starting on %s\n", timebuf, getenv("HOSTNAME"));
printf("Job nodelist: %s\n", getenv("SLURM_JOB_NODELIST"));
}
// Get my cpu
cpu = sched_getcpu();
// Get my hostname
gethostname(hostname, sizeof(hostname));
// Generate my info string
sprintf(my_string, "%6i: %s cpu %2i\n",
me, hostname, cpu);
// Rank 0 allocates memory to gather all info strings
if (!me) {
size_t size = npes * sizeof(my_string);
all_strings = malloc(size);
if (!all_strings) {
printf("ERROR: malloc(%lu) failed\n", size);
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
// Gather all info strings to rank 0
MPI_Gather(my_string, sizeof(my_string), MPI_CHAR,
all_strings, sizeof(my_string), MPI_CHAR,
0, MPI_COMM_WORLD);
// Rank 0 prints out all info strings in sorted order by pe
if (!me) {
for (i = 0; i < npes; i++) {
printf("%s", &all_strings[i * sizeof(my_string)]);
}
}
// Sleep 10 seconds
sleep(10);
MPI_Barrier(MPI_COMM_WORLD);
// Print finish time
if (!me) {
time(&timer);
tm_info = localtime(&timer);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s: finished\n", timebuf);
}
MPI_Finalize();
return EXIT_SUCCESS;
}