GettingError clearing Memory

Hi,
I am getting following error if I try to :
 
delete [] recv_buffer;


My complete code is given below:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <cmath>
#include <mpi.h>
#include <fstream>
#include <ctime>
#include <vector>
#define N 4000000

int rank, size;
double* computeDivisions(double* recv_buffer, int, int);
void GaussianElimination(double **,double *b ,double *y);
int main(int argc, char * argv[])
{
  double sTime, eTime, rTime;
  std::ifstream inFile;
  int num_rows = 2000;
  int num_cols = 2000;
  int num_processors =16;
  int cur_control = 0;
  double * send_buffer = NULL;
  double * recv_buffer = NULL;
  double ** data = NULL;
  double determinant;
  double *div_buffer=NULL;
  int irow =0; int icol=0; int iIndex =0;
  std::vector<double> file_buffer;
   double **M_div=NULL;


  double **M_A, *I_A, *I_B, *I_Y;
  double *Output, Pivot;
  int iChunkSizeRow = 2000/num_processors;
  int iChunkSizeCol = 2000;
  int iTotalChunkSize = iChunkSizeRow * 2000;


   
  I_B = NULL;
  I_B = new double[num_rows];
  if(I_B == NULL){
    std::cout<< " I_A can't be allocated memory";
    MPI_Finalize();
    return -2;
  }
  I_A = NULL;
  I_A = new double[N];
  if(I_A == NULL){
    std::cout<< " I_A can't be allocated memory";
    MPI_Finalize();
    return -2;
  }
  I_Y = NULL;
  I_Y = new double[N];
  if(I_Y== NULL){
    std::cout<< " I_B can't be allocated memory";
    MPI_Finalize();
    return -2;
  }
  
 recv_buffer = new double[N];//This size may be too large
 if(recv_buffer== NULL){
    std::cout<< " recv_buffer can't be allocated memory";
    return -2;
  }

  div_buffer = new double[N];//This size maybe too large
  if(div_buffer== NULL){
     std::cout<< " div_buffer can't be allocated memory";
     MPI_Finalize();
     return -2;
  }


  M_div = new double*[num_rows];//Table or Matriz of all Elements after divison
      for(int i = 0; i < num_cols; i++){
         M_div[i] = new double[num_cols];
         if(M_div[i]==NULL){
            std::cout<<"M_div can't be allocated";
            MPI_Finalize();
            return 0;
         }
      }

   for(int i = 0; i < num_cols; i++)
   {
    for(int j = 0; j < num_rows; j++)
      M_div[i][j] = 0;
   }

  M_A = new double*[num_cols];
  for(int i = 0; i < num_cols; i++){
    M_A[i] = new double[num_rows];
    if(M_A[i]==NULL){
       std::cout<<"M_A can't be allocated";
       MPI_Finalize();
       return 0;
    }
  }


  for(int i = 0; i < num_cols; i++)
  {
    for(int j = 0; j < num_rows; j++)
      M_A[i][j] = 0;
  }

  double dStarttime =0.0;
  double dEndtime = 0.0;

 
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  std::cout<<"data Storage "<<std::endl;
  if(!rank)
  {
       dStarttime = MPI_Wtime();
    

        for(irow=0; irow<num_rows; irow++)
	  	  for(icol=0; icol<num_cols; icol++)
			  M_A[irow][icol]= icol;
       
       //2d to 1d array is giving core dumped
       
       for(irow=0; irow<num_rows; irow++)
	  	  for(icol=0; icol<num_cols; icol++)
			  I_A[iIndex++] = M_A[irow][icol];

  

   }//if(!rank)

   //broadcasting values like num_rows, num_cols and iTotalChunkSize
   std::cout<<"broadcast "<<std::endl;
   MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
   MPI_Bcast (&num_cols, 1, MPI_INT, 0, MPI_COMM_WORLD);
   MPI_Bcast (&iTotalChunkSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
   
   //MPI Scatter: Distributing the data to all processes
   std::cout<<"Scatter "<<std::endl;
   MPI_Scatter(I_A, iTotalChunkSize, MPI_DOUBLE, recv_buffer, iTotalChunkSize, MPI_DOUBLE, 0, MPI_COMM_WORLD); 


   div_buffer = computeDivisions(recv_buffer, rank, iTotalChunkSize);

   
   

  
  std::cout<<"Gather "<<std::endl;
  int ret_val = MPI_Gather(recv_buffer, iTotalChunkSize, MPI_DOUBLE, div_buffer, iTotalChunkSize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  
  if(ret_val == MPI_SUCCESS)
     std::cout<<"MPI_Gather Success"<<std::endl;
  else if(ret_val == MPI_ERR_COMM)
     std::cout<<"MPI_Gather : MPI_ERR_COMM";
  else if(ret_val == MPI_ERR_COUNT)
    std::cout<<"MPI_Gather : MPI_ERR_COUNT";
  else if(ret_val == MPI_ERR_TYPE)
    std::cout<<"MPI_Gather : MPI_ERR_TYPE";
  else if(ret_val == MPI_ERR_BUFFER)
    std::cout<<"MPI_Gather : MPI_ERR_BUFFER";

  
   if(rank==0){
   

    
    int index2=0;int m=0;
    for(int k=0; k<num_rows; ++k) {
        m=k;
        for(int l=0;l<num_cols; ++l){
           if(m>0){
              m--;
              index2++;
              continue;
           }
           M_A[k][l] = recv_buffer[index2++];
        }
        I_Y[k] = I_B[k]/M_A[k][k];
        M_A[k][k]=1;
        for(int i=k+1; i<=num_rows-1; i++){
           for(int j=k+1; j<=num_cols-1; j++)
              M_A[i][j] = M_A[i][j] -M_A[i][k] *M_A[k][j];
           I_B[i]= I_B[i] -M_A[i][k] * I_Y[k];
           M_A[i][k] = 0;
        }
    }

     dEndtime = MPI_Wtime();
     std::cout<<"That took "<< dEndtime-dStarttime<<"seconds "<<std::endl;
    }
    

    delete [] I_A;//NEW
    
    for(int i = 0; i < num_rows; i++)
       delete [] M_div[i];
    delete [ ] M_div;
    delete [ ] div_buffer;
    delete [] I_Y;
    delete [ ]I_B;
    for(int i = 0; i < num_rows; i++)
       delete [] M_A[i];
    delete [ ] M_A;
     
    delete [] recv_buffer;//NEW
    MPI_Finalize();
    return 0;
}

double* computeDivisions(double* recv_buffer, int rank, int iTotalChunkSize){
    
     
     
         
            for(int j= rank+1; j <=iTotalChunkSize-1; ++j)
               recv_buffer[j] = recv_buffer[j]/recv_buffer[rank];
              
            
            return recv_buffer;
}


I am getting following messages:

There are lines above it also but no error
:
:
:
Gather
MPI_Gather Success
[lc2530hz:07030] *** Process received signal ***
[lc2530hz:07030] Signal: Segmentation fault (11)
[lc2530hz:07030] Signal code: Address not mapped (1)
[lc2530hz:07030] Failing at address: 0x7f3e87d45008
[lc2530hz:07030] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f3e8f06af20]
[lc2530hz:07030] [ 1] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x3d)[0x7f3e8f0c398d]
[lc2530hz:07030] [ 2] ./a.out(+0xc785)[0x55a02969b785]
[lc2530hz:07030] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f3e8f04db97]
[lc2530hz:07030] [ 4] MPI_Gather Success
./a.out(+0xb7ca)[0x55a02969a7ca]
[lc2530hz:07030] *** End of error message ***
[lc2530hz:07031] *** Process received signal ***
[lc2530hz:07031] Signal: Segmentation fault (11)
[lc2530hz:07031] Signal code: Address not mapped (1)
[lc2530hz:07031] Failing at address: 0x7f4184637008
[lc2530hz:07031] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f418b95cf20]
[lc2530hz:07031] [ 1] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x3d)[0x7f418b9b598d]
[lc2530hz:07031] [ 2] ./a.out(+0xc785)[0x55e668fb6785]
[lc2530hz:07031] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f418b93fb97]
[lc2530hz:07031] [ 4] ./a.out(+0xb7ca)[0x55e668fb57ca]
[lc2530hz:07031] *** End of error message ***
MPI_Gather Success
MPI_Gather Success
Elimination
[lc2530hz:07032] *** Process received signal ***
[lc2530hz:07032] Signal: Segmentation fault (11)
[lc2530hz:07032] Signal code: Address not mapped (1)
[lc2530hz:07032] Failing at address: 0x7fd451a98008
[lc2530hz:07032] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7fd458dbdf20]
[lc2530hz:07032] [ 1] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x3d)[0x7fd458e1698d]
[lc2530hz:07032] [ 2] ./a.out(+0xc785)[0x55f7a5b2e785]
[lc2530hz:07032] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7fd458da0b97]
[lc2530hz:07032] [ 4] ./a.out(+0xb7ca)[0x55f7a5b2d7ca]
[lc2530hz:07032] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 3 with PID 0 on node lc2530hz exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
zulfi@lc2530hz:~/c programs/MPI_PROG/GE_SV$



Some body please guide me.
Zulfi.

Last edited on
After
 
div_buffer = computeDivisions(recv_buffer, rank, iTotalChunkSize);
div_buffer will point at to the same location as recv_buffer.

This means that
 
delete[] div_buffer;
and
 
delete[] recv_buffer;
will both try to delete the same array.
Hi,
Thanks good insight.

Actually I created & allocated memory for both in my program. I passed rec_buffer as an argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
recv_buffer = new double[N];//This size may be too large
 if(recv_buffer== NULL){
    std::cout<< " recv_buffer can't be allocated memory";
    return -2;
  }

  div_buffer = new double[N];//This size maybe too large
  if(div_buffer== NULL){
     std::cout<< " div_buffer can't be allocated memory";
     MPI_Finalize();
     return -2;
  }


In computeDivision I passed recv_buffer as an argument.

 
 div_buffer = computeDivisions(recv_buffer, rank, iTotalChunkSize);


So I allocated memory twice. Shouldn't I "delete" twice. Arguments are local variables. So argument recv_buffer would die when the computeDivisions(...) terminates but what would happen to recv_buffer outside the function allocated at line#60?

I would try to follow your advise.
Zulfi.
Yes you should delete the same number of times you use new otherwise you have a memory leak.

recv_buffer is just a pointer to the first element in the array. This pointer is passed to the computeDivisions function. recv_buffer inside computeDivisions is a copy of the pointer that was passed to it but the underlying array that it points to is still the same. After the arrays has been modified the pointer is returned which means that computeDivisions will return the same pointer value as was passed to it. This is why div_buffer and recv_buffer both ends up pointing to the same array.

If you look at C functions that work with arrays like strcpy you'll see that they take the result array as argument too. Maybe this is a better solution.

 
computeDivisions(div_buffer, recv_buffer, rank, iTotalChunkSize);

This way the caller can decide how to create the array and the function doesn't need to care.

In modern C++ you shouldn't really have to use new and delete unless you are doing something very advanced. Instead of dynamically allocated arrays you would normally use std::vector, and to handle dynamically allocated objects you would often use a smart pointer such as std::unique_ptr. If you used std::vector it would automatically free up its elements when it goes out of scope, and it would automatically copy the elements when copied which makes it behave more like other value types.
Last edited on
Hi,
Thanks for providing me the option.

God bless you.

Zulfi.
Topic archived. No new replies allowed.