Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <omp.h>
- #include <iostream>
- #include <windows.h>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- void func()
- {
- for (int i = 0; i < 500000; i++)
- {
- rand();
- }
- }
- int main()
- {
- omp_set_num_threads(3);
- #pragma omp parallel for
- for (int i = 0; i < 100; i++)
- {
- cout << i;
- func();
- }
- #pragma omp parallel
- {
- #pragma omp sections nowait
- {
- cout << endl << "Stroka" << omp_get_num_threads() << endl;
- #pragma omp section
- for (int i = 0; i < 5; i++)
- {
- cout << 1;
- func();
- }
- #pragma omp section
- for (int i = 0; i < 5; i++)
- {
- cout << 2;
- func();
- }
- } //закрытие секции новайт
- #pragma omp barrier //код завершенной секции завершил параллельную работу
- for (int i = 0; i < 10; i++)
- {
- cout << 3;
- func();
- }
- }
- cout << "\n\n\n";
- #pragma omp parallel num_threads(3)
- {
- #pragma omp single //всегда выполняется в самом 1 потоке
- // Only a single thread can read the input.
- cout << omp_get_thread_num() << " first single " << endl;
- // Multiple threads in the team compute the results.
- cout << omp_get_thread_num() << " main " << endl;
- #pragma omp single
- //Only a single thread can write the output.
- cout << omp_get_thread_num() << " sec single " << endl;
- }
- int a[5], i;
- #pragma omp parallel num_threads(3)
- {
- // Perform some computation.
- #pragma omp for
- for (i = 0; i < 5; i++) a[i] = i * i;
- // Print intermediate results.
- #pragma omp master
- for (i = 0; i < 5; i++) printf_s("a[%d] = %d\n", i, a[i]);
- // Wait.
- #pragma omp barrier
- // Continue with the computation.
- #pragma omp for
- for (i = 0; i < 5; i++) a[i] += i;
- }
- cout << "\n\n\n";
- // omp_init_lock(&my_lock);
- //
- //#pragma omp parallel num_threads(4)
- // {
- // int tid = omp_get_thread_num();
- // int i, j;
- // /* omp_set_lock(&my_lock);*/
- // for (i = 0; i < 5; ++i) {
- // omp_set_lock(&my_lock);
- // omp_test_lock(&my_lock);
- // printf_s("Thread %d - starting locked region\n", tid);
- // printf_s("Thread %d - ending locked region\n", tid);
- // omp_unset_lock(&my_lock);
- // }
- // omp_unset_lock(&my_lock);
- // }
- // omp_destroy_lock(&my_lock);
- //
- // cout << endl << endl << endl;
- // omp_init_lock(&my_lock);
- //#pragma omp parallel num_threads(4)
- // {
- // int tid = omp_get_thread_num();
- // int i, j;
- // omp_set_lock(&my_lock);
- // for (i = 0; i < 5; ++i)
- // {
- // omp_test_lock(&my_lock);
- // printf_s("Thread %d - starting locked region\n", tid);
- // printf_s("Thread %d - ending locked region\n", tid);
- // omp_unset_lock(&my_lock);
- // }
- // }
- //
- // omp_destroy_lock(&my_lock);
- omp_lock_t my_lock;
- omp_init_lock(&my_lock);
- #pragma omp parallel num_threads(4)
- {
- int tid = omp_get_thread_num();
- int i, j;
- omp_set_lock(&my_lock);
- for (i = 0; i < 5; ++i)
- {
- printf_s("Thread %d - starting locked region\n", tid);
- printf_s("Thread %d - ending locked region\n", tid);
- omp_unset_lock(&my_lock);
- }
- }
- omp_destroy_lock(&my_lock);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement