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_jag_2d.hpp>
00038 #include <util/rect_list.hpp>
00039 #include <iostream>
00040 #include <math.h>
00041 #include <assert.h>
00042 #include <util/tools.hpp>
00043 #include <oned/direct_cut.hpp>
00044
00045 template <typename T, typename Pr,
00046 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, Pr, false>& prefixSumArray, int length, int *cutIndexes, T),
00047 T (*onedalgoX)(int procCount, const util::Aggreg2Dto1D<T, Pr, true>& prefixSumArray, int length, int *cutIndexes, T) >
00048 T twod::JagMHeurHor<T,Pr,onedalgoY, onedalgoX>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00049 {
00050 using namespace util;
00051 assert(procCount > 0);
00052
00053 int procX;
00054 if (P==0)
00055 procX = intfactor(procCount);
00056 else if (P == -1)
00057 {
00058 procX = (int)floor(sqrt(procCount*((double)prefixSumArray.prefixsizeX()-1)/(prefixSumArray.prefixsizeX()-1)));
00059 if (procX == 0)
00060 procX = 1;
00061 }
00062 else
00063 procX = P;
00064 return m_way_jag_2d_internal(procX, procCount, prefixSumArray, parts);
00065 }
00066
00067
00068
00069
00070 template <typename T, typename Pr,
00071 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, Pr, false>& prefixSumArray, int length, int *cutIndexes, T),
00072 T (*onedalgoX)(int procCount, const util::Aggreg2Dto1D<T, Pr, true>& prefixSumArray, int length, int *cutIndexes, T) >
00073 T twod::JagMHeurHor<T,Pr,onedalgoY, onedalgoX>::m_way_jag_2d_internal(int procX, int m, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00074
00075 {
00076 using namespace util;
00077 rectangle r;
00078 int *x_cuts;
00079 int *y_cuts;
00080 int i,j;
00081 T max_load = -1;
00082 x_cuts = new int[procX-1];
00083 y_cuts = new int[m-1];
00084
00085
00086 assert(prefixSumArray.prefixsizeY() > 0);
00087 assert(prefixSumArray.prefixsizeX() > 0);
00088 Aggreg2Dto1D<T, Pr, true> ps1dx(prefixSumArray, 1, prefixSumArray.prefixsizeX() - 1, 1, prefixSumArray.prefixsizeY() -1);
00089 onedalgoX( procX, ps1dx, prefixSumArray.prefixsizeX(), x_cuts, -1);
00090
00091
00092
00093 int allocated = 0;
00094 int *ln_alloc = new int[procX];
00095 T *row_weight = new T[procX];
00096
00097 r.y_top_l = 1;
00098 r.y_bot_r = prefixSumArray.prefixsizeY() - 1;
00099 r.x_top_l = 1;
00100 for(i = 0; i < procX; i++)
00101 {
00102 r.x_bot_r = (i == procX - 1) ? prefixSumArray.prefixsizeX() - 1 :x_cuts[i];
00103
00104 row_weight[i] = r.get_load<T, Pr>(prefixSumArray);
00105 ln_alloc[i] = ((int) ceil((m - procX) * row_weight[i] / ((double)prefixSumArray[prefixSumArray.prefixsizeX() - 1][prefixSumArray.prefixsizeY() -1])));
00106
00107 if(ln_alloc[i] >= prefixSumArray.prefixsizeY())
00108 ln_alloc[i]=prefixSumArray.prefixsizeY()-1;
00109 allocated += ln_alloc[i];
00110 r.x_top_l = r.x_bot_r + 1;
00111
00112 }
00113
00114
00115
00116
00117 int extra_left = m-allocated;
00118
00119 assert (extra_left >= 0);
00120 while (extra_left != 0)
00121 {
00122 int max_index = 0;
00123 for (i=1; i<procX; i++)
00124 {
00125 if(((double)row_weight[max_index])/ln_alloc[max_index]
00126 < ((double)row_weight[i])/ln_alloc[i])
00127 max_index = i;
00128 }
00129
00130 if(ln_alloc[max_index] < prefixSumArray.prefixsizeY()-1)
00131 ln_alloc[max_index]++;
00132 extra_left--;
00133 }
00134 assert(extra_left == 0);
00135
00136
00137 r.x_top_l = 1;
00138
00139 for(i = 0; i < procX; i++)
00140 {
00141
00142 r.y_top_l = 1;
00143 r.y_bot_r = prefixSumArray.prefixsizeY() - 1;
00144 r.x_bot_r = (i == procX - 1) ? prefixSumArray.prefixsizeX() - 1 :x_cuts[i];
00145 if(r.x_top_l <= r.x_bot_r)
00146 {
00147 Aggreg2Dto1D<T, Pr, false> ps1dx_row(prefixSumArray, r.x_top_l, r.x_bot_r, 1, prefixSumArray.prefixsizeY() - 1);
00148
00149 onedalgoY(ln_alloc[i], ps1dx_row, prefixSumArray.prefixsizeY(), y_cuts, -1);
00150
00151
00152
00153 for(j = 0; j < ln_alloc[i]; j++)
00154 {
00155 r.y_bot_r = (j == ln_alloc[i] - 1) ? prefixSumArray.prefixsizeY() - 1 :y_cuts[j];
00156 if(r.valid_bound(prefixSumArray))
00157 {
00158 parts.add_rect(r);
00159 if(r.get_load<T,Pr>(prefixSumArray) > max_load)
00160 max_load = r.get_load<T,Pr>(prefixSumArray);
00161 r.y_top_l = r.y_bot_r + 1;
00162 }
00163 else
00164 break;
00165 }
00166 }
00167
00168 r.x_top_l = r.x_bot_r + 1;
00169 }
00170
00171 delete[] ln_alloc;
00172 delete[] row_weight;
00173
00174
00175
00176 delete[]x_cuts;
00177 delete[]y_cuts;
00178 assert(max_load>=0);
00179 return max_load;
00180 }
00181
00182
00183 template <typename T, typename Pr,
00184 T (*onedalgoY) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, false>& prefixSumArray, int length, int *cutIndexes, T),
00185 T (*onedalgoX) (int procCount, const util::Aggreg2Dto1D<T, util::TransposePrefix2D<T, Pr>, true>& prefixSumArray, int length, int *cutIndexes, T)
00186 >
00187 T twod::JagMHeurVer<T, Pr, onedalgoY, onedalgoX>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00188 {
00189 JagMHeurHor<T, util::TransposePrefix2D<T, Pr >, onedalgoY, onedalgoX> foo;
00190 foo.setP(P);
00191
00192 util::TransposePrefix2D<T, Pr> tpr (prefixSumArray);
00193
00194 util::RectList<T, util::TransposePrefix2D<T, Pr> > intermediate (tpr);
00195
00196 T val = foo.part(procCount,tpr,intermediate);
00197
00198 util::TransposePrefix2D<T, Pr>::convertRectList(intermediate, parts);
00199
00200 return val;
00201 }
00202
00203
00204 template <typename T, typename Pr>
00205 T twod::JagMHeurBest<T,Pr>::part(int procCount, const Pr& prefixSumArray, util::RectList<T,Pr> &parts)
00206 {
00207 using namespace util;
00208 RectList<T,Pr > partsv(prefixSumArray);
00209 RectList<T,Pr > partsh(prefixSumArray);
00210 JagMHeurVer<T,Pr> foov;
00211 JagMHeurHor<T,Pr> fooh;
00212 T ort1 = foov.part(procCount, prefixSumArray, partsv);
00213 T ort2 = fooh.part(procCount, prefixSumArray, partsh);
00214 if(ort1 > ort2)
00215 {
00216 parts.copy_into(partsh);
00217 return ort2;
00218 }
00219 else
00220 {
00221 parts.copy_into(partsv);
00222 return ort1;
00223 }
00224 }