-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_autotuning_schwimmbad.py
246 lines (192 loc) · 7.71 KB
/
parallel_autotuning_schwimmbad.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
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
242
243
244
245
#from charm4py import entry_method, chare, Chare, Array, Reducer, Future, charm
#from charm4py.pool import PoolScheduler, Pool
#from charm4py.charm import Charm, CharmRemote
#from charm4py.chare import GROUP, MAINCHARE, ARRAY, CHARM_TYPES, Mainchare, Group, ArrayMap
#from charm4py.sections import SectionManager
#import inspect
#import sys
import hjson
import pyopencl as cl
import numpy as np
import grudge.loopy_dg_kernels as dgk
import os
import grudge.grudge_array_context as gac
import loopy as lp
from os.path import exists
from grudge.loopy_dg_kernels.run_tests import run_single_param_set, generic_test
from grudge.grudge_array_context import convert
#from grudge.execution import diff_prg, elwise_linear
import mpi4py.MPI as MPI
from schwimmbad import SerialPool, MPIPool
#from schwimmbad.mpi import MPIAsyncPool
def get_queue(pe_num, platform_num):
platforms = cl.get_platforms()
gpu_devices = platforms[platform_num].get_devices(device_type=cl.device_type.GPU)
ctx = cl.Context(devices=[gpu_devices[pe_num % len(gpu_devices)]])
queue = cl.CommandQueue(ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)
return queue
def test(args):
platform_id, knl, tlist_generator, params, test_fn = args
comm = MPI.COMM_WORLD # Assume we're using COMM_WORLD. May need to change this in the future
queue = get_queue(comm.Get_rank(), platform_id)
result = run_single_param_set(queue, knl, tlist_generator, params, test_fn)
return result
def unpickle_kernel(fname):
from pickle import load
f = open(fname, "rb")
program = load(f)
f.close()
return program
def autotune_pickled_kernels(path, platform_id, actx_class, comm):
from os import listdir
dir_list = listdir(path)
for f in dir_list:
if f.endswith(".pickle"):
fname = path + "/" + f
print("===============================================")
print("Autotuning", fname)
knl = unpickle_kernel(fname)
knl_id = f.split(".")[0]
knl_id = knl_id.split("_")[-1]
print("Kernel ID", knl_id)
print("New kernel ID", gac.unique_program_id(knl))
assert knl_id == gac.unique_program_id(knl)
knl = lp.set_options(knl, lp.Options(no_numpy=True, return_dict=True))
knl = gac.set_memory_layout(knl)
assert knl_id == gac.unique_program_id(knl)
print(knl)
pid = gac.unique_program_id(knl)
hjson_file_str = f"hjson/{knl.default_entrypoint.name}_{pid}.hjson"
if not exists(hjson_file_str):
parallel_autotune(knl, platform_id, actx_class, comm)
else:
print("hjson file exists, skipping")
def parallel_autotune(knl, platform_id, actx_class, comm):
# Create queue, assume all GPUs on the machine are the same
platforms = cl.get_platforms()
gpu_devices = platforms[platform_id].get_devices(device_type=cl.device_type.GPU)
n_gpus = len(gpu_devices)
ctx = cl.Context(devices=[gpu_devices[comm.Get_rank() % n_gpus]])
profiling = cl.command_queue_properties.PROFILING_ENABLE
queue = cl.CommandQueue(ctx, properties=profiling)
import pyopencl.tools as cl_tools
actx = actx_class(
comm,
queue,
allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)))
knl = lp.set_options(knl, lp.Options(no_numpy=True, return_dict=True))
knl = gac.set_memory_layout(knl)
pid = gac.unique_program_id(knl)
os.makedirs(os.path.dirname("./hjson"), exist_ok=True)
hjson_file_str = f"hjson/{knl.default_entrypoint.name}_{pid}.hjson"
#assert comm.Get_size() > 1
#assert charm.numPes() > 1
#assert charm.numPes() - 1 <= charm.numHosts()*len(gpu_devices)
#assert charm.numPes() <= charm.numHosts()*(len(gpu_devices) + 1)
# Check that it can assign one PE to each GPU
# The first PE is used for scheduling
# Not certain how this will work with multiple nodes
from run_tests import run_single_param_set
tlist_generator, pspace_generator = actx.get_generators(knl)
params_list = pspace_generator(actx.queue, knl)
# Could make a massive list with all kernels and parameters
args = [(platform_id, knl, tlist_generator, p, generic_test,) for p in params_list]
# May help to balance workload
# Should test if shuffling matters
from random import shuffle
shuffle(args)
#a = Array(AutotuneTask, dims=(len(args)), args=args[0])
#a.get_queue()
#result = charm.pool.map(do_work, args)
#pool_proxy = Chare(BalancedPoolScheduler, onPE=0) # Need to use own charm++ branch to make work
#pool_proxy = Chare(PoolScheduler, onPE=0)
#mypool = MPIAsyncPool()
mypool = MPIPool()#Pool(pool_proxy)
#mypool = SerialPool()
if isinstance(mypool, MPIPool) and not mypool.is_master():
mypool.wait()
sys.exit(0)
sort_key = lambda entry: entry[0]
transformations = {}
if len(args) > 0: # Guard against empty list
results = list(mypool.map(test, args))
mypool.close()
results.sort(key=sort_key)
#for r in results:
# print(r)
# Workaround for pocl CUDA bug
# whereby times are imprecise
ret_index = 0
for i, result in enumerate(results):
if result[0] > 1e-7:
ret_index = i
break
avg_time, transformations, data = results[ret_index]
else:
mypool.close()
od = {"transformations": transformations}
out_file = open(hjson_file_str, "wt+")
hjson.dump(od, out_file,default=convert)
out_file.close()
return transformations
"""
def main(args):
# Create queue, assume all GPUs on the machine are the same
platforms = cl.get_platforms()
platform_id = 0
gpu_devices = platforms[platform_id].get_devices(device_type=cl.device_type.GPU)
n_gpus = len(gpu_devices)
ctx = cl.Context(devices=[gpu_devices[charm.myPe() % n_gpus]])
profiling = cl.command_queue_properties.PROFILING_ENABLE
queue = cl.CommandQueue(ctx, properties=profiling)
assert charm.numPes() > 1
#assert charm.numPes() - 1 <= charm.numHosts()*len(gpu_devices)
assert charm.numPes() <= charm.numHosts()*(len(gpu_devices) + 1)
# Check that it can assign one PE to each GPU
# The first PE is used for scheduling
# Not certain how this will work with multiple nodes
from grudge.execution import diff_prg, elwise_linear_prg
knl = diff_prg(3, 1000000, 3, np.float64)
params = dgk.run_tests.gen_autotune_list(queue, knl)
args = [[param, knl] for param in params]
# May help to balance workload
from random import shuffle
shuffle(args)
#a = Array(AutotuneTask, dims=(len(args)), args=args[0])
#a.get_queue()
#result = charm.pool.map(do_work, args)
pool_proxy = Chare(BalancedPoolScheduler, onPE=0)
mypool = Pool(pool_proxy)
result = mypool.map(do_work, args)
sort_key = lambda entry: entry[0]
result.sort(key=sort_key)
for r in result:
print(r)
"""
def main():
from mirgecom.array_context import MirgecomAutotuningArrayContext as Maac
comm = MPI.COMM_WORLD
autotune_pickled_kernels("./pickled_programs", 0, Maac, comm)
print("DONE!")
exit()
"""
def worker(task):
a, b = task
return a**2 + b**2
def main(args):
# Here we generate some fake data
import random
a = [random.random() for _ in range(10000)]
b = [random.random() for _ in range(10000)]
tasks = list(zip(a, b))
results = pool.map(worker, tasks)
pool.close()
print(results[:8])
"""
if __name__ == "__main__":
import sys
main()
#pool = MPIPool()
#if not pool.is_master():
# pool.wait()
# sys.exit(0)