|
| 1 | +// REQUIRES: gpu |
| 2 | +// UNSUPPORTED: cuda || hip |
| 3 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 4 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 5 | +// XFAIL: gpu && !esimd_emulator |
| 6 | +//==- thread_id_test.cpp - Test to verify thread id functionlity-==// |
| 7 | +// |
| 8 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | +// See https://llvm.org/LICENSE.txt for license information. |
| 10 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +// This is basic test to validate thread id functions. |
| 15 | +// TODO: Enable the test once the GPU RT supporting the functionality reaches |
| 16 | +// the CI |
| 17 | + |
| 18 | +#include <cmath> |
| 19 | +#include <iostream> |
| 20 | +#include <sycl/ext/intel/esimd.hpp> |
| 21 | +#include <sycl/ext/intel/esimd/simd.hpp> |
| 22 | +#include <sycl/sycl.hpp> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +int ErrCnt = 0; |
| 26 | +template <typename DataT> |
| 27 | +using shared_allocator = sycl::usm_allocator<DataT, sycl::usm::alloc::shared>; |
| 28 | +template <typename DataT> |
| 29 | +using shared_vector = std::vector<DataT, shared_allocator<DataT>>; |
| 30 | + |
| 31 | +int test_thread_id() { |
| 32 | + sycl::queue Queue; |
| 33 | + shared_allocator<int32_t> Allocator(Queue); |
| 34 | + constexpr int32_t SIZE = 32; |
| 35 | + |
| 36 | + shared_vector<int32_t> VectorOutputThreadId(SIZE, -1, Allocator); |
| 37 | + shared_vector<int32_t> VectorOutputSubdeviceId(SIZE, -1, Allocator); |
| 38 | + |
| 39 | + auto GlobalRange = sycl::range<1>(SIZE); |
| 40 | + sycl::range<1> LocalRange{1}; |
| 41 | + sycl::nd_range<1> Range(GlobalRange, LocalRange); |
| 42 | + |
| 43 | + { |
| 44 | + Queue.submit([&](sycl::handler &cgh) { |
| 45 | + int32_t *VectorOutputThreadIdPtr = VectorOutputThreadId.data(); |
| 46 | + int32_t *VectorOutputSubdeviceIdPtr = VectorOutputSubdeviceId.data(); |
| 47 | + |
| 48 | + auto Kernel = ([=](sycl::nd_item<1> ndi) [[intel::sycl_explicit_simd]] { |
| 49 | + using namespace sycl::ext::intel::esimd; |
| 50 | + auto Idx = ndi.get_global_id(0); |
| 51 | + |
| 52 | + simd<int32_t, 1> VectorResultThreadId = |
| 53 | + sycl::ext::intel::experimental::esimd::get_hw_thread_id(); |
| 54 | + simd<int32_t, 1> VectorResultSubdeviceId = |
| 55 | + sycl::ext::intel::experimental::esimd::get_subdevice_id(); |
| 56 | + |
| 57 | + VectorResultThreadId.copy_to(VectorOutputThreadIdPtr + Idx); |
| 58 | + VectorResultSubdeviceId.copy_to(VectorOutputSubdeviceIdPtr + Idx); |
| 59 | + }); |
| 60 | + |
| 61 | + cgh.parallel_for(Range, Kernel); |
| 62 | + }); |
| 63 | + Queue.wait(); |
| 64 | + } |
| 65 | + |
| 66 | + int Result = 0; |
| 67 | + |
| 68 | + // Check if all returned elements are non negative |
| 69 | + Result |= |
| 70 | + !std::all_of(VectorOutputSubdeviceId.begin(), |
| 71 | + VectorOutputSubdeviceId.end(), [](int i) { return i >= 0; }); |
| 72 | + Result |= |
| 73 | + !std::all_of(VectorOutputThreadId.begin(), VectorOutputThreadId.end(), |
| 74 | + [](int i) { return i >= 0; }); |
| 75 | + |
| 76 | + // Check if returned values are not the same |
| 77 | + std::sort(VectorOutputThreadId.begin(), VectorOutputThreadId.end()); |
| 78 | + Result |= |
| 79 | + std::equal(VectorOutputThreadId.begin() + 1, VectorOutputThreadId.end(), |
| 80 | + VectorOutputThreadId.begin()); |
| 81 | + |
| 82 | + return Result; |
| 83 | +} |
| 84 | + |
| 85 | +int main() { |
| 86 | + |
| 87 | + int TestResult = 0; |
| 88 | + |
| 89 | + TestResult |= test_thread_id(); |
| 90 | + |
| 91 | + if (!TestResult) { |
| 92 | + std::cout << "Pass" << std::endl; |
| 93 | + } |
| 94 | + return TestResult; |
| 95 | +} |
0 commit comments