Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
LPTpartitioner.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MODEL is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef _model_LPTpartitioner_h_
9#define _model_LPTpartitioner_h_
10
11#include <map>
12#include <vector>
13#include <deque>
14#include <algorithm> // std::min_element, std::max_element
15#include <assert.h>
16#include <iostream>
17#include <iomanip>
18
19namespace model {
20
21
25 template <typename T>
27 /* inheritance */ private std::multimap<double,T*>,
28 /* inheritance */ private std::vector<std::deque<T*> >,
29 /* inheritance */ private std::vector<double>
30 {
31
32 public:
33
34 typedef std::multimap<double,T*> BaseMapType;
35 typedef std::deque<T*> BaseDequeType;
36 typedef std::vector<BaseDequeType> BaseDequeVectorType;
37 typedef std::vector<double> BaseWeightVectorType;
38
39
40 static bool partitionIsValid;
41
42
43 void clear()
44 {
45 BaseMapType::clear();
46 BaseDequeVectorType::clear();
47 BaseWeightVectorType::clear();
48 partitionIsValid=false;
49 }
50
51 /**********************************************************************/
52 void insert(const double& w,T* pT)
53 {
54 assert(w>=0 && "WEIGHT MUST BE POSITIVE");
55 BaseMapType::insert(std::make_pair(w,pT));
56 partitionIsValid=false;
57 }
58
59 /**********************************************************************/
60 void partition(const int& nProc)
61 {
62 // Resize
63 BaseDequeVectorType::resize(nProc);
64 BaseWeightVectorType::resize(nProc,0.0);
65
66 // Traverse BaseMapType in descending order (highest to lowest weight)
67 for (typename BaseMapType::const_reverse_iterator rIter =BaseMapType::rbegin();
68 /* */ rIter!=BaseMapType::rend();
69 /* */ ++rIter)
70 {
71 // Compute the index of minimum weight in BaseWeightVectorType
72 const size_t argMin(min_element(BaseWeightVectorType::begin(), BaseWeightVectorType::end())-BaseWeightVectorType::begin());
73 // Add the current weigth to the bin of minimum weigth
74 BaseWeightVectorType::operator[](argMin)+=rIter->first;
75 // Add the current pointer to the bin of minimum weight
76 BaseDequeVectorType::operator[](argMin).push_back(rIter->second);
77 }
78
80
81 }
82
83 /**********************************************************************/
84 double totalWeight() const
85 {
86 double temp(0.0);
87 for (unsigned int k=0;k<BaseWeightVectorType::size();++k)
88 {
89 temp+=weight(k);
90 }
91 return temp;
92 }
93
94
95 /**********************************************************************/
96 size_t bins() const
97 {
98 return BaseDequeVectorType::size();
99 }
100
101 /**********************************************************************/
102 const BaseDequeType& bin(const int& k) const
103 {
104 return BaseDequeVectorType::operator[](k);
105 }
106
107 /**********************************************************************/
108 const double& weight(const int& k) const
109 {
110 return BaseWeightVectorType::operator[](k);
111 }
112
113 /**********************************************************************/
114 void show() const
115 {
116
117 std::cout<<"Processor loads [%]: ";
118 const double wT(totalWeight());
119 for (unsigned int k=0;k<BaseWeightVectorType::size();++k)
120 {
121 double wr(weight(k)/wT);
122
123 std::cout<<std::fixed << std::setprecision(2)<<wr*100.0<<" ";
124
125// std::cout<<"processor "<<std::setw(3)<<k<<std::setw(5)<<" ("<<wr*100<<"%): ";
126// int nC(wr*20*BaseWeightVectorType::size());
127// for (int c=0;c<nC;++c)
128// {
129// std::cout<<"*";
130// }
131// std::cout<<"\n";
132 }
133 std::cout<<std::endl;
134
135 }
136
137
138 };
139
140 // declare static data
141 template <typename T>
143
144
145} // end namespace
146#endif
Template implementation of the Longest Processing Time (LPT) algorithm for task partiotioning.
std::vector< BaseDequeType > BaseDequeVectorType
std::deque< T * > BaseDequeType
void partition(const int &nProc)
void insert(const double &w, T *pT)
const BaseDequeType & bin(const int &k) const
const double & weight(const int &k) const
std::multimap< double, T * > BaseMapType
std::vector< double > BaseWeightVectorType
double totalWeight() const
static bool partitionIsValid
REAL w[16]
int k
vertex temp