LField.hpp 21.1 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
5 6
 *
 * This program was prepared by PSI.
gsell's avatar
gsell committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 * All rights in the program are reserved by PSI.
 * Neither PSI nor the author(s)
 * makes any warranty, express or implied, or assumes any liability or
 * responsibility for the use of this software
 *
 * Visit www.amas.web.psi for more details
 *
 ***************************************************************************/

// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
20
 *
gsell's avatar
gsell committed
21 22 23 24 25 26 27
 *
 * Visit http://people.web.psi.ch/adelmann/ for more details
 *
 ***************************************************************************/

// include files
#include "Field/LField.h"
uldis_l's avatar
uldis_l committed
28

gsell's avatar
gsell committed
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
#include "Utility/PAssert.h"
#include "Utility/IpplStats.h"
#include "Utility/Unique.h"
#include <cstdlib>

// the number of bytes in a single cache line, this is generally set
// by configuration options, but gets a default value if none is given
#ifndef IPPL_CACHE_LINE_SIZE
#define IPPL_CACHE_LINE_SIZE 32
#endif

// the number of "offset blocks" to use.  We will add a small offset
// to the beginning of where in each malloced storage block the LField
// data is stored, to try to avoid having several blocks all map to
// the same cache line.  This is the maximum number of blocks that we
// will add as an offset, where each block is the size of a cache line.
#ifndef IPPL_OFFSET_BLOCKS
#define IPPL_OFFSET_BLOCKS 16
#endif

// a debugging output message macro
#ifdef DEBUG_LFIELD
#define LFIELDMSG(x) x
#else
#define LFIELDMSG(x)
#endif


//////////////////////////////////////////////////////////////////////
//
// Initialize numeric types to zero.
// Everything else uses the default ctor.
//
//////////////////////////////////////////////////////////////////////

template<class T>
struct LFieldInitializer
{
  static void apply(T&) {}
};

#define MAKE_INITIALIZER(T)        \
template <>                        \
struct LFieldInitializer<T>        \
{                                  \
  static void apply(T& x) { x=0; } \
};

MAKE_INITIALIZER(bool)
MAKE_INITIALIZER(char)
MAKE_INITIALIZER(short)
MAKE_INITIALIZER(int)
MAKE_INITIALIZER(long)
MAKE_INITIALIZER(float)
MAKE_INITIALIZER(double)
MAKE_INITIALIZER(long long)

//////////////////////////////////////////////////////////////////////
//
// Construct given the sizes.
// This builds it compressed.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
94 95
LField<T,Dim>::LField(const NDIndex<Dim>& owned,
		      const NDIndex<Dim>& allocated,
gsell's avatar
gsell committed
96
		      int vnode)
uldis_l's avatar
uldis_l committed
97 98 99 100 101 102 103 104 105 106 107 108
: vnode_m(vnode),
  P(0),
  Pinned(false),
  Owned(owned),
  Allocated(allocated),
  Begin(owned, CompressedData),
  End(CompressedData),
  overlapCacheInited(false),
  allocCompressIndex(0),
  ownedCompressIndex(-1),
  offsetBlocks(Unique::get() % IPPL_OFFSET_BLOCKS)
{
109

uldis_l's avatar
uldis_l committed
110 111 112 113 114 115 116 117 118 119 120 121 122
  // Give the LField some initial (compressed) value
  LFieldInitializer<T>::apply(*Begin);

  // If we are not actually doing compression, expand the storage out,
  // and copy the initial value to all the elements
  if (IpplInfo::noFieldCompression)
    this->ReallyUncompress(true);

  //INCIPPLSTAT(incLFields);
}

//UL: for pinned mempory allocation
template<class T, unsigned Dim>
123 124
LField<T,Dim>::LField(const NDIndex<Dim>& owned,
		      const NDIndex<Dim>& allocated,
uldis_l's avatar
uldis_l committed
125
		      int vnode, bool p)
gsell's avatar
gsell committed
126 127
  : vnode_m(vnode),
    P(0),
uldis_l's avatar
uldis_l committed
128
    Pinned(p),
