def get_jacobian(Is, I_dat, O, wrt): dy = edsl.jacobian(O, [wrt])[0] program = edsl.Program('program', [O, dy]) binder = pld_exec.Binder(program) executable = binder.compile() for i in range(len(Is)): binder.input(Is[i]).copy_from_ndarray(I_dat[i]) executable.run() return binder.output(dy).as_ndarray()
def _compile(self, inputs): for node, data in zip(self._inputs, inputs): dtype = node.tensor.shape.dtype shape = edsl.LogicalShape(dtype, data.shape) node.tensor.bind(shape) outputs = [x.tensor for x in self._outputs] updates = [(x[0].tensor, x[1].tensor) for x in self._updates] program = edsl.Program(self._name, outputs, updates) return plaidml_exec.Executable(program, [x.tensor for x in self._inputs])
def toroidal_shell_integral(n, minval, maxval, eps, benchmark=False): coordvals = np.linspace(minval, maxval, n, dtype=np.float32) delta = (maxval - minval) / (n - 1) # grid spacing X_data = coordvals.reshape(n, 1, 1) Y_data = coordvals.reshape(1, n, 1) Z_data = coordvals.reshape(1, 1, n) X = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, X_data.shape)) Y = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Y_data.shape)) Z = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Z_data.shape)) # f-rep of torodial shell f(x, y, z) = (sqrt(x^2 + y^2) - 1)^2 + z^2 + (0.1)^2 F = sq(edsl.sqrt(sq(X) + sq(Y)) - 1.0) + sq(Z) - sq(0.1) # moment of inertia about z axis at each point g(x, y, z) = x^2 + y^2 G = sq(X) + sq(Y) DFDX = partial(F, 'x', delta) DFDY = partial(F, 'y', delta) DFDZ = partial(F, 'z', delta) # chi: occupancy function: 1 inside the region (f<0), 0 outside the region (f>0) DCHIDX = partial_chi(F, 'x', delta) DCHIDY = partial_chi(F, 'y', delta) DCHIDZ = partial_chi(F, 'z', delta) NUMER = DFDX * DCHIDX + DFDY * DCHIDY + DFDZ * DCHIDZ DENOM = edsl.sqrt(sq(DFDX) + sq(DFDY) + sq(DFDZ)) H = edsl.select(DENOM < eps, 0, NUMER / DENOM) O = sum(-G * H) program = edsl.Program('toroidal_shell_integral', [O]) binder = plaidml_exec.Binder(program) executable = binder.compile() def run(): binder.input(X).copy_from_ndarray(X_data) binder.input(Y).copy_from_ndarray(Y_data) binder.input(Z).copy_from_ndarray(Z_data) executable.run() return binder.output(O).as_ndarray() if benchmark: # the first run will compile and run print('compiling...') result = run() # subsequent runs should not include compile time print('running...') ITERATIONS = 100 elapsed = timeit.timeit(run, number=ITERATIONS) print('runtime:', elapsed / ITERATIONS) else: result = run() return result * (delta**3)
def _compile(self, inputs): for node, data in zip(self._inputs, inputs): dtype = node.tensor.shape.dtype shape = edsl.LogicalShape(dtype, data.shape) node.tensor.bind(shape) outputs = [x.tensor for x in self._outputs] updates = [(x[0].tensor, x[1].tensor) for x in self._updates] program = edsl.Program(self._name, outputs, updates) def make_buffer(tensor): # convert LogicalShape into TensorShape shape = plaidml.TensorShape(tensor.shape.dtype, tensor.shape.int_dims) return plaidml.Buffer(_device, shape) input_bindings = [(x.tensor, make_buffer(x.tensor)) for x in self._inputs] output_bindings = [(x, make_buffer(x)) for x in program.outputs] return plaidml_exec.Executable(program, _device, _target, input_bindings, output_bindings)
def main(): print(""" PlaidML Setup ({0}) Thanks for using PlaidML! Some Notes: * Bugs and other issues: https://github.com/plaidml/plaidml * Questions: https://stackoverflow.com/questions/tagged/plaidml * Say hello: https://groups.google.com/forum/#!forum/plaidml-dev * PlaidML is licensed under the Apache License 2.0 """.format(plaidml.__version__)) devices = sorted(plaidml_exec.list_devices()) targets = sorted(plaidml_exec.list_targets()) if not devices: print(""" No OpenCL devices found. Check driver installation. Read the helpful, easy driver installation instructions from our README: http://github.com/plaidml/plaidml """) sys.exit(-1) dev_idx = 0 if len(devices) > 1: print(""" Multiple devices detected (You can override by setting PLAIDML_DEVICE). Please choose a default device: """) for i, device in enumerate(devices): print(" {} : {}".format(i + 1, device)) choices = [str(i + 1) for i in range(len(devices))] dev_idx = int(choice_prompt("\nDefault device", choices, "1")) plaidml_settings.set('PLAIDML_DEVICE', devices[dev_idx - 1]) device = plaidml_settings.get('PLAIDML_DEVICE') print() print("Selected device:") print(" {}".format(device)) print() print("A target determines the compiler configuration and should be matched with your device.") print("Please choose a default target:") for i, target in enumerate(targets): print(" {} : {}".format(i + 1, target)) choices = [str(i + 1) for i in range(len(targets))] tgt_idx = int(choice_prompt("\nDefault target", choices, "1")) plaidml_settings.set('PLAIDML_TARGET', targets[tgt_idx - 1]) target = plaidml_settings.get('PLAIDML_TARGET') print() print("Selected target:") print(" {}".format(target)) print() print("Almost done. Multiplying some matrices...") print("Tile code:") print(" function (B[X, Z], C[Z, Y]) -> (A) { A[x, y : X, Y] = +(B[x, z] * C[z, y]); }") shape = edsl.LogicalShape(plaidml.DType.FLOAT32, [3, 3]) B = edsl.Tensor(shape) C = edsl.Tensor(shape) X, Y, Z = edsl.TensorDims(3) x, y, z = edsl.TensorIndexes(3) B.bind_dims(X, Z) C.bind_dims(Z, Y) A = edsl.TensorOutput(X, Y) A[x, y] += B[x, z] * C[z, y] program = edsl.Program('plaidml_setup', [A]) plaidml_exec.run(program, [(B, np.random.rand(3, 3)), (C, np.random.rand(3, 3))]) print("Whew. That worked.") print() settings_path = plaidml_settings.get('PLAIDML_SETTINGS') save = choice_prompt("Save settings to {0}".format(settings_path), ["y", "n"], "y") if save == "y": plaidml_settings.save() print("Success!") print()