简体   繁体   English

C ++多线程优化

[英]C++ Multithreading optimization

I am new to threading in C++ but I've done enough reading to at least get what I'm working on to compile. 我是C ++线程技术的新手,但是我已经做了大量的阅读工作,至少可以了解到我正在编译的内容。 As of yet it hasn't improved performance at all. 到目前为止,它根本没有改善性能。 Right now I just have it creating the number of threads as there are loops but I can imagine that can pretty quickly cause the system to thrash. 现在,由于存在循环,我只是用它来创建线程数,但是我可以想象,这会很快导致系统崩溃。 Is there a better alternative to brute force controlling the number of threads? 除了蛮力控制线程数之外,还有更好的选择吗? I also intend to run this on the WestGrid computing system where I can specify the number of processors to use. 我还打算在WestGrid计算系统上运行此程序,在该系统上可以指定要使用的处理器数量。 What is the best way to set the number of threads to optimize for the number of processors. 设置线程数以优化处理器数量的最佳方法是什么。

void ExecuteCRTProcess(const long &numberOfRows, ZZ* rij, const ZZ &powRoh, const int &rowLength, long* PublicKey, ZZ* rQ0, const ZZ &Q0, ZZ* primes, const ZZ &productOfPrimes, ZZ* resultsArray, const bool IsItPrimeArray, long* caseStudy, long* caseStudyTracker, const ZZ &X0, const long &Roh){
int rc;
pthread_t threads[numberOfRows];
struct parameters ThreadParameters[numberOfRows];

for(int i = 0; i< numberOfRows ; i++){
    FillR(rij,powRoh,rowLength); // fill up the vector rij with random numbers between 0 powRoh
    MultiplyVectorByTwo(rij, rowLength, i, IsItPrimeArray); //Multiply rij vector by 2. If calculating Xi' also add 1 to appropriate values.
    ThreadParameters[i].rij = rij;
    ThreadParameters[i].rQ0 = rQ0;
    ThreadParameters[i].primes = primes;
    ThreadParameters[i].rowLength = rowLength;
    ThreadParameters[i].Q0 = Q0;
    ThreadParameters[i].i = i;
    ThreadParameters[i].X0 = X0;
    rc = pthread_create(&threads[i],NULL,CRTNew,(void *)&ThreadParameters[i]);
    if(rc){
        cout << "Error: unable to create thread, " << rc << endl;
        exit(-1);
    }
    for(long j = 0; j< rowLength; j++){
        cout << (resultsArray[i] % primes[j]) << " ";
    }
    cout << endl;*/

}
for(int i = 0; i< numberOfRows; i++){
    pthread_join(threads[i], NULL);
    resultsArray[i] = ThreadParameters[i].result;
    }
}

The threads created run this function 创建的线程运行此功能

void* CRTNew(void *threadArg){
    struct parameters *local_data;
    local_data = (struct parameters *) threadArg;
    ZZ a, p, A, P, crt;
    long Z, Public;
    a = local_data->rQ0[local_data->i];
    p = local_data->Q0;
    A = local_data->rij[0];
    P = local_data->primes[0];
    for(int i = 1; i<=local_data->rowLength; i++){
       A = A%P;
       Z = CRT(a, p, A, P);
       A = local_data->rij[i]; P = local_data->primes[i];
       if(i == local_data->rowLength) Public = Z;
    }
   if(a < 0) crt = a+p;
   else crt = a%p;
   local_data->result = crt%local_data->X0;
   pthread_exit(NULL);

} }

'What is the best way to set the number of threads to optimize for the number of processors'; “设置线程数以优化处理器数量的最佳方法是什么?”;

1) Create [num of cores] threads, (or maybe a few more), at app startup. 1)在应用启动时创建[核心数]个线程(或更多)。

2) Never create any more threads 2)不再创建任何线程

3) Never let the threads terminate. 3)切勿让线程终止。

4) Have them wait for work tasks on a producer-consumer thread, in the manner of a pool. 4)让他们以池的方式在生产者-消费者线程上等待工作任务。

Alternatively, use a thread pool class or equivalent parallel language feature that already works. 或者,使用已经起作用的线程池类或等效的并行语言功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM