Skip to content

Commit

Permalink
add scaled unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fbusato committed Nov 26, 2024
1 parent dee9385 commit 0a33496
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 69 deletions.
18 changes: 9 additions & 9 deletions libcudacxx/include/cuda/std/__linalg/scaled.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ template <class _ScalingFactor, class _NestedAccessor>
class scaled_accessor
{
public:
using __element_type = _CUDA_VSTD::add_const_t<
decltype(_CUDA_VSTD::declval<_ScalingFactor>() * _CUDA_VSTD::declval<typename _NestedAccessor::__element_type>())>;
using __reference = _CUDA_VSTD::remove_const_t<__element_type>;
using __data_handle_type = typename _NestedAccessor::__data_handle_type;
using __offset_policy = scaled_accessor<_ScalingFactor, typename _NestedAccessor::__offset_policy>;
using element_type = _CUDA_VSTD::add_const_t<
decltype(_CUDA_VSTD::declval<_ScalingFactor>() * _CUDA_VSTD::declval<typename _NestedAccessor::element_type>())>;
using reference = _CUDA_VSTD::remove_const_t<element_type>;
using data_handle_type = typename _NestedAccessor::data_handle_type;
using offset_policy = scaled_accessor<_ScalingFactor, typename _NestedAccessor::offset_policy>;

_CCCL_HIDE_FROM_ABI constexpr scaled_accessor() = default;

Expand All @@ -90,13 +90,13 @@ class scaled_accessor
, __nested_accessor_(__a)
{}

_LIBCUDACXX_HIDE_FROM_ABI constexpr __reference access(__data_handle_type __p, size_t __i) const
_LIBCUDACXX_HIDE_FROM_ABI constexpr reference access(data_handle_type __p, size_t __i) const
{
return __scaling_factor_ * typename _NestedAccessor::__element_type(__nested_accessor_.access(__p, __i));
return __scaling_factor_ * typename _NestedAccessor::element_type(__nested_accessor_.access(__p, __i));
}

_LIBCUDACXX_HIDE_FROM_ABI
typename __offset_policy::__data_handle_type constexpr offset(__data_handle_type __p, size_t __i) const
typename offset_policy::data_handle_type constexpr offset(data_handle_type __p, size_t __i) const
{
return __nested_accessor_.offset(__p, __i);
}
Expand All @@ -121,7 +121,7 @@ namespace __detail

template <class _ScalingFactor, class _NestedAccessor>
using __scaled_element_type =
_CUDA_VSTD::add_const_t<typename _CUDA_VSTD::linalg::scaled_accessor<_ScalingFactor, _NestedAccessor>::__element_type>;
_CUDA_VSTD::add_const_t<typename _CUDA_VSTD::linalg::scaled_accessor<_ScalingFactor, _NestedAccessor>::element_type>;

} // namespace __detail

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++11
// UNSUPPORTED: msvc && c++14, msvc && c++17

#include <cuda/std/__linalg/scaled.h>
#include <cuda/std/cassert>
#include <cuda/std/type_traits>

int main(int, char**)
{
// operator() value
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2, md);

assert(scaled_md(0) == 42 * 2);
assert(scaled_md(1) == 43 * 2);
}
// operator() type
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2.0f, md);

static_assert(cuda::std::is_same<decltype(scaled_md(0)), float>::value, "wrong type");
static_cast<void>(scaled_md);
}
// access()
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2, md);

assert(scaled_md.accessor().access(d.data(), 1) == 43 * 2);
}
// offset()
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2, md);

assert(scaled_md.accessor().offset(d.data(), 1) == d.data() + 1);
}
// scaling_factor()
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2, md);

assert(scaled_md.accessor().scaling_factor() == 2);
}
// nested_accessor()
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md = cuda::std::linalg::scaled(2, md);

static_assert(cuda::std::is_same<decltype(scaled_md.accessor().nested_accessor()),
cuda::std::default_accessor<element_t>>::value,
"wrong type");
static_cast<void>(scaled_md);
}
// composition
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md1 = cuda::std::linalg::scaled(2, md);
auto scaled_md2 = cuda::std::linalg::scaled(3, scaled_md1);

assert(scaled_md2(0) == 42 * 2 * 3);
assert(scaled_md2(1) == 43 * 2 * 3);
}
// copy constructor
{
using element_t = int;
cuda::std::array<element_t, 2> d{42, 43};
cuda::std::mdspan md(d.data(), 2);
auto scaled_md1 = cuda::std::linalg::scaled(2, md);
auto scaled_md2 = scaled_md1;

assert(scaled_md2(0) == 42 * 2);
assert(scaled_md2(1) == 43 * 2);
}
return 0;
}

0 comments on commit 0a33496

Please sign in to comment.