/
pm_cluster_level_scheduler.hpp
371 lines (285 loc) · 12 KB
/
pm_cluster_level_scheduler.hpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#pragma once
#ifndef idC49A2D93_44C9_41C1_BFCE81120109B873
#define idC49A2D93_44C9_41C1_BFCE81120109B873
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
Mahder.Gebremedhin@liu.se 2014-03-13
*/
#include "gc.h"
#include <tbb/parallel_for.h>
#include <tbb/tick_count.h>
// #include <sys/types.h>
// #include <sys/syscall.h>
#include "pm_clustering.hpp"
namespace openmodelica { namespace parmodelica {
template <typename TaskType>
struct TBBConcurrentStepExecutor {
typedef TaskSystem_v2<TaskType> TaskSystemType;
typedef typename TaskSystemType::GraphType GraphType;
typedef typename TaskSystemType::ClusterType ClusterType;
typedef typename TaskSystemType::ClusterIdType ClusterIdType;
typedef typename TaskSystemType::ClusterLevels ClusterLevels;
typedef typename ClusterLevels::value_type SameLevelClusterIdsType;
typedef typename SameLevelClusterIdsType::iterator ClusteIdIter;
private:
GraphType& sys_graph;
public:
TBBConcurrentStepExecutor(GraphType& g) : sys_graph(g) {}
void operator()(tbb::blocked_range<ClusteIdIter>& range) const {
// pid_t id;
// /* Register thread to bohem GC if it is not registered already*/
// if(!GC_thread_is_registered()) {
// id = syscall(SYS_gettid);
// fprintf(stderr,"Found unregisterd thread = %d \n", id);
// struct GC_stack_base sb;
// memset (&sb, 0, sizeof(sb));
// GC_get_stack_base(&sb);
// GC_register_my_thread (&sb);
// // std::cerr << "New Theread registerd = " << GC_thread_is_registered() << std::endl;
// }
// else {
// id = syscall(SYS_gettid);
// if(!knownthreads.count(id)) {
// fprintf(stderr,"parmod registerd thread = %d \n", id);
// knownthreads.insert(id);
// }
// }
for (ClusteIdIter clustid_iter = range.begin(); clustid_iter != range.end(); ++clustid_iter) {
ClusterIdType& curr_clust_id = *clustid_iter;
ClusterType& curr_clust = sys_graph[curr_clust_id];
curr_clust.execute();
}
}
};
template <typename TaskType,
typename clustetring1 = cluster_merge_common, /* for now default here*/
typename clustetring2 = cluster_merge_level_for_bins, /* for now default here*/
typename clustetring3 = cluster_none,
typename clustetring4 = cluster_none,
typename clustetring5 = cluster_none>
class StepLevels : boost::noncopyable {
public:
typedef TaskSystem_v2<TaskType> TaskSystemType;
typedef typename TaskSystemType::GraphType GraphType;
typedef typename TaskSystemType::ClusterType ClusterType;
typedef typename TaskSystemType::ClusterIdType ClusterIdType;
typedef typename TaskSystemType::ClusterLevels ClusterLevels;
typedef typename ClusterLevels::value_type SameLevelClusterIdsType;
public:
size_t max_num_threads;
const TaskSystemType& task_system_org;
TaskSystemType task_system;
TBBConcurrentStepExecutor<TaskType> step_executor;
bool profiled;
bool schedule_available;
int total_evaluations;
int parallel_evaluations;
int sequential_evaluations;
double par_avg_at_last_sch;
double par_current_avg;
double total_parallel_cost;
bool has_run_parallel;
public:
PMTimer execution_timer;
PMTimer clustering_timer;
PMTimer step_timer;
std::vector<double> parallel_eval_costs;
StepLevels(TaskSystemType& ts, size_t mnt)
: max_num_threads(mnt)
, task_system_org(ts)
, task_system("invalid", mnt) // implement a constrctor with no parameters and remove this
, step_executor(task_system.sys_graph) {
// GC_allow_register_threads();
// GC_use_threads_discovery();
profiled = false;
schedule_available = false;
total_evaluations = 0;
parallel_evaluations = 0;
sequential_evaluations = 0;
total_parallel_cost = 0;
par_avg_at_last_sch = 0;
has_run_parallel = false;
}
bool avg_needs_reschedule() {
double diff = std::abs(par_avg_at_last_sch - par_current_avg);
double change = diff / par_avg_at_last_sch;
if (change > 0.5) {
// std::cout << "Reschedule needed P: " << par_avg_at_last_sch << " :C: " << par_current_avg << std::endl;
return true;
}
return false;
}
bool reschedule_needed() {
if (!this->schedule_available)
return true;
if (this->avg_needs_reschedule())
return true;
return false;
}
void clear_schedule() {
task_system = task_system_org;
profiled = false;
schedule_available = false;
}
void execute_and_schedule() {
clear_schedule();
profile_execute();
schedule();
par_avg_at_last_sch = par_current_avg;
}
void schedule() {
clustering_timer.start_timer();
if (task_system.levels_valid == false)
task_system.update_node_levels();
clustetring1::apply(task_system);
clustetring1::dump_graph(task_system);
clustetring2::apply(task_system);
clustetring2::dump_graph(task_system);
clustetring3::apply(task_system);
clustetring3::dump_graph(task_system);
clustetring4::apply(task_system);
clustetring4::dump_graph(task_system);
clustetring5::apply(task_system);
clustetring5::dump_graph(task_system);
schedule_available = true;
task_system.levels_valid = false;
task_system.update_node_levels();
estimate_speedup();
clustering_timer.stop_timer();
}
void execute() {
if (this->reschedule_needed())
return execute_and_schedule();
// paranoia
if (task_system.levels_valid == false)
exit(1);
execution_timer.start_timer();
step_timer.start_timer();
typename ClusterLevels::iterator level_iter = task_system.clusters_by_level.begin();
/*! Skip the first level. Which contains only the root node */
++level_iter;
int level_number = 1;
for (; level_iter != task_system.clusters_by_level.end(); ++level_iter, ++level_number) {
SameLevelClusterIdsType& current_level = *level_iter;
// if(current_level.level_cost > 0.009) {
tbb::parallel_for(tbb::blocked_range<typename SameLevelClusterIdsType::iterator>(current_level.begin(),
current_level.end()),
step_executor);
// }
// else {
// typename SameLevelClusterIdsType::iterator clustid_iter = current_level.begin();
// for( ;clustid_iter != current_level.end(); ++clustid_iter) {
// ClusterIdType& curr_clust_id = *clustid_iter;
// ClusterType& curr_clust = sys_graph[curr_clust_id];
// curr_clust.execute();
// }
// }
}
execution_timer.stop_timer();
step_timer.stop_timer();
++this->total_evaluations;
++this->parallel_evaluations;
// if(total_evaluations%100 == 0) {
double step_cost = step_timer.get_elapsed_time();
parallel_eval_costs.push_back(step_cost);
total_parallel_cost += step_cost;
par_current_avg = total_parallel_cost / this->parallel_evaluations;
// std::cout << total_evaluations << " : " << parallel_evaluations << " : " << step_cost << " : " <<
// par_current_avg << std::endl; std::cout << "P" << " : " << total_evaluations << " : " << step_cost << " :
// "<< par_current_avg << std::endl;
step_timer.reset_timer();
// }
if (!has_run_parallel) {
par_avg_at_last_sch = par_current_avg;
has_run_parallel = true;
}
}
void profile_execute() {
// if(this->total_evaluations == 0)
// std::cout << "Type" << " : " << "Eval" << " : " << "Eval_cost" << " : "<< "Curr_Par_Avg" << " : " <<
// "Prev_Sch_Avg"<< std::endl;
GraphType& sys_graph = task_system.sys_graph;
typename GraphType::vertex_iterator vert_iter, vert_end;
boost::tie(vert_iter, vert_end) = vertices(sys_graph);
execution_timer.start_timer();
step_timer.start_timer();
/*! skip the root node. */
++vert_iter;
for (; vert_iter != vert_end; ++vert_iter) {
sys_graph[*vert_iter].profile_execute();
}
++this->total_evaluations;
++this->sequential_evaluations;
step_timer.stop_timer();
execution_timer.stop_timer();
double step_cost = step_timer.get_elapsed_time();
// utility::log("") << "Profiled on step :" << this->total_evaluations << " cost: " << step_cost << std::endl;
std::cout << "S"
<< " : " << this->total_evaluations << " : " << step_cost << " : " << par_current_avg << " : "
<< par_avg_at_last_sch << std::endl;
step_timer.reset_timer();
// task_system.dump_graphml("profiled_" + std::to_string(this->total_evaluations));
this->profiled = true;
}
void estimate_speedup() {
if (task_system.levels_valid == false)
task_system.update_node_levels();
GraphType& sys_graph = task_system.sys_graph;
double total_level_scheduler_cost = 0;
double total_system_cost = 0;
typename ClusterLevels::iterator level_iter = task_system.clusters_by_level.begin();
/*! Skip the first level. Which contains only the root node and some invlaidated clusters.*/
++level_iter;
int level_number = 1;
for (; level_iter != task_system.clusters_by_level.end(); ++level_iter, ++level_number) {
SameLevelClusterIdsType& current_level = *level_iter;
cluster_cost_comparator_by_id<GraphType> cccbi(sys_graph);
// sort in decreasing order
std::sort(current_level.rbegin(), current_level.rend(), cccbi);
total_level_scheduler_cost += sys_graph[current_level.front()].cost;
total_system_cost += current_level.total_level_cost;
}
// utility::log("") << "Total_system_cost: " << total_system_cost << std::endl;
// utility::log("") << "Total_level_scheduler_cost: " << total_level_scheduler_cost << std::endl;
// utility::log("") << "Ideal speedup: " << total_system_cost/total_level_scheduler_cost << std::endl;
}
};
/*! The default level scheduler uses these two clusterings*/
// template<typename Tasktype>
// using LevelScheduler = StepLevels<TaskType
// , cluster_merge_common
// , cluster_merge_level_for_bins
// >;
template <typename TaskType>
struct LevelScheduler : StepLevels<TaskType, cluster_merge_common, cluster_merge_level_for_bins> {};
}} // namespace openmodelica::parmodelica
#endif // header