Skip to content

Commit

Permalink
Merge pull request #3169 from JasonRuonanWang/datatype
Browse files Browse the repository at this point in the history
move Span class to dedicated files
  • Loading branch information
JasonRuonanWang authored Apr 18, 2022
2 parents 3880a57 + 43d8304 commit 84cbfa6
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 100 deletions.
1 change: 1 addition & 0 deletions source/adios2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(adios2_core
core/Stream.cpp core/Stream.tcc
core/Variable.cpp core/Variable.tcc
core/VariableBase.cpp
core/Span.cpp core/Span.tcc
core/Group.cpp core/Group.tcc

#operator
Expand Down
23 changes: 23 additions & 0 deletions source/adios2/core/Span.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Span.cpp
*
* Created on: Apr 17, 2022
* Author: Jason Wang jason.ruonan.wang@gmail.com
*/

#include "Span.tcc"

namespace adios2
{
namespace core
{

#define declare_template_instantiation(T) template class Span<T>;
ADIOS2_FOREACH_PRIMITIVE_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_type

} // end namespace core
} // end namespace adios2
53 changes: 53 additions & 0 deletions source/adios2/core/Span.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Span.h
*
* Created on: Apr 17, 2022
* Author: Jason Wang jason.ruonan.wang@gmail.com
*/

#ifndef ADIOS2_CORE_SPAN_H_
#define ADIOS2_CORE_SPAN_H_

#include "adios2/core/VariableBase.h"

namespace adios2
{
namespace core
{

template <class T>
class Span
{
public:
std::pair<size_t, size_t> m_MinMaxMetadataPositions;

// internal position variables from which the engine
// can return a valid pointer any time
// BP5 needs two levels of reference, BP3/4 uses only one
size_t m_PayloadPosition = 0;
int m_BufferIdx = -1;

T m_Value = T{};

Span(Engine &engine, const size_t size);
~Span() = default;

size_t Size() const noexcept;
T *Data() const noexcept;

T &At(const size_t position);

T &operator[](const size_t position);

private:
Engine &m_Engine;
size_t m_Size = 0;
};

} // end namespace core
} // end namespace adios2

#endif // ADIOS2_CORE_SPAN_H_
66 changes: 66 additions & 0 deletions source/adios2/core/Span.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Span.tcc
*
* Created on: Apr 17, 2022
* Author: Jason Wang jason.ruonan.wang@gmail.com
*/

#ifndef ADIOS2_CORE_SPAN_TCC_
#define ADIOS2_CORE_SPAN_TCC_

#include "Span.h"

#include "adios2/core/Engine.h"

namespace adios2
{
namespace core
{

template <class T>
Span<T>::Span(Engine &engine, const size_t size)
: m_Engine(engine), m_Size(size)
{
}

template <class T>
size_t Span<T>::Size() const noexcept
{
return m_Size;
}

template <class T>
T *Span<T>::Data() const noexcept
{
return m_Engine.BufferData<T>(m_BufferIdx, m_PayloadPosition);
}

template <class T>
T &Span<T>::At(const size_t position)
{
if (position > m_Size)
{
helper::Throw<std::invalid_argument>(
"Core", "Span", "At",
"position " + std::to_string(position) +
" is out of bounds for span of size " + std::to_string(m_Size));
}

return (*this)[position];
}

template <class T>
T &Span<T>::operator[](const size_t position)
{
T &data = *m_Engine.BufferData<T>(m_BufferIdx,
m_PayloadPosition + position * sizeof(T));
return data;
}

} // end namespace core
} // end namespace adios2

#endif // ADIOS2_CORE_SPAN_TCC_
4 changes: 0 additions & 4 deletions source/adios2/core/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,5 @@ namespace core
ADIOS2_FOREACH_STDTYPE_1ARG(declare_type)
#undef declare_type

#define declare_template_instantiation(T) template class Span<T>;
ADIOS2_FOREACH_PRIMITIVE_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_type

} // end namespace core
} // end namespace adios2
33 changes: 1 addition & 32 deletions source/adios2/core/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>
/// \endcond

#include "Span.h"
#include "adios2/common/ADIOSTypes.h"
#include "adios2/core/VariableBase.h"
#include "adios2/helper/adiosMath.h"
Expand All @@ -29,38 +30,6 @@ namespace adios2
namespace core
{

template <class T>
class Span
{
public:
std::pair<size_t, size_t> m_MinMaxDataPositions;
std::pair<size_t, size_t> m_MinMaxMetadataPositions;

// internal position variables from which the engine
// can return a valid pointer any time
// BP5 needs two levels of reference, BP3/4 uses only one
size_t m_PayloadPosition = 0;
int m_BufferIdx = -1;

T m_Value = T{};

Span(Engine &engine, const size_t size);
~Span() = default;

size_t Size() const noexcept;
T *Data() const noexcept;

T &At(const size_t position);
const T &At(const size_t position) const;

T &operator[](const size_t position);
const T &operator[](const size_t position) const;

private:
Engine &m_Engine;
size_t m_Size = 0;
};

/**
* @param Base (parent) class for template derived (child) class Variable.
*/
Expand Down
64 changes: 0 additions & 64 deletions source/adios2/core/Variable.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -257,70 +257,6 @@ void Variable<T>::CheckRandomAccess(const size_t step,
}
}

// Span functions
template <class T>
Span<T>::Span(Engine &engine, const size_t size)
: m_Engine(engine), m_Size(size)
{
}

template <class T>
size_t Span<T>::Size() const noexcept
{
return m_Size;
}

template <class T>
T *Span<T>::Data() const noexcept
{
return m_Engine.BufferData<T>(m_BufferIdx, m_PayloadPosition);
}

template <class T>
T &Span<T>::At(const size_t position)
{
if (position > m_Size)
{
helper::Throw<std::invalid_argument>(
"Core", "Variable", "At",
"position " + std::to_string(position) +
" is out of bounds for span of size " + std::to_string(m_Size));
}

return (*this)[position];
}

template <class T>
const T &Span<T>::At(const size_t position) const
{
if (position > m_Size)
{
helper::Throw<std::invalid_argument>(
"Core", "Variable", "At",
"position " + std::to_string(position) +
" is out of bounds for span of size " + std::to_string(m_Size) +
" , in call to const T& Span<T>::At");
}

return (*this)[position];
}

template <class T>
T &Span<T>::operator[](const size_t position)
{
T &data = *m_Engine.BufferData<T>(m_BufferIdx,
m_PayloadPosition + position * sizeof(T));
return data;
}

template <class T>
const T &Span<T>::operator[](const size_t position) const
{
const T &data = *m_Engine.BufferData<T>(
m_BufferIdx, m_PayloadPosition + position * sizeof(T));
return data;
}

} // end namespace core
} // end namespace adios2

Expand Down

0 comments on commit 84cbfa6

Please sign in to comment.