Boost logo

Boost Users :

From: Ronald Garcia (garcia_at_[hidden])
Date: 2006-08-12 12:39:29


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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users



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