You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
14 KiB

  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. /* Bin-packing algorithm that attempts to use minimal number of bins B of
  24. size V to contain N items of varying sizes. */
  25. #include <string>
  26. #include <iostream>
  27. #include <cmath>
  28. #include "tbb/atomic.h"
  29. #include "tbb/task_scheduler_init.h"
  30. #include "tbb/tick_count.h"
  31. #include "tbb/flow_graph.h"
  32. #include "../../common/utility/utility.h"
  33. using namespace std;
  34. using namespace tbb;
  35. using namespace tbb::flow;
  36. typedef size_t size_type; // to represent non-zero indices, capacities, etc.
  37. typedef size_t value_type; // the type of items we are attempting to pack into bins
  38. typedef vector<value_type> bin; // we use a simple vector to represent a bin
  39. // Our bin packers will be function nodes in the graph that take value_type items and
  40. // return a dummy value. They will also implicitly send packed bins to the bin_buffer
  41. // node, and unused items back to the value_pool node:
  42. typedef function_node<value_type, continue_msg, rejecting> bin_packer;
  43. // Items are placed into a pool that all bin packers grab from, represent by a queue_node:
  44. typedef queue_node<value_type> value_pool;
  45. // Packed bins are placed in this buffer waiting to be serially printed and/or accounted for:
  46. typedef buffer_node<bin> bin_buffer;
  47. // Packed bins are taken from the_bin_buffer and processed by the_writer:
  48. typedef function_node<bin, continue_msg, rejecting> bin_writer;
  49. // Items are injected into the graph when this node sends them to the_value_pool:
  50. typedef source_node<value_type> value_source;
  51. // User-specified globals with default values
  52. size_type V = 42; // desired capacity for each bin
  53. size_type N = 1000; // number of elements to generate
  54. bool verbose = false; // prints bin details and other diagnostics to screen
  55. bool silent = false; // suppress all output except for time
  56. int num_bin_packers=-1; // number of concurrent bin packers in operation; default is #threads;
  57. // larger values can result in more bins at less than full capacity
  58. size_type optimality=1; // 1 (default) is highest the algorithm can obtain; larger numbers run faster
  59. // Calculated globals
  60. size_type min_B; // lower bound on the optimal number of bins
  61. size_type B; // the answer, i.e. number of bins used by the algorithm
  62. size_type *input_array; // stores randomly generated input values
  63. value_type item_sum; // sum of all randomly generated input values
  64. atomic<value_type> packed_sum; // sum of all values currently packed into all bins
  65. atomic<size_type> packed_items; // number of values currently packed into all bins
  66. atomic<size_type> active_bins; // number of active bin_packers
  67. bin_packer **bins; // the array of bin packers
  68. // This class is the Body type for bin_packer
  69. class bin_filler {
  70. bin my_bin; // the current bin that this bin_filler is packing
  71. size_type my_used; // capacity of bin used by current contents (not to be confused with my_bin.size())
  72. size_type relax, relax_val; // relaxation counter for determining when to settle for a non-full bin
  73. bin_packer* my_bin_packer; // ptr to the bin packer that this body object is associated with
  74. size_type bin_index; // index of the encapsulating bin packer in the global bins array
  75. value_pool* the_value_pool; // ptr to the pool of items to pack
  76. bin_buffer* the_bin_buffer; // ptr to the buffer of resulting bins
  77. value_type looking_for; // the minimum size of item this bin_packer will accept
  78. bool done; // flag to indicate that this binpacker has been deactivated
  79. public:
  80. bin_filler(size_t bidx, value_pool* q, bin_buffer* r) :
  81. my_used(0), relax(0), relax_val(0), my_bin_packer(NULL), bin_index(bidx), the_value_pool(q),
  82. the_bin_buffer(r), looking_for(V), done(false) {}
  83. continue_msg operator()(const value_type& item) {
  84. if (!my_bin_packer) my_bin_packer = bins[bin_index];
  85. if (done) the_value_pool->try_put(item); // this bin_packer is done packing items; put item back to pool
  86. else if (item > V) { // signal that packed_sum has reached item_sum at some point
  87. size_type remaining = active_bins--;
  88. if (remaining == 1 && packed_sum == item_sum) { // this is the last bin and it has seen everything
  89. // this bin_packer may not have seen everything, so stay active
  90. if (my_used>0) the_bin_buffer->try_put(my_bin);
  91. my_bin.clear();
  92. my_used = 0;
  93. looking_for = V;
  94. ++active_bins;
  95. }
  96. else if (remaining == 1) { // this is the last bin, but there are remaining items
  97. the_value_pool->try_put(V+1); // send out signal
  98. ++active_bins;
  99. }
  100. else if (remaining > 1) { // this is not the last bin; deactivate
  101. if (my_used < V/(1+optimality*.1)) { // this bin is ill-utilized; throw back items and deactivate
  102. packed_sum -= my_used;
  103. packed_items -= my_bin.size();
  104. for (size_type i=0; i<my_bin.size(); ++i)
  105. the_value_pool->try_put(my_bin[i]);
  106. the_value_pool->remove_successor(*my_bin_packer); // deactivate
  107. done = true;
  108. the_value_pool->try_put(V+1); // send out signal
  109. }
  110. else { // this bin is well-utilized; send out bin and deactivate
  111. the_value_pool->remove_successor(*my_bin_packer); // build no more bins
  112. done = true;
  113. if (my_used>0) the_bin_buffer->try_put(my_bin);
  114. the_value_pool->try_put(V+1); // send out signal
  115. }
  116. }
  117. }
  118. else if (item <= V-my_used && item >= looking_for) { // this item can be packed
  119. my_bin.push_back(item);
  120. my_used += item;
  121. packed_sum += item;
  122. ++packed_items;
  123. looking_for = V-my_used;
  124. relax = 0;
  125. if (packed_sum == item_sum) {
  126. the_value_pool->try_put(V+1); // send out signal
  127. }
  128. if (my_used == V) {
  129. the_bin_buffer->try_put(my_bin);
  130. my_bin.clear();
  131. my_used = 0;
  132. looking_for = V;
  133. }
  134. }
  135. else { // this item can't be packed; relax constraints
  136. ++relax;
  137. if (relax >= (N-packed_items)/optimality) { // this bin_packer has looked through enough items
  138. relax = 0;
  139. --looking_for; // accept a wider range of items
  140. if (looking_for == 0 && my_used < V/(1+optimality*.1) && my_used > 0 && active_bins > 1) {
  141. // this bin_packer is ill-utilized and can't find items; deactivate and throw back items
  142. size_type remaining = active_bins--;
  143. if (remaining > 1) { // not the last bin_packer
  144. the_value_pool->remove_successor(*my_bin_packer); // deactivate
  145. done = true;
  146. }
  147. else active_bins++; // can't deactivate last bin_packer
  148. packed_sum -= my_used;
  149. packed_items -= my_bin.size();
  150. for (size_type i=0; i<my_bin.size(); ++i)
  151. the_value_pool->try_put(my_bin[i]);
  152. my_bin.clear();
  153. my_used = 0;
  154. }
  155. else if (looking_for == 0 && (my_used >= V/(1+optimality*.1) || active_bins == 1)) {
  156. // this bin_packer can't find items but is well-utilized, so send it out and reset
  157. the_bin_buffer->try_put(my_bin);
  158. my_bin.clear();
  159. my_used = 0;
  160. looking_for = V;
  161. }
  162. }
  163. the_value_pool->try_put(item); // put unused item back to pool
  164. }
  165. return continue_msg(); // need to return something
  166. }
  167. };
  168. // source node uses this to send the values to the value_pool
  169. class item_generator {
  170. size_type counter;
  171. public:
  172. item_generator() : counter(0) {}
  173. bool operator()(value_type& m) {
  174. if (counter<N) {
  175. m = input_array[counter];
  176. ++counter;
  177. return true;
  178. }
  179. return false;
  180. }
  181. };
  182. // the terminal function_node uses this to gather stats and print bin information
  183. class bin_printer {
  184. value_type running_count;
  185. size_type item_count;
  186. value_type my_min, my_max;
  187. double avg;
  188. public:
  189. bin_printer() : running_count(0), item_count(0), my_min(V), my_max(0), avg(0) {}
  190. continue_msg operator()(bin b) {
  191. value_type sum=0;
  192. ++B;
  193. if (verbose)
  194. cout << "[ ";
  195. for (size_type i=0; i<b.size(); ++i) {
  196. if (verbose)
  197. cout << b[i] << " ";
  198. sum+=b[i];
  199. ++item_count;
  200. }
  201. if (sum < my_min) my_min = sum;
  202. if (sum > my_max) my_max = sum;
  203. avg += sum;
  204. running_count += sum;
  205. if (verbose)
  206. cout << "]=" << sum << "; Done/Packed/Total cap: " << running_count << "/" << packed_sum << "/" << item_sum
  207. << " items:" << item_count << "/" << packed_items << "/" << N << " B=" << B << endl;
  208. if (item_count == N) { // should be the last; print stats
  209. avg = avg/(double)B;
  210. if (!silent)
  211. cout << "SUMMARY: #Bins used: " << B << "; Avg size: " << avg << "; Max size: " << my_max
  212. << "; Min size: " << my_min << "\n Lower bound on optimal #bins: " << min_B
  213. << "; Start #bins: " << num_bin_packers << endl;
  214. }
  215. return continue_msg(); // need to return something
  216. }
  217. };
  218. int get_default_num_threads() {
  219. static int threads = 0;
  220. if (threads == 0)
  221. threads = tbb::task_scheduler_init::default_num_threads();
  222. return threads;
  223. }
  224. int main(int argc, char *argv[]) {
  225. try {
  226. utility::thread_number_range threads(get_default_num_threads);
  227. utility::parse_cli_arguments(argc, argv,
  228. utility::cli_argument_pack()
  229. //"-h" option for for displaying help is present implicitly
  230. .positional_arg(threads,"#threads",utility::thread_number_range_desc)
  231. .arg(verbose,"verbose"," print diagnostic output to screen")
  232. .arg(silent,"silent"," limits output to timing info; overrides verbose")
  233. .arg(N,"N"," number of values to pack")
  234. .arg(V,"V"," capacity of each bin")
  235. .arg(num_bin_packers,"#packers"," number of concurrent bin packers to use "
  236. "(default=#threads)")
  237. .arg(optimality,"optimality","controls optimality of solution; 1 is highest, use\n"
  238. " larger numbers for less optimal but faster solution")
  239. );
  240. if (silent) verbose = false; // make silent override verbose
  241. // Generate random input data
  242. srand(42);
  243. input_array = new value_type[N];
  244. item_sum = 0;
  245. for (size_type i=0; i<N; ++i) {
  246. input_array[i] = rand() % V + 1; // generate items that fit in a bin
  247. item_sum += input_array[i];
  248. }
  249. min_B = (item_sum % V) ? item_sum/V + 1 : item_sum/V;
  250. tick_count start = tick_count::now();
  251. for(int p = threads.first; p <= threads.last; p = threads.step(p)) {
  252. task_scheduler_init init(p);
  253. packed_sum = 0;
  254. packed_items = 0;
  255. B = 0;
  256. if (num_bin_packers == -1) num_bin_packers = p;
  257. active_bins = num_bin_packers;
  258. if (!silent)
  259. cout << "binpack running with " << item_sum << " capacity over " << N << " items, optimality="
  260. << optimality << ", " << num_bin_packers << " bins of capacity=" << V << " on " << p
  261. << " threads.\n";
  262. graph g;
  263. value_source the_source(g, item_generator(), false);
  264. value_pool the_value_pool(g);
  265. make_edge(the_source, the_value_pool);
  266. bin_buffer the_bin_buffer(g);
  267. bins = new bin_packer*[num_bin_packers];
  268. for (int i=0; i<num_bin_packers; ++i) {
  269. bins[i] = new bin_packer(g, 1, bin_filler(i, &the_value_pool, &the_bin_buffer));
  270. make_edge(the_value_pool, *(bins[i]));
  271. }
  272. bin_writer the_writer(g, 1, bin_printer());
  273. make_edge(the_bin_buffer, the_writer);
  274. the_source.activate();
  275. g.wait_for_all();
  276. for (int i=0; i<num_bin_packers; ++i) {
  277. delete bins[i];
  278. }
  279. delete[] bins;
  280. }
  281. utility::report_elapsed_time((tbb::tick_count::now() - start).seconds());
  282. delete[] input_array;
  283. return 0;
  284. } catch(std::exception& e) {
  285. cerr<<"error occurred. error text is :\"" <<e.what()<<"\"\n";
  286. return 1;
  287. }
  288. }