Default constructible views and sequences

Issue #184 new
Darcy Beurle created an issue

Is it possible to have a default constructed empty view into a vector?

The reason I ask is that for FE applications, there is typically a global solution vector which could encode several fields which belong to different bodies. In order to save on copying and additional storage, a local constant 'view' into this global vector would be nice (like what already exists) but allow this view to be reassigned as the program progresses.

Furthermore, this vector could also represent encoded fields (u1, u2, u3, t1, t2, t3 etc) such that a view into 'one' of these fields would be very advantageous.

Something like this very rough pseudo code

class sub_body
{
public:
   void update_view(vector_view view) { local_solution = view); }
private:
   vector_view local_solution;
}

class assembler
{
   void update()
   {
      for each sub_body
          update_view(solution(some_valid_range));
   }

    vector solution;
};

gsl has a span that is default constructible but it would be nice to have it handled natively in Blaze. Is this possible?

Comments (1)

  1. Klaus Iglberger

    Hi Darcy!

    Thanks for creating this issue. We understand the problem, but unfortunately it will not be as simple as assigning two views of the same kind in order to change a view. Semantically, there is a significant difference between views in the C++ standard and the Blaze views.

    Assigning to a view from the C++ standard (e.g. std::string_view and std::span) changes the view itself, i.e. it is not possible to assign to the represented elements:

    std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8 };
    std::span<int> sub1( v.data(), 4 );  // Creating a view on the elements [0..3]
    std::span<int> sub2( v.data()+4, 4 );  // Creating a view on the elements [4..7]
    sub1 = sub2;  // Modifying the view, not the elements; 'sub1' now is a view on the elements [4..7]
    

    In Blaze a view represents a reference into an existing vector or matrix, which can only be initialized, but not modified (just as a C++ reference). Assigning to a Blaze view changes the represented elements, not the view. This enables you for instance to assign one part of a vector to another part of the vector or one row of a matrix to another row:

    blaze::DynamicVector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8 };
    auto sub1 = subvector( v, 0UL, 4UL );  // Create a subvector on the elements [0..3]
    auto sub2 = subvector( v, 4UL, 4UL );  // Create a subvector on the elements [4..7]
    sub1 = sub2;  // Assign the elements [4..7] to the elements [0..3]; Modifying the elements, not the view
    
    blaze::DynamicMatrix<int> A{ { 1, 2, 3 }
                               , { 4, 5, 6 }
                               , { 7, 8, 9 } };
    auto row0 = row( A, 0UL );  // Create a view on the 0th row of A
    auto row2 = row( A, 2UL );  // Create a view on the 2nd row of A
    row0 = row2;  // Assign the 2nd row of A to the 0th row
    

    In both examples, the types of views on the left-hand side and right-hand side views are identical. In both cases the assignment does not modify the left-hand side range, but performs an assignment of the represented elements.

    Both the C++ standard approach and the Blaze approach have their merits. For numerical tasks we believe that the way Blaze handles assignment to views is more intuitive and convenient for most cases. Still, we understand your motivation and we will think about whether it is possible to combine your idea with the semantics of the Blaze views.

    Best regards,

    Klaus!

  2. Log in to comment