-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_async.cpp
207 lines (180 loc) · 5.76 KB
/
using_async.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <chrono>
#include <exception>
#include <functional>
#include <future>
#include <iostream>
#include <random>
template <typename T>
T element_if_all_equal(std::vector<T> v)
{
//if the vector is emty we cannot return an element
//this probably should be handled by the caller
if (v.size() == 0)
throw(std::runtime_error("comparing on empty vector"));
//if all elements are equal we return the first element
//TODO how do we treat the case where elemets are not equal?
if (std::equal(v.begin() + 1, v.end(), v.begin()))
{
return v[0];
}
else
{
//runtime error waiting to happen!
return -1;
}
}
class slsDetector
{
public:
slsDetector(){};
double exposureTime(double t)
{
// Code to sleep a random number of seconds, simulating work or network delay
// just to show that functions are run in parallel
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(0, 5);
auto sleep_time = uni(rng);
std::cout << det_id_ << ":slsDetector::exposureTime(" << t << ") --> sleep for " << sleep_time << "\n";
std::this_thread::sleep_for(std::chrono::seconds(sleep_time));
std::cout << det_id_ << ":done\n";
//set exposure time
exposureTime_ = t;
return exposureTime();
}
double exposureTime()
{
return exposureTime_;
}
double someOtherParameter(int i, double v)
{
std::cout << det_id_ << ":slsDetector::someOtherParameter(" << i << ", " << v << ")\n";
//throw to have something to catch
if (det_id_ == 2)
throw(std::runtime_error("Detector " + std::to_string(det_id_) + " has a problem"));
return static_cast<double>(i);
}
void do_something()
{
std::cout << det_id_ << ":slsDetector::do_something()\n";
}
size_t id()
{
return det_id_;
}
void id(size_t det_id)
{
det_id_ = det_id;
}
private:
double exposureTime_;
size_t det_id_;
};
class multiSlsDetector
{
public:
multiSlsDetector(size_t N)
: n_modules_(N)
{
//initialize detectors, this should probably be done using vectors or unique pointer
//but using normal pointers for similarity with the current implementation
slsDetector_ = new slsDetector[N];
for (size_t i = 0; i < N; ++i)
(*this)[i]->id(i);
}
~multiSlsDetector()
{
delete[] slsDetector_;
}
slsDetector *operator[](size_t i)
{
//Providing access to detectors with range checking
//throw exception if out of range
if (i < n_modules_)
return &slsDetector_[i];
else
throw(std::range_error("Detector does not exist"));
}
size_t n_modules() { return n_modules_; }
double exposureTime(double t)
{
// static cast to resolve overloaded functions
// using can be used to shorten typenames
// using tt = double(slsDetector::*)(double);
auto v = parallel_call(static_cast<double (slsDetector::*)(double)>(&slsDetector::exposureTime), t);
return element_if_all_equal(v);
}
double exposureTime()
{
auto v = parallel_call(static_cast<double (slsDetector::*)()>(&slsDetector::exposureTime));
return element_if_all_equal(v);
}
//overload from multi to set exposure time of a single detector
//I tend to prefer overloads to if statements in a bigger function
//but that is mostly style
double exposureTime(double t, size_t det_id)
{
return (*this)[det_id]->exposureTime(t);
}
void someOtherParameter(int i, double d){
parallel_call(&slsDetector::someOtherParameter, i, d);
}
void do_something(){
parallel_call(&slsDetector::do_something);
}
// Template for calling a member function for each slsDetector
// uses async to do this in parallel. Accepts variable number of arguments
// returns a vector of the functions return type
template <typename... CT, typename RT>
std::vector<RT> parallel_call(RT (slsDetector::*somefunc)(CT...), CT... Args)
{
std::vector<std::future<RT>> futures;
for (size_t det_id = 0; det_id < n_modules_; ++det_id)
{
futures.push_back(std::async(somefunc, (*this)[det_id], Args...));
}
std::vector<RT> result;
for (auto &i : futures)
result.push_back(i.get());
return result;
}
// Overload for functions that does not have a return type
// vector of void would not be so good...
template <typename... CT>
void parallel_call(void (slsDetector::*somefunc)(CT...), CT... Args)
{
std::vector<std::future<void>> futures;
for (size_t det_id = 0; det_id < n_modules_; ++det_id)
{
futures.push_back(std::async(somefunc, (*this)[det_id], Args...));
}
for (auto &i : futures)
i.get();
}
private:
slsDetector *slsDetector_;
size_t n_modules_;
};
int main()
{
multiSlsDetector d{5};
//Set the exposure time of all detectors
d.exposureTime(7.9);
auto t = d.exposureTime();
std::cout << "Got: " << t << "\n";
//Set the exposure time of a single detector
d.exposureTime(5.36, 2);
//calling a function that returns void
d.do_something();
//call an function where one module throws an exception
//the exception is propagated from the thread that async launched
//and caught at the top level. We can implement a class of exception
//that carries information about wich detector etc.
try{
d.someOtherParameter(5, 4.4);
}
catch(std::runtime_error& e){
std::cout << "Caught: " << e.what() << "\n";
}
return 0;
}