K
Kazik�
Hi,
for some reason that I don't understand I have problem with dynamic
allocation of memory. Though I think I did everything correctly, the
amount of RAM used by my application increases when the program is
running and finally the program runs out of memory completely. I have
found the part of the program that is responsible for that but I don't
understand how is it happening- from my perspective everything should
work fine (in the meaning that the usage of memory should be
constant). Here is this part of the code:
for(int k=0; k<Nq; k++){
double** Matrix = init_2_re(2*Nz);
for(int u=0; u<2*Nz; u++){
for(int v=0; v<2*Nz; v++){
Matrix[v] = AllMatrixTerms(2*Nz, u, v, qArray[k], g_ij, gd_ij,
dz, psirX, AnalyticalFrArray, z);
}
}
double* wr = new double[2*Nz];
double* wi = new double[2*Nz];
dgeevCPP(Matrix, 2*Nz, wr, wi);
delete [] Matrix;
delete [] wi;
delete [] wr;
}
The functions that have been used above are:
1)
double** init_2_re(const int n){
double **Matrix = new double*[n];
for(int i=0; i<n; i++)
Matrix = new double[n];
return Matrix;
}
2)
AllMatrixTerms is not important in the context of my problem- if you
put
Matrix[v] = 1.;
it would work exactly the same as using AllMatrixTerms and thus I
don't paste the code for that here.
3)
/
*-----------------------------------------------------------------------------------
* The routine dgeevCPP is a C-like routine for diagonalisation of a
* real-valued, non-symmetric matrix, using FORTRAN-base routine dgeev
* (google "dgeev" for details)
*
* Arguments:
* h- the C-like real-valued non symmetric matrix to be diagonalised.
* n- the order of the matrix h
* wr- an array storing real parts of calculated eigenvalues
* wi- an array storing imaginary parts of calculated eigenvalues
-----------------------------------------------------------------------------------
*/
void dgeevCPP(double** h, int n, double* wr, double* wi)
{
char jobvl = 'N'; //'N'- left eigenvectors of A are not computed
//'V'- left eigenvectors of A are computed
char jobvr = 'N'; //'N'- right eigenvectors of A are not computed
//'V'- right eigenvectors of A are computed
//Here, the matrix h is rewritten into FORTRAN-like array:
double* a = new double[n*n];
for (int j=0; j < n; j++){
for (int i=0; i < n; i++){
a[n*j+i] = h[j];
}
}
int ldvl = 1; // if jobvl='V', ldvl >= n
int ldvr = 1; // if jobvr='V', ldvr >= n
double* vl = new double[ldvl];
double* vr = new double[ldvr];
int lwork = 6*n; // lwork >= max(1,3*N), and if jobvl='V' or
jobvr='V', lwork >= 4*n
double* work = new double[lwork];
int info;
dgeev_(&jobvl, &jobvr, &n, a, &n, wr, wi, vl, &ldvl, vr, &ldvr,
work, &lwork, &info);
delete [] work;
delete [] vl;
delete [] vr;
delete [] a;
}
So, just as I said, the program works fine, but the amount of memory
constantly increases and if I put some large value of Nz, finally the
program crashes. From my point of you I delete everything that I
create, so I really don't understand what's going on.
I would be really grateful if somebody could help me with this stuff.
Thank you in advance,
for some reason that I don't understand I have problem with dynamic
allocation of memory. Though I think I did everything correctly, the
amount of RAM used by my application increases when the program is
running and finally the program runs out of memory completely. I have
found the part of the program that is responsible for that but I don't
understand how is it happening- from my perspective everything should
work fine (in the meaning that the usage of memory should be
constant). Here is this part of the code:
for(int k=0; k<Nq; k++){
double** Matrix = init_2_re(2*Nz);
for(int u=0; u<2*Nz; u++){
for(int v=0; v<2*Nz; v++){
Matrix[v] = AllMatrixTerms(2*Nz, u, v, qArray[k], g_ij, gd_ij,
dz, psirX, AnalyticalFrArray, z);
}
}
double* wr = new double[2*Nz];
double* wi = new double[2*Nz];
dgeevCPP(Matrix, 2*Nz, wr, wi);
delete [] Matrix;
delete [] wi;
delete [] wr;
}
The functions that have been used above are:
1)
double** init_2_re(const int n){
double **Matrix = new double*[n];
for(int i=0; i<n; i++)
Matrix = new double[n];
return Matrix;
}
2)
AllMatrixTerms is not important in the context of my problem- if you
put
Matrix[v] = 1.;
it would work exactly the same as using AllMatrixTerms and thus I
don't paste the code for that here.
3)
/
*-----------------------------------------------------------------------------------
* The routine dgeevCPP is a C-like routine for diagonalisation of a
* real-valued, non-symmetric matrix, using FORTRAN-base routine dgeev
* (google "dgeev" for details)
*
* Arguments:
* h- the C-like real-valued non symmetric matrix to be diagonalised.
* n- the order of the matrix h
* wr- an array storing real parts of calculated eigenvalues
* wi- an array storing imaginary parts of calculated eigenvalues
-----------------------------------------------------------------------------------
*/
void dgeevCPP(double** h, int n, double* wr, double* wi)
{
char jobvl = 'N'; //'N'- left eigenvectors of A are not computed
//'V'- left eigenvectors of A are computed
char jobvr = 'N'; //'N'- right eigenvectors of A are not computed
//'V'- right eigenvectors of A are computed
//Here, the matrix h is rewritten into FORTRAN-like array:
double* a = new double[n*n];
for (int j=0; j < n; j++){
for (int i=0; i < n; i++){
a[n*j+i] = h[j];
}
}
int ldvl = 1; // if jobvl='V', ldvl >= n
int ldvr = 1; // if jobvr='V', ldvr >= n
double* vl = new double[ldvl];
double* vr = new double[ldvr];
int lwork = 6*n; // lwork >= max(1,3*N), and if jobvl='V' or
jobvr='V', lwork >= 4*n
double* work = new double[lwork];
int info;
dgeev_(&jobvl, &jobvr, &n, a, &n, wr, wi, vl, &ldvl, vr, &ldvr,
work, &lwork, &info);
delete [] work;
delete [] vl;
delete [] vr;
delete [] a;
}
So, just as I said, the program works fine, but the amount of memory
constantly increases and if I put some large value of Nz, finally the
program crashes. From my point of you I delete everything that I
create, so I really don't understand what's going on.
I would be really grateful if somebody could help me with this stuff.
Thank you in advance,