I intend to use buffers std::vector<size_t> buffer(100)
, one in each thread in a parallelization of a loop, as suggested by this code:
std::vector<size_t> buffer(100);
#pragma omp parallel for private(buffer)
for(size_t j = 0; j < 10000; ++j) {
// ... code using the buffer ...
}
This code does not work. Although there is a buffer for every thread, those can have size 0.
How can I allocate the buffer in the beginning of each thread? Can I still use #pragma omp parallel for
? And can I do it more elegantly than this:
std::vector<size_t> buffer;
#pragma omp parallel for private(buffer)
for(size_t j = 0; j < 10000; ++j) {
if(buffer.size() != 100) {
#pragma omp critical
buffer.resize(100);
}
// ... code using the buffer ...
}