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 "prefix_sum_tools.hpp"
00038 #include <iostream>
00039
00040
00041
00042 template <typename T, typename Pr>
00043 int util::ParametricSearch<T,Pr>::probe(T bottleneck, int cutNumber, const Pr& prefixSumArray, int length)
00044 {
00045 assert (cutNumber >= 0);
00046 assert (length >= 0);
00047
00048 assert (prefixSumArray[0] == 0);
00049 return probeCut(bottleneck,cutNumber,prefixSumArray,length,NULL);
00050 }
00051
00052
00053 template <typename T, typename Pr>
00054 int util::ParametricSearch<T,Pr>::probeCut(T bottleneck, int cutNumber, const Pr& prefixSumArray, int length, int *cuts)
00055 {
00056 int processor, step = length / (cutNumber + 1), cutPoint;
00057 T prefixSumBottleneck = bottleneck, totalLoad = prefixSumArray[length - 1];
00058
00059 assert (cutNumber >= 0);
00060 assert (length >= 0);
00061
00062 assert (prefixSumArray[0] == 0);
00063
00064
00065 for(processor = 0; (processor < cutNumber) && (prefixSumBottleneck < totalLoad); processor++)
00066 {
00067 while((step < length) && (prefixSumArray[step] < prefixSumBottleneck))
00068 {
00069 step += (length / (cutNumber + 1));
00070 }
00071 if (step >= length)
00072 step = length-1;
00073 assert (step < length);
00074 cutPoint = binarySearch(prefixSumArray, step - (length / (cutNumber + 1)),
00075 step, prefixSumBottleneck);
00076 if (cuts != NULL)
00077 cuts[processor] = cutPoint;
00078 if(cutPoint > -1)
00079 prefixSumBottleneck = bottleneck + prefixSumArray[cutPoint];
00080 else
00081 return 0;
00082 }
00083
00084 if(prefixSumBottleneck >= totalLoad)
00085 {
00086 if((cuts != NULL) && (processor < cutNumber))
00087 for(;processor < cutNumber;processor++)
00088 cuts[processor] = length - 1;
00089 return 1;
00090 }
00091 else
00092 return 0;
00093 }
00094
00095
00096
00097 template <typename T, typename Pr>
00098 int util::ParametricSearch<T,Pr>::rprobe(const Pr& wpre, int length, const T& bottleneck,
00099 int numproc, int *s, const int *sl, const int *sh)
00100 {
00101 T bsum = bottleneck;
00102 int p;
00103
00104 for(p = 0; p < numproc - 1; p++)
00105 {
00106 assert(sl[p] <= sh[p]);
00107 assert(p < numproc);
00108 s[p] = binarySearch(wpre, sl[p], sh[p], bsum);
00109 if (p > 0)
00110 {
00111 assert (s[p]>=s[p-1]);
00112 assert (wpre[s[p]] - wpre[s[p-1]] <= bottleneck);
00113 }
00114 bsum = wpre[s[p]] + bottleneck;
00115 }
00116 if(bsum >= wpre[length - 1])
00117 return 1;
00118 else
00119 return 0;
00120 }
00121
00122
00123
00124 template <typename T, typename Pr>
00125 int util::ParametricSearch<T,Pr>::rprobe_interval(const Pr& wpre, int length, const T& bottleneck,
00126 int numproc, int *s, const int *sl, const int *sh)
00127 {
00128 int p;
00129 for(p = 0; p < numproc - 1; p++)
00130 {
00131 assert(p==0 || sl[p]>=sl[p-1]);
00132 assert(p==0 || sh[p]>=sh[p-1]);
00133 assert(sl[p] <= sh[p]);
00134 assert(p < numproc);
00135 s[p] = binarySearch_interval(wpre, sl[p], sh[p], bottleneck, (p==0?0:s[p-1]));
00136 assert (p == 0 || s[p]>=s[p-1] );
00137 assert (p == 0 || (wpre.interval(s[p-1],s[p]) <= bottleneck));
00138
00139 }
00140 if(wpre.interval(s[p - 1],length-1) <= bottleneck)
00141 return 1;
00142 else
00143 return 0;
00144 }
00145
00146
00147 template <typename T, typename Pr>
00148 int util::ParametricSearch<T,Pr>::probeCut_interval(T bottleneck, int cutNumber, const Pr& prefixSumArray, int length, int *cuts)
00149 {
00150 int processor, step = length / (cutNumber + 1), cutPoint;
00151 int lastCut = 0;
00152 assert (cutNumber >= 0);
00153 assert (length >= 0);
00154 assert (prefixSumArray.interval(0,0) == 0);
00155
00156 for(processor = 0; (processor < cutNumber) && (step < length); processor++)
00157 {
00158 while((step < length) && (prefixSumArray.interval(lastCut,step) <= bottleneck))
00159 {
00160 step += (length / (cutNumber + 1));
00161 }
00162 if (step >= length)
00163 step = length-1;
00164 assert (step < length);
00165 cutPoint = binarySearch_interval(prefixSumArray, step - (length / (cutNumber + 1)), step, bottleneck, lastCut);
00166 if (cuts != NULL)
00167 cuts[processor] = cutPoint;
00168 if(cutPoint > -1)
00169 lastCut = cutPoint;
00170 else
00171 return 0;
00172 }
00173
00174 if (processor <= cutNumber)
00175 {
00176 if (cuts != NULL)
00177 for(;processor < cutNumber;processor++)
00178 cuts[processor] = length - 1;
00179 return 1;
00180 }
00181 else
00182 return 0;
00183 }