-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcpu.py
More file actions
279 lines (240 loc) · 9.83 KB
/
Copy pathcpu.py
File metadata and controls
279 lines (240 loc) · 9.83 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from pyop2.types.dat import Dat as BaseDat, MixedDat, DatView
from pyop2.types.set import Set, ExtrudedSet, Subset, MixedSet
from pyop2.types.dataset import DataSet, GlobalDataSet, MixedDataSet
from pyop2.types.map import Map, MixedMap
from pyop2.parloop import AbstractParloop
from pyop2.global_kernel import AbstractGlobalKernel
from pyop2.types.access import READ, INC, MIN, MAX
from pyop2.types.mat import Mat
from pyop2.types.glob import Global as BaseGlobal, Constant as BaseConstant
from pyop2.backends import AbstractComputeBackend
from petsc4py import PETSc
from pyop2 import (
compilation,
mpi,
utils
)
import ctypes
import os
import loopy as lp
from contextlib import contextmanager
import numpy as np
class Dat(BaseDat):
@utils.cached_property
def _vec(self):
assert self.dtype == PETSc.ScalarType, \
"Can't create Vec with type %s, must be %s" % (self.dtype,
PETSc.ScalarType)
# Can't duplicate layout_vec of dataset, because we then
# carry around extra unnecessary data.
# But use getSizes to save an Allreduce in computing the
# global size.
size = self.dataset.layout_vec.getSizes()
data = self._data[:size[0]]
vec = PETSc.Vec().createWithArray(data, size=size,
bsize=self.cdim, comm=self.comm)
return vec
@contextmanager
def vec_context(self, access):
# PETSc Vecs have a state counter and cache norm computations
# to return immediately if the state counter is unchanged.
# Since we've updated the data behind their back, we need to
# change that state counter.
self._vec.stateIncrease()
yield self._vec
if access is not READ:
self.halo_valid = False
def ensure_availability_on_device(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
def ensure_availability_on_host(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
@mpi.collective
def copy(self, other, subset=None):
if other is self:
return
if subset is None:
# If the current halo is valid we can also copy these values across.
if self.halo_valid:
other._data[:] = self.data_ro
other.halo_valid = True
else:
other.data[:] = self.data_ro
elif subset.superset != self.dataset.set:
raise ex.MapValueError("The subset and dataset are incompatible")
else:
other.data[subset.owned_indices] = self.data_ro[subset.owned_indices]
class Global(BaseGlobal):
@utils.cached_property
def _vec(self):
assert self.dtype == PETSc.ScalarType, \
"Can't create Vec with type %s, must be %s" % (self.dtype,
PETSc.ScalarType)
# Can't duplicate layout_vec of dataset, because we then
# carry around extra unnecessary data.
# But use getSizes to save an Allreduce in computing the
# global size.
data = self._data
size = self.dataset.layout_vec.getSizes()
if self.comm.rank == 0:
return PETSc.Vec().createWithArray(data, size=size,
bsize=self.cdim,
comm=self.comm)
else:
return PETSc.Vec().createWithArray(np.empty(0, dtype=self.dtype),
size=size,
bsize=self.cdim,
comm=self.comm)
@contextmanager
def vec_context(self, access):
"""A context manager for a :class:`PETSc.Vec` from a :class:`Global`.
:param access: Access descriptor: READ, WRITE, or RW."""
yield self._vec
if access is not READ:
data = self._data
self.comm.Bcast(data, 0)
def ensure_availability_on_device(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
def ensure_availability_on_host(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
class Constant(BaseConstant):
@utils.cached_property
def _vec(self):
assert self.dtype == PETSc.ScalarType, \
"Can't create Vec with type %s, must be %s" % (self.dtype,
PETSc.ScalarType)
# Can't duplicate layout_vec of dataset, because we then
# carry around extra unnecessary data.
# But use getSizes to save an Allreduce in computing the
# global size.
data = self._data
size = self.dataset.layout_vec.getSizes()
if self.comm.rank == 0:
return PETSc.Vec().createWithArray(data, size=size,
bsize=self.cdim,
comm=self.comm)
else:
return PETSc.Vec().createWithArray(np.empty(0, dtype=self.dtype),
size=size,
bsize=self.cdim,
comm=self.comm)
@contextmanager
def vec_context(self, access):
"""A context manager for a :class:`PETSc.Vec` from a :class:`Global`.
:param access: Access descriptor: READ, WRITE, or RW."""
yield self._vec
if access is not READ:
data = self._data
self.comm.Bcast(data, 0)
def ensure_availability_on_device(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
def ensure_availability_on_host(self):
from pyop2.op2 import compute_backend
assert compute_backend is cpu_backend
# data transfer is noop for CPU backend
class GlobalKernel(AbstractGlobalKernel):
@utils.cached_property
def code_to_compile(self):
"""Return the C/C++ source code as a string."""
from pyop2.codegen.rep2loopy import generate
wrapper = generate(self.builder)
code = lp.generate_code_v2(wrapper)
if self.local_kernel.cpp:
from loopy.codegen.result import process_preambles
preamble = "".join(
process_preambles(getattr(code, "device_preambles", [])))
device_code = "\n\n".join(str(dp.ast) for dp in code.device_programs)
return preamble + '\nextern "C" {\n' + device_code + "\n}\n"
return code.device_code()
@PETSc.Log.EventDecorator()
@mpi.collective
def compile(self, comm):
"""Compile the kernel.
:arg comm: The communicator the compilation is collective over.
:returns: A ctypes function pointer for the compiled function.
"""
extension = "cpp" if self.local_kernel.cpp else "c"
cppargs = (
tuple("-I%s/include" % d for d in utils.get_petsc_dir())
+ tuple("-I%s" % d for d in self.local_kernel.include_dirs)
+ ("-I%s" % os.path.abspath(os.path.dirname(__file__)),)
)
ldargs = (
tuple("-L%s/lib" % d for d in utils.get_petsc_dir())
+ tuple("-Wl,-rpath,%s/lib" % d for d in utils.get_petsc_dir())
+ ("-lpetsc", "-lm")
+ tuple(self.local_kernel.ldargs)
)
return compilation.load(self, extension, self.name,
cppargs=cppargs,
ldargs=ldargs,
restype=ctypes.c_int,
comm=comm)
class Parloop(AbstractParloop):
@PETSc.Log.EventDecorator("ParLoopRednBegin")
@mpi.collective
def reduction_begin(self):
"""Begin reductions."""
requests = []
for idx in self._reduction_idxs:
glob = self.arguments[idx].data
mpi_op = {INC: mpi.MPI.SUM,
MIN: mpi.MPI.MIN,
MAX: mpi.MPI.MAX}.get(self.accesses[idx])
if mpi.MPI.VERSION >= 3:
requests.append(self.comm.Iallreduce(glob._data,
glob._buf,
op=mpi_op))
else:
self.comm.Allreduce(glob._data, glob._buf, op=mpi_op)
return tuple(requests)
@PETSc.Log.EventDecorator("ParLoopRednEnd")
@mpi.collective
def reduction_end(self, requests):
"""Finish reductions."""
if mpi.MPI.VERSION >= 3:
mpi.MPI.Request.Waitall(requests)
for idx in self._reduction_idxs:
glob = self.arguments[idx].data
glob._data[:] = glob._buf
else:
assert len(requests) == 0
for idx in self._reduction_idxs:
glob = self.arguments[idx].data
glob._data[:] = glob._buf
class CPUBackend(AbstractComputeBackend):
GlobalKernel = GlobalKernel
Parloop = Parloop
Set = Set
ExtrudedSet = ExtrudedSet
MixedSet = MixedSet
Subset = Subset
DataSet = DataSet
MixedDataSet = MixedDataSet
Map = Map
MixedMap = MixedMap
Dat = Dat
MixedDat = MixedDat
DatView = DatView
Mat = Mat
Global = Global
Constant = Constant
GlobalDataSet = GlobalDataSet
PETScVecType = PETSc.Vec.Type.STANDARD
def turn_on_offloading(self):
pass
def turn_off_offloading(self):
pass
@property
def cache_key(self):
return (type(self),)
cpu_backend = CPUBackend()