gsell's avatar
gsell committed
129 130 131 132 133 134 135 136 137
    Owned(owned),
    Allocated(allocated),
    Begin(owned, CompressedData),
    End(CompressedData),
    overlapCacheInited(false),
    allocCompressIndex(0),
    ownedCompressIndex(-1),
    offsetBlocks(Unique::get() % IPPL_OFFSET_BLOCKS)
{
138

gsell's avatar
gsell committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  // Give the LField some initial (compressed) value
  LFieldInitializer<T>::apply(*Begin);

  // If we are not actually doing compression, expand the storage out,
  // and copy the initial value to all the elements
  if (IpplInfo::noFieldCompression)
    this->ReallyUncompress(true);

  //INCIPPLSTAT(incLFields);
}

//////////////////////////////////////////////////////////////////////
//
// Deep copy constructor.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
LField<T,Dim>::LField(const LField<T,Dim>& lf)
  : vnode_m(lf.vnode_m),
    P(0),
uldis_l's avatar
uldis_l committed
160
    Pinned(false),
gsell's avatar
gsell committed
161 162 163 164 165 166 167 168 169
    Owned(lf.Owned),
    Allocated(lf.Allocated),
    Begin(CompressedData),
    End(CompressedData),
    overlapCacheInited(false),
    allocCompressIndex(lf.allocCompressIndex),
    ownedCompressIndex(lf.ownedCompressIndex),
    offsetBlocks(Unique::get() % IPPL_OFFSET_BLOCKS)
{
170 171


gsell's avatar
gsell committed
172 173 174 175 176 177 178 179 180 181 182 183

  if ( lf.IsCompressed() )
    {
      // Build a compressed iterator.
      Begin = iterator(Owned,CompressedData);

      // get the constant value in lf.
      CompressedData = lf.CompressedData;
    }
  else
    {
      // Make sure we have something in this LField
184
      PAssert_NE(lf.Allocated.size(), 0);
gsell's avatar
gsell committed
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

      // If it is not compressed, allocate storage
      int n = lf.Allocated.size();
      allocateStorage(n);

      // Copy the data over.
      std::copy(lf.P, lf.P + n, P);

      // Build an iterator that counts over the real data.
      Begin = iterator(P,Owned,Allocated,CompressedData);
    }

  //INCIPPLSTAT(incLFields);
}


//////////////////////////////////////////////////////////////////////
//
// Destructor: just free the memory, if it's there.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
LField<T,Dim>::~LField()
{
  deallocateStorage();
}


//////////////////////////////////////////////////////////////////////
//
// Let the user tell us to try to compress.
// Return quickly if we already are compressed.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
bool
LField<T,Dim>::TryCompress(bool baseOnPhysicalCells)
{
225 226


gsell's avatar
gsell committed
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

  if (IsCompressed() || IpplInfo::noFieldCompression)
    return false;

  LFIELDMSG(Inform dbgmsg("LField::TryCompress", INFORM_ALL_NODES));
  LFIELDMSG(dbgmsg << "Trying to compress LField with domain = "<<getOwned());
  LFIELDMSG(dbgmsg << ", baseOnPhysicalCells = " << baseOnPhysicalCells<<endl);

  if (baseOnPhysicalCells)
    {
      if (CanCompressBasedOnPhysicalCells())
	{
	  CompressBasedOnPhysicalCells();
	  return true;
	}
    }
  else
    {
      if (CanCompress() )
	{
	  Compress();
	  return true;
	}
    }

  return false;
}


