I have a memory allocation problem. But I don't know where the problem is
The error comes up while running the loop below at 55th iteration. Below that, the code gives me what I want.
The code segment where the error happens (whole code is too long):
while(k<75){
domainz1.getVerticalBoundaryBegin(xz1,e1,row);
domainz2.getVerticalBoundaryBegin(xz2,e2,row);
domainz3.getVerticalBoundaryBegin(xz3,e3,row);
domainz4.getVerticalBoundaryBegin(xz4,e4,row);
domainz5.getVerticalBoundaryBegin(xz5,e5,row);
domainz6.getVerticalBoundaryBegin(xz6,e6,row);
domaine1.drichletFunctionalUpdateVertical(Ae1,be1,e1,ydimDom);
domaine2.drichletFunctionalUpdateVertical(Ae2,be2,e1,1);
domaine2.drichletFunctionalUpdateVertical(Ae2,be2,e2,ydimDom);
domaine3.drichletFunctionalUpdateVertical(Ae3,be3,e2,1);
domaine3.drichletFunctionalUpdateVertical(Ae3,be3,e3,ydimDom);
domaine4.drichletFunctionalUpdateVertical(Ae4,be4,e3,1);
domaine4.drichletFunctionalUpdateVertical(Ae4,be4,e4,ydimDom);
domaine5.drichletFunctionalUpdateVertical(Ae5,be5,e4,1);
domaine5.drichletFunctionalUpdateVertical(Ae5,be5,e5,ydimDom);
domaine6.drichletFunctionalUpdateVertical(Ae6,be6,e5,1);
domaine6.drichletFunctionalUpdateVertical(Ae6,be6,e6,ydimDom);
domaine7.drichletFunctionalUpdateVertical(Ae7,be7,e6,1);
gmressolver2d(Ae1,xe1,be1,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae2,xe2,be2,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae3,xe3,be3,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae4,xe4,be4,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae5,xe5,be5,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae6,xe6,be6,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Ae7,xe7,be7,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
//*******************************************************************
domaine1.getVerticalBoundaryBegin(xe1,z1,row);
domaine2.getVerticalBoundaryBegin(xe2,z2,row);
domaine3.getVerticalBoundaryBegin(xe3,z3,row);
domaine4.getVerticalBoundaryBegin(xe4,z4,row);
domaine5.getVerticalBoundaryBegin(xe5,z5,row);
domaine6.getVerticalBoundaryBegin(xe6,z6,row);
domaine7.getVerticalBoundaryBegin(xe7,z7,row);
domainz1.drichletFunctionalUpdateVertical(Az1,bz1,z1,1);
domainz1.drichletFunctionalUpdateVertical(Az1,bz1,z2,ydimDom);
domainz2.drichletFunctionalUpdateVertical(Az2,bz2,z2,1);
domainz2.drichletFunctionalUpdateVertical(Az2,bz2,z3,ydimDom);
domainz3.drichletFunctionalUpdateVertical(Az3,bz3,z3,1);
domainz3.drichletFunctionalUpdateVertical(Az3,bz3,z4,ydimDom);
domainz4.drichletFunctionalUpdateVertical(Az4,bz4,z4,1);
domainz4.drichletFunctionalUpdateVertical(Az4,bz4,z5,ydimDom);
domainz5.drichletFunctionalUpdateVertical(Az5,bz5,z5,1);
domainz5.drichletFunctionalUpdateVertical(Az5,bz5,z6,ydimDom);
domainz6.drichletFunctionalUpdateVertical(Az6,bz6,z6,1);
domainz6.drichletFunctionalUpdateVertical(Az6,bz6,z7,ydimDom);
gmressolver2d开发者_如何转开发(Az1,xz1,bz1,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Az2,xz2,bz2,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Az3,xz3,bz3,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Az4,xz4,bz4,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Az5,xz5,bz5,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
gmressolver2d(Az6,xz6,bz6,KrylovDim,xdim,ydimDom,COP,twoDimStencil,0,1);
k++;
printf("%d iterations done\n",k);
}
Here almost all the terms in the brackets are arrays.
Ae1,Ae2,Az1,Az2,..... are 2D arrays 71x11
xe1,xz1,bz1,be1,e1,e2,z1,z2,......... are 1D arrays of size (71x11).
These class operators are transferring data between these arrays basically. this is a scientific code, gmressolver2d solves new problems in every iteration.
the point is my memory allocation always inceares and increases and I don't know what to do to prevent this.
Best regards, Emre.
edit: my array allocations.
template <typename T>
T ****AllocateDynamic4DArray( int nRows, int nCols, int nSlice, int kSlice)
{
T ****dynamicArray;
dynamicArray = new T***[nRows];
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i] = new T**[nCols];
for ( int j=0; j<nCols;j++){
dynamicArray[i][j] = new T*[nSlice];
for (int k=0; k<nSlice; k++){
dynamicArray[i][j][k] = new T[kSlice];
for(int l=0;l<kSlice;l++){
dynamicArray[i][j][k][l] = 0;
}
}
}
}
return dynamicArray;
}
template <typename T>
T ***AllocateDynamic3DArray(int nRows, int nCols, int nSlice){
T ***dynamicArray;
dynamicArray = new T**[nRows];
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i] = new T*[nCols];
for ( int j=0; j<nCols;j++){
dynamicArray[i][j] = new T[nSlice];
for (int k=0; k<nSlice; k++){
dynamicArray[i][j][k]= 0;
}
}
}
return dynamicArray;
}
template <typename T>
T **AllocateDynamic2DArray(int nRows, int nCols){
T **dynamicArray;
dynamicArray = new T*[nRows];
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i] = new T[nCols];
for ( int j=0; j<nCols;j++){
dynamicArray[i][j]= 0;
}
}
return dynamicArray;
}
template <typename T>
T *AllocateDynamicVector(int nRows){
T *dynamicArray;
dynamicArray = new T[nRows];
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i]= 0;
}
return dynamicArray;
}
Run the program in your debugger, and set the debugger so that it breaks when an exception is thrown. Once the program is halted (because of the exception), use the call stack feature of your debugger to see in which context your exception is thrown.
I'd like to give you more detailed instructions, but you don't specify what platform/compiler you're using.
This how-to describes how to set up Visual Studio to break upon any exception.
This how-to describes how to use the call stack.
Use UMDH.EXE to take snapshots of the app's heap usage before successive loops, then compare the two snapshots. If properly set up, the callstacks of all alloactions in the interim will be seen.
When deleting the arrays in the manner you're allocating them, a single call to delete [] array_name
won't work because each index in a dimension of the array is a newly allocated "sub-array" of pointers. So you're going to have to loop through each of the row/columns/slice/etc. of the array and call delete []
on the array of pointers that make up the indicies of the array. For instance, to deallocate one of your 3D-arrays, you'll have to-do something like the following:
for (int i=0; i < column_size; i++)
{
for (int j=0; j < slice_size; j++)
{
delete [] array_name[i][j];
}
delete [] array_name[i];
}
delete [] array_name;
精彩评论