Skip to content

Use get_type_of_expression in typing_form_sympy_inspection to infer types

DANGER ZONE: this changes something in the core behavior of pystencils. Be careful before merging!

In summary, when typing_form_sympy_inspection reaches the point where it would just use default_type, we try to use get_type_of_expression to infer the actual type. We use information of previously defined variables in current scope.

Another approach would be to just type all the intermediate variable with auto.


    x = pystencils.fields('x:  float32[3d]')
    assignments = pystencils.AssignmentCollection({
        a: cast_func(10, create_type('float64')),
        b: cast_func(10, create_type('uint16')),
        e: 11,
        c: b,
        f: c + b,
        d: c + b + x.center + e,
        x.center: c + b + x.center
    })

Before:

FUNC_PREFIX void kernel(float * RESTRICT _data_x, int64_t const _size_x_0, int64_t const _size_x_1,
 int64_t const _size_x_2, int64_t const _stride_x_0, int64_t const _stride_x_1, int64_t const _stri
de_x_2)
{
   const double a = 10.0;
   const double b = 10;
   const double e = 11.0;
   const double c = b;
   const double f = b + c;
   for (int ctr_0 = 0; ctr_0 < _size_x_0; ctr_0 += 1)
   {
      float * RESTRICT _data_x_00 = _data_x + _stride_x_0*ctr_0;
      for (int ctr_1 = 0; ctr_1 < _size_x_1; ctr_1 += 1)
      {
         float * RESTRICT _data_x_00_10 = _stride_x_1*ctr_1 + _data_x_00;
         for (int ctr_2 = 0; ctr_2 < _size_x_2; ctr_2 += 1)
         {
            const double d = b + c + e + _data_x_00_10[_stride_x_2*ctr_2];
            _data_x_00_10[_stride_x_2*ctr_2] = b + c + _data_x_00_10[_stride_x_2*ctr_2];
         }
      }
   }
}

After:

FUNC_PREFIX void kernel(float * RESTRICT _data_x, int64_t const _size_x_0, int64_t const _size_x_1,
 int64_t const _size_x_2, int64_t const _stride_x_0, int64_t const _stride_x_1, int64_t const _stri
de_x_2)
{
   const double a = 10.0;
   const uint16_t b = 10;
   const int64_t e = 11.0;
   const uint16_t c = b;
   const uint16_t f = b + c;
   for (int ctr_0 = 0; ctr_0 < _size_x_0; ctr_0 += 1)
   {
      float * RESTRICT _data_x_00 = _data_x + _stride_x_0*ctr_0;
      for (int ctr_1 = 0; ctr_1 < _size_x_1; ctr_1 += 1)
      {
         float * RESTRICT _data_x_00_10 = _stride_x_1*ctr_1 + _data_x_00;
         for (int ctr_2 = 0; ctr_2 < _size_x_2; ctr_2 += 1)
         {
            const float d = b + c + e + _data_x_00_10[_stride_x_2*ctr_2];
            _data_x_00_10[_stride_x_2*ctr_2] = b + c + _data_x_00_10[_stride_x_2*ctr_2];
         }
      }
   }
}
Edited by Stephan Seitz

Merge request reports