251 template <
class Integral>
253 template <
class Integral>
255 template <
class Integral>
257 template <
class Integral>
259 template <
class Integral>
261 template <
class Integral>
263 template <
class Integral>
265 template <
class Integral>
267 const Integral )
const;
303 ASSERT(
last >=
p,
"Scratch cannot have negative size!");
307 ASSERT(
last >=
p,
"Scratch cannot have negative size!");
308 return static_cast<bool>(
last -
p == 0);
322 void*
operator new(std::size_t) =
delete;
323 void*
operator new(std::size_t,
void*
p) =
delete;
324 void*
operator new[](std::size_t) =
delete;
325 void*
operator new[](std::size_t,
void*
p) =
delete;
326 void operator delete(
void*) =
delete;
327 void operator delete(
void*
p,
void*) =
delete;
328 void operator delete[](
void*) =
delete;
329 void operator delete[](
void*
p,
void*) =
delete;
348 template <
class Integral>
349 inline void testBounds(
const Integral& i)
const;
350 template <
class Integral>
355 template <
class Integral>
359 template <
class Integral>
390 p(reinterpret_cast<T*>(
Scratch::m_nextfree)),
410 p(reinterpret_cast<T*>(
Scratch::m_nextfree)),
411 last(p + (num0 * num1)),
432 p(reinterpret_cast<T*>(
Scratch::m_nextfree)),
433 last(p + (num0 * num1 * num2)),
462 std::stringstream errorMessage, suggestionMessage;
463 errorMessage <<
"Scratch is not big enough - exceeded size in " << m_calling_function <<
" with variable "
464 << m_variable_name << std::endl;
465 errorMessage <<
" " << m_memsize / (1024.0 * 1024.0) <<
" MB required, but there are only "
467 errorMessage <<
" Suggested to raise the property scratchSize from "
472 m_log << errorMessage.str();
473 mTerm(1, AT_, errorMessage.str());
478 for(
MInt i = 0; i < (signed)this->size(); i++) {
481 "ScratchSpace '" + m_variable_name +
"' out of bounds during init.");
485 for(
MInt i = 0; i < (signed)this->size(); i++) {
490#ifdef MAIA_EXTRA_DEBUG
492 if((*it) ==
this)
continue;
493 if((*it)->m_variable_name == this->m_variable_name) {
494 m_log <<
"WARNING: SCRATCH_DOUBLE_ALLOCATION: double usage of variable " << this->m_variable_name
495 <<
" allocated in " << (*it)->m_calling_function <<
" and " << this->m_calling_function << std::endl;
503 ScratchList::iterator iter;
527 if((*it)->m_object_id == m_object_id) {
548 std::stringstream
id, mem, memsize;
552 memsize << m_memsize;
556 message +=
"\n-----------------------------\n";
557 message +=
"Calling function:\t";
559 message +=
"\nVariable name:\t\t";
561 message +=
"\nMemory size:\t\t";
563 message +=
"\nMemory pointer:\t\t";
566 message += std::to_string(size());
584 std::stringstream
id, memsize, funct, var;
585 memsize << m_memsize;
600template <
class Integral>
602 ASSERT(checkBounds(i),
"SCRATCH_OUT_OF_BOUNDS ERROR: accessing "
603 << m_variable_name <<
" allocated from " << m_calling_function <<
" with index " << i
604 <<
" out of range [0," << size() <<
")." << std::endl
605 <<
"Note: if the array range = [0,0) the array is EMPTY. "
606 <<
"You cannot dereference elements of an empty array!");
622template <
class Integral>
624#if defined(MAIA_ASSERTS)
630template <
class Integral>
632#if defined(MAIA_ASSERTS)
640template <
class Integral>
642 return operator()(i);
645template <
class Integral>
647 return operator()(i);
655template <
class Integral>
657 ASSERT(i >= 0 && i < m_size0 && j >= 0 && j < m_size1,
658 "SCRATCH_OUT_OF_BOUNDS ERROR: accessing " << m_variable_name <<
" allocated from " << m_calling_function
659 <<
" with indices " << i <<
"," << j <<
". Dimensions are "
660 << m_size0 <<
"," << m_size1 <<
".");
661 return p[m_size1 * i + j];
665template <
class Integral>
668 ASSERT(i >= 0 && i < m_size0 && j >= 0 && j < m_size1 && k >= 0 && k < m_size2,
669 "SCRATCH_OUT_OF_BOUNDS ERROR: accessing "
670 << m_variable_name <<
" allocated from " << m_calling_function <<
" with indices " << i <<
"," << j <<
","
671 << k <<
". Dimensions are " << m_size0 <<
"," << m_size1 <<
"," << m_size2 <<
".");
672 return p[m_size1 * m_size2 * i + m_size2 * j + k];
676template <
class Integral>
678 ASSERT(i >= 0 && i < m_size0 && j >= 0 && j < m_size1,
679 "SCRATCH_OUT_OF_BOUNDS ERROR: accessing " << m_variable_name <<
" allocated from " << m_calling_function
680 <<
" with indices " << i <<
"," << j <<
". Dimensions are "
681 << m_size0 <<
"," << m_size1 <<
".");
682 return p[m_size1 * i + j];
686template <
class Integral>
688 const Integral k)
const {
689 ASSERT(i >= 0 && i < m_size0 && j >= 0 && j < m_size1 && k >= 0 && k < m_size2,
690 "SCRATCH_OUT_OF_BOUNDS ERROR: accessing "
691 << m_variable_name <<
" allocated from " << m_calling_function <<
" with indices " << i <<
"," << j <<
","
692 << k <<
". Dimensions are " << m_size0 <<
"," << m_size1 <<
"," << m_size2 <<
".");
693 return p[m_size1 * m_size2 * i + m_size2 * j + k];
701 p[m_size1 * m_size2 * i + m_size2 * j + k] = S.
p[m_size1 * m_size2 * i + m_size2 * j + k];
726 os << std::setw(7) << i <<
": " << s[i] <<
"\n";
739#ifdef MAIA_EXTRA_DEBUG
741 m_log <<
"SCRATCH WARNING: passing a pointer to the empty scratch space " << m_variable_name <<
" allocated from "
742 << m_calling_function <<
"! Make sure this pointer will NEVER be dereferenced!" << std::endl;
786 if(i >=
m_size0)
mTerm(1, FUN_,
"index i = " + to_string(i) +
" out-of-bounds [0, " + to_string(
m_size0) +
")");
787 if(j >=
m_size1)
mTerm(1, FUN_,
"index j = " + to_string(j) +
" out-of-bounds [0, " + to_string(
m_size1) +
")");
791 if(i >=
m_size0)
mTerm(1, FUN_,
"index i = " + to_string(i) +
" out-of-bounds [0, " + to_string(
m_size0) +
")");
792 if(j >=
m_size1)
mTerm(1, FUN_,
"index j = " + to_string(j) +
" out-of-bounds [0, " + to_string(
m_size1) +
")");
This class holds the complete scratch space.
static size_t getAvailableMemory()
Returns the amount of available memory in scratch.
static MFloat getAvailableMemory()
static ScratchList m_scratchSpaces
static MString printSelfReport()
Returns a shortened string summing up the scratch space state information.
static const uintptr_t ALIGNMENT_BOUNDARY
static size_t m_number_of_elements
static MString printSelf()
Returns a string summing up the scratch state information and all scratch space elements information.
static char * getEndPointer()
Returns a pointer to the end of the scratch.
static MString printSelfScratch()
Returns a string summing up the scratch state information.
static size_t getTotalMemory()
Returns the amount of total available memory in scratch.
static size_t m_number_of_cells
static char * m_totalScratch
static size_t m_usedmemsize
This class is a base class for all ScratchSpaces.
virtual MString printSelf() const =0
const MString m_variable_name
ScratchSpaceBase(size_t num, size_t size, MString name, MString varname)
Constructor.
const MString m_calling_function
static const uintptr_t ALIGNMENT_BOUNDARY
const size_t m_memsizePadded
virtual MString printSelfReport() const =0
This class is a ScratchSpace.
ScratchSpace(const ScratchSpace< T > &)=delete
const value_type & const_reference
ScratchSpace(const size_type size_, const MString &, const MString &)
reference operator[](const size_type pos)
MString printSelf() const override
Returns a string summing up this scratch space element information.
~ScratchSpace()
Destructor.
friend std::ostream & operator<<(std::ostream &os, const ScratchSpace &s)
Print contents of scratch space object.
reference operator()(const size_type pos)
MBool checkBounds(const Integral &i, maia::maia_signed) const
void checkForEmptyScratch() const
Checks if scratch space is empty before returning a pointer to it.
reference operator[](const Integral)
1D Scratch access
const_reference operator()(const size_type pos) const
reference operator()(const size_type i, const size_type j)
T * getPointer() const
Deprecated: use begin() instead!
const_reference operator[](const size_type pos) const
void testBounds(const Integral &i) const
Assert the bounds of 1D acces to an array.
const_reference operator()(const size_type i, const size_type j) const
MBool checkBounds(const Integral &i, maia::maia_unsigned) const
void fill(T val)
fill the scratch with a given value
pointer p
Deprecated: use [] instead!
MBool checkBounds(const Integral &i) const
ScratchSpace< T > & operator=(ScratchSpace< T > &)
copy entries of S to this, given there is enough space available
ScratchSpace(const size_type size0, const size_type size1, const MString &, const MString &)
void init()
extended constructor functionality
const_pointer data() const
reference operator()(const Integral)
1D Scratch access
const_pointer const_iterator
MString printSelfReport() const override
Returns a shortened string summing up this scratch space element information.
const value_type * const_pointer
size_t getMemsize() const
void mTerm(const MInt errorCode, const MString &location, const MString &message)
const MString const MString & message
constexpr T mMin(const T &x, const T &y)
constexpr T mMax(const T &x, const T &y)
std::basic_string< char > MString
Namespace for auxiliary functions/classes.
std::list< ScratchSpaceBase * > ScratchList
std::ostream & operator<<(std::ostream &os, const ScratchSpace< T > &s)
Print contents of scratch space object.
reference operator[](size_type i)
const_reference operator[](size_type i) const