简体   繁体   中英

C++ why does this parallel for loop with vector.insert() crash when loop size gets larger?

I have nested loops that inserts integers into a vector of empty vectors a1i.

vector<vector<int>> a1i = {{},{},{},{},{},{},{}...};

#pragma omp parallel for simd
for(int x = 0;x < a1i.size();x++){
   for(int y = 0; y < x - 1; y++){
      a1i[x].insert(a1i[x].end(),y);
   }
}

the vector would then looks something like this after the loop.

a1i = {
{},
{},
{0},
{0,1},
{0,1,2},
{0,1,2,3},
{0,1,2,3,4}
...
}

the loop behaves nicely when the size of a1i is small but if it is big eg5000 elements my program crashes because of the loops.

If I estimate the size of the vector of vectors, to be half a 5000 by 5000 vector of vectors of integers it should only take up around 50,000,000 bytes (50 megabytes).I am sure I have more than 50 megabytes memory. The vector is a global variable so it should be in heap?

Or is there something else here that takes up massive amounts of memory?

Is my estimation in the right order of magnitude?

Is the problem trying to vector.insert() concurrently?

The way I see it is that each thread will be inserting into a different vector within a1i so I don't suspect anything there.

Am I missing something that is very obvious?

The expected OMP gain of parallel work on all a1i[x] which are indeed independent so omp thread safe will be decreased by the cost of increasing a1i[x] vector size dynamically (by calling insert or push_back). A better solution performance wise would be:

vector<vector<int>> a1i = {{},{},{},{},{},{},{}...};

#pragma omp parallel for simd
for(int x = 0;x < a1i.size();x++){
  a1i[x].reserve(x); 
   for(int y = 0; y < x - 1; y++){
      a1i[x].push_back(y);
   }
}

or

#pragma omp parallel for simd
for(int x = 0;x < a1i.size();x++){
  a1i[x].resize(x); 
   for(int y = 0; y < x - 1; y++){
      a1i[x]= y;
   }
}

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