Skip to content

Instantly share code, notes, and snippets.

@Adiqq
Created December 13, 2017 21:59
Show Gist options
  • Save Adiqq/625120e5e37e25ef62ff2b1de6c02ac4 to your computer and use it in GitHub Desktop.
Save Adiqq/625120e5e37e25ef62ff2b1de6c02ac4 to your computer and use it in GitHub Desktop.
pi.cpp
#include <stdio.h>
#include <time.h>
#include <omp.h>
long long num_steps = 100000000;
double step, table[40];
int main(int argc, char* argv[])
{
clock_t start, stop;
double x, pi, sum = 0.0;
int i,j;
omp_set_num_threads(2);
step = 1. / (double)num_steps;
for (j = 0; j < num_steps; j++){
start = clock();
double sum = 0;
#pragma omp parallel
{
int id = omp_get_thread_num();
table[j + id] = 0;
#pragma omp for
for (i = 0; i < num_steps; i++)
{
double x = (i + .5)*step;
//unieważnienie całej linii danych
table[j + id] += 4.0 / (1. + x*x);
}
#pragma omp atomic
sum += table[j + id];
}
pi = sum*step;
stop = clock();
printf("Wartosc liczby PI wynosi %15.12f\n", pi);
printf("Czas przetwarzania wynosi %f sekund\n", ((double)(stop - start) / 1000.0));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment