简体   繁体   中英

Error with openmp for Nested for-loop

I have used Eigen library in my object oriented cpp code. I defined a main object, 2DGF, and in some methods of it, I have used openmp. Now I want to use openmp in another method as following:

#pragma omp parallel num_threads(NON) 
   #pragma omp for 
for(unsigned int iE=0;iE<NE;iE++){
   for(unsigned int iK=0;iK<NK;iK++){
      for(unsigned int ii=0; ii<NL ;ii++){
          for(unsigned int jj=0; jj<NL ;jj++){
              if(abs(CoorX[ii]-CoorX[jj])<DiogLim){
                 G.insert(iE*NL+ii,iK*NL+jj)=0;
                 GR.insert(iE*NL+ii,iK*NL+jj)=0;
                 S.insert(iE*NL+ii,iK*NL+jj)=0;   
                 SR.insert(iE*NL+ii,iK*NL+jj)=0;   
              }
          }
       }
    }
}  

where G, GR, S and SR are sparse matrices. Without using openmp it works with no problem. But when I use the openmp, I receive the following error:
2DGF : malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted (core dumped)

May anybody help me to fix it?

I don't know much about Eigen. But I read a bit in their documentation About the method insert() of SparseMatrix it says that it is reserving room for other inserts. Did you reserve enough space for all inserts?

If not, then it could be the same bug that is present in the following code.

#include <iostream>
#include <vector>
#include <omp.h>

int main()
{
    std::vector< int > vInts;

#pragma omp parallel for
    for (int i = 0; i < 1000; i++) {
        vInts.push_back( i );
    }

    return 0;
}

This might also end in an segmentation fault. Reason is that the vector is relocated to a different region int memory. This can be avoided by reserving enough memory for the vector. In this case vInts.reserve(1000) .

SparseMatrix also offers a reserve method. Try this, at least to rule it out as a possible bug.

Edit: Here is the code with your for-loops and a simple assignment in the innermost loop. The program compiles with g++ -fopenmp -Wall -Werror main.cpp without error or warnings. The program executes normally with the ouput that i=99.

#include <iostream>
#include <omp.h>
#include <vector>

#define NON 3

int main()
{
    int i = 0;

    unsigned int NE = 100;
    unsigned int NK = 100;
    unsigned int NL = 100;

#pragma omp parallel num_threads(NON) 
        #pragma omp for 
    for(unsigned int iE=0;iE<NE;iE++){
       for(unsigned int iK=0;iK<NK;iK++){
          for(unsigned int ii=0; ii<NL ;ii++){
              for(unsigned int jj=0; jj<NL ;jj++){
                  i = jj;
              }
           }
        }
    } 

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM