1 | //===------- Offload API tests - olMemcpy --------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "../common/Fixtures.hpp" |
10 | #include <OffloadAPI.h> |
11 | #include <gtest/gtest.h> |
12 | |
13 | using olMemcpyTest = OffloadQueueTest; |
14 | OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olMemcpyTest); |
15 | |
16 | TEST_P(olMemcpyTest, SuccessHtoD) { |
17 | constexpr size_t Size = 1024; |
18 | void *Alloc; |
19 | ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc)); |
20 | std::vector<uint8_t> Input(Size, 42); |
21 | ASSERT_SUCCESS( |
22 | olMemcpy(Queue, Alloc, Device, Input.data(), Host, Size, nullptr)); |
23 | olWaitQueue(Queue); |
24 | olMemFree(Alloc); |
25 | } |
26 | |
27 | TEST_P(olMemcpyTest, SuccessDtoH) { |
28 | constexpr size_t Size = 1024; |
29 | void *Alloc; |
30 | std::vector<uint8_t> Input(Size, 42); |
31 | std::vector<uint8_t> Output(Size, 0); |
32 | |
33 | ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc)); |
34 | ASSERT_SUCCESS( |
35 | olMemcpy(Queue, Alloc, Device, Input.data(), Host, Size, nullptr)); |
36 | ASSERT_SUCCESS( |
37 | olMemcpy(Queue, Output.data(), Host, Alloc, Device, Size, nullptr)); |
38 | ASSERT_SUCCESS(olWaitQueue(Queue)); |
39 | for (uint8_t Val : Output) { |
40 | ASSERT_EQ(Val, 42); |
41 | } |
42 | ASSERT_SUCCESS(olMemFree(Alloc)); |
43 | } |
44 | |
45 | TEST_P(olMemcpyTest, SuccessDtoD) { |
46 | constexpr size_t Size = 1024; |
47 | void *AllocA; |
48 | void *AllocB; |
49 | std::vector<uint8_t> Input(Size, 42); |
50 | std::vector<uint8_t> Output(Size, 0); |
51 | |
52 | ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &AllocA)); |
53 | ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &AllocB)); |
54 | ASSERT_SUCCESS( |
55 | olMemcpy(Queue, AllocA, Device, Input.data(), Host, Size, nullptr)); |
56 | ASSERT_SUCCESS( |
57 | olMemcpy(Queue, AllocB, Device, AllocA, Device, Size, nullptr)); |
58 | ASSERT_SUCCESS( |
59 | olMemcpy(Queue, Output.data(), Host, AllocB, Device, Size, nullptr)); |
60 | ASSERT_SUCCESS(olWaitQueue(Queue)); |
61 | for (uint8_t Val : Output) { |
62 | ASSERT_EQ(Val, 42); |
63 | } |
64 | ASSERT_SUCCESS(olMemFree(AllocA)); |
65 | ASSERT_SUCCESS(olMemFree(AllocB)); |
66 | } |
67 | |
68 | TEST_P(olMemcpyTest, SuccessHtoHSync) { |
69 | constexpr size_t Size = 1024; |
70 | std::vector<uint8_t> Input(Size, 42); |
71 | std::vector<uint8_t> Output(Size, 0); |
72 | |
73 | ASSERT_SUCCESS(olMemcpy(nullptr, Output.data(), Host, Input.data(), Host, |
74 | Size, nullptr)); |
75 | |
76 | for (uint8_t Val : Output) { |
77 | ASSERT_EQ(Val, 42); |
78 | } |
79 | } |
80 | |
81 | TEST_P(olMemcpyTest, SuccessDtoHSync) { |
82 | constexpr size_t Size = 1024; |
83 | void *Alloc; |
84 | std::vector<uint8_t> Input(Size, 42); |
85 | std::vector<uint8_t> Output(Size, 0); |
86 | |
87 | ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc)); |
88 | ASSERT_SUCCESS( |
89 | olMemcpy(nullptr, Alloc, Device, Input.data(), Host, Size, nullptr)); |
90 | ASSERT_SUCCESS( |
91 | olMemcpy(nullptr, Output.data(), Host, Alloc, Device, Size, nullptr)); |
92 | for (uint8_t Val : Output) { |
93 | ASSERT_EQ(Val, 42); |
94 | } |
95 | ASSERT_SUCCESS(olMemFree(Alloc)); |
96 | } |
97 | |
98 | TEST_P(olMemcpyTest, SuccessSizeZero) { |
99 | constexpr size_t Size = 1024; |
100 | std::vector<uint8_t> Input(Size, 42); |
101 | std::vector<uint8_t> Output(Size, 0); |
102 | |
103 | // As with std::memcpy, size 0 is allowed. Keep all other arguments valid even |
104 | // if they aren't used. |
105 | ASSERT_SUCCESS( |
106 | olMemcpy(nullptr, Output.data(), Host, Input.data(), Host, 0, nullptr)); |
107 | } |
108 | |