简体   繁体   English

在C ++中对向量进行并行操作的最快方法?

[英]Fastest Way to Parallelize Operation over a Vector in C++?

I am trying to parallelize operations over a large vector of objects in C++. 我正在尝试并行处理C ++中大量对象的操作。 I have written parallel programs in Java before, but I have just started using C++. 我以前用Java编写过并行程序,但是我刚开始使用C ++。

The current code uses an iterator over the vector. 当前代码在向量上使用迭代器。 What would be the fastest way to parallelize this? 什么是最快的并行化方法? My current thoughts are... 我目前的想法是...

  1. Using the .size() function and using a forloop through the vector. 使用.size()函数并在向量中使用forloop。 However, I am worried about the runtime of the .size() function, is it O(N) or O(1)? 但是,我担心.size()函数的运行时,是O(N)还是O(1)? Also would forloops be slower than using an iterator? 而且forloops会比使用迭代器慢吗?

  2. Somehow splitting the vector, and create iterators for the new vectors in parallel? 以某种方式拆分向量,并为新向量并行创建迭代器? If so, what would be a good method of splitting the vector with a fast runtime? 如果是这样,那么以快速的运行时间分割向量的好方法是什么?

Or is there some faster way to do this? 还是有一些更快的方法来做到这一点?

However, I am worried about the runtime of the .size() function, is it O(N) or O(1)? 但是,我担心.size()函数的运行时,是O(N)还是O(1)?

vector<...>::size() is O(1). vector<...>::size()为O(1)。

Also would forloops be slower than using an iterator? 而且forloops会比使用迭代器慢吗?

In most cases, I doubt it. 在大多数情况下,我对此表示怀疑。 In some cases, an algorithm which takes iterators may be optimized based on the value type of the iterator. 在某些情况下,可以基于迭代器的值类型来优化采用迭代器的算法。 Benchmark it. 进行基准测试。

Somehow splitting the vector, and create iterators for the new vectors in parallel? 以某种方式拆分向量,并为新向量并行创建迭代器? If so, what would be a good method of splitting the vector with a fast runtime? 如果是这样,那么以快速的运行时间分割向量的好方法是什么?

Vector iterators are random access. 向量迭代器是随机访问的。 It is a very cheap operation to just find the distance from begin to end (O(1)), and split it in half. 仅查找起点到终点的距离(O(1)),然后将其分成两部分,这是非常便宜的操作。

auto begin = v.begin();
auto end = v.end();
auto mid = begin + (end - begin)/2;
algorithm(begin, mid);
algorithm(mid, end);

如果向量没有被迭代器修改,我会将向量划分为N个部分,并为每个线程分配其向量,然后,如果需要,一个线程会汇总所有其他线程的结果。

look into boost threads. 看看增强线程。 I find them very intuitive 我觉得它们很直观

我相信这个答案是有意义的: https : //stackoverflow.com/a/2547725/1524700根据您的平台,我还将英特尔线程构建模块添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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