Registered Member
|
I have an array used in computation and I want to reuse its parts of as temporary arrays, say 1 and 3 quarters. What is the proper way of doing so?
Are blocks treated as efficiently as arrays, because I need to do a lot of array-like computations on those blocks. |
Moderator
|
yes you can use block, or you can also use Map. The Map option has the advantage to allow you to layout the available memory as you want and so to keep the data contiguous and aligned. E.g.:
MatrixXd temp(m,n); Map<MatrixXd,Aligned> q1(temp.data(),m1,n1); int offset1 = (m1*n1)+16-((m1*n1)%16); Map<VectorXd,Aligned> q2(temp.data()+offset1,n2); int offset2 = offset1 + (n2)+16-(n2%16); Map<Matrix4d,Aligned> q3(temp.data()+offset2); (yes you can do it smarter if (m1*n1)%16 == 0 ...) |
Registered Member
|
That is exactly what I was looking for, thank you. Just a couple of small questions...
is it m1*n1 that must divide 16, I thought it is the physical address that should have 0 at the end in hex, so if doubles each take 8 bytes, it would be enough to have m1*n1 even, no? Another question is whether the data() refers to contiguous memory block, or you do alignment for each column? I checked that in debug mode in gcc, and the block is contiguous, but I want to be sure... |
Moderator
|
For the Matrix object there is no padding. So yes .data() refers to a contiguous memory block. Of course if you access to .data() of a block expression:
double* data = mat.block(i,j,rows,cols).data(); then you also have to use the innerStride() and outerStride() to know how to access to the elements. See the documentation of these functions for more details. You can also use Map with such strides if you want. Again look at the documentation of Map for the details. The tricks with the "%16" is to make sure the first element of the q2 and q3 objects are aligned. For dynamic sized objects that's just a fine tuning optimizations, this is much more important for fixed size objects. |
Registered Member
|
Right, if we want to have 16-byte aligned address, and matrix contains 8-byte objects, would not it be enough to check (n1*m1)%2 for the offset to be aligned? |
Moderator
|
oops, sorry I mixed up sizes in elements and sizes in bytes...
|
Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft