0

So I have this loop here:

/* loop */
omp_set_dynamic(0);
#pragma omp parallel num_threads(1)
#pragma omp for ordered private(iterationPoints, normalizedPoints, grid)
for(iterations = maxIter; iterations >= minIter; iterations -= stepSizeIter){

        /* calculate iterationPoints */
        iterationPoints = (double **) malloc(iterations *sizeof(double *));
        int i;
        for(i = 0; i < iterations; i++){
            iterationPoints[i] = (double *) malloc(DIMS *sizeof(double));
        }
        calcuTraj(iterationPoints,a,b,c,x,y,z,h,iterations, bounds, (iterations == maxIter));

        /* normalize Data */
        normalizedPoints = (double **) malloc(iterations *sizeof(double *));
        for(i = 0; i < iterations; i++){
            normalizedPoints[i] = (double *) malloc(DIMS * sizeof(double));
        }

        #pragma omp ordered
        printf("%d %f  %f  %f  %f  %f  %f\n",iterations,bounds[0][0] ,bounds[0][1], bounds[1][0], bounds[1][1], bounds[2][0], bounds[2][1]);

        normalize(iterationPoints, normalizedPoints, bounds, iterations); 

        /* creating 3D Array for the grid of boxes in space*/

        /* setting minimum for sidelength of the grid */
        double minGridSideLength = 1; 
        /* calculating array size */
        int boxesPerDim = ceil(minGridSideLength/epsion) +1;

        //printf("boxesPerDim: %d \n", boxesPerDim);

        /* create grid array */
        grid = (bool ***) malloc(boxesPerDim *sizeof(bool **));

        int j_X, j_Y; 
        for(j_X = 0; j_X < boxesPerDim; j_X++){

            grid[j_X] = (bool **) malloc(boxesPerDim *sizeof(bool *));

            for(j_Y = 0; j_Y < boxesPerDim; j_Y++){
               grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));
            }
        }   

        /* count hitted boxes */
        int boxesHit = boxCount(normalizedPoints, grid, iterations, epsion, boxesPerDim);

        #pragma omp ordered
        fprintf(file,"%d  %d\n", iterations, boxesHit);
        //printf("%d \n",boxesHit);


        /* free storage */
        free(iterationPoints);
        free(grid);
        free(normalizedPoints);

}

I set the number of threads to 1, so there is no multithreading which would lead to an exploding ram for high iterations. So in this configuration the loop would run step after step. the function 'calculatedTraj(..)' calculates the 'iterationpoints'. If I run it from minIter = 20*10^6 to maxIter = 50*10^6 iterations with the stepsize = 10*10^6 my ram is exploding and the process is getting killed. But if I run the program for minIter = maxIter = 50*10^6 it is working and the ram is fine. So why it is not possible to do it with a for loop, because at the end of every loop part I am freeing the allocated memory, so why is the ram exploding then ?

Chopin
  • 33
  • 2
  • 4
    Just count the number of `malloc` you are doing, and the number of `free` you are doing. Hint: freeing the array variable does not free the items of the array – LoPiTaL Jan 07 '18 at 12:12
  • Probably not the problem but have you made sure that there are no memory leaks? We cannot see the other functions so the problem of exploding RAM might be there. – Thomas Nobel Jan 07 '18 at 12:13
  • I will try to do what @LoPiTaL suggested, when it is not working I will tell you. Then it would probably be a memory leak. – Chopin Jan 07 '18 at 12:15
  • I have tried, what @LoPiTaL suggested, and now it is working. – Chopin Jan 07 '18 at 12:29
  • OT: This `grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));` shall be `grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool));` – alk Jan 07 '18 at 12:34
  • You do not need to cast `malloc/calloc` - it is bad practice - see https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Ed Heal Jan 07 '18 at 12:36
  • 2
    Oh, I was expecting to see some pictures of the exploding RAM chips - or at least that they would melt by overheating in the tight loop. Disappointed... – Bo Persson Jan 07 '18 at 13:41

1 Answers1

0
grid[j_X] = (bool **) malloc(boxesPerDim *sizeof(bool *));
grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));

You haven't free'd these.

Similar to having some struct:

struct whatever {
int something
char *stringly
};

struct whatever *whtvr = malloc(sizeof(struct whatever));
whtvr->stringly = malloc(strlen("whatever") + 1);

...

You have to free the string's memory before you ditch the struct's memory.

free(whtvr->stringly);
free(whtvr);