12 #define TRAINING_RUNS 1
25 const static int data = T;
32 template <
unsigned int dimens>
43 std::map<std::pair<size_t,size_t>, ImplType> m_data;
47 ExecPlanNew<1>() : calibrated(
false), idx(-1) {}
60 Point2D(
const Point2D& copy)
65 Point2D(
size_t _d1,
size_t _d2): dim1(_d1), dim2(_d2)
68 bool operator < (
const Point2D & p)
const
71 return (dim2 < p.dim2);
73 return (dim1 < p.dim1);
83 std::map< std::pair<Point2D, Point2D>, ImplType> m_data;
85 std::multimap< std::pair<Point2D, Point2D>, std::pair<Point2D, ImplType> > openPoints;
87 ExecPlanNew<2>() : isOpen(
false), calibrated(
false), idx(-1) {}
101 ExtraData(): actDimensions(-1), memUp(0), memDown(0)
114 ImpDetail(std::string _impName, ImplType _impType,
void (*_impPtr)(
void*)):impName(_impName), impType(_impType), impPtr(_impPtr)
120 void (*impPtr)(
void*);
129 TrainingData(std::vector<size_t> &_problemSize,
unsigned int _nImpls): problemSize(_problemSize), nImpls(_nImpls), extra(NULL), callBackFunction(NULL), callBackFunctionMapReduce(NULL)
131 assert(nImpls > 0 && nImpls <= MAX_EXEC_PLANS);
132 for(
size_t i=0; i<nImpls; ++i)
150 std::vector<size_t> &problemSize;
151 double exec_time[MAX_EXEC_PLANS];
152 void (*callBackFunction)(
void*,
size_t*,
unsigned int);
153 void (*callBackFunctionMapReduce)(
void*,
void*,
size_t*,
unsigned int);
169 Point(std::vector<size_t> _problemSize,
unsigned int _nImpls) : problemSize(_problemSize),
nImpls(_nImpls)
172 for(
unsigned int i=0; i<
nImpls; ++i)
174 cpuImpTime[i].second = -1;
175 ompImpTime[i].second = -1;
176 cudaImpTime[i].second = -1;
177 bestImpTime[i].second = -1;
178 cpuImpTime[i].first = NULL;
179 ompImpTime[i].first = NULL;
180 cudaImpTime[i].first = NULL;
181 bestImpTime[i].first = NULL;
184 std::vector<size_t> problemSize;
185 std::pair<ImpDetail*, double> cpuImpTime[MAX_EXEC_PLANS];
186 std::pair<ImpDetail*, double> ompImpTime[MAX_EXEC_PLANS];
187 std::pair<ImpDetail*, double> cudaImpTime[MAX_EXEC_PLANS];
188 std::pair<ImpDetail*, double> bestImpTime[MAX_EXEC_PLANS];
198 std::string getStringImplType(ImplType type)
219 unsigned int maxDepth;
220 unsigned int numLeafClosedRanges;
221 unsigned int numLeafTotalRanges;
222 unsigned int numTotalRanges;
223 unsigned int numTrainingPoints;
224 unsigned int numTrainingExec;
238 Node(std::vector<size_t> &_lowBounds, std::vector<size_t> &_uppBounds,
unsigned int _nImpls): lowBounds(_lowBounds), uppBounds(_uppBounds), level(0), father(NULL), id(-1), nImpls(_nImpls)
240 for(
int i=0; i<MAX_EXEC_PLANS; ++i)
242 bestNodeImp[i] = NULL;
248 std::vector<Point*> points;
249 std::vector<size_t> uppBounds;
250 std::vector<size_t> lowBounds;
253 std::vector<Node*> childs;
254 ImpDetail* bestNodeImp[MAX_EXEC_PLANS];
255 bool isClosed[MAX_EXEC_PLANS];
263 std::string padding =
"";
264 for(
unsigned int i=0; i<node.level; ++i)
266 os << padding <<
"\"" << node.level <<
"\" ( ";
267 for (
unsigned int i=0; i<node.lowBounds.size(); ++i)
269 if(i!= node.lowBounds.size()-1)
270 os << node.lowBounds[i] <<
",";
272 os << node.lowBounds[i];
275 for (
unsigned int i=0; i<node.uppBounds.size(); ++i)
277 if(i!= node.uppBounds.size()-1)
278 os << node.uppBounds[i] <<
",";
280 os << node.uppBounds[i];
282 for(
unsigned int i=0; i<node.nImpls; ++i)
289 assert(node.bestNodeImp[i] != NULL);
290 os <<
" " << getStringImplType(node.bestNodeImp[i]->impType) <<
" ";
295 if(i == (node.nImpls-1))
299 for(
unsigned int i=0; i<node.childs.size(); ++i)
301 os << *(node.childs[i]);
312 if(childs.empty() ==
false)
315 for(
unsigned int i=0; i<childs.size(); ++i)
322 for(
unsigned int i=0; i<points.size(); ++i)
330 for(
unsigned int i=0; i<points.size(); ++i)
339 template <
int dimens>
340 void constructExecPlanNew(ExecPlanNew<dimens> &plan, StatsTuner &stats,
int idx)
342 constructExecPlanNew(plan, stats, idx, identity<dimens>());
349 template <
unsigned int dimens>
350 void constructExecPlanNew(ExecPlanNew<dimens> &plan, StatsTuner &stats,
int idx, identity<dimens>)
355 void constructExecPlanNew(ExecPlanNew<1> &plan, StatsTuner &stats,
int idx, identity<1>)
357 assert(uppBounds.size() == lowBounds.size() && uppBounds.size() == 1);
359 stats.numTrainingPoints += 2;
360 stats.numTotalRanges++;
362 if(childs.empty() ==
false)
364 for(
unsigned int i=0; i<childs.size(); ++i)
367 childs[i]->constructExecPlanNew<1>(plan, stats, idx);
372 stats.numLeafTotalRanges++;
376 stats.numLeafClosedRanges++;
378 DEBUG_TUNING_LEVEL3(
"Closed space: " << points.size() <<
", " << lowBounds[0] <<
" - " << uppBounds[0] <<
"\n");
379 assert(bestNodeImp[idx] != NULL);
380 assert( plan.m_data.insert(std::make_pair(std::make_pair(lowBounds[0], uppBounds[0]), bestNodeImp[idx]->impType)).second ==
true);
384 assert(points.size() > 1);
385 DEBUG_TUNING_LEVEL3(
"Open space: " << points.size() <<
", " << lowBounds[0] <<
" - " << uppBounds[0] <<
"\n");
388 assert(points[0]->bestImpTime[idx].first != NULL && points[1]->bestImpTime[idx].first != NULL);
389 assert(points[0]->bestImpTime[idx].first->impType != points[1]->bestImpTime[idx].first->impType);
391 std::pair<ImplType, double> bestPoint1(points[0]->bestImpTime[idx].first->impType, points[0]->bestImpTime[idx].second);
392 std::pair<ImplType, double> bestPoint2(points[1]->bestImpTime[idx].first->impType, points[1]->bestImpTime[idx].second);
394 double secBestPoint1 = 0;
395 switch(points[1]->bestImpTime[idx].first->impType)
398 secBestPoint1 = points[0]->cpuImpTime[idx].second;
401 secBestPoint1 = points[0]->ompImpTime[idx].second;
404 secBestPoint1 = points[0]->cudaImpTime[idx].second;
410 std::pair<ImplType, double> secondBestPoint1(points[1]->bestImpTime[idx].first->impType, secBestPoint1);
412 double secBestPoint2 = 0;
413 switch(points[0]->bestImpTime[idx].first->impType)
416 secBestPoint2 = points[1]->cpuImpTime[idx].second;
419 secBestPoint2 = points[1]->ompImpTime[idx].second;
422 secBestPoint2 = points[1]->cudaImpTime[idx].second;
428 std::pair<ImplType, double> secondBestPoint2(points[0]->bestImpTime[idx].first->impType, secBestPoint2);
436 double A1 = secondBestPoint2.second - bestPoint1.second;
437 ssize_t B1 = lowBounds[0] - uppBounds[0];
438 double C1 = A1*(lowBounds[0]) + B1*(bestPoint1.second);
440 double A2 = bestPoint2.second - secondBestPoint1.second;
442 double C2 = A2*(lowBounds[0]) + B2*(secondBestPoint1.second);
444 double delta = A1*B2 - A2*B1;
448 size_t x = (size_t)((B2*C1 - B1*C2)/delta);
450 DEBUG_TUNING_LEVEL3(
"------------------------------------------\n");
451 DEBUG_TUNING_LEVEL3(
"(" << lowBounds[0] <<
"-" << x <<
") " << getStringImplType(bestPoint1.first) <<
"\n");
452 DEBUG_TUNING_LEVEL3(
"(" << (x+1) <<
"-" << uppBounds[0] <<
") " << getStringImplType(bestPoint2.first) <<
"\n");
453 DEBUG_TUNING_LEVEL3(
"------------------------------------------\n");
455 assert( plan.m_data.insert(std::make_pair(std::make_pair(lowBounds[0], x), bestPoint1.first)).second == true );
456 assert( plan.m_data.insert(std::make_pair(std::make_pair(x+1, uppBounds[0]), bestPoint2.first)).second == true );
460 if(plan.m_data.empty() ==
false)
461 plan.calibrated =
true;
481 Trainer(std::vector<ImpDetail*> &impls, std::vector<size_t> &lowerBounds, std::vector<size_t> &upperBounds,
unsigned int maxDepth,
unsigned int _nImpls,
ExtraData &extra,
void (*_callBack1)(
void*,
size_t*,
unsigned int),
void (*_callBack2)(
void*,
void*,
size_t*,
unsigned int),
bool Oversampling =
false);
484 template <
unsigned int dimens>
492 ExecPlanNew<1> newPlan;
493 std::pair< std::pair<size_t, size_t >, ImplType> prevBest;
495 for(std::map<std::pair<size_t,size_t>, ImplType>::iterator it = plan.m_data.begin(); it != plan.m_data.end(); ++it)
504 if(it->second == prevBest.second)
506 assert(prevBest.first.second = it->first.first-1);
507 prevBest.first.second = it->first.second;
511 assert(newPlan.m_data.insert(prevBest).second ==
true);
517 assert(newPlan.m_data.insert(prevBest).second ==
true);
519 newPlan.idx = plan.idx;
534 void train(Node* &rootNode,
bool discardFirst);
535 void generateSubSpaces(std::vector<size_t> &baseLowBound, std::vector<size_t> &baseUppBound, Node *father);
536 void generateAllPossibleCombinations(std::vector<size_t> &lowBound, std::vector<size_t> &uppBound, std::vector<std::vector<size_t> > &combinations,
bool overSample);
538 std::vector<size_t> &m_lowerBounds;
539 std::vector<size_t> &m_upperBounds;
540 unsigned int m_maxDepth;
543 std::vector<ImpDetail*> &m_impls;
544 void (*callBackFunction)(
void*,
size_t*,
unsigned int);
545 void (*callBackFunctionMapReduce)(
void*,
void*,
size_t*,
unsigned int);
555 #include "trainer.inl"
Definition: trainer.h:235
Trainer(std::vector< ImpDetail * > &impls, std::vector< size_t > &lowerBounds, std::vector< size_t > &upperBounds, unsigned int maxDepth, unsigned int _nImpls, ExtraData &extra, void(*_callBack1)(void *, size_t *, unsigned int), void(*_callBack2)(void *, void *, size_t *, unsigned int), bool Oversampling=false)
Definition: trainer.inl:69
Definition: trainer.h:166
void compressExecPlanNew(ExecPlanNew< 1 > &plan)
Definition: trainer.h:490
void constructExecPlanNew(ExecPlanNew< dimens > *plan, StatsTuner &stats)
Definition: trainer.inl:103
end Node class...
Definition: trainer.h:478
unsigned int nImpls
Definition: trainer.h:191
void train()
Definition: trainer.inl:78
Contains a class declaration for Environment class.
~Node()
Definition: trainer.h:310
friend std::ostream & operator<<(std::ostream &os, const Node &node)
Definition: trainer.h:261