'Added excercises for OpenMP'

This commit is contained in:
Arkadiusz Hypki 2024-05-06 13:28:29 +02:00
parent e239bfb7d9
commit c003de06f0
3 changed files with 75 additions and 1 deletions

View File

@ -46,4 +46,7 @@ grandmother(X,Y) :- female(X),parent(Z,Y),parent(X,Z).
sister(X,Y) :- female(X),parent(Z,X),parent(Z,Y),X\=Y.
brother(X,Y) :- male(X),parent(Z,X),parent(Z,Y),X\=Y.
cousin(X,Y) :- uncle(Z,Y),parent(Z,X).
cousin(X,Y) :- aunt(Z,Y),parent(Z,X).
cousin(X,Y) :- aunt(Z,Y),parent(Z,X).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z),ancestor(Z,Y).

12
07_OpenMP/desc Normal file
View File

@ -0,0 +1,12 @@
Excercise 1
This is our quick tutorial.
Excercise 2
Implement in OpenMP the search of the maximum value in an array with randomly generated numbers. Check the speed up from 1 up to X threads.
Why there is no speed up at some point?
Excercise 3
Implement bubble sort in OpenMP. Can you make it faster than 1 threaded quicksort?

View File

@ -0,0 +1,59 @@
/*
============================================================================
Name : openmp1.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
void sum() {
int partial_Sum, total_Sum;
#pragma omp parallel private(partial_Sum) shared(total_Sum)
{
partial_Sum = 0;
total_Sum = 0;
#pragma omp for
for(int i = 1; i <= 100; i++) {
partial_Sum += i;
}
//Create thread safe region.
#pragma omp critical
{
//add each threads partial sum to the total sum
total_Sum += partial_Sum;
}
}
printf("Total Sum: %d\n", total_Sum);
}
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
const int numThreads = 4;
int a[numThreads];
omp_set_num_threads(numThreads);
#pragma omp parallel
{
int id = omp_get_thread_num();
a[id] = id + 1;
}
for (int i = 0; i < numThreads; i++) {
printf("Hello from thread %d\n", a[i]);
}
sum();
// sum2();
return EXIT_SUCCESS;
}