Commit 859b7b5d authored by Christoph Gerum's avatar Christoph Gerum

Added more openmp examples

parent bb4b3e7e
......@@ -19,6 +19,7 @@ void main ()
x = (i+0.5)*step;
sum += 4.0/(1.0+x*x);
}
#pragma omp critical
pi += sum * step;
}
......
#include <omp.h>
#include <stdio.h>
static long num_steps = 100000000;
double step;
void main ()
{
int i; double x, pi = 0.0, sum = 0.0;
step = 1.0/(double) num_steps;
#pragma omp parallel for reduction(+:sum), private(x)
for (i=0;i< num_steps; i++) {
x = (i+0.5)*step;
sum += 4.0/(1.0+x*x);
}
pi = sum * step;
printf("PI is aproximately %f\n", pi);
}
File added
#include <omp.h>
#include <stdio.h>
const int num_steps = 10000;
void main()
{
int sum = 0;
#pragma omp parallel
{
int id, nthrds, i;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
for(i = id; i <= num_steps; i += nthrds){
//#pragma omp critical
#pragma omp atomic
sum += i;
}
}
printf("Sum from 0 to %d is %d\n", num_steps, sum);
}
#include <omp.h>
#include <stdio.h>
const int num_steps = 10000;
void main()
{
int i, sum = 0;
#pragma omp parallel for reduction(+: sum)
for(i = 0; i <= num_steps; i++){
sum += i;
}
printf("Sum from 0 to %d is %d\n", num_steps, sum);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment