|
| 1 | +from pyop2.types.dat import Dat as BaseDat, MixedDat, DatView |
| 2 | +from pyop2.types.set import Set, ExtrudedSet, Subset, MixedSet |
| 3 | +from pyop2.types.dataset import DataSet, GlobalDataSet, MixedDataSet |
| 4 | +from pyop2.types.map import Map, MixedMap |
| 5 | +from pyop2.parloop import AbstractParLoop, AbstractJITModule |
| 6 | +from pyop2.types.mat import Mat |
| 7 | +from pyop2.glob import Global |
| 8 | +from pyop2.backends import AbstractComputeBackend |
| 9 | +from petsc4py import PETSc |
| 10 | +from . import ( |
| 11 | + compilation, |
| 12 | + configuration as conf, |
| 13 | + datatypes as dtypes, |
| 14 | + mpi, |
| 15 | + utils |
| 16 | +) |
| 17 | + |
| 18 | +import ctypes |
| 19 | +import os |
| 20 | +import loopy as lp |
| 21 | + |
| 22 | + |
| 23 | +class Dat(BaseDat): |
| 24 | + @utils.cached_property |
| 25 | + def _vec(self): |
| 26 | + assert self.dtype == PETSc.ScalarType, \ |
| 27 | + "Can't create Vec with type %s, must be %s" % (self.dtype, PETSc.ScalarType) |
| 28 | + # Can't duplicate layout_vec of dataset, because we then |
| 29 | + # carry around extra unnecessary data. |
| 30 | + # But use getSizes to save an Allreduce in computing the |
| 31 | + # global size. |
| 32 | + size = self.dataset.layout_vec.getSizes() |
| 33 | + data = self._data[:size[0]] |
| 34 | + vec = PETSc.Vec().createWithArray(data, size=size, bsize=self.cdim, comm=self.comm) |
| 35 | + return vec |
| 36 | + |
| 37 | + |
| 38 | +class JITModule(AbstractJITModule): |
| 39 | + @utils.cached_property |
| 40 | + def code_to_compile(self): |
| 41 | + from pyop2.codegen.builder import WrapperBuilder |
| 42 | + from pyop2.codegen.rep2loopy import generate |
| 43 | + |
| 44 | + builder = WrapperBuilder(kernel=self._kernel, |
| 45 | + iterset=self._iterset, |
| 46 | + iteration_region=self._iteration_region, |
| 47 | + pass_layer_to_kernel=self._pass_layer_arg) |
| 48 | + for arg in self._args: |
| 49 | + builder.add_argument(arg) |
| 50 | + |
| 51 | + wrapper = generate(builder) |
| 52 | + code = lp.generate_code_v2(wrapper) |
| 53 | + |
| 54 | + if self._kernel._cpp: |
| 55 | + from loopy.codegen.result import process_preambles |
| 56 | + preamble = "".join(process_preambles(getattr(code, "device_preambles", []))) |
| 57 | + device_code = "\n\n".join(str(dp.ast) for dp in code.device_programs) |
| 58 | + return preamble + "\nextern \"C\" {\n" + device_code + "\n}\n" |
| 59 | + return code.device_code() |
| 60 | + |
| 61 | + @PETSc.Log.EventDecorator() |
| 62 | + @mpi.collective |
| 63 | + def compile(self): |
| 64 | + # If we weren't in the cache we /must/ have arguments |
| 65 | + if not hasattr(self, '_args'): |
| 66 | + raise RuntimeError("JITModule has no args associated with it, should never happen") |
| 67 | + |
| 68 | + compiler = conf.configuration["compiler"] |
| 69 | + extension = "cpp" if self._kernel._cpp else "c" |
| 70 | + cppargs = self._cppargs |
| 71 | + cppargs += ["-I%s/include" % d for d in utils.get_petsc_dir()] + \ |
| 72 | + ["-I%s" % d for d in self._kernel._include_dirs] + \ |
| 73 | + ["-I%s" % os.path.abspath(os.path.dirname(__file__))] |
| 74 | + ldargs = ["-L%s/lib" % d for d in utils.get_petsc_dir()] + \ |
| 75 | + ["-Wl,-rpath,%s/lib" % d for d in utils.get_petsc_dir()] + \ |
| 76 | + ["-lpetsc", "-lm"] + self._libraries |
| 77 | + ldargs += self._kernel._ldargs |
| 78 | + |
| 79 | + self._fun = compilation.load(self, |
| 80 | + extension, |
| 81 | + self._wrapper_name, |
| 82 | + cppargs=cppargs, |
| 83 | + ldargs=ldargs, |
| 84 | + restype=ctypes.c_int, |
| 85 | + compiler=compiler, |
| 86 | + comm=self.comm) |
| 87 | + # Blow away everything we don't need any more |
| 88 | + del self._args |
| 89 | + del self._kernel |
| 90 | + del self._iterset |
| 91 | + |
| 92 | + @utils.cached_property |
| 93 | + def argtypes(self): |
| 94 | + index_type = dtypes.as_ctypes(dtypes.IntType) |
| 95 | + argtypes = (index_type, index_type) |
| 96 | + argtypes += self._iterset._argtypes_ |
| 97 | + for arg in self._args: |
| 98 | + argtypes += arg._argtypes_ |
| 99 | + seen = set() |
| 100 | + for arg in self._args: |
| 101 | + maps = arg.map_tuple |
| 102 | + for map_ in maps: |
| 103 | + for k, t in zip(map_._kernel_args_, map_._argtypes_): |
| 104 | + if k in seen: |
| 105 | + continue |
| 106 | + argtypes += (t,) |
| 107 | + seen.add(k) |
| 108 | + return argtypes |
| 109 | + ... |
| 110 | + |
| 111 | + |
| 112 | +class ParLoop(AbstractParLoop): |
| 113 | + |
| 114 | + def prepare_arglist(self, iterset, *args): |
| 115 | + arglist = iterset._kernel_args_ |
| 116 | + for arg in args: |
| 117 | + arglist += arg._kernel_args_ |
| 118 | + seen = set() |
| 119 | + for arg in args: |
| 120 | + maps = arg.map_tuple |
| 121 | + for map_ in maps: |
| 122 | + if map_ is None: |
| 123 | + continue |
| 124 | + for k in map_._kernel_args_: |
| 125 | + if k in seen: |
| 126 | + continue |
| 127 | + arglist += (k,) |
| 128 | + seen.add(k) |
| 129 | + return arglist |
| 130 | + |
| 131 | + @utils.cached_property |
| 132 | + def _jitmodule(self): |
| 133 | + return JITModule(self.kernel, self.iterset, *self.args, |
| 134 | + iterate=self.iteration_region, |
| 135 | + pass_layer_arg=self._pass_layer_arg) |
| 136 | + |
| 137 | + @mpi.collective |
| 138 | + def _compute(self, part, fun, *arglist): |
| 139 | + with self._compute_event: |
| 140 | + self.log_flops(part.size * self.num_flops) |
| 141 | + fun(part.offset, part.offset + part.size, *arglist) |
| 142 | + |
| 143 | + |
| 144 | +class CPUBackend(AbstractComputeBackend): |
| 145 | + ParLoop = ParLoop |
| 146 | + Set = Set |
| 147 | + ExtrudedSet = ExtrudedSet |
| 148 | + MixedSet = MixedSet |
| 149 | + Subset = Subset |
| 150 | + DataSet = DataSet |
| 151 | + MixedDataSet = MixedDataSet |
| 152 | + Map = Map |
| 153 | + MixedMap = MixedMap |
| 154 | + Dat = Dat |
| 155 | + MixedDat = MixedDat |
| 156 | + DatView = DatView |
| 157 | + Mat = Mat |
| 158 | + Global = Global |
| 159 | + GlobalDataSet = GlobalDataSet |
| 160 | + PETScVecType = 'standard' |
| 161 | + |
| 162 | + def turn_on_offloading(self): |
| 163 | + pass |
| 164 | + |
| 165 | + def turn_off_offloading(self): |
| 166 | + pass |
| 167 | + |
| 168 | + |
| 169 | +cpu_backend = CPUBackend() |
0 commit comments