//////////////////////////////////////////////////////////////////////
//
// Look through the data and figure out if it can be compressed
// to the given value.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
bool
LField<T,Dim>::CanCompress(T val) const
{
267 268


gsell's avatar
gsell committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

  // Debugging macro
  LFIELDMSG(Inform dbgmsg("CanCompress"));

  // We definitely can't do this if compression is disabled.
  if (IpplInfo::noFieldCompression)
    return false;

  // If it is already compressed, we can compress it to any value.
  if (IsCompressed())
    //return *Begin == val;
    return true;

  // It is not currently compressed ... so go through and check
  // to see if all the elements are the same as the given argument.

  int sz = getAllocated().size();
  ADDIPPLSTAT(incCompressionCompareMax, sz);
  T *ptr1 = P;
  T *mid1 = P + allocCompressIndex;
  T *end1 = P + sz;
290 291

  PAssert_GT(sz, 0);
gsell's avatar
gsell committed
292
  PAssert(P != 0);
293 294
  PAssert_GE(allocCompressIndex, 0);
  PAssert_LT(allocCompressIndex, sz);
gsell's avatar
gsell committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

  // Quick short-cut check: compare to the last value in the
  // array that did not match before.

  if (IpplInfo::extraCompressChecks)
    {
      LFIELDMSG(dbgmsg << "Doing short-cut check, comparing " << *mid1);
      LFIELDMSG(dbgmsg << " to " << val << " at last-alloc-domain-failed");
      LFIELDMSG(dbgmsg << " index of " << allocCompressIndex << endl);
      ADDIPPLSTAT(incCompressionCompares, 1);

      if (!(*mid1 == val))
	{
	  LFIELDMSG(dbgmsg << "Short-cut check determined we cannot ");
	  LFIELDMSG(dbgmsg << "compress, by comparing " << *mid1<<" to ");
	  LFIELDMSG(dbgmsg << val << " at last-alloc-domain-failed index");
	  LFIELDMSG(dbgmsg << " of " << allocCompressIndex << endl);

	  // It failed the test, so we can just keep the same index to
314
	  // check next time, and return.
gsell's avatar
gsell committed
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
	  return false;
	}
    }

  // Check from the beginning to the last-checked-index

  LFIELDMSG(dbgmsg << "Checking for compression for " << sz << " items, ");
  LFIELDMSG(dbgmsg << "comparing to value = " << val << endl);

  if (IpplInfo::extraCompressChecks)
    {
      // First check from last-failed-position to end, since we've
      // already looked at *mid1 and should have that section of memory
      // in cache
      T *checkptr = mid1 + 1;
      while (checkptr != end1)
	{
	  if (!(*checkptr++ == val))
	    {
	      LFIELDMSG(dbgmsg << "Found that we cannot compress, after ");
	      LFIELDMSG(dbgmsg << (checkptr - mid1) << " compares (");
	      LFIELDMSG(dbgmsg << *(checkptr-1) << " != " << val << ")");
	      LFIELDMSG(dbgmsg << endl);
	      ADDIPPLSTAT(incCompressionCompares, (checkptr - mid1));
	      allocCompressIndex = (checkptr - ptr1) - 1;
	      return false;
	    }
	}

      // Next, check from the first position to the last-failed-position.
      checkptr = ptr1;
      while (checkptr != mid1)
	{
	  if (!(*checkptr++ == val))
	    {
	      LFIELDMSG(dbgmsg << "Found that we cannot compress, after ");
	      LFIELDMSG(dbgmsg << (checkptr - ptr1) + (end1 - mid1));
	      LFIELDMSG(dbgmsg << " compares (");
	      LFIELDMSG(dbgmsg << *(checkptr-1) << " != " << val << ")");
	      LFIELDMSG(dbgmsg << endl);
	      ADDIPPLSTAT(incCompressionCompares,
			   (checkptr - ptr1) + (end1 - mid1));
	      allocCompressIndex = (checkptr - ptr1) - 1;
	      return false;
	    }
	}
    }
  else
    {
      while (ptr1 != end1)
	{
	  if (!(*ptr1++ == val))
	    {
	      LFIELDMSG(dbgmsg << "Found that we cannot compress, after ");
	      LFIELDMSG(dbgmsg << (ptr1 - P) << " compares (");
	      LFIELDMSG(dbgmsg << *(ptr1-1) << " != " << val << ")");
	      LFIELDMSG(dbgmsg << endl);
	      ADDIPPLSTAT(incCompressionCompares, (ptr1 - P));
	      allocCompressIndex = (ptr1 - P) - 1;
	      return false;
	    }
	}
    }

  // If we are at this point, we did not find anything that did not
  // match, so we can compress (woo hoo).
381

gsell's avatar
gsell committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  LFIELDMSG(dbgmsg << "Found that we CAN compress, after " << sz);
  LFIELDMSG(dbgmsg << " compares." << endl);
  ADDIPPLSTAT(incCompressionCompares, sz);
  allocCompressIndex = 0;
  return true;
}


