Why is this OpenMP program giving me different answers every time?
问题 I'm trying make this program run with multiple threads. #include <stdio.h> #include <time.h> #include <omp.h> #define NUM_THREADS 4 static long num_steps = 1000000000; int main() { int i; double x, pi, sum = 0.0; double step = 1.0/(double)num_steps; clock_t start = clock(), diff; #pragma omp parallel for num_threads(NUM_THREADS) reduction (+:sum) for (i = 0; i < num_steps; i++) { x = (i+0.5)*step; sum += 4.0/(1.0 + x*x); } #pragma omp ordered pi = step*sum; printf("pi = %.15f\n %d iterations