Let arr
be declared as follows.
std::vector<std::vector<int>> arr(10,000,000);
My code in serial is something like
for (const auto [x, y] : XY) {
arr[x].push_back(y);
}
I use openmp and define an array of locks as follows.
std::vector<omp_lock_t> locks(10,000,000);
With locks
, I use
#pragma omp parallel for schedule(dynamic)
for (const auto [x, y] : XY) {
omp_set_lock(&locks[x]);
arr[x].push_back(y);
omp_unset_lock(&locks[x]);
}
Such an approach works in my machine (windows linux subsystem). But then I find the following post
c/c++ maximum number of mutexes allowed in Linux
which raises the concern whether I have used too many locks, and my program may not work for other platforms (those with a limit on the number of locks allowed).
I wonder if there is another way in which I still have the same control granularity as above and it does not have upper limit on the number allowed. Can I use something like compare and swap?