问题描述
我正在尝试利用带有以下代码的 opencilk(麻省理工学院目前的 cilk 发行版)减速器。
#include <cilk/cilk.h>
#include "opencilk_reducer.hpp"
#include <cilk/reducer_opadd.h>
#include <iostream>
#include <chrono>
int test_count = 100;
unsigned int compute(unsigned int i)
{
return i^2 + 10 * i + 1024; // return a value computed from i
}
void measure_cilk_sum(uint16_t n)
{
cilk::reducer_opadd<int> sum;
//defining the sum as a reducer with an int value
cilk_for (int i = 1; i <= n; ++i)
{
sum += compute(i);
{
}
double averageout_cilk_sum(uint16_t n)
{
auto start = std::chrono::high_resolution_clock::Now();
// average out 100 executions to avoid varying results
for (auto i = 0; i < test_count; i++)
{
measure_cilk_sum(n);
}
auto end = std::chrono::high_resolution_clock::Now();
std::chrono::duration<double,std::milli> elapsed_seconds = end-start;
return elapsed_seconds.count() / test_count;
}
int main(int argc,char* argv[])
{
for(unsigned int n = 1000 ; n < 100000 ; n += 1000 )
{
auto time = averageout_cilk_sum(n);
}
return 0;
}
我正在使用 opencilk 团队提供的 Tapi/LLVM 编译器进行编译。我已经尝试使用和不使用 -O3
优化标志进行编译,但并行循环并没有比简单的顺序 for-loop
更快(我没有把它放在这里,因为它非常微不足道,我不想要代码段太大)无论如何。
我还尝试了从数百到数百万的所有 n
范围,但顺序似乎总是更快。我知道这个任务相当简单,但应该有一个范围,并行运行比顺序运行更快。
为什么并行 cilk-for 的运行速度不比顺序对应的快?
注意#1:我平均每个 n
运行 100 次,以标准化开销异常值。
注意#2:我使用 cilk-plus 标签代替不存在的 opencilk 标签。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)