不了解多核并行化。但对于简单的循环,它可能会修改很少。对于以下示例,如何在VC ++中使用多核计算进行简单循环?
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
void foo(int n, double* a, double* b, double *c, double*d, double* e, double* f, double* g)
{
for (int i = 0; i < n; ++i)
{
a[i] = b[i] * a[i] + c[i] * (d[i] + e[i] + f[i] + g[i]);
}
}
int main()
{
int m = 1001001;
vector<double> a(m), b(m), c(m), d(m), f(m);
std::clock_t startcputime = std::clock();
for (int i = 0; i < 1000; ++i)
foo(1000000, &a[0], &b[0], &c[0], &d[0], &d[1], &f[0], &f[1000]);
double cpu_duration = (std::clock() - startcputime) / (double)CLOCKS_PER_SEC;
std::cout << "Finished in " << cpu_duration << " seconds [CPU Clock] " << std::endl;
}
答案 0 :(得分:4)
并行模式库(VC ++的一部分)中的concurrency
命名空间包含parallel_for
,这正是您想要的。
void parallel_foo(int n, double* a, double* b, double *c, double*d, double* e, double* f, double* g)
{
concurrency::parallel_for(static_cast<size_t>(0), static_cast<size_t>(n), [&](size_t i) {
a[i] = b[i] * a[i] + c[i] * (d[i] + e[i] + f[i] + g[i]);
});
}
我建议您使用size_t
进行&#39; n&#39;。这样它会变得更清洁:
void parallel_foo(size_t n, double* a, double* b, double *c, double*d, double* e, double* f, double* g)
{
concurrency::parallel_for(0, n, [&](size_t i) {
a[i] = b[i] * a[i] + c[i] * (d[i] + e[i] + f[i] + g[i]);
});
}
您还应该查看msdn
上的示例您可以使用&amp; amp;&#39;作为备选。它更强大,更复杂。
答案 1 :(得分:3)
您可以使用OpenMP
,只需在#pragma omp parallel for
循环之前添加for
。
void foo(int n, double* a, double* b, double *c, double*d, double* e, double* f, double* g)
{
#pragma omp parallel for
for (int i = 0; i < n; ++i)
{
a[i] = b[i] * a[i] + c[i] * (d[i] + e[i] + f[i] + g[i]);
}
}
此外,如果您使用Microsoft Visual Studio,则需要在设置中启用OpenMP Support