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