tbb串行任务比并行更快

时间:2018-05-29 21:24:56

标签: tbb

我创建了计算行列式的串行任务:

class serialTask : public task {
matrix m;
int *determinant;
serialTask(matrix m_, int* sum) : m(m_), determinant(sum) {}
task* execute()
{
    *determinant = 0;
    for (int i = 0; i < m.get_size(); i++)
    {
        int cur_elem = m.get_values()[0][i];
        if (m.get_size() == 1)
        {
            *determinant += cur_elem;
        }
        else
        {
            int x;
            matrix new_m = m.cut_matrix(0, i);
            serialTask& task = *new(allocate_child()) serialTask(new_m, &x);
            set_ref_count(2);
            spawn_and_wait_for_all(task);
            int step = 2 + i;
            *determinant += cur_elem * pow(-1, step) * x;
        }
    }
    return NULL;
}

};

...和并行任务:

class parallelTask : public task {
matrix m;
int *determinant;
int* results;
parallelTask(matrix mat, int* sum) : m(mat), determinant(sum), results(new int[m.get_size()]) {}
task* execute()
{
    task_list tasks;
    int *elems = new int[m.get_size()];
    if (m.get_size() == 1)
    {
        *determinant = m.get_values()[0][0];
    }
    else
    {
        *determinant = 0;
        for (int i = 0; i < m.get_size(); i++)
        {
            elems[i] = m.get_values()[0][i];
            matrix new_m = m.cut_matrix(0, i);
            tasks.push_back(*new(allocate_child()) parallelTask(new_m, &results[i]));
        }
        set_ref_count(m.get_size() + 1);
        spawn_and_wait_for_all(tasks);
        for (int j = 0; j < m.get_size(); j++)
        {
            int step = 1 + j + 1;
            *determinant += elems[j] * pow(-1, step) * results[j];
        }
    }
    return NULL;
}

};

对于大矩阵,并行任务应该比串行任务更快,而对于小矩阵则更慢。但情况恰恰相反:对于大矩阵,串行任务比并行快得多。

有人能告诉我问题在哪里吗?

1 个答案:

答案 0 :(得分:0)

尝试使用&#34; CutOff&#34; TBB Fibonacci example中描述的参数,以便并行完成工作所涉及的开销不会占用任务中的工作(甚至不到5-10%,如建议的那样)。否则,使用并行版本的算法是不合理的。