Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <omp.h>
- #define CIRCLE(step, i) sqrt(4.0 - (double) (i) * (step) * (double) (i) * (step))
- int main() {
- omp_set_num_threads(omp_get_num_procs());
- int N;
- FILE* datp = fopen("apr22-pi1.dat", "r");
- if (datp == NULL) {
- printf("Error. Couldn't open the data file.");
- exit(1);
- }
- if (fscanf(datp, "%d", &N) != 1) {
- printf("Error. Couldn't read the number from the data file.");
- exit(2);
- }
- fclose(datp);
- int i, ibeg, iend, size, rank;
- double step = 2.0 / (double) N;
- double f_old, f_new, S = 0.0;
- #pragma omp parallel shared(step, size, N) private(i, ibeg, iend, rank, f_old, f_new) reduction(+:S)
- {
- size = omp_get_num_threads();
- rank = omp_get_thread_num();
- ibeg = (N / size) * rank;
- iend = (rank == size - 1) ? (N - 1) : ((N / size) * (rank + 1) - 1);
- f_old = CIRCLE(step, ibeg);
- S = 0.0;
- for (i = ibeg; i <= iend; ++i) {
- f_new = CIRCLE(step, i + 1);
- S += (f_old + f_new) * step / 2.0;
- f_old = f_new;
- }
- }
- printf("Pi = %f\n", S);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement