|
Boost Users : |
Subject: Re: [Boost-users] multi_array with large dimensions crashes
From: Brian Davis (bitminer_at_[hidden])
Date: 2011-11-28 17:44:40
I would like to point out something (that may be known), but may be
confusing to others that read this post and will affect the data written to
the arrays as it appears linearly. This is regarding the fastest
increasing axis:
in C/C++ float c_array[z_dim][y_dim][x_dim] ;
defines a row major array with the x_dim axis being the axis of the fastest
increasing index.
You can specify any axis you want to be the fastest increasing dimension
just so long is that is how you expect the data to appear in linear memory
and it is accessed accordingly.
I think:
boost::extents[xDim][yDim][zDim]
should be:
boost::extents[zDim][yDim][xDim]
as:
#include <boost/multi_array.hpp>
int main( void )
{
int x_dim = 4;
int y_dim = 3;
int z_dim = 2;
float* mat_1_data = new float[x_dim * y_dim * z_dim];
float* mat_2_data = new float[x_dim * y_dim * z_dim];
boost::multi_array_ref<float, 3> mat_1( mat_1_data,
boost::extents[z_dim][y_dim][x_dim] );
boost::multi_array_ref<float, 3> mat_2( mat_2_data,
boost::extents[x_dim][y_dim][z_dim] );
for( int z = 0; z < z_dim; z++ ){
for( int y = 0; y < y_dim; y++ ){
for( int x = 0; x < x_dim; x++ ){
mat_1[z][y][x] = z * x_dim * y_dim + y * x_dim + x;
}
}
}
for( int x = 0; x < x_dim; x++ ){
for( int y = 0; y < y_dim; y++ ){
for( int z = 0; z < z_dim; z++ ){
mat_2[x][y][z] = x * y_dim * z_dim + y * z_dim + z;
}
}
}
for( int i = 0; i < 12; i++ )
{
printf( "%f : %f\n", mat_1_data[i], mat_2_data[i] );
}
delete [] mat_1_data;
delete [] mat_2_data;
}
yields:
0.000000 : 0.000000
1.000000 : 1.000000
2.000000 : 2.000000
3.000000 : 3.000000
4.000000 : 4.000000
5.000000 : 5.000000
6.000000 : 6.000000
7.000000 : 7.000000
8.000000 : 8.000000
9.000000 : 9.000000
10.000000 : 10.000000
11.000000 : 11.000000
with the first matrix mat_1 being "typical usage" where x is the axis of
fastest increase as:
mat_2[x][y][z] = x * y_dim * z_dim + y * z_dim + z;
indexing is a bit odd, but perfectly acceptable so long as this is what is
desired
if the order of the for loops is changed for mat_2 so it loops z then y
then x the array will not be written linearly and can be seen if the array
is printed while it is being filled in.
though what was originally posted may very well be what is desired by the
original author.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net