//////////////////////////////////////////////////////////////////////
//
// Return true if this LField can be compressed based on physical
// cells only and false if it could not.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
bool LField<T,Dim>::CanCompressBasedOnPhysicalCells() const
{
400 401


gsell's avatar
gsell committed
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

  // Debugging macro

  LFIELDMSG(Inform dbgmsg("LField::CanCompressBasedOnPhysicalCells",
			  INFORM_ALL_NODES));

  // We definitely can't do this if compression is disabled.
  if (IpplInfo::noFieldCompression)
    return false;

  // If it is already compressed, we can compress it to any value.
  if (IsCompressed())
    return true;

  // Make an iterator over my owned domain. The cast is there because
  // this version of begin() is not a const member function.

  iterator p = const_cast<LField<T,Dim>*>(this)->begin(getOwned());

  // Get the value to compare against, either the first item or
  // an item from the last point where our compression check failed.

  T val = *p;
  int sz = getOwned().size();
  if (IpplInfo::extraCompressChecks && ownedCompressIndex > 0)
    {
      // There was a previous value, so get that one to compare against
429
      PAssert_LT((unsigned int) ownedCompressIndex, getAllocated().size());
gsell's avatar
gsell committed
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
      val = *(P + ownedCompressIndex);
      LFIELDMSG(dbgmsg << "Checking owned cells using previous ");
      LFIELDMSG(dbgmsg << "comparison value " << val << " from index = ");
      LFIELDMSG(dbgmsg << ownedCompressIndex << " against " << sz);
      LFIELDMSG(dbgmsg << " elements." << endl);
    }
  else
    {
      // We just use the first element, and will compare against
      // the rest, so we know we can skip comparing to this first element.
      ++p;
      --sz;
      LFIELDMSG(dbgmsg << "Checking owned cells using first element " << val);
      LFIELDMSG(dbgmsg << " for comparison against " << sz << " items."<<endl);
    }

  // Loop through the other physical cells until we encounter one that
  // doesn't match the 1st cell. If this occurs, we can't compress.

  ADDIPPLSTAT(incCompressionCompareMax, sz - 1);
  for (int i=0; i < sz; ++i, ++p)
    {
      if (!(*p == val))
	{
	  LFIELDMSG(dbgmsg << "Found that we cannot compress, after ");
	  LFIELDMSG(dbgmsg << i + 1 << " compares." << endl);
	  ADDIPPLSTAT(incCompressionCompares, i + 1);
	  ownedCompressIndex = (&(*p)) - P;
	  LFIELDMSG(dbgmsg << "changed ownedCompressIndex to ");
	  LFIELDMSG(dbgmsg << ownedCompressIndex << endl);
	  return false;
	}
    }

  // Since we made it here, we can compress.

  LFIELDMSG(dbgmsg << "Found that we CAN compress, after ");
  LFIELDMSG(dbgmsg << sz << " compares." << endl);
  ADDIPPLSTAT(incCompressionCompares, sz);
  ownedCompressIndex = (-1);
  return true;
}


//////////////////////////////////////////////////////////////////////
//
// Force a compression to a specified value.  This version compresses
// the entire allocated domain.  If this is called when compression
// is turned off, it instead copies the given value into the whole
// domain's storage, so that it at least makes the whole domain
// equal to the value.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
void
LField<T,Dim>::Compress(const T& val)
{
488 489


gsell's avatar
gsell committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

  LFIELDMSG(Inform dbgmsg("LField::Compress", INFORM_ALL_NODES));
  LFIELDMSG(dbgmsg << "Compressing LField with domain = " << getOwned());
  LFIELDMSG(dbgmsg << " to new value = " << val << ", already compressed = ");
  LFIELDMSG(dbgmsg << (IsCompressed() ? 1 : 0) << endl);

  // When compression is disabled, interpret this to mean "assign every element
  // of the LField to the specified value," which is equivalent to compressing
  // the LField to the value then uncompressing it:

  if (IpplInfo::noFieldCompression)
    {
      for (iterator lit = begin(); lit != end(); ++lit)
	*lit = val;

      return;
    }

  // Compression is enabled if we're here, so save the compressed value and
  // free up memory if necessary.  We copy the value into the compressed
  // value storage, and then if we're currently compressed, we free up
  // that memory and update our iterators.

  CompressedData = val;
  if (!IsCompressed())
    {
      Begin.Compress(CompressedData);
      deallocateStorage();
    }

  //INCIPPLSTAT(incCompresses);
}


