我想并行化一个 for 循环,其中更新 unordered_map 的值:
unordered_map<string,double> umap {{"foo", 0}, {"bar", 0}};
#pragma omp parallel for reduction(my_reduction:umap)
for (int i = 0; i < 100; ++i)
{
// some_string(i) would return either "foo" or "bar"
umap[some_string(i)] += some_double(i);
}
Run Code Online (Sandbox Code Playgroud)
因此,unordered_map 中不会创建新条目,只会更新现有条目的总和。
在这个答案中,用户声明的归约是针对向量的情况定义的。在 unordered_map 的情况下,用户声明的归约是否可以类似地定义?