brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.6 KiB · cc67d78 Raw
204 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "../common/Fixtures.hpp"10#include <OffloadAPI.h>11#include <gtest/gtest.h>12 13using olMemcpyTest = OffloadQueueTest;14OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olMemcpyTest);15 16struct olMemcpyGlobalTest : OffloadGlobalTest {17  void SetUp() override {18    RETURN_ON_FATAL_FAILURE(OffloadGlobalTest::SetUp());19    ASSERT_SUCCESS(20        olGetSymbol(Program, "read", OL_SYMBOL_KIND_KERNEL, &ReadKernel));21    ASSERT_SUCCESS(22        olGetSymbol(Program, "write", OL_SYMBOL_KIND_KERNEL, &WriteKernel));23    ASSERT_SUCCESS(olCreateQueue(Device, &Queue));24    ASSERT_SUCCESS(olGetSymbolInfo(25        Global, OL_SYMBOL_INFO_GLOBAL_VARIABLE_ADDRESS, sizeof(Addr), &Addr));26 27    LaunchArgs.Dimensions = 1;28    LaunchArgs.GroupSize = {64, 1, 1};29    LaunchArgs.NumGroups = {1, 1, 1};30 31    LaunchArgs.DynSharedMemory = 0;32  }33 34  ol_kernel_launch_size_args_t LaunchArgs{};35  void *Addr;36  ol_symbol_handle_t ReadKernel;37  ol_symbol_handle_t WriteKernel;38  ol_queue_handle_t Queue;39};40OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olMemcpyGlobalTest);41 42TEST_P(olMemcpyTest, SuccessHtoD) {43  constexpr size_t Size = 1024;44  void *Alloc;45  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc));46  std::vector<uint8_t> Input(Size, 42);47  ASSERT_SUCCESS(olMemcpy(Queue, Alloc, Device, Input.data(), Host, Size));48  olSyncQueue(Queue);49  olMemFree(Alloc);50}51 52TEST_P(olMemcpyTest, SuccessDtoH) {53  constexpr size_t Size = 1024;54  void *Alloc;55  std::vector<uint8_t> Input(Size, 42);56  std::vector<uint8_t> Output(Size, 0);57 58  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc));59  ASSERT_SUCCESS(olMemcpy(Queue, Alloc, Device, Input.data(), Host, Size));60  ASSERT_SUCCESS(olMemcpy(Queue, Output.data(), Host, Alloc, Device, Size));61  ASSERT_SUCCESS(olSyncQueue(Queue));62  for (uint8_t Val : Output) {63    ASSERT_EQ(Val, 42);64  }65  ASSERT_SUCCESS(olMemFree(Alloc));66}67 68TEST_P(olMemcpyTest, SuccessDtoD) {69  constexpr size_t Size = 1024;70  void *AllocA;71  void *AllocB;72  std::vector<uint8_t> Input(Size, 42);73  std::vector<uint8_t> Output(Size, 0);74 75  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &AllocA));76  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &AllocB));77  ASSERT_SUCCESS(olMemcpy(Queue, AllocA, Device, Input.data(), Host, Size));78  ASSERT_SUCCESS(olMemcpy(Queue, AllocB, Device, AllocA, Device, Size));79  ASSERT_SUCCESS(olMemcpy(Queue, Output.data(), Host, AllocB, Device, Size));80  ASSERT_SUCCESS(olSyncQueue(Queue));81  for (uint8_t Val : Output) {82    ASSERT_EQ(Val, 42);83  }84  ASSERT_SUCCESS(olMemFree(AllocA));85  ASSERT_SUCCESS(olMemFree(AllocB));86}87 88TEST_P(olMemcpyTest, SuccessHtoHSync) {89  constexpr size_t Size = 1024;90  std::vector<uint8_t> Input(Size, 42);91  std::vector<uint8_t> Output(Size, 0);92 93  ASSERT_SUCCESS(94      olMemcpy(nullptr, Output.data(), Host, Input.data(), Host, Size));95 96  for (uint8_t Val : Output) {97    ASSERT_EQ(Val, 42);98  }99}100 101TEST_P(olMemcpyTest, SuccessDtoHSync) {102  constexpr size_t Size = 1024;103  void *Alloc;104  std::vector<uint8_t> Input(Size, 42);105  std::vector<uint8_t> Output(Size, 0);106 107  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, Size, &Alloc));108  ASSERT_SUCCESS(olMemcpy(nullptr, Alloc, Device, Input.data(), Host, Size));109  ASSERT_SUCCESS(olMemcpy(nullptr, Output.data(), Host, Alloc, Device, Size));110  for (uint8_t Val : Output) {111    ASSERT_EQ(Val, 42);112  }113  ASSERT_SUCCESS(olMemFree(Alloc));114}115 116TEST_P(olMemcpyTest, SuccessSizeZero) {117  constexpr size_t Size = 1024;118  std::vector<uint8_t> Input(Size, 42);119  std::vector<uint8_t> Output(Size, 0);120 121  // As with std::memcpy, size 0 is allowed. Keep all other arguments valid even122  // if they aren't used.123  ASSERT_SUCCESS(olMemcpy(nullptr, Output.data(), Host, Input.data(), Host, 0));124}125 126TEST_P(olMemcpyGlobalTest, SuccessRoundTrip) {127  void *SourceMem;128  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,129                            64 * sizeof(uint32_t), &SourceMem));130  uint32_t *SourceData = (uint32_t *)SourceMem;131  for (auto I = 0; I < 64; I++)132    SourceData[I] = I;133 134  void *DestMem;135  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,136                            64 * sizeof(uint32_t), &DestMem));137 138  ASSERT_SUCCESS(139      olMemcpy(Queue, Addr, Device, SourceMem, Host, 64 * sizeof(uint32_t)));140  ASSERT_SUCCESS(olSyncQueue(Queue));141  ASSERT_SUCCESS(142      olMemcpy(Queue, DestMem, Host, Addr, Device, 64 * sizeof(uint32_t)));143  ASSERT_SUCCESS(olSyncQueue(Queue));144 145  uint32_t *DestData = (uint32_t *)DestMem;146  for (uint32_t I = 0; I < 64; I++)147    ASSERT_EQ(DestData[I], I);148 149  ASSERT_SUCCESS(olMemFree(DestMem));150  ASSERT_SUCCESS(olMemFree(SourceMem));151}152 153TEST_P(olMemcpyGlobalTest, SuccessWrite) {154  void *SourceMem;155  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,156                            LaunchArgs.GroupSize.x * sizeof(uint32_t),157                            &SourceMem));158  uint32_t *SourceData = (uint32_t *)SourceMem;159  for (auto I = 0; I < 64; I++)160    SourceData[I] = I;161 162  void *DestMem;163  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,164                            LaunchArgs.GroupSize.x * sizeof(uint32_t),165                            &DestMem));166  struct {167    void *Mem;168  } Args{DestMem};169 170  ASSERT_SUCCESS(171      olMemcpy(Queue, Addr, Device, SourceMem, Host, 64 * sizeof(uint32_t)));172  ASSERT_SUCCESS(olSyncQueue(Queue));173  ASSERT_SUCCESS(olLaunchKernel(Queue, Device, ReadKernel, &Args, sizeof(Args),174                                &LaunchArgs));175  ASSERT_SUCCESS(olSyncQueue(Queue));176 177  uint32_t *DestData = (uint32_t *)DestMem;178  for (uint32_t I = 0; I < 64; I++)179    ASSERT_EQ(DestData[I], I);180 181  ASSERT_SUCCESS(olMemFree(DestMem));182  ASSERT_SUCCESS(olMemFree(SourceMem));183}184 185TEST_P(olMemcpyGlobalTest, SuccessRead) {186  void *DestMem;187  ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,188                            LaunchArgs.GroupSize.x * sizeof(uint32_t),189                            &DestMem));190 191  ASSERT_SUCCESS(192      olLaunchKernel(Queue, Device, WriteKernel, nullptr, 0, &LaunchArgs));193  ASSERT_SUCCESS(olSyncQueue(Queue));194  ASSERT_SUCCESS(195      olMemcpy(Queue, DestMem, Host, Addr, Device, 64 * sizeof(uint32_t)));196  ASSERT_SUCCESS(olSyncQueue(Queue));197 198  uint32_t *DestData = (uint32_t *)DestMem;199  for (uint32_t I = 0; I < 64; I++)200    ASSERT_EQ(DestData[I], I * 2);201 202  ASSERT_SUCCESS(olMemFree(DestMem));203}204