//////////////////////////////////////////////////////////////////////
//
// This function does a compressed based on physical cells only.
// It will compress to the value of the first element in the owned
// domain (instead of in the allocated domain).  If compression is
// turned off, this does nothing, it does not even attempt to fill
// in the owned domain with a value.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
void
LField<T,Dim>::CompressBasedOnPhysicalCells()
{
538 539


gsell's avatar
gsell committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

  // We do nothing in this case if compression is turned off.

  if (IpplInfo::noFieldCompression)
    return;

  // Set compression value to first element in owned domain, and free up
  // memory if necessary.

  CompressedData = *(begin(getOwned()));
  if (!IsCompressed())
    {
      Begin.Compress(CompressedData);
      deallocateStorage();
    }

  //INCIPPLSTAT(incCompresses);
}


//////////////////////////////////////////////////////////////////////
//
// We know this is compressed, so uncompress it.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
void LField<T,Dim>::ReallyUncompress(bool fill_domain)
{

570 571 572


  PAssert_NE(Allocated.size(), 0);
gsell's avatar
gsell committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

  // Allocate the data.

  int n = Allocated.size();
  allocateStorage(n);

  LFIELDMSG(Inform dbgmsg("LField::ReallyUncompress", INFORM_ALL_NODES));
  LFIELDMSG(dbgmsg << "Uncompressing LField with domain = " << getOwned());
  LFIELDMSG(dbgmsg << ", fill_domain = " << (fill_domain ? 1 : 0) << endl);

  // Copy the constant value into the new space.

  if (fill_domain)
    {
      T val = *Begin;
      for (int i=0; i<n; i++)
	P[i] = val;
    }
#ifdef IPPL_PURIFY
  else
    {
      // To avoid Purify UMR's, fill with default value anyway
      T val = T();
      for (int i=0; i<n; i++)
	P[i] = val;
    }
#endif

  // Make the Begin iterator point to the new data.

  Begin = iterator(P,Owned,Allocated,CompressedData);

  // Indicate we've done one more decompress

  //INCIPPLSTAT(incDecompresses);
}


//////////////////////////////////////////////////////////////////////
//
// get an iterator over a subrange.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
618
typename LField<T,Dim>::iterator
gsell's avatar
gsell committed
619 620 621
LField<T,Dim>::begin(const NDIndex<Dim>& domain)
{
  // Remove this profiling because this is too lightweight.
622 623
  //
  //
gsell's avatar
gsell committed
624 625 626 627 628 629 630 631 632 633 634 635 636
  return iterator(P,domain,Allocated,CompressedData);
}


//////////////////////////////////////////////////////////////////////
//
// Get an iterator over a subrange, when we might want to try to
// compress the data in the subrange without affecting the rest of
// the LField data.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
637
typename LField<T,Dim>::iterator
gsell's avatar
gsell committed
638
LField<T,Dim>::begin(const NDIndex<Dim>& domain, T& compstore)
639
{
gsell's avatar
gsell committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653

  if (IsCompressed())
    compstore = CompressedData;
  return iterator(P,domain,Allocated,compstore);
}


//////////////////////////////////////////////////////////////////////
//
// Swap the pointers between two LFields.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
654
void
gsell's avatar
gsell committed
655 656
LField<T,Dim>::swapData( LField<T,Dim>& a )
{
657 658


gsell's avatar
gsell committed
659 660 661 662 663 664 665

  // Swap the pointers to the data.
  {
    T *temp=P;
    P=a.P;
    a.P=temp;
  }
666

gsell's avatar
gsell committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
  // Swap the compressed data.
  {
    T temp = CompressedData;
    CompressedData = a.CompressedData;
    a.CompressedData = temp;
  }

  // Swap the last-compared-for-compression indices
  {
    int temp = allocCompressIndex;
    allocCompressIndex = a.allocCompressIndex;
    a.allocCompressIndex = temp;
    temp = ownedCompressIndex;
    ownedCompressIndex = a.ownedCompressIndex;
    a.ownedCompressIndex = temp;
  }

  // Swap the offset block value
  {
    int temp = offsetBlocks;
    offsetBlocks = a.offsetBlocks;
    a.offsetBlocks = temp;
  }

  // Reinitialize the begin iterators.
  Begin = iterator(P,Owned,Allocated,CompressedData);
  a.Begin = iterator(a.P,a.Owned,a.Allocated,a.CompressedData);

  // Make sure the domains agree.
  PAssert(Owned == a.Owned);
  PAssert(Allocated == a.Allocated);

  // Should we swap the overlap caches?
}


//////////////////////////////////////////////////////////////////////
//
// Actualy allocate storage for the LField data, doing any special
// memory tricks needed for performance.  Sets P pointer to new memory.
//
//////////////////////////////////////////////////////////////////////

710
// allocate memory for LField and if DKS is used and page-locked (pl) is +1 allocate
uldis_l's avatar
uldis_l committed
711
// page-locked memory for storage
gsell's avatar
gsell committed
712
template<class T, unsigned Dim>
713
void
gsell's avatar
gsell committed
714 715 716
LField<T,Dim>::allocateStorage(int newsize)
{
  PAssert(P == 0);
717 718
  PAssert_GT(newsize, 0);
  PAssert_GE(offsetBlocks, 0);
gsell's avatar
gsell committed
719 720 721 722 723 724 725 726 727 728

  // Determine how many blocks to offset the data, if we are asked to

  int extra = 0;
  if (IpplInfo::offsetStorage)
    extra = offsetBlocks*IPPL_CACHE_LINE_SIZE / sizeof(T);

  // Allocate the storage, creating some extra to account for offset, and
  // then add in the offset.
#ifdef IPPL_DIRECTIO
729
  P = (T *)valloc(sizeof(T) * (newsize + extra));
uldis_l's avatar
uldis_l committed
730 731
#else
    P = new T[newsize + extra];
gsell's avatar
gsell committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745
#endif
  P += extra;

  ADDIPPLSTAT(incLFieldBytes, (newsize+extra)*sizeof(T));
}


//////////////////////////////////////////////////////////////////////
//
// Actually free the storage used in the LField, if any.  Resets P to zero.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
746
void
gsell's avatar
gsell committed
747 748 749 750 751 752 753 754 755 756 757 758 759
LField<T,Dim>::deallocateStorage()
{
  if (P != 0)
    {
      // Determine how many blocks to offset the data, if we are asked to.
      // If so, move the P pointer back.

      if (IpplInfo::offsetStorage)
	P -= (offsetBlocks*IPPL_CACHE_LINE_SIZE / sizeof(T));

      // Free the storage

#ifdef IPPL_DIRECTIO
760
      free(P);
uldis_l's avatar
uldis_l committed
761
#else
762
      delete [] P;
gsell's avatar
gsell committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
#endif
      // Reset our own pointer to zero

      P = 0;
    }
}


//////////////////////////////////////////////////////////////////////
//
// print an LField out
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
void LField<T,Dim>::write(std::ostream& out) const
{
780 781


gsell's avatar
gsell committed
782 783 784 785 786 787 788 789
  for (iterator p = begin(); p!=end(); ++p)
    out << *p << " ";
}


/***************************************************************************
 * $RCSfile: LField.cpp,v $   $Author: adelmann $
 * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:26 $
790 791
 * IPPL_VERSION_ID: $Id: LField.cpp,v 1.1.1.1 2003/01/23 07:40:26 adelmann Exp $
 ***************************************************************************/