Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created April 5, 2017 09:59
Show Gist options
  • Save kaityo256/b369fe31beccee239f922cb0ff5589bd to your computer and use it in GitHub Desktop.
Save kaityo256/b369fe31beccee239f922cb0ff5589bd to your computer and use it in GitHub Desktop.
OpenMP sample with therad_local
/*
# 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");
}
@kaityo256
Copy link
Author

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

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