00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <twod/m_way_probe.hpp>
00038
00039 template <typename T, typename Pr,
00040 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, Pr, false>& prefixSumArray, int length, int *cutIndexes, T),
00041 T (*onedalgoX)(int procCount, const util::Aggreg2Dto1D<T, Pr, true>& prefixSumArray, int length, int *cutIndexes, T)>
00042 T twod::JagMHeurProbeHor<T,Pr,onedalgoY,onedalgoX>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00043 {
00044 using namespace std;
00045
00046 twod::PartBase<T, Pr >* pb = NULL;
00047 T ub = (pb = new JagMHeurHor<T,Pr,onedalgoY,onedalgoX>())->part(procCount, prefixSumArray, parts);
00048 delete pb;
00049
00050 int *firstDimCuts = new int[procCount+1];
00051
00052 int procX=util::extractXDim<T,Pr>(parts, firstDimCuts);
00053
00054 int *procs = new int[procX];
00055 int **cuts = new int*[procX];
00056 int *lengths = new int[procX];
00057 for(int i = 0; i < procX;i++)
00058 {
00059 cuts[i] = new int[procCount];
00060 lengths[i] = prefixSumArray.prefixsizeY();
00061
00062
00063 }
00064
00065 util::Aggreg2Dto1D<T, Pr, false> **rows = new util::Aggreg2Dto1D<T, Pr, false>*[procX];
00066
00067
00068 int top = 1;
00069
00070 for(int i=1;i<=procX;i++)
00071 {
00072
00073 rows[i-1] = new util::Aggreg2Dto1D<T, Pr, false>(prefixSumArray, top, firstDimCuts[i], 1, prefixSumArray.prefixsizeY()-1);
00074
00075 top = firstDimCuts[i] + 1;
00076 }
00077 #ifndef NDEBUG
00078 {
00079 T total=0;
00080 for(int i=0;i<procX;i++)
00081 {
00082 total += (*rows[i])[prefixSumArray.prefixsizeY()-1];
00083 }
00084 assert(total==prefixSumArray[prefixSumArray.prefixsizeX()-1][prefixSumArray.prefixsizeY()-1]);
00085 }
00086 #endif
00087 util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > arrs((const util::Aggreg2Dto1D<T, Pr, false>**)rows, lengths, procX);
00088
00089 T lb = multiarraypart(procCount, arrs, ub, procs, cuts);
00090
00091
00092
00093 parts.clear();
00094 top = 1;
00095 for(int i = 0; i< procX; i++)
00096 {
00097 int left = 1;
00098 for(int j = 0; j<procs[i]-1;j++)
00099 {
00100
00101 util::rectangle r(top,firstDimCuts[i+1],left,cuts[i][j]);
00102 parts.add_rect(r);
00103 left = cuts[i][j] + 1;
00104 }
00105 util::rectangle r2(top,firstDimCuts[i+1],left,prefixSumArray.prefixsizeY()-1);
00106 parts.add_rect(r2);
00107 top = firstDimCuts[i+1] + 1;
00108 }
00109
00110
00111 delete [] firstDimCuts;
00112 delete [] lengths;
00113 delete [] procs;
00114 for(int i = 0; i < procX;i++)
00115 {
00116 delete [] cuts[i];
00117 delete rows[i];
00118 }
00119 delete [] cuts;
00120 delete [] rows;
00121 return lb;
00122 }
00123
00124
00125 template <typename T, typename Pr,
00126 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, Pr, false>& prefixSumArray, int length, int *cutIndexes, T),
00127 T (*onedalgoX)(int procCount, const util::Aggreg2Dto1D<T, Pr, true>& prefixSumArray, int length, int *cutIndexes, T)>
00128 T twod::JagMHeurProbeHor<T,Pr,onedalgoY,onedalgoX>::multiarraypart(int procCount, const util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > & arrs, T ub, int* procs, int** oricuts)
00129 {
00130 const bool debug=false;
00131
00132 T lb = 0;
00133 int procX = arrs.arrayCount();
00134
00135 int **lbar = new int*[procX];
00136 int **ubar = new int*[procX];
00137 int **cuts = new int*[procX];
00138
00139
00140 for(int i = 0; i < procX;i++)
00141 {
00142 ubar[i] = new int[procCount];
00143 lbar[i] = new int[procCount];
00144 cuts[i] = new int[procCount];
00145 for(int j = 0; j < procCount; j++)
00146 {
00147 lbar[i][j] = 1;
00148 ubar[i][j] = arrs.getLength(i)-1;
00149 }
00150 }
00151
00152 assert((util::MProbe<T,util::Aggreg2Dto1D<T, Pr, false>, util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > >::probe(ub, arrs, procCount, lbar, ubar, procs, cuts)) == true);
00153
00154 int i=0;
00155 int startinterval = 1;
00156
00157 while (i<procX)
00158 {
00159 if (debug)
00160 std::cerr<<"array : "<<i<<" startinterval: "<<startinterval<<std::endl;
00161
00162 assert((util::MProbe<T,util::Aggreg2Dto1D<T, Pr, false>, util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > >::probe(ub, arrs, procCount, lbar, ubar, procs, cuts)) == true);
00163
00164
00165 int lowbound = startinterval;
00166 int highbound = arrs.getLength(i)-1;
00167
00168 bool lastinterval;
00169
00170 if (startinterval >= highbound)
00171 {
00172 lastinterval = true;
00173 }
00174 else
00175 {
00176 int middle = highbound;
00177 T bmid = arrs[i][middle] - arrs[i][startinterval-1];
00178 bool ok;
00179 if (bmid <= lb)
00180 ok = false;
00181 else if (bmid >= ub)
00182 ok = true;
00183 else
00184 {
00185 ok = util::MProbe<T,util::Aggreg2Dto1D<T, Pr, false>, util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > >::probe(bmid, arrs, procCount, lbar, ubar, procs, cuts);
00186 if(ok)
00187 {
00188 ub = bmid;
00189 }
00190 else
00191 {
00192 lb = bmid;
00193 }
00194 }
00195 lastinterval = !ok;
00196 }
00197
00198
00199 if (lastinterval)
00200 {
00201 if (debug)
00202 std::cerr<<"changing interval"<<std::endl;
00203 startinterval = 1;
00204 i++;
00205 }
00206 else
00207 {
00208 while (lowbound < highbound)
00209 {
00210 if (debug)
00211 std::cerr<<"searching for end of the interval : [ "<<lowbound<<" ; "<<highbound<<" ]"<<std::endl;
00212 int middle = (highbound+lowbound)/2;
00213
00214 T bmid = arrs[i][middle] - arrs[i][startinterval-1];
00215 bool ok;
00216 if (bmid <= lb)
00217 ok = false;
00218 else if (bmid >= ub)
00219 ok = true;
00220 else
00221 {
00222 ok = util::MProbe<T,util::Aggreg2Dto1D<T, Pr, false>, util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > >::probe(bmid, arrs, procCount, lbar, ubar, procs, cuts);
00223 if(ok)
00224 {
00225 ub = bmid;
00226 }
00227 else
00228 {
00229 lb = bmid;
00230 }
00231 }
00232
00233 if (ok)
00234 {
00235 highbound = middle;
00236 }
00237 else
00238 {
00239 lowbound = middle+1;
00240 }
00241 }
00242
00243 if (startinterval == highbound)
00244 highbound++;
00245 startinterval = highbound;
00246 }
00247
00248 }
00249
00250
00251 #ifndef NDEBUG
00252 bool ret =
00253 #endif
00254 util::MProbe<T,util::Aggreg2Dto1D<T, Pr, false>, util::Multiarrays<T,util::Aggreg2Dto1D<T, Pr, false> > >::probe(ub, arrs, procCount, lbar, ubar, procs, oricuts);
00255 assert(ret);
00256
00257 for(int i = 0; i < procX;i++)
00258 {
00259 delete [] lbar[i];
00260 delete [] ubar[i];
00261 delete [] cuts[i];
00262 }
00263 delete[] lbar;
00264 delete[] ubar;
00265 delete[] cuts;
00266
00267 return ub;
00268 }
00269
00270 template <typename T, typename Pr,
00271 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, false>& prefixSumArray, int length, int *cutIndexes, T),
00272 T (*onedalgoX) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, true>& prefixSumArray, int length, int *cutIndexes, T) >
00273
00274 T twod::JagMHeurProbeVer<T,Pr,onedalgoY,onedalgoX>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00275 {
00276 JagMHeurProbeHor<T, util::TransposePrefix2D<T, Pr > > foo;
00277
00278 util::TransposePrefix2D<T, Pr> tpr (prefixSumArray);
00279
00280 util::RectList<T, util::TransposePrefix2D<T, Pr> > intermediate (tpr);
00281
00282 T val = foo.part(procCount,tpr,intermediate);
00283
00284 util::TransposePrefix2D<T, Pr>::convertRectList(intermediate, parts);
00285 return val;
00286 }
00287
00288 template <typename T, typename Pr>
00289 T twod::JagMHeurProbeBest<T,Pr>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00290 {
00291 using namespace util;
00292 RectList<T,Pr > partsv(prefixSumArray);
00293 RectList<T,Pr > partsh(prefixSumArray);
00294 JagMHeurProbeVer<T,Pr> foov;
00295 JagMHeurProbeHor<T,Pr> fooh;
00296 T ort1 = foov.part(procCount, prefixSumArray, partsv);
00297 T ort2 = fooh.part(procCount, prefixSumArray, partsh);
00298 if(ort1 > ort2)
00299 {
00300 parts.copy_into(partsh);
00301 return ort2;
00302 }
00303 else
00304 {
00305 parts.copy_into(partsv);
00306 return ort1;
00307 }
00308 }
00309
00310 template <typename T, typename Pr,
00311 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, Pr, false>& prefixSumArray, int length, int *cutIndexes, T),
00312 T (*onedalgoX)(int procCount, const util::Aggreg2Dto1D<T, Pr, true>& prefixSumArray, int length, int *cutIndexes, T)>
00313 twod::JagMHeurProbeHor<T,Pr,onedalgoY,onedalgoX>::~JagMHeurProbeHor(){}
00314
00315 template <typename T, typename Pr,
00316 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, false>& prefixSumArray, int length, int *cutIndexes, T),
00317 T (*onedalgoX) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, true>& prefixSumArray, int length, int *cutIndexes, T) >
00318
00319 twod::JagMHeurProbeVer<T,Pr,onedalgoY,onedalgoX>::~JagMHeurProbeVer(){}
00320
00321 template <typename T, typename Pr>
00322 twod::JagMHeurProbeBest<T,Pr>::~JagMHeurProbeBest(){}