1 //===------- unittests/Plugins/NextgenPluginsTest.cpp - Plugin tests ------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
12 #include <unordered_set>
14 const int DEVICE_ID
= 0;
15 std::unordered_set
<int> setup_map
;
17 int init_test_device(int ID
) {
18 if (setup_map
.find(ID
) != setup_map
.end()) {
19 return OFFLOAD_SUCCESS
;
21 if (__tgt_rtl_init_plugin() == OFFLOAD_FAIL
||
22 __tgt_rtl_init_device(ID
) == OFFLOAD_FAIL
) {
26 return OFFLOAD_SUCCESS
;
29 // Test plugin initialization
30 TEST(NextgenPluginsTest
, PluginInit
) {
31 EXPECT_EQ(OFFLOAD_SUCCESS
, init_test_device(DEVICE_ID
));
34 // Test GPU allocation and R/W
35 TEST(NextgenPluginsTest
, PluginAlloc
) {
36 int32_t test_value
= 23;
37 int32_t host_value
= -1;
38 int64_t var_size
= sizeof(int32_t);
40 // Init plugin and device
41 EXPECT_EQ(OFFLOAD_SUCCESS
, init_test_device(DEVICE_ID
));
45 __tgt_rtl_data_alloc(DEVICE_ID
, var_size
, nullptr, TARGET_ALLOC_DEFAULT
);
47 // Check that the result is not null
48 EXPECT_NE(device_ptr
, nullptr);
50 // Submit data to device
51 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_submit(DEVICE_ID
, device_ptr
,
52 &test_value
, var_size
));
54 // Read data from device
55 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_retrieve(DEVICE_ID
, &host_value
,
56 device_ptr
, var_size
));
59 EXPECT_EQ(host_value
, test_value
);
62 EXPECT_EQ(OFFLOAD_SUCCESS
,
63 __tgt_rtl_data_delete(DEVICE_ID
, device_ptr
, TARGET_ALLOC_DEFAULT
));
66 // Test async GPU allocation and R/W
67 TEST(NextgenPluginsTest
, PluginAsyncAlloc
) {
68 int32_t test_value
= 47;
69 int32_t host_value
= -1;
70 int64_t var_size
= sizeof(int32_t);
71 __tgt_async_info
*info
;
73 // Init plugin and device
74 EXPECT_EQ(OFFLOAD_SUCCESS
, init_test_device(DEVICE_ID
));
76 // Check if device supports async
77 // Platforms like x86_64 don't support it
78 if (__tgt_rtl_init_async_info(DEVICE_ID
, &info
) == OFFLOAD_SUCCESS
) {
80 void *device_ptr
= __tgt_rtl_data_alloc(DEVICE_ID
, var_size
, nullptr,
81 TARGET_ALLOC_DEFAULT
);
83 // Check that the result is not null
84 EXPECT_NE(device_ptr
, nullptr);
86 // Submit data to device asynchronously
87 EXPECT_EQ(OFFLOAD_SUCCESS
,
88 __tgt_rtl_data_submit_async(DEVICE_ID
, device_ptr
, &test_value
,
91 // Wait for async request to process
92 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_synchronize(DEVICE_ID
, info
));
94 // Read data from device
95 EXPECT_EQ(OFFLOAD_SUCCESS
,
96 __tgt_rtl_data_retrieve_async(DEVICE_ID
, &host_value
, device_ptr
,
99 // Wait for async request to process
100 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_synchronize(DEVICE_ID
, info
));
103 EXPECT_EQ(host_value
, test_value
);
106 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_delete(DEVICE_ID
, device_ptr
,
107 TARGET_ALLOC_DEFAULT
));
111 // Test GPU data exchange
112 TEST(NextgenPluginsTest
, PluginDataSwap
) {
113 int32_t test_value
= 23;
114 int32_t host_value
= -1;
115 int64_t var_size
= sizeof(int32_t);
117 // Look for compatible device
119 for (int i
= 1; i
< __tgt_rtl_number_of_devices(); i
++) {
120 if (__tgt_rtl_is_data_exchangable(DEVICE_ID
, i
)) {
126 // Only run test if we have multiple GPUs to test
127 // GPUs must be compatible for test to work
128 if (DEVICE_TWO
>= 1) {
130 EXPECT_EQ(OFFLOAD_SUCCESS
, init_test_device(DEVICE_ID
));
131 EXPECT_EQ(OFFLOAD_SUCCESS
, init_test_device(DEVICE_TWO
));
133 // Allocate memory on both GPUs
134 // DEVICE_ID will be the source
135 // DEVICE_TWO will be the destination
136 void *source_ptr
= __tgt_rtl_data_alloc(DEVICE_ID
, var_size
, nullptr,
137 TARGET_ALLOC_DEFAULT
);
138 void *dest_ptr
= __tgt_rtl_data_alloc(DEVICE_TWO
, var_size
, nullptr,
139 TARGET_ALLOC_DEFAULT
);
141 // Check for success in allocation
142 EXPECT_NE(source_ptr
, nullptr);
143 EXPECT_NE(dest_ptr
, nullptr);
145 // Write data to source
146 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_submit(DEVICE_ID
, source_ptr
,
147 &test_value
, var_size
));
149 // Transfer data between devices
150 EXPECT_EQ(OFFLOAD_SUCCESS
,
151 __tgt_rtl_data_exchange(DEVICE_ID
, source_ptr
, DEVICE_TWO
,
152 dest_ptr
, var_size
));
154 // Read from destination device (DEVICE_TWO) memory
155 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_retrieve(DEVICE_TWO
, &host_value
,
156 dest_ptr
, var_size
));
159 EXPECT_EQ(host_value
, test_value
);
162 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_delete(DEVICE_ID
, source_ptr
,
163 TARGET_ALLOC_DEFAULT
));
164 EXPECT_EQ(OFFLOAD_SUCCESS
, __tgt_rtl_data_delete(DEVICE_TWO
, dest_ptr
,
165 TARGET_ALLOC_DEFAULT
));