Skip to content

Instantly share code, notes, and snippets.

@mixstef
Last active May 14, 2021 16:01
Show Gist options
  • Save mixstef/7d0599527be06bcb9d2891cc0f4857ea to your computer and use it in GitHub Desktop.
Save mixstef/7d0599527be06bcb9d2891cc0f4857ea to your computer and use it in GitHub Desktop.
Ατζέντα εργαστηρίου Παράλληλου Προγραμματισμού 14/5/2021

Ατζέντα εργαστηρίου Παράλληλου Προγραμματισμού 14/5/2021

Στο gist αυτό θα αναρτηθούν λύσεις και υποδείξεις κατά τη διεξαγωγή του εργαστηρίου.

#include <stdio.h>
#include <stdlib.h>
// compile with: gcc -O2 -Wall pi-integral.c -o pi-integral
#define N 1000000 // integration steps
int main() {
double pi,w,sum,x;
w = 1.0/N; // integration step
sum = 0.0;
for (int i=1;i<=N;i++) {
x = w*(i-0.5); // midpoint
sum += 4.0/(1.0+x*x); // NOTE: without mult by step (w), done later
}
pi = w*sum;
printf("Computed pi=%.10f\n",pi);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
// compile with: gcc -fopenmp -O2 -Wall pi-integral-omp3.c -o pi-integral-omp3
#define N 1000000 // integration steps
int main() {
double pi,w,sum,x;
w = 1.0/N; // integration step
sum = 0.0;
#pragma omp parallel for shared(w) reduction(+:sum) private(x)
for (int i=1;i<=N;i++) {
x = w*(i-0.5); // midpoint
sum += 4.0/(1.0+x*x); // NOTE: without mult by step (w), done later
}
pi = w*sum;
printf("Computed pi=%.10f\n",pi);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
// compile with: gcc -fopenmp -O2 -Wall pi-integral-omp.c -o pi-integral-omp
#define N 1000000 // integration steps
int main() {
double pi,w,sum,x;
w = 1.0/N; // integration step
pi = 0.0;
sum = 0.0;
#pragma omp parallel shared(w,pi) firstprivate(sum)
{
#pragma omp for private(x)
for (int i=1;i<=N;i++) {
x = w*(i-0.5); // midpoint
sum += 4.0/(1.0+x*x); // NOTE: without mult by step (w), done later
}
#pragma omp critical
{
pi += w*sum;
}
}
printf("Computed pi=%.10f\n",pi);
return 0;
}
// compile with: gcc -fopenmp -O2 -Wall tasks-omp.c -o tasks-omp
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main() {
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
printf("A\n");
#pragma omp task
printf("B\n");
printf("C\n");
}
}
return 0;
}
// compile with: gcc -fopenmp -O2 -Wall tasks-omp-wait.c -o tasks-omp-wait
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main() {
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
printf("A\n");
#pragma omp task
printf("B\n");
#pragma omp taskwait
printf("C\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment