Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
多线程计算平均值和std并不能提高效率 我是C++多线程编程领域的新手,我尝试用多线程并行计算数据的平均值和标准差,以减少时间开销。我计算平均值和标准偏差的功能如下 void cal_mean_std(float* data, float* mean, float* sd, int N, int start_index, int span_cols) { int value; for(int j = start_index; j < start_index + span_cols; j++){ mean[j] = 0; sd[j] = 0; for (int i = 0; i < N; i++) { value = data[j * N + i]; mean[j] += value; sd[j] += value * value; } mean[j] = mean[j] / N; sd[j] = sqrt(sd[j] / N - mean[j] * mean[j]); } }_C++_Multithreading_C++11_Parallel Processing - Fatal编程技术网

多线程计算平均值和std并不能提高效率 我是C++多线程编程领域的新手,我尝试用多线程并行计算数据的平均值和标准差,以减少时间开销。我计算平均值和标准偏差的功能如下 void cal_mean_std(float* data, float* mean, float* sd, int N, int start_index, int span_cols) { int value; for(int j = start_index; j < start_index + span_cols; j++){ mean[j] = 0; sd[j] = 0; for (int i = 0; i < N; i++) { value = data[j * N + i]; mean[j] += value; sd[j] += value * value; } mean[j] = mean[j] / N; sd[j] = sqrt(sd[j] / N - mean[j] * mean[j]); } }

多线程计算平均值和std并不能提高效率 我是C++多线程编程领域的新手,我尝试用多线程并行计算数据的平均值和标准差,以减少时间开销。我计算平均值和标准偏差的功能如下 void cal_mean_std(float* data, float* mean, float* sd, int N, int start_index, int span_cols) { int value; for(int j = start_index; j < start_index + span_cols; j++){ mean[j] = 0; sd[j] = 0; for (int i = 0; i < N; i++) { value = data[j * N + i]; mean[j] += value; sd[j] += value * value; } mean[j] = mean[j] / N; sd[j] = sqrt(sd[j] / N - mean[j] * mean[j]); } },c++,multithreading,c++11,parallel-processing,C++,Multithreading,C++11,Parallel Processing,谢谢大家。最后,我发现我的代码有什么问题。计时器时钟计算CPU消耗时间,而不是墙壁时间。注释不用于扩展讨论;这段对话已经结束。 x.mean = new float[x.M]; x.sd = new float[x.M]; std::vector<std::thread> thread_pool; int h = 4; thread_pool.reserve(h); int SNIPs = static_cast<int>

谢谢大家。最后,我发现我的代码有什么问题。计时器时钟计算CPU消耗时间,而不是墙壁时间。

注释不用于扩展讨论;这段对话已经结束。
    x.mean = new float[x.M];
    x.sd = new float[x.M];
    std::vector<std::thread> thread_pool;

    int h = 4;
    thread_pool.reserve(h);
    int SNIPs = static_cast<int>(x.M / h + 1);
    int SNIPs_final = x.M - (h - 1) * SNIPs;
     for (int i = 0; i < h - 1; i++)
     {
         thread_pool.push_back(std::thread(std::bind(cal_mean_std, x.data, x.mean, x.sd,
                                                 x.N, i*SNIPs, SNIPs)));
     }
    thread_pool.push_back(std::thread(std::bind(cal_mean_std, x.data, x.mean, x.sd,
                                                 x.N, (h-1)*SNIPs, SNIPs_final)));
    for (int i = 0; i < h; i++)
        thread_pool.at(i).join();
#include <thread>
#include <vector>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <math.h>

void gen_matrix(int N, int P, float* data){
    for (int i = 0; i < N * P; i++)
    {
        data[i] = rand() % 10;
    }
}

void cal_mean_std(float* data, float* mean, float* sd, int N, int start_index, int span_cols)
{
    int value;
    for(int j = start_index; j < start_index + span_cols; j++){
        mean[j] = 0;
        sd[j] = 0;
        for (int i = 0; i < N; i++) {
            value = data[j * N + i];
            mean[j] += value;
            sd[j] += value * value;
        }
        mean[j] = mean[j] / N;
        sd[j] = sqrt(sd[j] / N - mean[j] * mean[j]);
    }
}

int main()
{
    int N = 5000;
    int P = 300000;
    float* data = new float[N*P];
    gen_matrix(N, P, data);
    float* mean = new float[P];
    float* std = new float[P];
    std::vector<std::thread> thread_pool;
    clock_t t1;
    t1 = clock();
    int h = 1;
    thread_pool.reserve(h);
    int SNIPs = static_cast<int>(P / h + 1);
    int SNIPs_final = P - (h - 1) * SNIPs;
    for (int i = 0; i < h - 1; i++)
    {
        thread_pool.push_back(std::thread(std::bind(cal_mean_std, data, mean, std,
                                                    N, i*SNIPs, SNIPs)));
    }
    thread_pool.push_back(std::thread(std::bind(cal_mean_std, data, mean, std,
                                                N, (h-1)*SNIPs, SNIPs_final)));
    for (int i = 0; i < h; i++)
        thread_pool.at(i).join();
    std::cout <<"Time for the cal mean and std is " << (clock() - t1) * 1.0/CLOCKS_PER_SEC << std::endl;
    return 0;
}