Armadillo library question (people with knowledge about any other linear algebra c++ package may be able to help)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//solves the initial value problem using forward Euler method and outputs the results
double* System::ForwardEulerOdeSolve(double tau, double t_0, double t_e) {
	
  //calculate the number of time steps
  int n=floor((t_e-t_0)/tau)+1;

  int sv_n=v_0.n_elem;//the number of state variables in the equations
  int sd=sv_n+1;//number of all variables including time
	
  //initialize output matrix
  //first row contains time, the rest of the rows contain the evolution of state variables wrt to time
//-----------------------------------    
  double *pM(NULL);
  pM=new double[n*sd];  
  mat OUT(pM, n, sd, true);
//-----------------------------------	
  //fill in the initial conditions
  OUT(0,0)=t_0;
  OUT(0,span(1, sv_n))=trans(v_0);
	
  //create a column and a row vector used as temporary vectors in the calculation process
  vec v_temp=v_0;
  rowvec rs_temp=zeros<mat>(1,sv_n);
  
  //create the respective pointer row and column vectors for quick transition
  rowvec r_temp=rowvec(v_temp.memptr(), sv_n, false, false);
  vec vs_temp=vec(rs_temp.memptr(), sv_n, false, false);
	
  //iterate to get the complete solution
  for (int i=1; i<n; i++) {
    OUT(i,0)=OUT(i-1,0)+tau;//next time step
    rs_temp=OUT(i-1,span(1,sv_n));//row vector with previous state    
    v_temp=ForwardEulerOde(tau,vs_temp);//column vector with current state
    OUT(i,span(1, sv_n))=r_temp;//add current state to the output matrix
  }
	
  return pM;
}



The method is meant to solve initial value ODE problems using forward Euler and it works quite well. However, I do not want to output the whole solution matrix, but rather output a pointer to it. However, I am pretty sure that Armadillo will deallocate the memory that matrix OUT uses as soon as I finish performing the method (correct me if I am wrong - in this case problem is solved). Hence, I was wondering if it was possible to use something similar to what is written within the //------ lines, i.e. allocate the memory dynamically using standard array, create a matrix of fixed size on this memory and then output the initial pointer. Is it going to work? Is there an easier way of doing the same thing? Also, what if the size of the matrix is unknown at the point of allocation? What is the best way to proceed in this case?
Last edited on
The best way would probably be to add at member to the System class, of type 'mat *'. Then on line 15 do
1
2
3
4
//Suppose you called the member 'matrix':
this->matrix=new mat(pM, n, sd, true);
//This line is just to avoid changing the existing code:
mat &OUT=*this->matrix;
Thanks for prompt reply. It makes sense. Probably in this case I can just output pointer to matrix (mat*) itself. However, is there a way to be sure that the memory starting from pM with the size of n*sd will stay allocated even after I exit the method and dispose of the instance of System object upon which I operated? I am mainly worried that Armadillo destructor can do something fancy, i.e. deallocate the memory anyway as soon as I exit the method.
is there a way to be sure that the memory starting from pM with the size of n*sd will stay allocated even after I exit the method
Actually, I just noticed that you never free it. Memory that is allocated with new has to be deleted, otherwise it becomes a memory leak.

I am mainly worried that Armadillo destructor can do something fancy, i.e. deallocate the memory anyway as soon as I exit the method.
The documentation should explicitly state how it handles deallocation of that pointer. My guess is that it's left up to the client program, since you're not passing a callback pointer to a deallocator.
Actually, I just noticed that you never free it. Memory that is allocated with new has to be deleted, otherwise it becomes a memory leak.

Well, what i am really trying to achieve is to output the matrix containing the solution without having to create its copy, i.e. by outputing a pointer to it. The instances of other classes will have to use the same matrix for post processing the results. So, I am almost trying to create a global, but disposable variable, i.e. I still have to be able to deallocate memory when I will need it. Obviously i will output not only the pointer, but also the size of the matrix, so deallocation should not be a problem (possibly I will do it through an instance of Solution class that will contain pointer to matrix, its width and length as its members). So, generally, a temporary memory leak is exactly what i am trying to achieve. Not sure if that is the best way of approaching this whole thing though, so any suggestions would be appreciated.

The documentation should explicitly state how it handles deallocation of that pointer. My guess is that it's left up to the client program, since you're not passing a callback pointer to a deallocator.

Wish it did. Is there a test that one can perform (without modifying the library code) to see what actually happens? For example, is it possible to check how much memory is available on the heap after I exit the method?

Thank you for your help.
There's no such thing as a "temporary memory leak". A memory leak occurs when a program loses all pointers to a block of memory, such that it's no longer possible where it was or to free it.

I find it very hard to believe that it doesn't mention memory management at all. Read it more carefully.
Topic archived. No new replies allowed.