Tôi đang sử dụng phương pháp monte carlo để tính pi và làm một kinh nghiệm cơ bản với lập trình song song và OpenMPParallel, nhưng chậm hơn
vấn đề là khi tôi sử dụng 1 chủ đề, x lần lặp lại, luôn luôn chạy nhanh hơn so với chủ đề n , x lặp lại. bất cứ ai đó có thể trả lời tôi tại sao?
Ví dụ mã chạy như thế này "a.out 1 1000000", trong đó 1 là chủ đề và 1000000 sự lặp
include <omp.h>
include <stdio.h>
include <stdlib.h>
include <iostream>
include <iomanip>
include <math.h>
using namespace std;
int main (int argc, char *argv[]) {
double arrow_area_circle, pi;
float xp, yp;
int i, n;
double pitg= atan(1.0)*4.0; //for pi error
cout << "Number processors: " << omp_get_num_procs() << endl;
//Number of divisions
iterarions=atoi(argv[2]);
arrow_area_circle = 0.0;
#pragma omp parallel num_threads(atoi(argv[1]))
{
srandom(omp_get_thread_num());
#pragma omp for private(xp, yp) reduction(+:arrow_area_circle) //*,/,-,+
for (i = 0; i < iterarions; i++) {
xp=rand()/(float)RAND_MAX;
yp=rand()/(float)RAND_MAX;
if(pow(xp,2.0)+pow(yp,2.0)<=1) arrow_area_circle++;
}
}
pi = 4*arrow_area_circle/iterarions;
cout << setprecision(18) << "PI = " << pi << endl << endl;
cout << setprecision(18) << "Erro = " << pitg-pi << endl << endl;
return 0;
}
tôi có bộ đôi lõi, nhưng tôi sẽ thử một giải pháp cho arrow_area_circle – blueomega
Bạn sẽ thấy tốc độ tăng lên ở 2 luồng và sau đó giảm tốc độ sau đó. – Michael