0

我正在尝试通过以下代码利用 openCilk(MIT 目前的 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:我平均每个运行100次n以标准化开销异常值。

注意#2:我使用 cilk-plus 标签代替了不存在的 openCilk 标签。

4

0 回答 0