Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C+时结果不准确+;多线程标准库 使用C++库学习概率模型的分类参数。_C++_Multithreading - Fatal编程技术网

使用C+时结果不准确+;多线程标准库 使用C++库学习概率模型的分类参数。

使用C+时结果不准确+;多线程标准库 使用C++库学习概率模型的分类参数。,c++,multithreading,C++,Multithreading,我的代码的第一个版本是完全顺序的,我得到了很好的结果: 但是推断时间相当长,所以我使用了多线程,我的代码运行得很好,速度也更快,但是我使用的线程越多,结果就越差 所以我猜这是线程之间的数据共享问题,但我不知道如何避免,因为为了避免这个问题,我将使用的所有数据复制到了一个向量中。在将数据文件传递给线程之前,我将其拆分为n_线程部分 // --- thread function void thread_inference(std::vector<std::vector<doubl

我的代码的第一个版本是完全顺序的,我得到了很好的结果:

但是推断时间相当长,所以我使用了多线程,我的代码运行得很好,速度也更快,但是我使用的线程越多,结果就越差

所以我猜这是线程之间的数据共享问题,但我不知道如何避免,因为为了避免这个问题,我将使用的所有数据复制到了一个向量中。在将数据文件传递给线程之前,我将其拆分为n_线程部分

// --- thread function

void thread_inference(std::vector<std::vector<double>> const &data,
            std::vector<double> const &truth, std::vector<int> const &confusion_vector)
{
    std::vector<int> &confusion_vector_bis = const_cast<std::vector<int> &>(confusion_vector);
    std::vector<double> &truth_bis = const_cast<std::vector<double> &>(truth);
    std::vector<std::vector<double>> &data_bis = const_cast<std::vector<std::vector<double>> &>(data);

    std::vector<double> predictions{};
    predictions = apply_prediction(data_bis);

    compute_confusion_matrix(predictions, testing_truth_bis, confusion_vector_bis);
}

// --- main
for (int i = 0; i < n_threads; i++)
{
    ...
    std::vector<int> confusion_vector{0,0,0,0};
    confusion_vectors.push_back(confusion_vector);

    DataObject data;
    datas.push_back(data);

    DataObject truth;
    truths.push_back(truth);
    ...
}

for (int i = 0; i < n_threads; i++)
{
    threadList.push_back(std::thread(thread_inference, std::ref(datas[i]), 
                std::ref(truths[i]), std::ref(confusion_vectors[i])));
}

std::for_each(threadList.begin(), threadList.end(), std::mem_fn(&std::thread::join));

for (int i = 0; i < nb_threads; i++)
    for (int j = 0; j < confusion_vectors[i].size(); j++)
        confusion_vector_final[j] += confusion_vectors[i][j];

/---线程函数
无效线程推理(标准::向量常量和数据,
标准::向量常数和真值,标准::向量常数和混淆(向量)
{
标准::向量和混淆向量=常量(混淆向量);
标准::向量和真值=常数(真值);
标准::向量和数据=常数(数据);
std::向量预测{};
预测=应用预测(数据);
计算混乱矩阵(预测、检验真相、混乱向量);
}
//---梅因
对于(int i=0;i
(我将代码简化为我认为有用的部分)


我使用本教程()来演示如何将参数传递给线程。

应用预测()
和/或
计算矩阵()
可能不是线程安全的;他们可以使用全局变量。你能确认这是否是事实吗?请提取a并在这里提供,以便人们可以复制你所说的。作为一个新用户,也需要阅读。此外,请不要使用文字图片。顺便说一句:开始使用
const\u cast
uses,它们应该是不必要的。有时候,仅仅重构看似无害的代码缺陷就可以修复你所没有看到的真正的错误。不用说,多线程编程远不止是学习如何创建和启动线程。您还没有向我们展示任何同步、防止数据竞争等功能。如果没有可运行的示例,就无法回答这些问题。我突然想到一件事。把这些向量相加就是正确的答案吗?如果他们都在进行某种回归,我可能认为最好的结果是某种平均值,而不是简单的总和。但是如果没有更多的代码,就无法理解您在做什么。在有多个解决方案的情况下,组合解决方案表现出无望的性能。不要发布文本的图片,而是将文本作为文本发布,