pthread在Windows中比serial快,但在Linux中比serial慢 我试图在Windows和Linux上运行蒙特卡洛算法,用相同的线程数(4个线程和4个CPU)运行相同的C++并行代码。虽然并行代码在Windows上比串行实现快,但在Linux上要慢得多

pthread在Windows中比serial快,但在Linux中比serial慢 我试图在Windows和Linux上运行蒙特卡洛算法,用相同的线程数(4个线程和4个CPU)运行相同的C++并行代码。虽然并行代码在Windows上比串行实现快,但在Linux上要慢得多,c++,linux,parallel-processing,pthreads,C++,Linux,Parallel Processing,Pthreads,节目如下: #include <iostream> #include <cstdlib> #include <ctime> #include <cmath> #include <pthread.h> #include <chrono> using namespace std; using ns = chrono::nanoseconds; using get_time = chrono::steady_clock; st

节目如下:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <pthread.h>
#include <chrono>

using namespace std;
using ns = chrono::nanoseconds;
using get_time = chrono::steady_clock;

static int thread_count = 4;
pthread_mutex_t myMutex;

struct args{
    int id;
    int random_count;
    double *pi;
};

double compute_pi(long n)
{
     double pi = 0;
     double x, y;
     for(long i=0; i<n; i++){
         x = -1 + 2 * double(rand())/RAND_MAX;
         y = -1 + 2 * double(rand())/RAND_MAX;
         if (sqrt(x*x + y*y) <= 1.0) pi++;
    }
    return 4*pi/n;
}

void* threadFunc(void *argin){
    args *inputs = (args*) argin;
    double my_sum = 0;
    double x, y;
    for(int i=0; i<inputs->random_count; i++){
        x = -1 + 2 * double(rand())/RAND_MAX;
        y = -1 + 2 * double(rand())/RAND_MAX;
        if (sqrt(x*x + y*y) <= 1.0) my_sum++;
    }
    pthread_mutex_lock(&myMutex);
    *(inputs->pi) += my_sum;
    pthread_mutex_unlock(&myMutex);
    return nullptr;
}

double compute_pi_parallel(long n)
{
    double pi = 0;
    int count_per_thread = n/thread_count;
    pthread_t *threads = new pthread_t[thread_count];
    args *funcInputs = new args[thread_count];
    pthread_mutex_init(&myMutex, nullptr);

    for(int i=0; i<thread_count; i++){
        funcInputs[i].id = i;
        funcInputs[i].random_count = i<n%thread_count ? count_per_thread+1 : 
    count_per_thread;
        funcInputs[i].pi = &pi;
        int rc = pthread_create(&threads[i], nullptr, threadFunc, (void *) 
                                &funcInputs[i]);
        if(rc) cerr << "error in thread creation!\n";
    }

    for(int i=0; i<thread_count; i++){
        int rc = pthread_join(threads[i], nullptr);
        if(rc) cerr << "Error in thread join!\n";
    }

    pthread_mutex_destroy(&myMutex);
    delete [] funcInputs;
    delete [] threads;
    return 4*pi/n;
}

int main(int argc, char* argv[])
{
    srand(time(nullptr));
    long n = 100000000;
    auto start = get_time::now();
    if (argc > 1){
        n = atol(argv[1]);
        if (argc == 3){
            thread_count = atoi(argv[2]);
            cout << "pi(parallel) = " << compute_pi_parallel(n) << endl;
            auto stop = get_time::now();
            auto diff = stop - start;
            cout<<"Elapsed time is :  "<< chrono::duration_cast<ns>
    (diff).count()/1e9<<" s "<<endl;
            return 0;
        }
    }

    cout << "pi = " << compute_pi(n) << endl;
    auto stop_s = get_time::now();
    auto diff_s = stop_s - start;
    cout << "pi(parallel) = " << compute_pi_parallel(n) << endl;
    auto stop_p = get_time::now();
    auto diff = stop_p - stop_s;
    cout<<"Elapsed time for serial is :  "<< chrono::duration_cast<ns>
    (diff_s).count()/1e9<<" s "<<endl;
    cout<<"Number of threads: "<< thread_count<< endl;
    cout<<"Elapsed time for parallel is :  "<< chrono::duration_cast<ns>
    (diff).count()/1e9<<" s "<<endl;
    return 0;
}
在Linux上:
$g++-std=c++11-g-Wall-omc mc.cpp-lpthread

输出:

pi = 3.14138
pi(parallel) = 3.14166
Elapsed time for serial is :  3.10837 s 
Number of threads: 4
Elapsed time for parallel is :  19.8226 s
我用$lscpu检查Linux上的CPU数量,并用$top监视CPU使用情况。Linux似乎使用了所有可用的内核,但仍然比串行代码慢。我在Windows虚拟机上运行Ubuntu 16.04 LTS程序


我想知道我在Linux上是否做错了什么。

您正在使用兰德。如果rand是线程安全的,则它是实现定义的。它可以简单地调用互斥锁。使用一个没有全局状态的现代C++随机数生成器。< /p>你在两个平台上都启用了优化吗?如果不是,那么从那里开始。为什么不用C++线程API?特别是,可能的复制,即代码> RAND()的GLUBC实现< /COD>确实使用互斥来同步线程之间的访问,所以在Linux上,您的解决方案只是序列化在<代码> RAND()/<代码>。
pi = 3.14138
pi(parallel) = 3.14166
Elapsed time for serial is :  3.10837 s 
Number of threads: 4
Elapsed time for parallel is :  19.8226 s