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 accessors to Views #579

Merged
merged 4 commits into from
Nov 7, 2022
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
- uses: DoozyX/clang-format-lint-action@v0.14
with:
exclude: './thirdparty'
Expand Down
7 changes: 7 additions & 0 deletions examples/alpaka/asyncblur/asyncblur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ struct BlurKernel
auto main(int argc, char** argv) -> int
try
{
#if defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 3 && __CUDACC_VER_MINOR__ < 4
// nvcc 11.3 fails to generate the template signature for llama::View, if it has a forward declaration with a default
// argument (which we need for the default accessor)
# warning "alpaka nbody example disabled for nvcc 11.3, because it generates invalid C++ code for the host compiler"
return -1;
#else
// ALPAKA
using Dim = alpaka::DimInt<2>;

Expand Down Expand Up @@ -390,6 +396,7 @@ try
}

return 0;
#endif
}
catch(const std::exception& e)
{
Expand Down
1 change: 0 additions & 1 deletion examples/alpaka/nbody/nbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ try
# warning "alpaka nbody example disabled for nvcc <= 11.5, because the compiler segfaults"
return -1;
#else

std::cout << problemSize / 1000 << "k particles (" << problemSize * llama::sizeOf<Particle> / 1024 << "kiB)\n"
<< "Caching " << threadsPerBlock << " particles (" << threadsPerBlock * llama::sizeOf<Particle> / 1024
<< " kiB) in shared memory\n"
Expand Down
8 changes: 8 additions & 0 deletions examples/alpaka/vectoradd/vectoradd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ struct AddKernel
auto main() -> int
try
{
#if defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 3 && __CUDACC_VER_MINOR__ < 4
// nvcc 11.3 fails to generate the template signature for llama::View, if it has a forward declaration with a default
// argument (which we need for the default accessor)
# warning "alpaka nbody example disabled for nvcc 11.3, because it generates invalid C++ code for the host compiler"
return -1;
#else

// ALPAKA
using Dim = alpaka::DimInt<1>;
using Size = std::size_t;
Expand Down Expand Up @@ -160,6 +167,7 @@ try
chrono.printAndReset("Copy D->H");

return 0;
#endif
}
catch(const std::exception& e)
{
Expand Down
11 changes: 9 additions & 2 deletions examples/cuda/pitch/pitch.cu
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ namespace llamaex
auto main() -> int
try
{
#if defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 3 && __CUDACC_VER_MINOR__ < 4
// nvcc 11.3 fails to generate the template signature for llama::View, if it has a forward declaration with a default
// argument (which we need for the default accessor)
# warning "alpaka nbody example disabled for nvcc 11.3, because it generates invalid C++ code for the host compiler"
return -1;
#else
int device = 0;
checkError(cudaGetDevice(&device));
cudaDeviceProp prop{};
Expand Down Expand Up @@ -164,7 +170,7 @@ try
}

// nvcc 11.3 fails to compile the AoS mapping here
#if !(defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ == 3)
# if !(defined(__NVCC__) && __CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ == 3)
{
std::byte* mem = nullptr;
checkError(cudaMalloc(&mem, widthBytes * extents[0]));
Expand All @@ -179,12 +185,13 @@ try

stbi_write_png("pitch2.png", extents[1], extents[0], 3, host2.data(), 0);
}
#endif
# endif

if(host1 != host2)
fmt::print("ERROR: produced two different images");

return 0;
#endif
}
catch(const std::exception& e)
{
Expand Down
64 changes: 64 additions & 0 deletions include/llama/Accessors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "macros.hpp"

#include <atomic>

namespace llama::accessor
{
/// Default accessor. Passes through the given reference.
struct Default
{
template<typename Reference>
LLAMA_FN_HOST_ACC_INLINE auto operator()(Reference&& r) const -> Reference
{
return std::forward<Reference>(r);
}
};

/// Allows only read access and returns values instead of references to memory.
struct ReadOnlyByValue
{
template<typename Reference>
LLAMA_FN_HOST_ACC_INLINE auto operator()(Reference&& r) const
{
using ValueType = std::decay_t<Reference>;
if constexpr(isProxyReference<ValueType>)
return static_cast<typename ValueType::value_type>(r);
else
return ValueType{r};
}
};

/// Allows only read access by qualifying the references to memory with const. Only works on l-value references.
struct Const
{
template<typename T>
LLAMA_FN_HOST_ACC_INLINE auto operator()(T& r) const -> const T&
{
return r;
}
};

/// Qualifies references to memory with __restrict. Only works on l-value references.
struct Restrict
{
template<typename T>
LLAMA_FN_HOST_ACC_INLINE auto operator()(T& r) const -> T& __restrict
{
return r;
}
};

#ifdef __cpp_lib_atomic_ref
/// Accessor wrapping a reference into a std::atomic_ref. Can only wrap l-value references.
struct Atomic
{
template<typename T>
LLAMA_FN_HOST_ACC_INLINE auto operator()(T& r) const -> std::atomic_ref<T>
{
return std::atomic_ref<T>{r};
}
};
#endif
} // namespace llama::accessor
4 changes: 2 additions & 2 deletions include/llama/RecordRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ namespace llama
else
{
LLAMA_FORCE_INLINE_RECURSIVE
return this->view.accessor(arrayIndex(), AbsolutCoord{});
return this->view.access(arrayIndex(), AbsolutCoord{});
}
}

Expand All @@ -460,7 +460,7 @@ namespace llama
else
{
LLAMA_FORCE_INLINE_RECURSIVE
return this->view.accessor(arrayIndex(), AbsolutCoord{});
return this->view.access(arrayIndex(), AbsolutCoord{});
}
}

Expand Down
Loading