-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.cpp
87 lines (66 loc) · 1.82 KB
/
job.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include "job.h"
#include <omp.h>
#include <cstdlib>
IJob::IJob(uint numThread, uint64_t numTask) : numThread(numThread), numTask(numTask) {}
auto IJob::run() -> void const
{
start = std::chrono::system_clock::now();
run_intern();
end = std::chrono::system_clock::now();
}
auto IJob::elapsedTime() -> double const
{
return (std::chrono::duration<double>(end - start)).count();
}
OmpJob::OmpJob(uint numThread, uint64_t numTask): IJob(numThread, numTask) { }
auto OmpJob::run_intern() -> void const
{
#pragma omp parallel num_threads((int)numThread)
{
auto threadId = omp_get_thread_num();
for (auto i = 0; i< numTask; ++i) {
if ((i % 100) == 0)
printf("%d ", i);
#pragma omp barrier
if (threadId == 0 && (i % 100) == 0)
printf("\n");
#pragma omp barrier
}
}
}
StdJob::StdJob(std::shared_ptr<IBarrier> barrier, uint numThread, uint64_t numTask): barrier(barrier), IJob(numThread, numTask) {
barrier->setNumTreads(numThread);
}
auto StdJob::work(uint threadId) -> void const
{
for (auto i = 0; i < numTask; ++i) {
if ((i % 100) == 0)
printf("%u ", i);
barrier->barrier();
if (threadId == 0 && (i % 100) == 0)
printf("\n");
barrier->barrier();
}
}
auto StdJob::work2(uint threadId, std::shared_ptr<IBarrier> barrier) -> void const
{
for (auto i = 0; i < numTask; ++i) {
if ((i % 100) == 0)
printf("%u ", i);
barrier->barrier();
if (threadId == 0 && (i % 100) == 0)
printf("\n");
barrier->barrier();
}
}
auto StdJob::run_intern() -> void const
{
std::vector<std::thread> workers;
workers.reserve(numThread); //reserve memory, capacity == nThreads
for (auto i = 0; i < numThread; ++i)
workers.push_back(std::thread(&StdJob::work, this, i));
//workers.push_back(std::thread(&StdJob::work2, this, i, std::ref(barrier)));
for(auto &worker: workers)
worker.join();
}