File: wapigen_timers.py

package info (click to toggle)
ga 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ansic: 192,963; fortran: 53,761; f90: 11,218; cpp: 5,784; makefile: 2,248; sh: 1,945; python: 1,734; perl: 534; csh: 134; asm: 106
file content (241 lines) | stat: -rwxr-xr-x 6,815 bytes parent folder | download | duplicates (8)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python

'''Generate the wapi_times.c source from the ga-papi.h header.'''

import sys

def get_signatures(header):
    # first, gather all function signatures from ga-papi.h aka argv[1]
    accumulating = False
    signatures = []
    current_signature = ''
    EXTERN = 'extern'
    SEMICOLON = ';'
    for line in open(header):
        line = line.strip() # remove whitespace before and after line
        if not line:
            continue # skip blank lines
        if EXTERN in line and SEMICOLON in line:
            signatures.append(line)
        elif EXTERN in line:
            current_signature = line
            accumulating = True
        elif SEMICOLON in line and accumulating:
            current_signature += line
            signatures.append(current_signature)
            accumulating = False
        elif accumulating:
            current_signature += line
    return signatures

class FunctionArgument(object):
    def __init__(self, signature):
        self.pointer = signature.count('*')
        self.array = '[' in signature
        signature = signature.replace('*','').strip()
        signature = signature.replace('[','').strip()
        signature = signature.replace(']','').strip()
        self.type,self.name = signature.split()

    def __str__(self):
        ret = self.type[:]
        ret += ' '
        for p in range(self.pointer):
            ret += '*'
        ret += self.name
        if self.array:
            ret += '[]'
        return ret

class Function(object):
    def __init__(self, signature):
        signature = signature.replace('extern','').strip()
        self.return_type,signature = signature.split(None,1)
        self.return_type = self.return_type.strip()
        signature = signature.strip()
        self.name,signature = signature.split('(',1)
        self.name = self.name.strip()
        signature = signature.replace(')','').strip()
        signature = signature.replace(';','').strip()
        self.args = []
        if signature:
            for arg in signature.split(','):
                self.args.append(FunctionArgument(arg.strip()))

    def get_call(self, name=None):
        sig = ''
        if not name:
            sig += self.name
        else:
            sig += name
        sig += '('
        if self.args:
            for arg in self.args:
                sig += arg.name
                sig += ', '
            sig = sig[:-2] # remove last ', '
        sig += ')'
        return sig

    def get_signature(self, name=None):
        sig = self.return_type[:]
        sig += ' '
        if not name:
            sig += self.name
        else:
            sig += name
        sig += '('
        if self.args:
            for arg in self.args:
                sig += str(arg)
                sig += ', '
            sig = sig[:-2] # remove last ', '
        sig += ')'
        return sig

    def __str__(self):
        return self.get_signature()

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'incorrect number of arguments'
        print 'usage: wapigen_counts.py <ga-papi.h> > <wapi_times.c>'
        sys.exit(len(sys.argv))

    # print headers
    print '''
#if HAVE_CONFIG_H
#   include "config.h"
#endif

#include <mpi.h>
#include "ga-papi.h"
#include "typesf2c.h"

static MPI_Comm comm;
static int me;
static int nproc;
static double global_start = 0;
static double global_stop = 0;

'''

    functions = {}
    # parse signatures into the Function class
    for sig in get_signatures(sys.argv[1]):
        function = Function(sig)
        functions[function.name] = function

    # for each function, generate a static count
    for name in sorted(functions):
        print 'static long count_%s = 0;' % name
    print ''

    # for each function, generate a static time
    for name in sorted(functions):
        print 'static double time_%s = 0;' % name
    print ''

    # now process the functions
    for name in sorted(functions):
        func = functions[name]
        if 'terminate' in name:
            continue
        if 'initialize' in name and 'initialized' not in name:
            continue
        func = functions[name]
        wnga_name = name.replace('pnga_','wnga_')
        if 'void' not in func.return_type:
            print '''
%s
{
    %s return_value;
    double local_start, local_stop;
    ++count_%s;
    local_start = MPI_Wtime();
    return_value = %s;
    local_stop = MPI_Wtime();
    time_%s += local_stop - local_start;
    return return_value;
}
''' % (func.get_signature(wnga_name),
        func.return_type, name, func.get_call(), name)
        else:
            print '''
%s
{
    double local_start, local_stop;
    ++count_%s;
    local_start = MPI_Wtime();
    %s;
    local_stop = MPI_Wtime();
    time_%s += local_stop - local_start;
}
''' % (func.get_signature(wnga_name), name, func.get_call(), name)

    # prepare to output the initialize function
    name = 'pnga_initialize'
    wnga_name = name.replace('pnga_','wnga_')
    func = functions[name]
    # output the initialize function
    print '''%s
{
    ++count_pnga_initialize;
    global_start = MPI_Wtime();
    %s;
    MPI_Comm_dup(GA_MPI_Comm(), &comm);
    MPI_Comm_rank(comm, &me);
    MPI_Comm_size(comm, &nproc);
}
''' % (func.get_signature(wnga_name), func.get_call())

    # prepare to output the initialize_ltd function
    name = 'pnga_initialize_ltd'
    wnga_name = name.replace('pnga_','wnga_')
    func = functions[name]
    # output the initialize_ltd function
    print '''%s
{
    ++count_pnga_initialize_ltd;
    global_start = MPI_Wtime();
    %s;
    MPI_Comm_dup(GA_MPI_Comm(), &comm);
    MPI_Comm_rank(comm, &me);
    MPI_Comm_size(comm, &nproc);
}
''' % (func.get_signature(wnga_name), func.get_call())

    # prepare to output the terminate function
    name = 'pnga_terminate'
    wnga_name = name.replace('pnga_','wnga_')
    func = functions[name]
    the_code = ''
    # establish 'the_code' to use in the body of terminate
    # it's printing the count of each function if it was called at least once
    the_code += '''
        double recvbuf = 0.0;
'''
    for name in sorted(functions):
        the_code += '''
        MPI_Reduce(&time_%s, &recvbuf, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
        if (me == 0) {
            printf("%s,%%ld,%%lf\\n", count_%s, recvbuf);
        }
''' % (name, name, name)
    # output the terminate function
    print '''%s
{
    ++count_pnga_terminate;
    %s;
    global_stop = MPI_Wtime();
    /* don't dump info if terminate more than once */
    if (1 == count_pnga_terminate) {
%s
        if (me == 0) {
            printf("global_stop-global_start,0,%%lf\\n",
                    global_stop-global_start);
        }
    }
    MPI_Comm_free(&comm);
}
''' % (func.get_signature(wnga_name), func.get_call(), the_code)