Go to the documentation of this file.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 #ifndef __PROPORTIONAL__
00038 #define __PROPORTIONAL__
00039 
00040 
00041 namespace util
00042 {
00058   template <typename T, typename Pr>
00059   void distribute_processors(const Pr& psa, const util::RectList<T,Pr> & v, std::vector<int> & alloc, int m);
00060   template <typename T, typename Pr>
00061   void distribute_processors(const Pr& psa, const util::RectList<T,Pr> & v, std::vector<int> & alloc, int m)
00062   {
00063     bool debug = true;
00064 
00065     assert (alloc.empty());
00066     assert (m > 0);
00067 
00068     int nbrect = v.rectangles.size();
00069 
00070     assert (nbrect > 0);
00071 
00072     if (debug)
00073       {
00074         std::cerr<<"distributing "<<m<<" processors on "<<nbrect<<" rectangles"<<std::endl;
00075       }
00076 
00077 
00078     alloc.reserve(nbrect);
00079     std::vector<T> load;
00080     load.reserve(nbrect);
00081 
00082     int allocated = 0;
00083 
00084     typename util::RectList<T, Pr>::container::const_iterator it = v.rectangles.begin();
00085 
00086     for (int i=0; i<nbrect; i++)
00087       {
00088         const util::rectangle &r = *it;
00089         load[i] = r.get_load<T,Pr> (psa);
00090 
00091         alloc[i] = ((int) ceil((m - nbrect) * load[i]
00092                                /((double)psa[psa.prefixsizeX() - 1][psa.prefixsizeY() -1])));
00093 
00094         allocated += alloc[i];
00095 
00096         if (debug)
00097           std::cerr<<alloc[i]<<" processors for "<<i<<"="<<*it<<". "
00098                    <<"Expected bottleneck for that part :"<<((double)load[i])/alloc[i]
00099                    <<std::endl;
00100 
00101         it++;
00102         
00103 
00104       }
00105 
00106     assert (allocated <= m);
00107 
00108     if (debug)
00109       {
00110         std::cerr<<allocated<<" processors already allocated. "<<m-allocated<<" to come"<<std::endl;
00111       }
00112 
00114     while (allocated != m)
00115       {
00116         assert (allocated < m);
00117         int max_index = 0;
00118 
00119         for (int i=1; i<nbrect; i++)
00120           {
00121             if (load[i] / (double)alloc[i] > load[max_index] / (double)alloc[max_index])
00122               max_index = i;
00123                                  
00124           }
00125 
00126         alloc[max_index]++;
00127 
00128         if (debug)
00129           std::cerr<<"revising allocation of "<<max_index<<". now "<<alloc[max_index]<<" processors. "
00130                    <<"Expected bottleneck for that part: "<<((double)load[max_index])/alloc[max_index]<<std::endl;
00131 
00132         allocated ++;
00133       }
00134 
00135     assert (allocated == m);
00136     if (debug)
00137       {
00138         int max_index = 0;
00139         
00140         for (int i=1; i<nbrect; i++)
00141           {
00142             if (load[i] / (double)alloc[i] > load[max_index] / (double)alloc[max_index])
00143               max_index = i;
00144             
00145           }
00146         double exp_bot = (load[max_index] / (double)alloc[max_index]);
00147         double perfect_balance = ((double)(psa[psa.prefixsizeX()-1][psa.prefixsizeY()-1])/m);
00148         std::cerr<<"Allocation complete! "
00149                  <<"Expected bottleneck : "<<exp_bot<<". "
00150                  <<"Expected load imbalance : "<<(exp_bot/perfect_balance)-1
00151                  <<std::endl;
00152 
00153       }
00154   }
00155 }
00156 #endif