Skip to content

Commit

Permalink
Simplify namespace of mathTest
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber authored and psychocoderHPC committed Nov 28, 2023
1 parent 4c981f0 commit f23b298
Show file tree
Hide file tree
Showing 13 changed files with 767 additions and 889 deletions.
178 changes: 84 additions & 94 deletions test/unit/math/src/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,91 @@

#include <ostream>

namespace alpaka
namespace mathtest
{
namespace test
//! Provides alpaka-style buffer with arguments' data.
//! TData can be a plain value or a complex data-structure.
//! The operator() is overloaded and returns the value from the correct Buffer,
//! either from the host (index) or device buffer (index, acc).
//! Index out of range errors are not checked.
//! @brief Encapsulates buffer initialisation and communication with Device.
//! @tparam TAcc Used accelerator, not interchangeable
//! @tparam TData The Data-type, only restricted by the alpaka-interface.
//! @tparam Tcapacity The size of the buffer.
template<typename TAcc, typename TData, size_t Tcapacity>
struct Buffer
{
namespace unit
using value_type = TData;
static constexpr size_t capacity = Tcapacity;
using Dim = typename alpaka::trait::DimType<TAcc>::type;
using Idx = typename alpaka::trait::IdxType<TAcc>::type;

// Defines using's for alpaka-buffer.
using DevHost = alpaka::DevCpu;
using PlatformHost = alpaka::Platform<DevHost>;
using BufHost = alpaka::Buf<DevHost, TData, Dim, Idx>;

using DevAcc = alpaka::Dev<TAcc>;
using PlatformAcc = alpaka::Platform<DevAcc>;
using BufAcc = alpaka::Buf<DevAcc, TData, Dim, Idx>;

PlatformHost platformHost;
DevHost devHost;

BufHost hostBuffer;
BufAcc devBuffer;
PlatformAcc platformAcc;

// Native pointer to access buffer.
TData* const pHostBuffer;
TData* const pDevBuffer;

// This constructor cant be used,
// because BufHost and BufAcc need to be initialised.
Buffer() = delete;

// Constructor needs to initialize all Buffer.
Buffer(DevAcc const& devAcc)
: devHost{alpaka::getDevByIdx(platformHost, 0)}
, hostBuffer{alpaka::allocMappedBufIfSupported<TData, Idx>(devHost, platformAcc, Tcapacity)}
, devBuffer{alpaka::allocBuf<TData, Idx>(devAcc, Tcapacity)}
, pHostBuffer{alpaka::getPtrNative(hostBuffer)}
, pDevBuffer{alpaka::getPtrNative(devBuffer)}
{
namespace math
}

// Copy Host -> Acc.
template<typename Queue>
auto copyToDevice(Queue queue) -> void
{
alpaka::memcpy(queue, devBuffer, hostBuffer);
}

// Copy Acc -> Host.
template<typename Queue>
auto copyFromDevice(Queue queue) -> void
{
alpaka::memcpy(queue, hostBuffer, devBuffer);
}

ALPAKA_FN_ACC auto operator()(size_t idx, TAcc const& /* acc */) const -> TData&
{
return pDevBuffer[idx];
}

ALPAKA_FN_HOST auto operator()(size_t idx) const -> TData&
{
return pHostBuffer[idx];
}

ALPAKA_FN_HOST friend auto operator<<(std::ostream& os, Buffer const& buffer) -> std::ostream&
{
os << "capacity: " << capacity << "\n";
for(size_t i = 0; i < capacity; ++i)
{
//! Provides alpaka-style buffer with arguments' data.
//! TData can be a plain value or a complex data-structure.
//! The operator() is overloaded and returns the value from the correct Buffer,
//! either from the host (index) or device buffer (index, acc).
//! Index out of range errors are not checked.
//! @brief Encapsulates buffer initialisation and communication with Device.
//! @tparam TAcc Used accelerator, not interchangeable
//! @tparam TData The Data-type, only restricted by the alpaka-interface.
//! @tparam Tcapacity The size of the buffer.
template<typename TAcc, typename TData, size_t Tcapacity>
struct Buffer
{
using value_type = TData;
static constexpr size_t capacity = Tcapacity;
using Dim = typename alpaka::trait::DimType<TAcc>::type;
using Idx = typename alpaka::trait::IdxType<TAcc>::type;

// Defines using's for alpaka-buffer.
using DevHost = alpaka::DevCpu;
using PlatformHost = alpaka::Platform<DevHost>;
using BufHost = alpaka::Buf<DevHost, TData, Dim, Idx>;

using DevAcc = alpaka::Dev<TAcc>;
using PlatformAcc = alpaka::Platform<DevAcc>;
using BufAcc = alpaka::Buf<DevAcc, TData, Dim, Idx>;

PlatformHost platformHost;
DevHost devHost;

BufHost hostBuffer;
BufAcc devBuffer;
PlatformAcc platformAcc;

// Native pointer to access buffer.
TData* const pHostBuffer;
TData* const pDevBuffer;

// This constructor cant be used,
// because BufHost and BufAcc need to be initialised.
Buffer() = delete;

// Constructor needs to initialize all Buffer.
Buffer(DevAcc const& devAcc)
: devHost{alpaka::getDevByIdx(platformHost, 0)}
, hostBuffer{alpaka::allocMappedBufIfSupported<TData, Idx>(devHost, platformAcc, Tcapacity)}
, devBuffer{alpaka::allocBuf<TData, Idx>(devAcc, Tcapacity)}
, pHostBuffer{alpaka::getPtrNative(hostBuffer)}
, pDevBuffer{alpaka::getPtrNative(devBuffer)}
{
}

// Copy Host -> Acc.
template<typename Queue>
auto copyToDevice(Queue queue) -> void
{
alpaka::memcpy(queue, devBuffer, hostBuffer);
}

// Copy Acc -> Host.
template<typename Queue>
auto copyFromDevice(Queue queue) -> void
{
alpaka::memcpy(queue, hostBuffer, devBuffer);
}

ALPAKA_FN_ACC auto operator()(size_t idx, TAcc const& /* acc */) const -> TData&
{
return pDevBuffer[idx];
}

ALPAKA_FN_HOST auto operator()(size_t idx) const -> TData&
{
return pHostBuffer[idx];
}

ALPAKA_FN_HOST friend auto operator<<(std::ostream& os, Buffer const& buffer) -> std::ostream&
{
os << "capacity: " << capacity << "\n";
for(size_t i = 0; i < capacity; ++i)
{
os << i << ": " << buffer.pHostBuffer[i] << "\n";
}
return os;
}
};

} // namespace math
} // namespace unit
} // namespace test
} // namespace alpaka
os << i << ": " << buffer.pHostBuffer[i] << "\n";
}
return os;
}
};
} // namespace mathtest
Loading

0 comments on commit f23b298

Please sign in to comment.