Skip to content

Destructuring field binding

Add DestructuringBindingsForFieldClass to use pystencils kernels in a more C++-ish way

DestructuringBindingsForFieldClass defines all field-related variables in its subordinated block. However, it leaves a TypedSymbol of type Field for each field undefined. By that trick we can generate kernels that accept structs as kernelparameters. Either to include a pystencils specific Field struct of the following definition:

template<DTYPE_T, DIMENSION>
struct Field
{
    DTYPE_T* data;
    std::array<int64_t, DIMENSION> shape;
    std::array<int64_t, DIMENSION> stride;
}

or to be able to destructure user defined types like pybind11::array, at::Tensor, tensorflow::Tensor.

The test generates a kernel like that:

FUNC_PREFIX void kernel(Field<double, 2>& x, Field<double, 2>& y, Field<double, 2>& z)
{
   _stride_z_1 = z.stride[1];
   _size_x_0 = x.shape[0];
   _stride_x_1 = x.stride[1];
   _stride_z_0 = z.stride[0];
   _size_x_1 = x.shape[1];
   _stride_y_1 = y.stride[1];
   _data_x = x.data;
   _stride_x_0 = x.stride[0];
   _data_z = z.data;
   _stride_y_0 = y.stride[0];
   _data_y = y.data;
   {
      for (int ctr_0 = 0; ctr_0 < _size_x_0; ctr_0 += 1)
      {
         double * RESTRICT _data_z_00 = _data_z + _stride_z_0*ctr_0;
         double * RESTRICT const _data_y_00 = _data_y + _stride_y_0*ctr_0;
         double * RESTRICT const _data_x_00 = _data_x + _stride_x_0*ctr_0;
         for (int ctr_1 = 0; ctr_1 < _size_x_1; ctr_1 += 1)
         {
            _data_z_00[_stride_z_1*ctr_1] = log(_data_x_00[_stride_x_1*ctr_1]*_data_y_00[_stride_y_1*ctr_1])*_data_y_00[_stride_y_1*ctr_1];
         }
      }
   }
}
Edited by Stephan Seitz

Merge request reports