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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <oned/nicol_plus.hpp>
00048
00049 template <typename T, typename Pr, bool DEBUG>
00050 T oned::NicolPlus<T,Pr,DEBUG>::nicol_plus_internal(const Pr& wpre, int length,
00051 int numproc, T wmax, int *sl, int *sh)
00052 {
00053 using namespace util;
00054 int ilast = 1, b;
00055 int ilow, ihigh, imid;
00056 T bottleneck, Bopt;
00057 T lb = wpre[length - 1] / numproc;
00058 T ub;
00059 if (wmax < 0)
00060 ub = wpre[length - 1];
00061 else
00062 ub = (wpre[length - 1] + wmax * (numproc - 1)) / numproc;
00063 int *s = new int[numproc];
00064 int *oris = s;
00065
00066
00067 Bopt = ub;
00068 for(b=0; b < numproc - 1; b++)
00069 {
00070
00071 ilow = (sl[b]<ilast?ilast-1:sl[b]);
00072 ihigh=length-1;
00073
00074
00075 if (debug) std::cerr<<"b = " << b << ", ilow = " << ilow << ",ihigh = " << ihigh << std::endl;
00076
00077 assert (ilow <= ihigh);
00078
00079 while(ilow < ihigh)
00080 {
00081 imid = (ilow + ihigh) / 2;
00082 bottleneck = wpre[imid] - wpre[ilast - 1];
00083
00084 assert (bottleneck >=0);
00085 if (debug) std::cerr<<"lb = " << lb << ", bottleneck = " << bottleneck << ", ub = "<< std::endl;
00086 if((lb <= bottleneck) && (bottleneck < ub))
00087 {
00088 if(ParametricSearch<T,Pr>::rprobe(wpre, length, bottleneck, numproc, s, sl, sh))
00089 {
00090 int *tmp;
00091 if (debug) std::cerr<<"rprobe ("<<bottleneck<<") is true"<<std::endl;
00092 if (DEBUG)
00093 assert ((ParametricSearch<T,Pr>::probe)(bottleneck, numproc-1, wpre, length));
00094
00095 tmp=s;
00096 s=sh;
00097 sh=tmp;
00098 ub = bottleneck;
00099 ihigh = imid;
00100 }
00101 else
00102 {
00103 int *tmp;
00104 if (debug) std::cerr<<"rprobe ("<<bottleneck<<") is false"<<std::endl;
00105 if (DEBUG)
00106 assert (!(ParametricSearch<T,Pr>::probe)(bottleneck, numproc-1, wpre, length));
00107
00108 tmp=s;
00109 s=sl;
00110 sl=tmp;
00111 lb = bottleneck;
00112 ilow = imid + 1;
00113 }
00114 }
00115 else if(bottleneck >= ub)
00116 {
00117 if (DEBUG)
00118 assert ((ParametricSearch<T,Pr>::probe)(bottleneck, numproc-1, wpre, length));
00119 ihigh = imid;
00120 }
00121 else
00122 {
00123 if (DEBUG)
00124 assert (! (ParametricSearch<T,Pr>::probe)(bottleneck, numproc-1, wpre, length));
00125
00126 ilow = imid + 1;
00127 }
00128 }
00129 assert (ilow == ihigh);
00130 assert(b < numproc);
00131
00132 Bopt = std::min(Bopt, wpre[ihigh] - wpre[ilast - 1]);
00133
00134
00135
00136 ilast = ihigh;
00137 }
00138 Bopt = std::min(wpre[length-1] - wpre[ilast - 1], Bopt);
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 delete[] oris;
00154
00155
00156 return Bopt;
00157 }
00158
00159
00160 template <typename T, typename Pr, bool DEBUG>
00161 T oned::NicolPlus<T,Pr, DEBUG>::nicol_plus(int procCount, const Pr & prefixSumArray,
00162 int length, int *cutIndexes, T max)
00163 {
00164 using namespace util;
00165 int i;
00166 int *sl = new int[procCount];
00167 int *sh = new int[procCount];
00168 T b;
00169 for(i = 0; i < procCount; i++)
00170 {
00171 sl[i] = 1;
00172 sh[i] = length - 1;
00173 }
00174
00175 b = nicol_plus_internal (prefixSumArray, length, procCount, max, sl, sh);
00176 if (cutIndexes != NULL)
00177 ParametricSearch<T,Pr>::probeCut (b, procCount-1, prefixSumArray,length, cutIndexes);
00178 delete[] sl;
00179 delete[] sh;
00180 return b;
00181 }