Created
April 5, 2017 09:59
-
-
Save kaityo256/b369fe31beccee239f922cb0ff5589bd to your computer and use it in GitHub Desktop.
OpenMP sample with therad_local
This file contains hidden or 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
| /* | |
| # Copyright H. Watanabe 2017 | |
| # Distributed under the Boost Software License, Version 1.0. | |
| # (See accompanying file LICENSE_1_0.txt or copy at | |
| # http://www.boost.org/LICENSE_1_0.txt) | |
| */ | |
| #include <iostream> | |
| #include <stdio.h> | |
| #include <vector> | |
| #include <chrono> | |
| #include <random> | |
| #include <algorithm> | |
| #include <numeric> | |
| #include <omp.h> | |
| #include <memory> | |
| class Worker { | |
| private: | |
| std::vector<double> mytask; | |
| public: | |
| int id; | |
| void work(int task) { | |
| mytask.push_back(task); | |
| printf("%d: %d\n", id, task); | |
| auto start = std::chrono::system_clock::now(); | |
| while (true) { | |
| auto end = std::chrono::system_clock::now(); | |
| auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); | |
| if (diff > task)break; | |
| } | |
| } | |
| void show_summary(void) { | |
| int sum = std::accumulate(mytask.begin(), mytask.end(), 0); | |
| printf("%d: total num = %d, total tasks = %d\n", id, mytask.size(), sum); | |
| } | |
| }; | |
| int | |
| main(void) { | |
| int nthreads = 0; | |
| #pragma omp parallel | |
| { | |
| nthreads = omp_get_num_threads(); | |
| } | |
| std::vector <std::unique_ptr<Worker> > workers(nthreads); | |
| thread_local Worker worker; | |
| #pragma omp parallel for | |
| for (int i = 0; i < nthreads; i++) { | |
| worker.id = i; | |
| } | |
| std::mt19937 mt; | |
| std::uniform_int_distribution<int> ud(0, 1000); | |
| std::vector<int> tasks; | |
| const int N = 20; | |
| for (int i = 0; i < N; i++) { | |
| int task = ud(mt); | |
| if (task > 900) { | |
| task *= 3; | |
| } | |
| tasks.push_back(task); | |
| std::cout << task << std::endl; | |
| } | |
| auto start = std::chrono::system_clock::now(); | |
| #pragma omp parallel for schedule(dynamic) | |
| for (int i = 0; i < N; i++) { | |
| worker.work(tasks[i]); | |
| } | |
| printf("------------\n"); | |
| #pragma omp parallel for | |
| for (int i = 0; i < nthreads; i++) { | |
| worker.show_summary(); | |
| } | |
| printf("------------\n"); | |
| auto end = std::chrono::system_clock::now(); | |
| int sum = std::accumulate(tasks.begin(), tasks.end(), 0); | |
| double ideal_task = static_cast<double>(sum) / static_cast<double>(nthreads); | |
| auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); | |
| printf("Total Tasks: %d\n", sum); | |
| printf("Ideal Tasks per thread: %d\n", static_cast<int>(ideal_task)); | |
| printf("Elapsed time [ms]: %d\n", diff); | |
| printf("------------\n"); | |
| } |
Author
kaityo256
commented
Apr 5, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment