Serving the Quantitative Finance Community

 
User avatar
renorm
Topic Author
Posts: 1
Joined: February 11th, 2010, 10:20 pm

Use and abuse of STL vector

October 7th, 2010, 5:48 am

C++ is good. It lets us to abuse almost everything.I want to use STL vector as a glorified pointer. Take a look at this code fragment and tell me whether it will work on any SSE2 platform that exists today. Thanks.
 
User avatar
Marine
Posts: 0
Joined: July 17th, 2003, 7:56 am

Use and abuse of STL vector

October 7th, 2010, 8:20 am

I imagine it will determine how exacting the momery allocation is done (different implemetations). It might work fine for the first 10 elements but as soon as it resizes you might be in trouble.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

Use and abuse of STL vector

October 7th, 2010, 8:33 am

isn't it a requirement that the memory allocated ny vector is contiguous so it should work.
Last edited by mj on October 6th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
rhtiwari
Posts: 0
Joined: January 15th, 2010, 12:41 pm

Use and abuse of STL vector

October 7th, 2010, 9:12 am

And since the memory is continuous, there is nothing stopping it from working.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Use and abuse of STL vector

October 7th, 2010, 9:25 am

QuoteOriginally posted by: renormC++ is good. It lets us to abuse almost everything.I want to use STL vector as a glorified pointer. Take a look at this code fragment and tell me whether it will work on any SSE2 platform that exists today. Thanks.yet another valarray (i.e. high performance vector)? You don't mention why you wish to this? But I can have a guess. Have u gamesdev.net?
Last edited by Cuchulainn on October 6th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
renorm
Topic Author
Posts: 1
Joined: February 11th, 2010, 10:20 pm

Use and abuse of STL vector

October 7th, 2010, 10:22 am

outrun,STL doesn't guarantee alignment (btw, it is 16-byte, not 16-bit). You get vectors aligned by replacing the stock allocator with an aligned one. Alignment refers to the beginning of a memory block. v[0] would be always 16-byte aligned provided your allocator is 16-byte aligned. Then every 4th single and every 2nd double would be 16-byte aligned. That is exactly what we need for SIMD.Cuchulainn,I want to use vectors with SIMD. SSE intrinsics require pointers.valarray is not about performance. It lets us to perform some math operations on an array of elements. std::vector is a storage. Both are awkwardly named. std::vector should have been named dynamic array. Name vector is more suitable for valarray.From the responses I see that it is safe to assume that vectors are always continuously allocated. That is what I hoped for. Is it mandated by the C++ standard?
Last edited by renorm on October 6th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
lballabio
Posts: 0
Joined: January 19th, 2004, 12:34 pm

Use and abuse of STL vector

October 7th, 2010, 11:57 am

QuoteOriginally posted by: renormFrom the responses I see that it is safe to assume that vectors are always continuously allocated. That is what I hoped for. Is it mandated by the C++ standard?From what I can see, it's mandated in the draft C++0X, but not in the C++03 standard.
 
User avatar
renorm
Topic Author
Posts: 1
Joined: February 11th, 2010, 10:20 pm

Use and abuse of STL vector

October 7th, 2010, 12:21 pm

Good to here it. Thanks for the info.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Use and abuse of STL vector

October 7th, 2010, 2:03 pm

Anyone know concurrent vector
 
User avatar
renorm
Topic Author
Posts: 1
Joined: February 11th, 2010, 10:20 pm

Use and abuse of STL vector

October 7th, 2010, 4:45 pm

outrun,What do you mean by how OS/CPU knows....Cuchulainn,VS2010 PPL is very similar to TBB. Both emphasize task based parallelism. All high level stuff from TBB is in PPL, but some useful low level stuff, such as explicit task scheduling and direct thread support (similar to Bost.Thread), are missing in PPL (or I couldn't find them in MSDN). Both TBB and PPL designed to be compatible. Switching from one to another is straightforward. TBB is portable and de facto standard. I use TBB's concurrent containers and TLS. Concurrent vector is by far the most useful container. Its behavior is similar to that of STL deque. Elements never relocate and not stored contiguously. Btw, listing operator[] as thread-safe is somewhat misleading. Only returning a reference is thread safe. Overwriting elements through that reference isn't thread-safe.
Last edited by renorm on October 6th, 2010, 10:00 pm, edited 1 time in total.