Created
April 19, 2023 11:14
-
-
Save filipsedivy/55e65a99275cda0fd91325fc628e2d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <omp.h> | |
int main() { | |
int proc = omp_get_num_procs(); | |
int data[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 110}; | |
std::cout << "Pocet dostupnych jader: " << proc << std::endl; | |
// Proměnné vytvořené mimo paralelní sekci, jsou ve výchozím nastavení sdílené mezi vlákny | |
int B = 10; | |
int sum = 0; | |
#pragma omp parallel firstprivate(B) shared(sum) | |
{ | |
B++; | |
#pragma omp barrier | |
printf("B: %i\n", B); | |
#pragma omp for reduction(+:sum) | |
for (int i = 0; i < 8; i++) { | |
#pragma omp critical | |
{ | |
sum += data[i]; | |
} | |
} | |
printf("Sum: %i\n", sum); | |
}; | |
#pragma omp parallel | |
{ | |
int id = omp_get_thread_num(); | |
printf("Hello from parallel section\n"); | |
#pragma omp barrier | |
printf("ID: %i, cislo: %i\n", id, data[id]); | |
#pragma omp sections | |
{ | |
#pragma omp section | |
{ | |
printf("Hello from section on on thread %i\n", id); | |
} | |
#pragma omp section | |
{ | |
printf("Hello from section on on thread %i\n", id); | |
} | |
}; | |
#pragma omp single | |
{ | |
printf("Hello from single section on thread %i\n", id); | |
}; | |
#pragma omp for nowait schedule(dynamic) | |
for (int i = 0; i < 12; i++) { | |
printf("ID: %i, value: %i \n", id, data[i]); | |
} | |
}; | |
printf("Hello from serial section \n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment