LField.hpp 20.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// Class LField
//   Local Field class
//
// Copyright (c) 2003 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL 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 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
gsell's avatar
gsell committed
18
#include "Field/LField.h"
uldis_l's avatar
uldis_l committed
19

gsell's avatar
gsell committed
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
#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>
85
LField<T,Dim>::LField(const NDIndex<Dim>& owned,
86 87
                      const NDIndex<Dim>& allocated,
                      int vnode)
uldis_l's avatar
uldis_l committed
88 89 90 91 92 93 94 95 96 97 98 99
: 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)
{
100

uldis_l's avatar
uldis_l committed
101 102 103 104 105 106 107 108 109 110 111 112 113
  // 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>
114
LField<T,Dim>::LField(const NDIndex<Dim>& owned,
115 116
                      const NDIndex<Dim>& allocated,
                      int vnode, bool p)
gsell's avatar
gsell committed
117 118
  : vnode_m(vnode),
    P(0),
uldis_l's avatar
uldis_l committed
119
    Pinned(p),
gsell's avatar
gsell committed
120 121 122 123 124 125 126 127 128
    Owned(owned),
    Allocated(allocated),
    Begin(owned, CompressedData),
    End(CompressedData),
    overlapCacheInited(false),
    allocCompressIndex(0),
    ownedCompressIndex(-1),
    offsetBlocks(Unique::get() % IPPL_OFFSET_BLOCKS)
{
129

gsell's avatar
gsell committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  // 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
151
    Pinned(false),
gsell's avatar
gsell committed
152 153 154 155 156 157 158 159 160
    Owned(lf.Owned),
    Allocated(lf.Allocated),
    Begin(CompressedData),
    End(CompressedData),
    overlapCacheInited(false),
    allocCompressIndex(lf.allocCompressIndex),
    ownedCompressIndex(lf.ownedCompressIndex),
    offsetBlocks(Unique::get() % IPPL_OFFSET_BLOCKS)
{
161 162


gsell's avatar
gsell committed
163 164 165 166 167 168 169 170 171 172 173 174

  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
175
      PAssert_NE(lf.Allocated.size(), 0);
gsell's avatar
gsell committed
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

      // 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)
{
216 217


gsell's avatar
gsell committed
218 219 220 221 222 223 224 225 226 227 228

  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())
229 230 231 232
        {
          CompressBasedOnPhysicalCells();
          return true;
        }
gsell's avatar
gsell committed
233 234 235 236
    }
  else
    {
      if (CanCompress() )
237 238 239 240
        {
          Compress();
          return true;
        }
gsell's avatar
gsell committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    }

  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
{
258 259


gsell's avatar
gsell committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

  // 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;
281 282

  PAssert_GT(sz, 0);
gsell's avatar
gsell committed
283
  PAssert(P != 0);
284 285
  PAssert_GE(allocCompressIndex, 0);
  PAssert_LT(allocCompressIndex, sz);
gsell's avatar
gsell committed
286 287 288 289 290 291 292 293 294 295 296 297

  // 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))
298 299 300 301 302 303 304 305 306 307
        {
          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
          // check next time, and return.
          return false;
        }
gsell's avatar
gsell committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    }

  // 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)
322 323 324 325 326 327 328 329 330 331 332 333
        {
          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;
            }
        }
gsell's avatar
gsell committed
334 335 336 337

      // Next, check from the first position to the last-failed-position.
      checkptr = ptr1;
      while (checkptr != mid1)
338 339 340 341 342 343 344 345 346 347 348 349 350 351
        {
          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;
            }
        }
gsell's avatar
gsell committed
352 353 354 355
    }
  else
    {
      while (ptr1 != end1)
356 357 358 359 360 361 362 363 364 365 366 367
        {
          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;
            }
        }
gsell's avatar
gsell committed
368 369 370 371
    }

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

gsell's avatar
gsell committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
  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
{
391 392


gsell's avatar
gsell committed
393 394 395 396

  // Debugging macro

  LFIELDMSG(Inform dbgmsg("LField::CanCompressBasedOnPhysicalCells",
397
                          INFORM_ALL_NODES));
gsell's avatar
gsell committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  // 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
420
      PAssert_LT((unsigned int) ownedCompressIndex, getAllocated().size());
gsell's avatar
gsell committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
      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))
444 445 446 447 448 449 450 451 452
        {
          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;
        }
gsell's avatar
gsell committed
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
    }

  // 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)
{
479 480


gsell's avatar
gsell committed
481 482 483 484 485 486 487 488 489 490 491 492 493

  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)
494
        *lit = val;
gsell's avatar
gsell committed
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

      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()
{
529 530


gsell's avatar
gsell committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

  // 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)
{

561 562 563


  PAssert_NE(Allocated.size(), 0);
gsell's avatar
gsell committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

  // 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++)
580
        P[i] = val;
gsell's avatar
gsell committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    }

  // 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>
600
typename LField<T,Dim>::iterator
gsell's avatar
gsell committed
601 602 603
LField<T,Dim>::begin(const NDIndex<Dim>& domain)
{
  // Remove this profiling because this is too lightweight.
604 605
  //
  //
gsell's avatar
gsell committed
606 607 608 609 610 611 612 613 614 615 616 617 618
  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>
619
typename LField<T,Dim>::iterator
gsell's avatar
gsell committed
620
LField<T,Dim>::begin(const NDIndex<Dim>& domain, T& compstore)
621
{
gsell's avatar
gsell committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635

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


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

template<class T, unsigned Dim>
636
void
gsell's avatar
gsell committed
637 638
LField<T,Dim>::swapData( LField<T,Dim>& a )
{
639 640


gsell's avatar
gsell committed
641 642 643 644 645 646 647

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

gsell's avatar
gsell committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 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
  // 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.
//
//////////////////////////////////////////////////////////////////////

template<class T, unsigned Dim>
693
void
gsell's avatar
gsell committed
694 695 696
LField<T,Dim>::allocateStorage(int newsize)
{
  PAssert(P == 0);
697 698
  PAssert_GT(newsize, 0);
  PAssert_GE(offsetBlocks, 0);
gsell's avatar
gsell committed
699 700 701 702 703 704 705 706 707

  // 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.
708
  P = new T[newsize + extra]();
gsell's avatar
gsell committed
709 710 711 712 713 714 715 716 717 718 719 720 721
  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>
722
void
gsell's avatar
gsell committed
723 724 725 726 727 728 729 730
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)
731
        P -= (offsetBlocks*IPPL_CACHE_LINE_SIZE / sizeof(T));
gsell's avatar
gsell committed
732

733
      delete [] P;
gsell's avatar
gsell committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747
      P = 0;
    }
}


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

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


gsell's avatar
gsell committed
750 751
  for (iterator p = begin(); p!=end(); ++p)
    out << *p << " ";
752
}