1//===-- unittests/Runtime/CUDA/AllocatorCUF.cpp -----------------*- C++ -*-===//
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 "cuda_runtime.h"
10#include "gtest/gtest.h"
11#include "flang-rt/runtime/allocator-registry.h"
12#include "flang-rt/runtime/descriptor.h"
13#include "flang-rt/runtime/terminator.h"
14#include "flang/Runtime/CUDA/allocator.h"
15#include "flang/Runtime/CUDA/descriptor.h"
16#include "flang/Runtime/allocatable.h"
17#include "flang/Support/Fortran.h"
18
19using namespace Fortran::runtime;
20using namespace Fortran::runtime::cuda;
21
22static OwningPtr<Descriptor> createAllocatable(
23 Fortran::common::TypeCategory tc, int kind, int rank = 1) {
24 return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
25 CFI_attribute_allocatable);
26}
27
28TEST(AllocatableCUFTest, SimpleDeviceAllocate) {
29 using Fortran::common::TypeCategory;
30 RTNAME(CUFRegisterAllocator)();
31 // REAL(4), DEVICE, ALLOCATABLE :: a(:)
32 auto a{createAllocatable(TypeCategory::Real, 4)};
33 a->SetAllocIdx(kDeviceAllocatorPos);
34 EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx());
35 EXPECT_FALSE(a->HasAddendum());
36 RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);
37 RTNAME(AllocatableAllocate)
38 (*a, /*asyncObject=*/nullptr, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__,
39 __LINE__);
40 EXPECT_TRUE(a->IsAllocated());
41 RTNAME(AllocatableDeallocate)
42 (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
43 EXPECT_FALSE(a->IsAllocated());
44}
45
46TEST(AllocatableCUFTest, SimplePinnedAllocate) {
47 using Fortran::common::TypeCategory;
48 RTNAME(CUFRegisterAllocator)();
49 // INTEGER(4), PINNED, ALLOCATABLE :: a(:)
50 auto a{createAllocatable(TypeCategory::Integer, 4)};
51 EXPECT_FALSE(a->HasAddendum());
52 a->SetAllocIdx(kPinnedAllocatorPos);
53 EXPECT_EQ((int)kPinnedAllocatorPos, a->GetAllocIdx());
54 EXPECT_FALSE(a->HasAddendum());
55 RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);
56 RTNAME(AllocatableAllocate)
57 (*a, /*asyncObject=*/nullptr, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__,
58 __LINE__);
59 EXPECT_TRUE(a->IsAllocated());
60 RTNAME(AllocatableDeallocate)
61 (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
62 EXPECT_FALSE(a->IsAllocated());
63}
64
65TEST(AllocatableCUFTest, DescriptorAllocationTest) {
66 using Fortran::common::TypeCategory;
67 RTNAME(CUFRegisterAllocator)();
68 // REAL(4), DEVICE, ALLOCATABLE :: a(:)
69 auto a{createAllocatable(TypeCategory::Real, 4)};
70 Descriptor *desc = nullptr;
71 desc = RTNAME(CUFAllocDescriptor)(a->SizeInBytes());
72 EXPECT_TRUE(desc != nullptr);
73 RTNAME(CUFFreeDescriptor)(desc);
74}
75

source code of flang-rt/unittests/Runtime/CUDA/AllocatorCUF.cpp