Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created April 5, 2017 03:27
Show Gist options
  • Save kaityo256/210f9abcd3cd7696e2d19ab32ca2aa28 to your computer and use it in GitHub Desktop.
Save kaityo256/210f9abcd3cd7696e2d19ab32ca2aa28 to your computer and use it in GitHub Desktop.
Sample codes for schedule(dynamic)
/*
# 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");
}
@kaityo256
Copy link
Author

$ g++ -O2 -std=c++11 -fopenmp test.cpp -o a.out
$ OMP_NUM_THREADS=4 ./a.out 
(snip)
------------
0: total num = 9, total tasks = 8801
1: total num = 4, total tasks = 6444
2: total num = 3, total tasks = 6452
3: total num = 4, total tasks = 6043
------------
Total Tasks: 27740
Ideal Tasks per thread: 6935
Elapsed time [ms]: 8822
------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment