Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip Iterator #68

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pull-request-validation:
- chmod +x /usr/bin/clang-format
- clang-format --version
# Check C++ code style
- source $CI_PROJECT_DIR/script/check_cpp_code_style.sh
# - source $CI_PROJECT_DIR/script/check_cpp_code_style.sh

variables:
VIKUNJA_ALPAKA_VERSIONS: "0.6.0 0.6.1 0.7.0 0.8.0"
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.18)
add_subdirectory("reduce/")
add_subdirectory("transform/")
add_subdirectory("zipIterator/")
4 changes: 4 additions & 0 deletions example/zipIterator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.18)
set(_TARGET_NAME "example_zip_iterator")
alpaka_add_executable(${_TARGET_NAME} src/zipIterator-main.cpp)
target_link_libraries(${_TARGET_NAME} PUBLIC vikunja::internalvikunja)
260 changes: 260 additions & 0 deletions example/zipIterator/src/zipIterator-main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/* Copyright 2021 Hauke Mewes, Simeon Ehrig, Victor
*
* This file is part of vikunja.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <vikunja/transform/transform.hpp>
#include <vikunja/mem/iterator/ZipIterator.hpp>

#include <alpaka/alpaka.hpp>

#include <iostream>
#include <variant>

template<std::size_t I = 0, typename FuncT, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type forEach(std::tuple<Tp...> &, FuncT) // Unused arguments are given no names
{
}

template<std::size_t I = 0, typename FuncT, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type forEach(std::tuple<Tp...>& t, FuncT f)
{
f(std::get<I>(t));
forEach<I + 1, FuncT, Tp...>(t, f);
}

template<typename TIteratorTupleVal>
void printTuple(TIteratorTupleVal tuple)
{
std::cout << "(";
int index = 0;
int tupleSize = std::tuple_size<TIteratorTupleVal>{};
forEach(tuple, [&index, tupleSize](auto &x) { std::cout << x << (++index < tupleSize ? ", " : ""); });
std::cout << ")";
}

int main()
{
// Define the accelerator here. Must be one of the enabled accelerators.
using TAcc = alpaka::AccCpuSerial<alpaka::DimInt<3u>, std::uint64_t>;

// Alpaka index type
using Idx = alpaka::Idx<TAcc>;
// Alpaka dimension type
using Dim = alpaka::Dim<TAcc>;
// Type of the extent vector
using Vec = alpaka::Vec<Dim, Idx>;
// Find the index of the CUDA blockIdx.x component. Alpaka somehow reverses
// these, i.e. the x component of cuda is always the last value in the vector
constexpr Idx xIndex = Dim::value - 1u;
// number of elements to reduce
const Idx n = static_cast<Idx>(10);
// create extent
Vec extent(Vec::all(static_cast<Idx>(1)));
extent[xIndex] = n;

// define device, platform, and queue types.
using DevAcc = alpaka::Dev<TAcc>;
using PltfAcc = alpaka::Pltf<DevAcc>;
// using QueueAcc = alpaka::test::queue::DefaultQueue<alpaka::Dev<TAcc>>;
using PltfHost = alpaka::PltfCpu;
using DevHost = alpaka::Dev<PltfHost>;
using QueueAcc = alpaka::Queue<TAcc, alpaka::Blocking>;
using QueueHost = alpaka::QueueCpuBlocking;

// Get the host device.
DevHost devHost(alpaka::getDevByIdx<PltfHost>(0u));
// Get a queue on the host device.
QueueHost queueHost(devHost);
// Select a device to execute on.
DevAcc devAcc(alpaka::getDevByIdx<PltfAcc>(0u));
// Get a queue on the accelerator device.
QueueAcc queueAcc(devAcc);

// allocate memory both on host and device.
auto deviceMem(alpaka::allocBuf<uint64_t, Idx>(devAcc, extent));
auto hostMem(alpaka::allocBuf<uint64_t, Idx>(devHost, extent));
// Fill memory on host with numbers from 0...n-1.
uint64_t* hostNative = alpaka::getPtrNative(hostMem);
for(Idx i = 0; i < n; ++i)
hostNative[i] = static_cast<uint64_t>(i + 1);
// Copy to accelerator.
alpaka::memcpy(queueAcc, deviceMem, hostMem, extent);
uint64_t* deviceNative = alpaka::getPtrNative(deviceMem);

// allocate memory both on host and device.
auto deviceMemChar(alpaka::allocBuf<char, Idx>(devAcc, extent));
auto hostMemChar(alpaka::allocBuf<char, Idx>(devHost, extent));
std::vector<char> chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
// Fill memory on host with char from 'a' to 'j'.
char* hostNativeChar = alpaka::getPtrNative(hostMemChar);
for(Idx i = 0; i < n; ++i)
{
hostNativeChar[i] = chars[i];
}
// Copy to accelerator.
alpaka::memcpy(queueAcc, deviceMemChar, hostMemChar, extent);
char* deviceNativeChar = alpaka::getPtrNative(deviceMemChar);

// allocate memory both on host and device.
auto deviceMemDouble(alpaka::allocBuf<double, Idx>(devAcc, extent));
auto hostMemDouble(alpaka::allocBuf<double, Idx>(devHost, extent));
// Fill memory on host with double numbers from 10.12...(n-1 + 10.12).
double* hostNativeDouble = alpaka::getPtrNative(hostMemDouble);
for(Idx i = 0; i < n; ++i)
hostNativeDouble[i] = static_cast<double>(i + 10.12);
// Copy to accelerator.
alpaka::memcpy(queueAcc, deviceMemDouble, hostMemDouble, extent);
double* deviceNativeDouble = alpaka::getPtrNative(deviceMemDouble);

std::cout << "\nTesting zip iterator in host with tuple<uint64_t, char, double>\n\n";

using TIteratorTuplePtr = std::tuple<uint64_t*, char*, double*>;
using TIteratorTupleVal = std::tuple<uint64_t, char, double>;
TIteratorTuplePtr zipTuple = std::make_tuple(hostNative, hostNativeChar, hostNativeDouble);
vikunja::mem::iterator::ZipIterator<TIteratorTuplePtr, TIteratorTupleVal> zipIter(zipTuple);

std::cout << "*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

std::cout << "*++zipIter: ";
printTuple(*++zipIter);
std::cout << "\n*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

std::cout << "*zipIter++: ";
printTuple(*zipIter++);
std::cout << "\n*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

zipIter += 6;
std::cout << "*zipIter += 6;\n"
<< "*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

zipIter -= 2;
std::cout << "*zipIter -= 2;\n"
<< "*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

std::cout << "*--zipIter: ";
printTuple(*--zipIter);
std::cout << "\n*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

std::cout << "*zipIter--: ";
printTuple(*zipIter--);
std::cout << "\n*zipIter: ";
printTuple(*zipIter);
std::cout << "\n\n";

// std::cout << "Double the number values of the tuple:\n"
// << "zipIter = std::make_tuple(2 * std::get<0>(*zipIter), std::get<1>(*zipIter), 2 * std::get<2>(*zipIter));\n"
// << "*zipIter: ";
// zipIter = std::make_tuple(2 * std::get<0>(*zipIter), std::get<1>(*zipIter), 2 * std::get<2>(*zipIter));
// printTuple(*zipIter);
// std::cout << "\n\n";

std::cout << "*(zipIter + 2): ";
printTuple(*(zipIter + 2));
std::cout << "\n\n";

std::cout << "*(zipIter - 3): ";
printTuple(*(zipIter - 3));
std::cout << "\n\n";

std::cout << "zipIter[0]: ";
printTuple(zipIter[0]);
std::cout << "\n";

std::cout << "zipIter[2]: ";
printTuple(zipIter[2]);
std::cout << "\n";

// std::cout << "zipIter[4] (number values has been doubled): ";
// printTuple(zipIter[4]);
// std::cout << "\n";

// std::cout << "Revert the number values for index 4\n";
// zipIter = std::make_tuple(std::get<0>(*zipIter) / 2, std::get<1>(*zipIter), std::get<2>(*zipIter) / 2);

std::cout << "zipIter[4]: ";
printTuple(zipIter[4]);
std::cout << "\n";

std::cout << "zipIter[6]: ";
printTuple(zipIter[6]);
std::cout << "\n";

std::cout << "zipIter[9]: ";
printTuple(zipIter[9]);
std::cout << "\n\n"
<< "-----\n\n";

TIteratorTuplePtr deviceZipTuple = std::make_tuple(deviceNative, deviceNativeChar, deviceNativeDouble);
vikunja::mem::iterator::ZipIterator<TIteratorTuplePtr, TIteratorTupleVal> deviceZipIter(deviceZipTuple);

auto deviceMemResult(alpaka::allocBuf<TIteratorTupleVal, Idx>(devAcc, extent));
auto hostMemResult(alpaka::allocBuf<TIteratorTupleVal, Idx>(devHost, extent));
TIteratorTupleVal* hostNativeResultPtr = alpaka::getPtrNative(hostMemResult);
TIteratorTupleVal* deviceNativeResultPtr = alpaka::getPtrNative(deviceMemResult);

auto doubleNum = [] ALPAKA_FN_HOST_ACC(TIteratorTupleVal const& t)
{
return std::make_tuple(2 * std::get<0>(t), std::get<1>(t), 2 * std::get<2>(t));
};

vikunja::transform::deviceTransform<TAcc>(
devAcc,
queueAcc,
extent[Dim::value - 1u],
deviceZipIter,
deviceNativeResultPtr,
doubleNum);

// Copy the data back to the host for validation.
alpaka::memcpy(queueAcc, hostMemResult, deviceMemResult, extent);

std::cout << "Testing accelerator: " << alpaka::getAccName<TAcc>() << " with size: " << extent.prod() << "\n"
<< "-----\n";

bool isTransformSuccess = true;
for(Idx i = 0; i < n; ++i)
{
std::cout << "n=" << i << " | Expected result: ("
<< 2 * (i + 1) << ", " << chars[i] << ", " << 2 * (i + 10.12)
<< ") | Actual result: ";
printTuple(hostNativeResultPtr[i]);

if((2 * (i + 1) == std::get<0>(hostNativeResultPtr[i])) &&
(chars[i] == std::get<1>(hostNativeResultPtr[i])) &&
(2 * (i + 10.12) == std::get<2>(hostNativeResultPtr[i]))
) {
std::cout << " | OK\n";
}
else
{
std::cout << " | NOT OK\n";
isTransformSuccess = false;
}
}

if(isTransformSuccess)
std::cout << "-----\n"
<< "Transform was successful!\n\n";
else
std::cout << "-----\n"
<< "Transform was NOT successful!\n\n";

return 0;
}
89 changes: 89 additions & 0 deletions include/vikunja/access/BaseStrategy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright 2022 Hauke Mewes, Jonas Schenke, Simeon Ehrig
*
* This file is part of vikunja.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <alpaka/alpaka.hpp>

namespace vikunja::MemAccess
{
//! Base class to implement memory access strategy.
//!
//! \tparam TIdx Index type
template<typename TIdx>
class BaseStrategy
{
protected:
TIdx m_index;
TIdx const m_maximum;

public:
//-----------------------------------------------------------------------------
//! Constructor.
//!
//! \param index The index.
//! \param maximum The first index outside of the iterator memory.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BaseStrategy(TIdx const index, TIdx const maximum)
: m_index(index)
, m_maximum(maximum)
{
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BaseStrategy(const BaseStrategy& other) = default;

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator==(const BaseStrategy& other) const -> bool
{
return (this->m_index == other.m_index) && (this->m_maximum == other.m_maximum);
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator!=(const BaseStrategy& other) const -> bool
{
return !operator==(other);
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator<(const BaseStrategy& other) const -> bool
{
return m_index < other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator>(const BaseStrategy& other) const -> bool
{
return m_index > other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator<=(const BaseStrategy& other) const -> bool
{
return m_index <= other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator>=(const BaseStrategy& other) const -> bool
{
return m_index >= other.m_index;
}

//-----------------------------------------------------------------------------
//! Get the current index.
//!
//! Returns a const reference to the current index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator*() const -> TIdx const&
{
return m_index;
}

//-----------------------------------------------------------------------------
//! Set the current index
//!
//! Returns a reference to the current index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator*() -> TIdx&
{
return m_index;
}
};

} // namespace vikunja::MemAccess
Loading