empq_adaptive_impl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /****************************************************************************
  2. *
  3. * MODULE: iostream
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. *
  8. * Iostream is a library that implements streams, external memory
  9. * sorting on streams, and an external memory priority queue on
  10. * streams. These are the fundamental components used in external
  11. * memory algorithms.
  12. * Credits: The library was developed by Laura Toma. The kernel of
  13. * class STREAM is based on the similar class existent in the GPL TPIE
  14. * project developed at Duke University. The sorting and priority
  15. * queue have been developed by Laura Toma based on communications
  16. * with Rajiv Wickremesinghe. The library was developed as part of
  17. * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
  18. * Rajiv Wickremesinghe as part of the Terracost project.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details. *
  29. * **************************************************************************/
  30. #ifndef __EMPQ_ADAPTIVE_IMPL_H
  31. #define __EMPQ_ADAPTIVE_IMPL_H
  32. #include <stdio.h>
  33. #include <assert.h>
  34. #include "ami_config.h"
  35. #include "ami_stream.h"
  36. #include "mm.h"
  37. #include "mm_utils.h"
  38. #include "empq_adaptive.h"
  39. #include "ami_sort.h"
  40. // EMPQAD_DEBUG defined in "empqAdaptive.H"
  41. //------------------------------------------------------------
  42. //allocate an internal pqueue of size precisely twice
  43. //the size of the pqueue within the em_pqueue;
  44. //
  45. //This constructor uses a user defined amount of memory
  46. template<class T, class Key>
  47. EMPQueueAdaptive<T,Key>::EMPQueueAdaptive(size_t inMem) {
  48. regim = INMEM;
  49. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: starting in-memory pqueue"
  50. << endl;
  51. //------------------------------------------------------------
  52. //set the size precisely as in empq constructor since we cannot
  53. //really call the em__pqueue constructor, because we don't want
  54. //the space allocated; we just want the sizes;
  55. //AMI_err ae;
  56. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: desired memory: "
  57. << ( (float)inMem/ (1<< 20)) << "MB" << endl;
  58. initPQ(inMem);
  59. }
  60. //------------------------------------------------------------
  61. // This more resembles the original constuctor which is greedy
  62. template<class T, class Key>
  63. EMPQueueAdaptive<T,Key>::EMPQueueAdaptive() {
  64. regim = INMEM;
  65. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: starting in-memory pqueue"
  66. << endl;
  67. //------------------------------------------------------------
  68. //set the size precisely as in empq constructor since we cannot
  69. //really call the em__pqueue constructor, because we don't want
  70. //the space allocated; we just want the sizes;
  71. size_t mm_avail = getAvailableMemory();
  72. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: available memory: "
  73. << ( (float)mm_avail/ (1<< 20)) << "MB" << endl;
  74. initPQ(mm_avail);
  75. }
  76. //------------------------------------------------------------
  77. // This metod initialized the PQ based on the memory passed
  78. // into it
  79. template<class T, class Key>
  80. void
  81. EMPQueueAdaptive<T,Key>::initPQ(size_t initMem) {
  82. AMI_err ae;
  83. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: desired memory: "
  84. << ( (float)initMem/ (1<< 20)) << "MB" << endl;
  85. /* same calculations as empq constructor in order to estimate
  86. overhead memory; this is because we want to allocate a pqueue of
  87. size exactly double the size of the pqueue inside the empq;
  88. switching from this pqueue to empq when the memory fills up will
  89. be simple */
  90. //------------------------------------------------------------
  91. //AMI_STREAM memory usage
  92. size_t sz_stream;
  93. AMI_STREAM<T> dummy;
  94. if ((ae = dummy.main_memory_usage(&sz_stream,
  95. MM_STREAM_USAGE_MAXIMUM)) !=
  96. AMI_ERROR_NO_ERROR) {
  97. cerr << "EMPQueueAdaptive constr: failing to get stream_usage\n";
  98. exit(1);
  99. }
  100. //account for temporary memory usage
  101. unsigned short max_nbuf = 2;
  102. unsigned int buf_arity = initMem/(2 * sz_stream);
  103. if (buf_arity > MAX_STREAMS_OPEN) buf_arity = MAX_STREAMS_OPEN;
  104. unsigned long mm_overhead = buf_arity*sizeof(merge_key<Key>) +
  105. max_nbuf * sizeof(em_buffer<T,Key>) +
  106. 2*sz_stream + max_nbuf*sz_stream;
  107. mm_overhead *= 8; //overestimate..this should be fixed with
  108. //a precise accounting of the extra memory required
  109. EMPQAD_DEBUG cout << "sz_stream: " << sz_stream << " buf_arity: " << buf_arity <<
  110. " mm_overhead: " << mm_overhead << " mm_avail: " << initMem << ".\n";
  111. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: memory overhead set to "
  112. << ((float)mm_overhead / (1 << 20)) << "MB" << endl;
  113. if (mm_overhead > initMem) {
  114. cerr << "overhead bigger than available memory ("<< initMem << "); "
  115. << "increase -m and try again\n";
  116. exit(1);
  117. }
  118. initMem -= mm_overhead;
  119. //------------------------------------------------------------
  120. long pqsize = initMem/sizeof(T);
  121. EMPQAD_DEBUG cout << "EMPQUEUEADAPTIVE: pqsize set to " << pqsize << endl;
  122. //initialize in memory pqueue and set em to NULL
  123. im = new MinMaxHeap<T>(pqsize);
  124. assert(im);
  125. em = NULL;
  126. }
  127. template<class T, class Key>
  128. EMPQueueAdaptive<T,Key>::~EMPQueueAdaptive() {
  129. switch(regim) {
  130. case INMEM:
  131. delete im;
  132. break;
  133. case EXTMEM:
  134. delete em;
  135. break;
  136. case EXTMEM_DEBUG:
  137. delete dim;
  138. delete em;
  139. break;
  140. }
  141. }
  142. //return the maximum nb of elts that can fit
  143. template<class T, class Key>
  144. long
  145. EMPQueueAdaptive<T,Key>::maxlen() const {
  146. long m=-1;
  147. switch(regim) {
  148. case INMEM:
  149. assert(im);
  150. m = im->get_maxsize();
  151. break;
  152. case EXTMEM:
  153. assert(em);
  154. m = em->maxlen();
  155. break;
  156. case EXTMEM_DEBUG:
  157. m = em->maxlen();
  158. break;
  159. }
  160. return m;
  161. }
  162. //return true if empty
  163. template<class T, class Key>
  164. bool
  165. EMPQueueAdaptive<T,Key>::is_empty() const {
  166. bool v = false;
  167. switch(regim) {
  168. case INMEM:
  169. assert(im);
  170. v = im->empty();
  171. break;
  172. case EXTMEM:
  173. assert(em);
  174. v = em->is_empty();
  175. break;
  176. case EXTMEM_DEBUG:
  177. assert(dim->empty() == em->is_empty());
  178. v = em->is_empty();
  179. break;
  180. }
  181. return v;
  182. }
  183. //return true if full
  184. template<class T, class Key>
  185. bool
  186. EMPQueueAdaptive<T,Key>::is_full() const {
  187. cerr << "EMPQueueAdaptive::is_full(): sorry not implemented\n";
  188. assert(0);
  189. exit(1);
  190. }
  191. //return the element with minimum priority in the structure
  192. template<class T, class Key>
  193. bool
  194. EMPQueueAdaptive<T,Key>::min(T& elt) {
  195. bool v=false, v1;
  196. T tmp;
  197. switch(regim) {
  198. case INMEM:
  199. assert(im);
  200. v = im->min(elt);
  201. break;
  202. case EXTMEM:
  203. assert(em);
  204. v = em->min(elt);
  205. break;
  206. case EXTMEM_DEBUG:
  207. v1 = dim->min(tmp);
  208. v = em->min(elt);
  209. //dim->verify();
  210. if(!(tmp==elt)) {
  211. cerr << "------------------------------" << endl;
  212. cerr << dim << endl;
  213. cerr << "------------------------------" << endl;
  214. em->print();
  215. cerr << "------------------------------" << endl;
  216. cerr << "tmp=" << tmp << endl;
  217. cerr << "elt=" << elt << endl;
  218. cerr << "------------------------------" << endl;
  219. dim->destructiveVerify();
  220. }
  221. assert(v == v1);
  222. assert(tmp == elt);
  223. break;
  224. }
  225. return v;
  226. }
  227. /* switch over to using an external priority queue */
  228. template<class T, class Key>
  229. void
  230. EMPQueueAdaptive<T,Key>::clear() {
  231. switch(regim) {
  232. case INMEM:
  233. im->clear();
  234. break;
  235. case EXTMEM:
  236. em->clear();
  237. break;
  238. case EXTMEM_DEBUG:
  239. dim->clear();
  240. break;
  241. }
  242. }
  243. template<class T, class Key>
  244. void
  245. EMPQueueAdaptive<T,Key>::verify() {
  246. switch(regim) {
  247. case INMEM:
  248. im->verify();
  249. break;
  250. case EXTMEM:
  251. break;
  252. case EXTMEM_DEBUG:
  253. dim->verify();
  254. break;
  255. }
  256. }
  257. //extract all elts with min key, add them and return their sum
  258. template<class T, class Key>
  259. bool
  260. EMPQueueAdaptive<T,Key>::extract_all_min(T& elt) {
  261. bool v=false, v1;
  262. T tmp;
  263. switch(regim) {
  264. case INMEM:
  265. assert(im);
  266. v = im->extract_all_min(elt);
  267. break;
  268. case EXTMEM:
  269. assert(em);
  270. v = em->extract_all_min(elt);
  271. break;
  272. case EXTMEM_DEBUG:
  273. v1 = dim->extract_all_min(tmp);
  274. v = em->extract_all_min(elt);
  275. assert(dim->size() == em->size());
  276. assert(v == v1);
  277. assert(tmp == elt);
  278. break;
  279. }
  280. return v;
  281. }
  282. //return the nb of elements in the structure
  283. template<class T, class Key>
  284. long
  285. EMPQueueAdaptive<T,Key>::size() const {
  286. long v=0, v1;
  287. switch(regim) {
  288. case INMEM:
  289. assert(im);
  290. v = im->size();
  291. break;
  292. case EXTMEM:
  293. assert(em);
  294. v = em->size();
  295. break;
  296. case EXTMEM_DEBUG:
  297. v1 = dim->size();
  298. v = em->size();
  299. assert(v == v1);
  300. break;
  301. }
  302. return v;
  303. }
  304. // ----------------------------------------------------------------------
  305. template<class T, class Key>
  306. bool
  307. EMPQueueAdaptive<T,Key>::extract_min(T& elt) {
  308. bool v=false, v1;
  309. T tmp;
  310. switch(regim) {
  311. case INMEM:
  312. assert(im);
  313. v = im->extract_min(elt);
  314. break;
  315. case EXTMEM:
  316. assert(em);
  317. v = em->extract_min(elt);
  318. break;
  319. case EXTMEM_DEBUG:
  320. v1 = dim->extract_min(tmp);
  321. v = em->extract_min(elt);
  322. assert(v == v1);
  323. assert(tmp == elt);
  324. assert(dim->size() == em->size());
  325. break;
  326. }
  327. return v;
  328. }
  329. //------------------------------------------------------------
  330. /* insert an element; if regim == INMEM, try insert it in im, and if
  331. it is full, extract_max pqsize/2 elements of im into a stream,
  332. switch to EXTMEM and insert the stream into em; if regim is
  333. EXTMEM, insert in em; */
  334. template<class T, class Key>
  335. bool
  336. EMPQueueAdaptive<T,Key>::insert(const T& elt) {
  337. bool v=false;
  338. switch(regim) {
  339. case INMEM:
  340. if (!im->full()) {
  341. im->insert(elt);
  342. v = true;
  343. } else {
  344. makeExternal();
  345. v = em->insert(elt); //insert the element
  346. }
  347. break;
  348. case EXTMEM:
  349. v = em->insert(elt);
  350. break;
  351. case EXTMEM_DEBUG:
  352. dim->insert(elt);
  353. v = em->insert(elt);
  354. assert(dim->size() == em->size());
  355. break;
  356. }
  357. return v;
  358. }
  359. template<class T, class Key>
  360. void
  361. EMPQueueAdaptive<T,Key>::makeExternalDebug() {
  362. assert(size() == 0);
  363. switch(regim) {
  364. case INMEM:
  365. makeExternal();
  366. break;
  367. case EXTMEM:
  368. break;
  369. case EXTMEM_DEBUG:
  370. assert(0);
  371. break;
  372. }
  373. dim = new UnboundedMinMaxHeap<T>();
  374. regim = EXTMEM_DEBUG;
  375. }
  376. template<class T>
  377. class baseCmpType {
  378. public:
  379. static int compare(const T& x, const T& y) {
  380. return (x < y ? -1 : (x > y ? 1 : 0));
  381. }
  382. };
  383. /* switch over to using an external priority queue */
  384. template<class T, class Key>
  385. void
  386. EMPQueueAdaptive<T,Key>::makeExternal() {
  387. AMI_err ae;
  388. #ifndef NDEBUG
  389. long sizeCheck;
  390. sizeCheck = size();
  391. #endif
  392. assert(regim == INMEM);
  393. regim = EXTMEM;
  394. EMPQAD_DEBUG cout << endl
  395. << "EMPQUEUEADAPTIVE: memory full: "
  396. << "switching to external-memory pqueue " << endl;
  397. //create an AMI_stream and write in it biggest half elts of im;
  398. AMI_STREAM<T> *amis0 = new AMI_STREAM<T>();
  399. AMI_STREAM<T> *amis1 = NULL;
  400. assert(amis0);
  401. unsigned long pqsize = im->size();
  402. //assert(im->size() == im->get_maxsize());
  403. T x;
  404. for (unsigned long i=0; i< pqsize/2; i++) {
  405. int z = im->extract_max(x);
  406. assert(z);
  407. ae = amis0->write_item(x);
  408. assert(ae == AMI_ERROR_NO_ERROR);
  409. }
  410. assert(amis0->stream_len() == pqsize/2);
  411. EMPQAD_DEBUG { cout << "written " << pqsize/2
  412. << " elts to stream\n"; cout.flush(); }
  413. assert(im->size() == pqsize/2 + (pqsize % 2));
  414. EMPQAD_DEBUG LOG_avail_memo();
  415. //sort the stream
  416. baseCmpType<T> fun;
  417. AMI_sort(amis0, &amis1, &fun); //XXX laura: replaced this to use a cmp obj
  418. assert(amis1);
  419. delete amis0;
  420. EMPQAD_DEBUG { cout << "sorted the stream\n"; cout.flush(); }
  421. EMPQAD_DEBUG LOG_avail_memo();
  422. //set im to NULL and initialize em from im and amis1
  423. em = new em_pqueue<T,Key>(im, amis1);
  424. im = NULL;
  425. assert(em);
  426. EMPQAD_DEBUG { cout << "empq initialized from im\n"; cout.flush(); }
  427. EMPQAD_DEBUG {em->print_size();}
  428. EMPQAD_DEBUG LOG_avail_memo();
  429. #ifndef NDEBUG
  430. assert(sizeCheck == size());
  431. #endif
  432. }
  433. #endif