Question

I am using Armadillo for linear algebra. I setup a pretty big vector (at least 35000000 elements). I have another vector of length half of the big vector. I am using the fftw to do the fourier transform on the big vector but the first half of the data is copied from the small vector as below

#include <armadillo>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "fftw3.h"

using namespace std;
using namespace arma;

int main(void)
{
  arma::Col<double> v1, v2;
  v1.resize(35000000);
  v2.resize(17500000);
  // initialize v2
  for (int i=0; i<4096; i++) // repeat 4096 times
  {
    v1.rows(0, 17500000) = v2;
    fftw_complex* in = reinterpret_cast<fftw_complex*>(v1.colptr(0));
    fftw_plan plan = fftw_plan_dft_1d(35000000, in, in, FFTW_FORWARD, FFTW_MEASURE);
    v2 = v1.rows(0, 175000000);
  }
}

this code is pretty slow because we need to copy the elemenets from v2 to v1 and backward. Is that anyway to have v1's element refer to the v2 instead of copy?

Was it helpful?

Solution

Not exactly sure what you're trying to achieve, but you can get the pointer to the memory used by the vector (or matrix) via the .memptr() function. This pointer can then be used, along with an offset, to create a new vector (or matrix) which uses external/auxiliary memory through dedicated vector constructors and matrix constructors.

For example:

vec v1(35000000);

vec v2(v1.memptr(), 17500000, false); // v2 will now share memory with v1

Incidentally, don't use the .resize() function with Armadillo vectors and matrices unless you really mean to preserve existing data. Use .set_size() instead, which is much faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top