多线程计算均值和标准差不会提高效率

时间:2019-01-18 11:45:38

标签: c++ multithreading c++11 parallel-processing

我是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]);
    }
}

我指定每个线程的起始索引和计算范围,并按如下所示激活我的thread_pool。

    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();

其中x.M是我的数据列的总数。但是,我发现以这种方式执行并不能提高程序效率。我不确定是什么问题。

实际上,我们可以模拟数据进行计算。我的数据大小是5k x 300k。使用for循环对一个线程的所有数据进行顺序计算需要15秒。我的多线程版本有时需要16秒。

仿真代码如下,我发现当我使用h = 1时,程序需要6秒钟才能完成。但是,当我使用h = 4时,程序需要14秒才能完成。

#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;
}

1 个答案:

答案 0 :(得分:1)

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