Re: [boost] Re: Boost Array Initialization Technique

In-Reply-To: <005501c566b3$1fc76eb0$2b792518@heronnest> cdiggins@videotron.ca (christopher diggins) wrote (abridged):
I strongly disagree. Forcing initialization makes code that is not useful for high performance numerical processing. When we declare a container of 1000000 elements, we don't want to waste time initializing each, unless we want to.
I think the best solution in this case is to use an alternative "collection" for numerical processing. Using a class intended as general purpose array for numerical processing I think would not be a good idea.
I doubt we need a whole class. A special argument might do, to let us choose: boost::array<int,1000> b( boost::uninitialised ); boost::array<int,1000> b( boost::initialised ); The question for me is what the default should be. And in my view, the default should be safe rather than fast. ---- For what it's worth, I think auto variables, std::complex and even std::vector should be the same: initialised by default but with uninitialised available as an option. And since this is now a language change, perhaps "boost::initialised" should be spelt "?" for brevity. int x; // x == 0. int y = ?; // y is uninitialised. int *px = new int; // *px == 0. int *py = new int( ? ) // *py is uninitialised. vector<int> vx( 100 ); // vx.front() is 0. vector<int> vy( 100, ? ); Vectors are complicated in that their elements currently need to be copyable. Either the standard needs to specify when they are copied, so that eg vy is OK as long as the vector is not resized; or else it could make initialisation implementation-defined in the ? case. Then on most platforms, vector<int> would not need initialisation but vector<double> would. User code would not be able to tell the difference. Basically, initialising with ? is telling the implementation that the user will always write to the variable before reading it. The implementation exploits that knowledge as best it can. -- Dave Harris, Nottingham, UK.

Dave Harris wrote:
In-Reply-To: <005501c566b3$1fc76eb0$2b792518@heronnest> cdiggins@videotron.ca (christopher diggins) wrote (abridged):
I strongly disagree. Forcing initialization makes code that is not useful for high performance numerical processing. When we declare a container of 1000000 elements, we don't want to waste time initializing each, unless we want to.
I think the best solution in this case is to use an alternative "collection" for numerical processing. Using a class intended as general purpose array for numerical processing I think would not be a good idea.
I doubt we need a whole class. A special argument might do, to let us choose:
boost::array<int,1000> b( boost::uninitialised ); boost::array<int,1000> b( boost::initialised );
array can't be given constructors without sacrificing the aggregate initialization syntax. Jonathan

Dave Harris wrote:
In-Reply-To: <005501c566b3$1fc76eb0$2b792518@heronnest> cdiggins@videotron.ca (christopher diggins) wrote (abridged):
I strongly disagree. Forcing initialization makes code that is not useful for high performance numerical processing. When we declare a container of 1000000 elements, we don't want to waste time initializing each, unless we want to.
I think the best solution in this case is to use an alternative "collection" for numerical processing. Using a class intended as general purpose array for numerical processing I think would not be a good idea.
I doubt we need a whole class. A special argument might do, to let us choose:
boost::array<int,1000> b( boost::uninitialised ); boost::array<int,1000> b( boost::initialised );
That's a reasonable proposition. But for the initialized case, you want to be specifying a value to initialize to - zero is *not* a reasonable default for all circumstance.
The question for me is what the default should be. And in my view, the default should be safe rather than fast.
From what I gather, and the commentary you make below, I seriously beg to differ. Back to the year dot with C & C++ we've never had such a thing as pre-initialization of arrays and it'd be a bad time to start such a movement now. Furthermore, for persons like myself writing plenty of numerical code, and algorithmic development - the default better be the fastest and with a minimalist take on assumptions - as always ... trust the developer to know what they are doing.
---- For what it's worth, I think auto variables, std::complex and even std::vector should be the same: initialised by default but with uninitialised available as an option. And since this is now a language change, perhaps "boost::initialised" should be spelt "?" for brevity.
int x; // x == 0. int y = ?; // y is uninitialised. int *px = new int; // *px == 0. int *py = new int( ? ) // *py is uninitialised. vector<int> vx( 100 ); // vx.front() is 0. vector<int> vy( 100, ? );
Please let's not go down this path; and as for vectors, we can opt to initialize on construction - I can't see the benifit of such a proposal, but to safeguard against the forgetful. my $0.02 Cheers, -- Manfred Doudar MetOcean Engineers www.metoceanengineers.com

On Mon, Jun 06, 2005 at 12:22:14PM +0800, Manfred Doudar wrote:
Dave Harris wrote:
In-Reply-To: <005501c566b3$1fc76eb0$2b792518@heronnest> cdiggins@videotron.ca (christopher diggins) wrote (abridged):
I strongly disagree. Forcing initialization makes code that is not useful for high performance numerical processing. When we declare a container of 1000000 elements, we don't want to waste time initializing each, unless we want to.
I think the best solution in this case is to use an alternative "collection" for numerical processing. Using a class intended as general purpose array for numerical processing I think would not be a good idea.
I doubt we need a whole class. A special argument might do, to let us choose:
boost::array<int,1000> b( boost::uninitialised ); boost::array<int,1000> b( boost::initialised );
That's a reasonable proposition. But for the initialized case, you want to be specifying a value to initialize to - zero is *not* a reasonable default for all circumstance.
Besides which, you can already default-initialise (i.e. 0 for builtins) an array of any size: boost::array<int,1000> b = { }; which is the equivalent of: std::vector<int> v(1000); For those who would want to construct an array from a range of iterators, construct it uninitialised and use std::generate(). If you want the array to be const then copy-construct it from the return value of an inline function that uses std::generate() (or some other method). Inlining and RVO should make that pretty efficient, and if you're doing this it's probably once at startup anyway. For those who want the equivalent of: vector::vector(size_type, const value_type&); you could always use the preprocessor: boost::array<int,1000> b = { MAGIC_ARRAY_INIT(1000, 5) }; I think boost::array works fine uninitialised by default. jon
participants (4)
-
brangdon@cix.compulink.co.uk
-
Jonathan Turkanis
-
Jonathan Wakely
-
Manfred Doudar