[Boost][multi_array] copying a float array to a particular dimension of a multi_array
I would like to know if it is possible to use memcpy to copy a float array to a particular dimension of a multi_array. ex.: I declare a 3 dimension multi_array of floats typedef boost::multi_array < float, 3 > array_type3D_f_t; int dim1 = 2; int dim2 = 2; int dim3 = 4; array_type3D_f_t valueMatrix(boost::extents[dim1][dim2][dim3]); then for each 3rd dimension I would like to copy a float array into it using memcpy (the final real multi_array will contain approx 21 000 000 elements, and thts why I do wish to iterate ). for (int i = 0; i < dim3; ++i) { // iterate over each 3rd dimension of the multi_array float *arr = NULL; arr = (float*)malloc(sizeof(float) * dim1 * dim2); function(&arr) // fills in the array (2D contiguous (dim1 * dim2)) memcpy(&valueMatrix[something...], arr, sizeof(float) * dim1 * dim2) // I want to copy the vector into the multi_array's 3rd dimension } then when I want to access an element I simply do valueMatrix[1][1][1]; what i used to do is int level = 0; while(it1 != it2){ // it1 and it2 are iterators to a list of records holding the data float* arr; readData(it1, &arr); // read the data into the array for (int j = 0; j < dim2; ++j){ for (int i = 0; i < dim1; ++i){ valueMatrix[i][j][level] = datavec[ j * dim1+i ]; } } ++it1; // change level ++level; } I would like to replace the nested loops by a memcpy, is this possible? -- Sébastien Fortier
Hello Sebastien, On Aug 11, 2006, at 9:31 AM, Sebastien Fortier wrote:
I would like to know if it is possible to use memcpy to copy a float array to a particular dimension of a multi_array.
In some situations you can, but not as your array is currently specified.
ex.: I declare a 3 dimension multi_array of floats
typedef boost::multi_array < float, 3 > array_type3D_f_t; int dim1 = 2; int dim2 = 2; int dim3 = 4; array_type3D_f_t valueMatrix(boost::extents[dim1][dim2][dim3]);
then for each 3rd dimension I would like to copy a float array into it using memcpy (the final real multi_array will contain approx 21 000 000 elements, and thts why I do wish to iterate ).
If you change your multi_array so that the third dimension is the first dimension, then the dim1*dim2 elements will be stored contiguously in the array and you will then be able to memcpy them (or pass a pointer to the array buffer and avoid copying altogether). If you require the dimensions of the multi_array to be as you specified, then you will need to specify a different storage order for the array in order to make the dim1*dim2 elements contiguous. for example: /* switch the dimensions of the array */ array_type3D_f_t valueMatrix(boost::extents[dim3][dim1][dim2]); /* ... */ for (int i=0; i != dim3; ++i) { function(valueMatrix[i].origin()); // directly passes the multi_array buffer to the function } HTH, ron
for (int i = 0; i < dim3; ++i) { // iterate over each 3rd dimension of the multi_array float *arr = NULL; arr = (float*)malloc(sizeof(float) * dim1 * dim2); function(&arr) // fills in the array (2D contiguous (dim1 * dim2)) memcpy(&valueMatrix[something...], arr, sizeof(float) * dim1 * dim2) // I want to copy the vector into the multi_array's 3rd dimension }
then when I want to access an element I simply do
valueMatrix[1][1][1];
what i used to do is
int level = 0; while(it1 != it2){ // it1 and it2 are iterators to a list of records holding the data float* arr; readData(it1, &arr); // read the data into the array for (int j = 0; j < dim2; ++j){ for (int i = 0; i < dim1; ++i){ valueMatrix[i][j][level] = datavec[ j * dim1+i ]; } } ++it1; // change level ++level; }
I would like to replace the nested loops by a memcpy, is this possible?
-- Sébastien Fortier
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you again for your help, you mention specifying a different storage order for the multi_array, but you did not explain further... I tried what you have suggested and it seems to work just fine. Could you elaborate on the storage order stuff? Do you have any suggestions? Ronald Garcia wrote:
Hello Sebastien,
On Aug 11, 2006, at 9:31 AM, Sebastien Fortier wrote:
I would like to know if it is possible to use memcpy to copy a float array to a particular dimension of a multi_array.
In some situations you can, but not as your array is currently specified.
[...]
If you change your multi_array so that the third dimension is the first dimension, then the dim1*dim2 elements will be stored contiguously in the array and you will then be able to memcpy them (or pass a pointer to the array buffer and avoid copying altogether). If you require the dimensions of the multi_array to be as you specified, then you will need to specify a different storage order for the array in order to make the dim1*dim2 elements contiguous.
for example: /* switch the dimensions of the array */ array_type3D_f_t valueMatrix(boost::extents[dim3][dim1][dim2]);
/* ... */ for (int i=0; i != dim3; ++i) { function(valueMatrix[i].origin()); // directly passes the multi_array buffer to the function }
HTH, ron
[...]
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sébastien Fortier
participants (2)
-
Ronald Garcia
-
Sebastien Fortier