1 /*
2  * Copyright (C) 2014      Artem Polyakov <artpol84@gmail.com>
3  * Copyright (c) 2014-2017 Intel, Inc. All rights reserved.
4  * $COPYRIGHT$
5  *
6  * Additional copyrights may follow
7  *
8  * $HEADER$
9  */
10 
11 #include "opal_config.h"
12 
13 #include <stdio.h>
14 #include <mpi.h>
15 #include <unistd.h>
16 #include <getopt.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 
21 #include "hpctimer.h"
22 #include "mpigclock.h"
23 
24 typedef enum { Gen, Chk } prog_mode_t;
25 
26 static char *filename = NULL;
27 static int alg = 0;
28 prog_mode_t mode = Gen;
29 void print_help(char *progname);
30 int parse_opts(int rank, int argc, char **argv);
31 
print_help(char * progname)32 void print_help(char *progname)
33 {
34     printf("%s: ./%s -o <output file>\n", progname, progname);
35 }
36 
parse_opts(int rank,int argc,char ** argv)37 int parse_opts(int rank, int argc, char **argv)
38 {
39     while (1) {
40         int option_index = 0;
41         static struct option long_options[] = {
42             {"output", required_argument, 0, 'o' },
43             {"alg", required_argument, 0, 'a' },
44             {"help",   required_argument, 0, 'h' },
45             { 0,       0,                 0, 0   } };
46 
47         int c = getopt_long(argc, argv, "o:a:h",
48             long_options, &option_index);
49         if (c == -1)
50             break;
51         switch (c) {
52         case 'h':
53             if( rank == 0 )
54                 print_help(argv[0]);
55             return 1;
56         case 'o':
57             filename = strdup(optarg);
58             if( filename == NULL ){
59                 perror("Cannot allocate memory");
60                 return -1;
61             }
62             break;
63         case 'a':
64             alg = atoi(optarg);
65             break;
66         default:
67             return -1;
68         }
69     }
70     return 0;
71 }
72 
main(int argc,char ** argv)73 int main(int argc, char **argv)
74 {
75     MPI_Init(&argc, &argv);
76     MPI_Comm comm = MPI_COMM_WORLD;
77     int rank, commsize;
78     double offs = 0, rtt = 0;
79     char hname[OPAL_MAXHOSTNAMELEN];
80 
81     MPI_Comm_rank(comm, &rank);
82     MPI_Comm_size(comm, &commsize);
83 
84     int ret = parse_opts(rank, argc, argv);
85     if( ret < 0 ){
86         // Error exit
87         MPI_Finalize();
88         exit(1);
89     }else if( ret > 0 ){
90         // Normal exit after help printout
91         MPI_Finalize();
92         exit(0);
93     }
94 
95     if( filename == NULL ){
96         if( rank == 0 ){
97             print_help(argv[0]);
98         }
99         MPI_Finalize();
100         exit(1);
101     }
102 
103     if( gethostname(hname, sizeof(hname)) ){
104         perror("Cannot get hostname. Abort");
105         MPI_Abort(MPI_COMM_WORLD, 1);
106     }
107 
108     int rc = hpctimer_initialize("gettimeofday");
109 
110     if( rc == HPCTIMER_FAILURE ){
111         fprintf(stderr, "Fail to initialize hpc timer. Abort\n");
112         MPI_Abort(MPI_COMM_WORLD, 1);
113     }
114 
115 
116     if (commsize < 2) {
117         rtt = 0.0;
118         offs = 0.0;
119     } else {
120         if (1 == alg) {
121             offs = mpigclock_sync_log(comm, 0, &rtt);
122         } else {
123             offs = mpigclock_sync_linear(comm, 0, &rtt);
124         }
125     }
126 
127     double send[2] = { rtt, offs };
128     if( rank == 0 ){
129         double *measure = malloc(commsize*2*sizeof(double));
130         if( measure == NULL ){
131              fprintf(stderr, "Fail to allocate memory. Abort\n");
132              MPI_Abort(MPI_COMM_WORLD, 1);
133         }
134         char *hnames = malloc(OPAL_MAXHOSTNAMELEN * commsize);
135         if( hnames == NULL ){
136              fprintf(stderr, "Fail to allocate memory. Abort\n");
137              MPI_Abort(MPI_COMM_WORLD, 1);
138         }
139 
140         MPI_Gather(hname,sizeof(hname),MPI_CHAR,hnames,sizeof(hname),MPI_CHAR, 0, MPI_COMM_WORLD);
141         MPI_Gather(send,2,MPI_DOUBLE,measure,2, MPI_DOUBLE, 0, MPI_COMM_WORLD);
142         FILE *fp = fopen(filename,"w");
143         if( fp == NULL ){
144              fprintf(stderr, "Fail to open the file %s. Abort\n", filename);
145              MPI_Abort(MPI_COMM_WORLD, 1);
146         }
147         double (*m)[2] = (void*)measure;
148         char (*h)[OPAL_MAXHOSTNAMELEN] = (void*)hnames;
149         int i;
150         fprintf(fp, "# Used algorithm: %s\n", (alg ? "binary tree" : "linear"));
151         for(i=0; i<commsize;i++){
152             fprintf(fp, "%s %lf %lf\n", h[i], m[i][0], m[i][1]);
153         }
154         fclose(fp);
155     } else {
156         MPI_Gather(hname, sizeof(hname), MPI_CHAR, NULL, sizeof(hname), MPI_CHAR, 0, MPI_COMM_WORLD);
157         MPI_Gather(send,2, MPI_DOUBLE, NULL, 2, MPI_DOUBLE, 0, MPI_COMM_WORLD);
158     }
159 
160     MPI_Finalize();
161     return 0;
162 }
163