|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 2 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 3 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
| 5 | + |
| 6 | +//==---------- wrapped_usm_pointer.cpp - test pointers in struct ---------==// |
| 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 | +#include <iostream> |
| 15 | +#include <sycl/sycl.hpp> |
| 16 | + |
| 17 | +struct Simple { |
| 18 | + int *Data; |
| 19 | + int Addition; |
| 20 | +}; |
| 21 | + |
| 22 | +struct WrapperOfSimple { |
| 23 | + int Addition; |
| 24 | + Simple Obj; |
| 25 | +}; |
| 26 | + |
| 27 | +struct NonTrivial { |
| 28 | + int Addition; |
| 29 | + int *Data; |
| 30 | + |
| 31 | + NonTrivial(int *D, int A) : Data(D), Addition(A) {} |
| 32 | +}; |
| 33 | + |
| 34 | +using namespace sycl; |
| 35 | + |
| 36 | +int main() { |
| 37 | + constexpr int NumOfElements = 7; |
| 38 | + |
| 39 | + queue Q; |
| 40 | + |
| 41 | + NonTrivial NonTrivialObj(sycl::malloc_shared<int>(NumOfElements, Q), 38); |
| 42 | + Simple SimpleObj = {sycl::malloc_shared<int>(NumOfElements, Q), 42}; |
| 43 | + WrapperOfSimple WrapperOfSimpleObj = { |
| 44 | + 300, {sycl::malloc_shared<int>(NumOfElements, Q), 100500}}; |
| 45 | + |
| 46 | + // Test simple struct containing pointer. |
| 47 | + Q.parallel_for(NumOfElements, [=](id<1> Idx) { |
| 48 | + SimpleObj.Data[Idx] = Idx + SimpleObj.Addition; |
| 49 | + }); |
| 50 | + |
| 51 | + // Test simple non-trivial struct containing pointer. |
| 52 | + Q.parallel_for(NumOfElements, [=](id<1> Idx) { |
| 53 | + NonTrivialObj.Data[Idx] = Idx + NonTrivialObj.Addition; |
| 54 | + }); |
| 55 | + |
| 56 | + // Test nested struct containing pointer. |
| 57 | + Q.parallel_for(NumOfElements, [=](id<1> Idx) { |
| 58 | + WrapperOfSimpleObj.Obj.Data[Idx] = Idx + WrapperOfSimpleObj.Obj.Addition; |
| 59 | + }); |
| 60 | + |
| 61 | + // Test array of structs containing pointers. |
| 62 | + Simple SimpleArr[NumOfElements]; |
| 63 | + for (int i = 0; i < NumOfElements; ++i) { |
| 64 | + SimpleArr[i].Data = sycl::malloc_shared<int>(NumOfElements, Q); |
| 65 | + SimpleArr[i].Addition = 38 + i; |
| 66 | + } |
| 67 | + |
| 68 | + Q.parallel_for(range<2>(NumOfElements, NumOfElements), [=](item<2> Idx) { |
| 69 | + SimpleArr[Idx.get_id(0)].Data[Idx.get_id(1)] = |
| 70 | + Idx.get_id(1) + SimpleArr[Idx.get_id(0)].Addition; |
| 71 | + }); |
| 72 | + |
| 73 | + Q.wait(); |
| 74 | + |
| 75 | + auto Checker = [](auto Obj) { |
| 76 | + for (int i = 0; i < NumOfElements; ++i) { |
| 77 | + if (Obj.Data[i] != (i + Obj.Addition)) { |
| 78 | + std::cout << "line: " << __LINE__ << " result[" << i << "] is " |
| 79 | + << Obj.Data[i] << " expected " << i + Obj.Addition |
| 80 | + << std::endl; |
| 81 | + return true; // true if fail |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return false; |
| 86 | + }; |
| 87 | + |
| 88 | + bool Fail = false; |
| 89 | + Fail = Checker(SimpleObj); |
| 90 | + Fail = Checker(NonTrivialObj); |
| 91 | + Fail = Checker(WrapperOfSimpleObj.Obj); |
| 92 | + |
| 93 | + for (int i = 0; i < NumOfElements; ++i) |
| 94 | + Fail = Checker(SimpleArr[i]); |
| 95 | + |
| 96 | + // Free allocated memory. |
| 97 | + sycl::free(NonTrivialObj.Data, Q); |
| 98 | + sycl::free(SimpleObj.Data, Q); |
| 99 | + sycl::free(WrapperOfSimpleObj.Obj.Data, Q); |
| 100 | + |
| 101 | + for (int i = 0; i < NumOfElements; ++i) |
| 102 | + sycl::free(SimpleArr[i].Data, Q); |
| 103 | + |
| 104 | + return Fail; |
| 105 | +} |
0 commit comments