Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.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-pi2.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;
- double step, S;
- step = 2.0 / (double) N;
- S = 0.0;
- #pragma omp parallel shared(N, step) private(i) reduction(+:S)
- {
- #pragma omp for
- for (i = 0; i < N; i++) {
- S += (CIRCLE(step, i) + CIRCLE(step, i + 1)) * step / 2.0;
- }
- }
- printf("Pi = %f\n", S);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment