c++armadilloarrayfire

Transfer data from arrayfire arrays to armadillo structures


I would like to replace some of my calculations formerly written with armadillo with ArrayFire, in order to use some GPU capabilities. I do not want to replace everything, just the time-critical spots. Thus I need to convert armadillo-structures into arrayfire-structures and back. The first step is easy:

arma::colvec A_arma(5, arma::fill::ones);
af::array A_array(5, A_arma.memptr());//Done

Going back is more difficult. How do I get the data from the arrayfire-structure into the armadillo-structure? I can iterate over all elements and write them into the armadillo structure, but that would take a lot of time. Are there easier ways?


Solution

  • You can copy the data out of an af::array using the host member function. For example, you can copy the data from an ArrayFire array to a std::vector like this:

    af::array gpu_data = af::randu(10, 10);
    vector<float> cpu_data(gpu_data.elements());
    
    gpu_data.host<float>(cpu_data.data());
    

    This will copy the data in column major order to the cpu_data vector.

    I am not too familiar with armadillo's data structure but I am sure you can pass the pointer returned by the memptr to the host function and the data will be copied into it.