Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <omp.h>
- #define SIZE 10
- #define NUMBER_OF_THREADS 4
- void add_vectors(const int* v1, const int* v2, int* rez)
- {
- int sum = 0;
- #pragma omp parallel if(SIZE > 5) default(none) shared(v1, v2, rez, sum)
- {
- #pragma omp for schedule(guided, 2) reduction(+: sum)
- for (auto i = 0; i < SIZE; ++i)
- {
- sum = sum + v1[i] * v2[i];
- rez[i] = v1[i] + v2[i];
- printf("I am thread %d at %d\n", omp_get_thread_num(), i);
- }
- }
- printf("I am sum: %d\n", sum);
- }
- void f(int* p_value, const int number_of_executions)
- {
- for (auto i = 0; i < number_of_executions; ++i)
- {
- #pragma omp critical (p_value)
- {
- (*p_value)--;
- }
- }
- printf("I am function F from thread %d\n", omp_get_thread_num());
- }
- void g(int* p_value, const int number_of_executions)
- {
- for (auto i = 0; i < number_of_executions; ++i)
- {
- #pragma omp critical (p_value)
- {
- (*p_value)++;
- }
- }
- printf("I am function G from thread %d\n", omp_get_thread_num());
- }
- void print_rez_ex1(const int* rez)
- {
- printf("\n");
- for (auto i = 0; i < SIZE; ++i)
- {
- printf("REZ[%d] = %d\n", i, rez[i]);
- }
- }
- int main()
- {
- omp_set_num_threads(NUMBER_OF_THREADS);
- //-------------------------------------- EX 1 --------------------------------------
- printf("------------------ EX 1 -------------------\n");
- int v1[SIZE], v2[SIZE], v3[SIZE];
- for (auto i = 0; i < SIZE; ++i)
- {
- v1[i] = i;
- v2[i] = 1;
- }
- add_vectors(v1, v2, v3); // ADD
- print_rez_ex1(v3); // REZ
- //-------------------------------------- EX 2 --------------------------------------
- printf("\n\n\n\n------------------ EX 2 -------------------\n");
- int p_value = 0;
- #pragma omp parallel
- {
- #pragma omp sections
- {
- #pragma omp section
- {
- f(&p_value, 100000);
- }
- #pragma omp section
- {
- g(&p_value, 100000);
- }
- }
- }
- printf("P value is %d", p_value);
- }
Add Comment
Please, Sign In to add comment