Files
TheRock/math-libs/libhipcxx/.upstream-tests/test/cuda/warp/warp_shuffle.pass.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

161 lines
6.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the libcu++ 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) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
// MIT License
//
// Modifications Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// NOTE(HIP/AMD): warp uses PTX which is not supported on AMD devices
// UNSUPPORTED: hipcc, hiprtc
#include <cuda/std/array>
#include <cuda/std/cassert>
#include <cuda/std/cstdint>
#include <cuda/std/type_traits>
#include <cuda/warp>
#include "test_macros.h"
template <int Value>
inline constexpr auto width_v = cuda::std::integral_constant<int, Value>{};
template <int Value>
__device__ void test_semantic()
{
using cuda::device::warp_shuffle_down;
using cuda::device::warp_shuffle_idx;
using cuda::device::warp_shuffle_up;
using cuda::device::warp_shuffle_xor;
uint32_t data = threadIdx.x;
constexpr uint32_t mask = 0xFFFFFFFF;
for (int i = 0; i < 64; i++)
{
assert(warp_shuffle_idx(data, i, mask, width_v<Value>) == __shfl_sync(mask, data, i, Value));
}
for (int i = 1; i < Value; i++)
{
assert(warp_shuffle_down(data, i, mask, width_v<Value>) == __shfl_down_sync(mask, data, i, Value));
assert(warp_shuffle_up(data, i, mask, width_v<Value>) == __shfl_up_sync(mask, data, i, Value));
assert(warp_shuffle_xor(data, i, mask, width_v<Value>) == __shfl_xor_sync(mask, data, i, Value));
}
unused(data);
unused(mask);
if (Value == 16 && threadIdx.x < 16)
{
constexpr uint32_t mask2 = 0xFFFF;
int i = 4;
assert(warp_shuffle_idx(data, i, mask2, width_v<Value>) == __shfl_sync(mask2, data, i, Value));
assert(warp_shuffle_down(data, i, mask2, width_v<Value>) == __shfl_down_sync(mask2, data, i, Value));
assert(warp_shuffle_xor(data, i, mask2, width_v<Value>) == __shfl_xor_sync(mask2, data, i, Value));
assert(warp_shuffle_up(data, i, mask2, width_v<Value>) == __shfl_up_sync(mask2, data, i, Value));
assert(warp_shuffle_up<16>(data, i, mask2) == __shfl_up_sync(mask2, data, i, Value));
}
}
template <class T>
__device__ void test_non_trivial_types(const T& data)
{
using cuda::device::warp_shuffle_down;
using cuda::device::warp_shuffle_idx;
using cuda::device::warp_shuffle_up;
using cuda::device::warp_shuffle_xor;
const T default_value{};
// idx
{
auto& data1 = threadIdx.x == 0 ? data : default_value;
auto ret = warp_shuffle_idx(data1, 0);
assert(ret.data[0] == data[0] && ret.data[1] == data[1] && ret.data[2] == data[2] && ret.data[3] == data[3]);
unused(ret);
}
{
// down
auto& data1 = threadIdx.x >= 2 ? data : default_value;
auto ret = warp_shuffle_down(data1, 2);
assert(ret.data[0] == data[0] && ret.data[1] == data[1] && ret.data[2] == data[2] && ret.data[3] == data[3]);
unused(ret);
}
{
// up
auto& data1 = threadIdx.x < 30 ? data : default_value;
auto ret = warp_shuffle_up(data1, 2);
assert(ret.data[0] == data[0] && ret.data[1] == data[1] && ret.data[2] == data[2] && ret.data[3] == data[3]);
unused(ret);
}
{
// xor
auto& data1 = threadIdx.x % 2 == 0 ? data : default_value;
auto ret = warp_shuffle_xor(data1, 1);
auto cmp_data = threadIdx.x % 2 == 0 ? default_value : data;
assert(ret.data[0] == cmp_data[0] && ret.data[1] == cmp_data[1] && ret.data[2] == cmp_data[2]
&& ret.data[3] == cmp_data[3]);
unused(ret);
unused(cmp_data);
}
}
__device__ void test_overloadings()
{
using cuda::device::warp_shuffle_down;
using cuda::device::warp_shuffle_idx;
using cuda::device::warp_shuffle_up;
using cuda::device::warp_shuffle_xor;
uint32_t data = threadIdx.x;
constexpr uint32_t mask = 0xFFFFFFFF;
for (int i = 0; i < 64; i++)
{
assert(warp_shuffle_idx(data, i, mask) == __shfl_sync(mask, data, i));
}
for (int i = 1; i < 32; i++)
{
assert(warp_shuffle_down(data, i, mask) == __shfl_down_sync(mask, data, i));
assert(warp_shuffle_up(data, i, mask) == __shfl_up_sync(mask, data, i));
assert(warp_shuffle_xor(data, i, mask) == __shfl_xor_sync(mask, data, i));
}
}
__global__ void test_kernel()
{
test_semantic<1>();
test_semantic<2>();
test_semantic<4>();
test_semantic<8>();
test_semantic<16>();
test_semantic<32>();
test_overloadings();
test_non_trivial_types(cuda::std::array<double, 4>{1.0, 2.0, 3.0, 4.0});
double array[4] = {1.0, 2.0, 3.0, 4.0};
test_non_trivial_types(array);
auto ptr = threadIdx.x == 0 ? static_cast<const void*>(array) : nullptr;
assert(cuda::device::warp_shuffle_idx(ptr, 0) == static_cast<const void*>(array));
}
int main(int, char**)
{
NV_IF_TARGET(NV_IS_HOST, (test_kernel<<<1, 32>>>();))
return 0;
}