BobSteagall / wg21

Various documents and code related to proposals for WG21
University of Illinois/NCSA Open Source License
65 stars 11 forks source link

possibility of constructing vectors of matrices with uninitialized dara #73

Open RiccardoRossi opened 2 years ago

RiccardoRossi commented 2 years ago

dear developers,

i skimmed through the tests and i have the impression that vector and matrices are initialized to zero at construction.

in many cases this initialization is unwanted, as assignement will happen at a later stage.

this is crucial for example if one wants to do first touching on the entries of the vector to ensure numa locality (that's one of the reasons for which std::vector cannot be used in HPC code)

is there any way of avoiding it?

correaa commented 2 years ago

I agree that zero initialization of arrays is unnecessary in many cases, even if it goes against STL common practice (misguided in my opinion).

This is particularly true for trivially constructible types, doubles, float, ints, and some user defined types. (std::complex also, except that STL's complex is defective for not being trivially default constructible).

This is very important for performance of numeric software and also to detect use of uninitialized memory with runtime tests (e.g. with Valgrind).

In my library for arrays https://gitlab.com/correaa/boost-multi , which I am currently testing as storage engine for this proposal, zero initialization is optional for trivially constructible types.

If it serves as a example for this proposal, here it is some behavior example

multi::array<double, 2> A({100, 100}); // array of dimension 2 with uninitialized data, this is ok because double is trivial
multi::array<double, 2> B({100, 100}, {}); // array of dimension 2 with initialized data (to zero)
multi::array<double, 2> C({100, 100}, 99.); // array of dimension 2 with initialized data to 99.
multi::array<std::complex<double>, 2> D({100, 100}); // array of dimension 2 with initialized data (to zero) because std::complex is not trivially constructible

Please let me know if I am missing something. I hope this idiom catches on.