|
| 1 | +//===-- flang/unittests/Runtime/AllocatableCUF.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 "gtest/gtest.h" |
| 10 | +#include "../../../runtime/terminator.h" |
| 11 | +#include "flang/Common/Fortran.h" |
| 12 | +#include "flang/Runtime/CUDA/allocator.h" |
| 13 | +#include "flang/Runtime/allocatable.h" |
| 14 | + |
| 15 | +#include "cuda.h" |
| 16 | + |
| 17 | +using namespace Fortran::runtime; |
| 18 | + |
| 19 | +static OwningPtr<Descriptor> createAllocatable( |
| 20 | + Fortran::common::TypeCategory tc, int kind, int rank = 1) { |
| 21 | + return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr, |
| 22 | + CFI_attribute_allocatable); |
| 23 | +} |
| 24 | + |
| 25 | +thread_local static int32_t defaultDevice = 0; |
| 26 | + |
| 27 | +CUdevice getDefaultCuDevice() { |
| 28 | + CUdevice device; |
| 29 | + CUDA_REPORT_IF_ERROR(cuDeviceGet(&device, /*ordinal=*/defaultDevice)); |
| 30 | + return device; |
| 31 | +} |
| 32 | + |
| 33 | +class ScopedContext { |
| 34 | +public: |
| 35 | + ScopedContext() { |
| 36 | + // Static reference to CUDA primary context for device ordinal |
| 37 | + // defaultDevice. |
| 38 | + static CUcontext context = [] { |
| 39 | + CUDA_REPORT_IF_ERROR(cuInit(/*flags=*/0)); |
| 40 | + CUcontext ctx; |
| 41 | + // Note: this does not affect the current context. |
| 42 | + CUDA_REPORT_IF_ERROR( |
| 43 | + cuDevicePrimaryCtxRetain(&ctx, getDefaultCuDevice())); |
| 44 | + return ctx; |
| 45 | + }(); |
| 46 | + |
| 47 | + CUDA_REPORT_IF_ERROR(cuCtxPushCurrent(context)); |
| 48 | + } |
| 49 | + |
| 50 | + ~ScopedContext() { CUDA_REPORT_IF_ERROR(cuCtxPopCurrent(nullptr)); } |
| 51 | +}; |
| 52 | + |
| 53 | +TEST(AllocatableCUFTest, SimpleDeviceAllocate) { |
| 54 | + using Fortran::common::TypeCategory; |
| 55 | + Fortran::runtime::cuf::CUFRegisterAllocator(); |
| 56 | + ScopedContext ctx; |
| 57 | + // REAL(4), DEVICE, ALLOCATABLE :: a(:) |
| 58 | + auto a{createAllocatable(TypeCategory::Real, 4)}; |
| 59 | + a->SetAllocIdx(kDeviceAllocatorPos); |
| 60 | + EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx()); |
| 61 | + EXPECT_FALSE(a->HasAddendum()); |
| 62 | + RTNAME(AllocatableSetBounds)(*a, 0, 1, 10); |
| 63 | + RTNAME(AllocatableAllocate) |
| 64 | + (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); |
| 65 | + EXPECT_TRUE(a->IsAllocated()); |
| 66 | + RTNAME(AllocatableDeallocate) |
| 67 | + (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); |
| 68 | + EXPECT_FALSE(a->IsAllocated()); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(AllocatableCUFTest, SimplePinnedAllocate) { |
| 72 | + using Fortran::common::TypeCategory; |
| 73 | + Fortran::runtime::cuf::CUFRegisterAllocator(); |
| 74 | + ScopedContext ctx; |
| 75 | + // INTEGER(4), PINNED, ALLOCATABLE :: a(:) |
| 76 | + auto a{createAllocatable(TypeCategory::Integer, 4)}; |
| 77 | + EXPECT_FALSE(a->HasAddendum()); |
| 78 | + a->SetAllocIdx(kPinnedAllocatorPos); |
| 79 | + EXPECT_EQ((int)kPinnedAllocatorPos, a->GetAllocIdx()); |
| 80 | + EXPECT_FALSE(a->HasAddendum()); |
| 81 | + RTNAME(AllocatableSetBounds)(*a, 0, 1, 10); |
| 82 | + RTNAME(AllocatableAllocate) |
| 83 | + (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); |
| 84 | + EXPECT_TRUE(a->IsAllocated()); |
| 85 | + RTNAME(AllocatableDeallocate) |
| 86 | + (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); |
| 87 | + EXPECT_FALSE(a->IsAllocated()); |
| 88 | +} |
0 commit comments