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

Add vecmem/device interaction to alpaka test #549

Merged
merged 5 commits into from
Apr 23, 2024
Merged
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
4 changes: 4 additions & 0 deletions tests/alpaka/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
if(alpaka_ACC_GPU_CUDA_ENABLE)
enable_language(CUDA)
set_source_files_properties(alpaka_basic.cpp PROPERTIES LANGUAGE CUDA)
list(APPEND DEVICE_LIBRARIES vecmem::cuda)
elseif(alpaka_ACC_GPU_HIP_ENABLE)
enable_language(HIP)
set_source_files_properties(alpaka_basic.cpp PROPERTIES LANGUAGE HIP)
list(APPEND DEVICE_LIBRARIES vecmem::hip)
endif()

traccc_add_test( alpaka
alpaka_basic.cpp
LINK_LIBRARIES
GTest::gtest_main
alpaka::alpaka
vecmem::core
${DEVICE_LIBRARIES}
)
89 changes: 89 additions & 0 deletions tests/alpaka/alpaka_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
// Alpaka include(s).
#include <alpaka/alpaka.hpp>
#include <alpaka/example/ExampleDefaultAcc.hpp>
#include <vecmem/containers/data/vector_buffer.hpp>
#include <vecmem/containers/device_vector.hpp>
#include <vecmem/containers/vector.hpp>
#include <vecmem/memory/host_memory_resource.hpp>
#include <vecmem/utils/copy.hpp>

#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
#include <vecmem/memory/cuda/device_memory_resource.hpp>
#include <vecmem/memory/cuda/host_memory_resource.hpp>
#include <vecmem/utils/cuda/copy.hpp>
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED)
#include <vecmem/memory/hip/device_memory_resource.hpp>
#include <vecmem/memory/hip/host_memory_resource.hpp>
#include <vecmem/utils/hip/copy.hpp>
#endif

// GoogleTest include(s).
#include <gtest/gtest.h>
Expand Down Expand Up @@ -85,3 +100,77 @@ GTEST_TEST(AlpakaBasic, VectorOp) {
EXPECT_FLOAT_EQ(bufHost[i], std::sin(i));
}
}

struct VecMemOpKernel {
template <typename Acc>
ALPAKA_FN_ACC void operator()(
Acc const& acc, vecmem::data::vector_view<float> result) const {
using namespace alpaka;
auto const globalThreadIdx =
getIdx<alpaka::Grid, alpaka::Threads>(acc)[0u];

// It is possible to get index type from the accelerator,
// but for simplicity we just repeat the type here
if (globalThreadIdx < result.size()) {
result.ptr()[globalThreadIdx] = process(acc, globalThreadIdx);
}
}
};

GTEST_TEST(AlpakaBasic, VecMemOp) {

using namespace alpaka;
using Dim = DimInt<1>;
using Idx = uint32_t;

// Select a device and create queue for it
using Acc = ExampleDefaultAcc<Dim, Idx>;
auto const platformAcc = alpaka::Platform<Acc>{};
auto const devAcc = getDevByIdx(platformAcc, 0u);

using Queue = Queue<Acc, Blocking>;
auto queue = Queue{devAcc};

uint32_t n = 10000;

uint32_t blocksPerGrid = n;
uint32_t threadsPerBlock = 1;
uint32_t elementsPerThread = 4;
using WorkDiv = WorkDivMembers<Dim, Idx>;
auto workDiv = WorkDiv{blocksPerGrid, threadsPerBlock, elementsPerThread};

vecmem::host_memory_resource host_mr;
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
vecmem::cuda::copy vm_copy;
vecmem::cuda::device_memory_resource device_mr;
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED)
vecmem::hip::copy vm_copy;
vecmem::hip::device_memory_resource device_mr;
#else
vecmem::copy vm_copy;
vecmem::host_memory_resource device_mr;
#endif

vecmem::vector<float> host_vector{n, &host_mr};

auto host_buffer = vecmem::get_data(host_vector);
auto device_buffer = vm_copy.to(vecmem::get_data(host_vector), device_mr,
vecmem::copy::type::host_to_device);
auto data_dev_vec_buf = vecmem::get_data(device_buffer);
krasznaa marked this conversation as resolved.
Show resolved Hide resolved

std::cout << "Using alpaka accelerator: " << alpaka::getAccName<Acc>()
<< std::endl;

// Create a device for host for memory allocation, using the first CPU
// available
auto const platformDevCpu = alpaka::Platform<DevCpu>{};
auto devHost = getDevByIdx(platformDevCpu, 0u);

alpaka::exec<Acc>(queue, workDiv, VecMemOpKernel{}, data_dev_vec_buf);

vm_copy(device_buffer, host_buffer, vecmem::copy::type::device_to_host);

for (uint32_t i = 0u; i < n; i++) {
EXPECT_FLOAT_EQ(host_vector[i], std::sin(i));
}
}
Loading