-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheprun-gather-results.py
139 lines (113 loc) · 3.92 KB
/
eprun-gather-results.py
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
#!/usr/bin/env python3
# -*-coding: utf-8;-*-
# eprun-gather-results.py - It distributes and invokes shell commands
# on ranks. See "eprun.py" for a simpler example. A process on
# rank=0 reads a file COMMANDLIST.TXT, which contains lines of a key
# and a shell command. The shell command is invoked, which is assumed
# to create a file named same as the key. Each result file is read
# and the values are gathered to a process on rank=0. The results are
# currently assumed to be a single number, but it can be changed by
# redefining the result-reader function "read_result" defined here.
# USAGE: "mpiexec -n N python3 ./eprun-gather-results.py COMMANDLIST.TXT".
# For example, COMMANDLIST.TXT may contain commands one per line (note
# that "shut" command is a random number generator):
# dat0.txt shuf -i 0-99 -n 1 >dat0.txt
# dat1.txt shuf -i 0-99 -n 1 >dat1.txt
# dat2.txt shuf -i 0-99 -n 1 >dat2.txt
# ......
from mpi4py import MPI
import kmr4py
from optparse import OptionParser
from optparse import HelpFormatter
from optparse import IndentedHelpFormatter
import time
import sys
import re
from operator import itemgetter
kmr0 = kmr4py.KMR("world")
NPROCS = kmr0.nprocs
RANK = kmr0.rank
class NullHelpFormatter(HelpFormatter):
"""Suppress helps on except rank=0."""
def __init__(self,
indent_increment=0,
max_help_position=24,
width=None,
short_first=0):
HelpFormatter.__init__(
self, indent_increment, max_help_position, width, short_first)
def format_option(self, option):
return ""
def format_heading(self, heading):
return ""
def format_text(self, text):
return ""
def format_usage(self, usage):
return ""
if (RANK == 0):
options = OptionParser()
else:
options = OptionParser(formatter=NullHelpFormatter())
options.add_option("-t", "--trace",
dest="trace", action="store_true", default=False,
help="prints traces of invoking commands to stderr")
options.add_option("-m", "--per-core",
dest="percore", action="store_true", default=False,
help="invokes commands per core")
def read_commands(arg):
k00 = kmr0.make_kvs(value="cstring")
if (RANK == 0):
f = open(arg)
lines = f.readlines()
f.close()
for line in lines:
pair = line.split(" ", 1)
assert len(pair) == 2, "Ill formatted command list"
k00.add(pair[0], "sh\0-c\0" + (pair[1]).rstrip())
k00.add_kv_done()
return k00
def identitymap(kv, kvi, kvo, i, *_data):
(k, v) = kv
kvo.add(k, v)
return 0
def read_result(key_command, kvi, kvo, i, *_data):
"""Reads a file named as KEY, whose content is generated by execution
of the command, and puts the first field (as a string) to a KVS."""
(key, command) = key_command
f = open(key)
lines = f.readlines()
f.close()
for i, line in zip(range(len(lines)), lines):
resultfields = line.split()
kvo.add(key, resultfields[0])
return
## MAIN
(opts, args) = options.parse_args()
if (NPROCS == 1):
sys.stderr.write("eprun needs more than one rank; abort.\n")
sys.exit(1)
if (len(args) != 1):
if (RANK == 0):
sys.stderr.write("Usage: python3 ./eprun-gather-results.py [options] input-file.\n")
sys.exit(1)
if (opts.trace):
kmr0.set_option("trace_map_ms", "1")
threading = (opts.percore)
sys.stdout.flush()
sys.stderr.flush()
k20 = read_commands(args[0])
k21 = k20.map_ms_commands(read_result, nothreading=(not threading),
separator_space=False, value="cstring")
k22 = k21.replicate(rank_zero=True)
if (RANK == 0):
resultlist = kmr4py.listify(k22)
valuelist = list(map(itemgetter(1), resultlist))
print(valuelist)
k22.free()
kmr0.dismiss()
sys.stdout.flush()
sys.stderr.flush()
#time.sleep(1)
#if (RANK == 0):
# print("eprun OK")
#sys.stdout.flush()