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 run(program, inputs): def make_buffer(tensor): # convert LogicalShape into TensorShape shape = plaidml.TensorShape(tensor.shape.dtype, tensor.shape.int_dims) return plaidml.Buffer(device, shape) ibindings = [(x, make_buffer(x)) for x, y in inputs] obindings = [(x, make_buffer(x)) for x in program.outputs] exe = plaidml_exec.Executable(program, device, target, ibindings, obindings) return [x.as_ndarray() for x in exe([y for x, y in 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]) executable = plaidml_exec.Executable(program, [X, Y, Z]) def run(): outputs = executable([X_data, Y_data, Z_data]) return outputs[0].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)