I am new to threading in C++ but I've done enough reading to at least get what I'm working on to compile. 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. 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.
2) Never create any more threads
3) Never let the threads terminate.
4) Have them wait for work tasks on a producer-consumer thread, in the manner of a pool.
Alternatively, use a thread pool class or equivalent parallel language feature that already works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.