Created
April 5, 2017 03:27
-
-
Save kaityo256/210f9abcd3cd7696e2d19ab32ca2aa28 to your computer and use it in GitHub Desktop.
Sample codes for schedule(dynamic)
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; | |
| const int id; | |
| public: | |
| Worker(int i) : id(i) {} | |
| 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); | |
| #pragma omp parallel for | |
| for (int i = 0; i < nthreads; i++) { | |
| workers[i].reset(new Worker(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++) { | |
| int id = omp_get_thread_num(); | |
| workers[id]->work(tasks[i]); | |
| } | |
| printf("------------\n"); | |
| for (int i = 0; i < nthreads; i++) { | |
| workers[i]->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