xref: /llvm-project/flang/unittests/Runtime/CUDA/AllocatorCUF.cpp (revision 4cb2a519db10f54815c8a4ccd5accbedc1cdfd07)
114176339SValentin Clement (バレンタイン クレメン) //===-- flang/unittests/Runtime/AllocatableCUF.cpp ---------------*- C++-*-===//
214176339SValentin Clement (バレンタイン クレメン) //
314176339SValentin Clement (バレンタイン クレメン) // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
414176339SValentin Clement (バレンタイン クレメン) // See https://llvm.org/LICENSE.txt for license information.
514176339SValentin Clement (バレンタイン クレメン) // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
614176339SValentin Clement (バレンタイン クレメン) //
714176339SValentin Clement (バレンタイン クレメン) //===----------------------------------------------------------------------===//
814176339SValentin Clement (バレンタイン クレメン) 
914176339SValentin Clement (バレンタイン クレメン) #include "gtest/gtest.h"
1014176339SValentin Clement (バレンタイン クレメン) #include "../../../runtime/terminator.h"
1114176339SValentin Clement (バレンタイン クレメン) #include "flang/Common/Fortran.h"
1214176339SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/allocator.h"
13a3ccaed3SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/descriptor.h"
1414176339SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocatable.h"
15bbdb1e40SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocator-registry.h"
1614176339SValentin Clement (バレンタイン クレメン) 
17743e99dcSValentin Clement #include "cuda_runtime.h"
1814176339SValentin Clement (バレンタイン クレメン) 
1914176339SValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime;
2010d7805cSValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime::cuda;
2114176339SValentin Clement (バレンタイン クレメン) 
2214176339SValentin Clement (バレンタイン クレメン) static OwningPtr<Descriptor> createAllocatable(
2314176339SValentin Clement (バレンタイン クレメン)     Fortran::common::TypeCategory tc, int kind, int rank = 1) {
2414176339SValentin Clement (バレンタイン クレメン)   return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
2514176339SValentin Clement (バレンタイン クレメン)       CFI_attribute_allocatable);
2614176339SValentin Clement (バレンタイン クレメン) }
2714176339SValentin Clement (バレンタイン クレメン) 
2814176339SValentin Clement (バレンタイン クレメン) TEST(AllocatableCUFTest, SimpleDeviceAllocate) {
2914176339SValentin Clement (バレンタイン クレメン)   using Fortran::common::TypeCategory;
304c1dbbe7SValentin Clement (バレンタイン クレメン)   RTNAME(CUFRegisterAllocator)();
3114176339SValentin Clement (バレンタイン クレメン)   // REAL(4), DEVICE, ALLOCATABLE :: a(:)
3214176339SValentin Clement (バレンタイン クレメン)   auto a{createAllocatable(TypeCategory::Real, 4)};
3314176339SValentin Clement (バレンタイン クレメン)   a->SetAllocIdx(kDeviceAllocatorPos);
3414176339SValentin Clement (バレンタイン クレメン)   EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx());
3514176339SValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->HasAddendum());
3614176339SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);
3714176339SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableAllocate)
3814176339SValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
3983ccaad4SValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(a->IsAllocated());
4083ccaad4SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableDeallocate)
4183ccaad4SValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
4283ccaad4SValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->IsAllocated());
4383ccaad4SValentin Clement (バレンタイン クレメン) }
4483ccaad4SValentin Clement (バレンタイン クレメン) 
4514176339SValentin Clement (バレンタイン クレメン) TEST(AllocatableCUFTest, SimplePinnedAllocate) {
4614176339SValentin Clement (バレンタイン クレメン)   using Fortran::common::TypeCategory;
474c1dbbe7SValentin Clement (バレンタイン クレメン)   RTNAME(CUFRegisterAllocator)();
4814176339SValentin Clement (バレンタイン クレメン)   // INTEGER(4), PINNED, ALLOCATABLE :: a(:)
4914176339SValentin Clement (バレンタイン クレメン)   auto a{createAllocatable(TypeCategory::Integer, 4)};
5014176339SValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->HasAddendum());
5114176339SValentin Clement (バレンタイン クレメン)   a->SetAllocIdx(kPinnedAllocatorPos);
5214176339SValentin Clement (バレンタイン クレメン)   EXPECT_EQ((int)kPinnedAllocatorPos, a->GetAllocIdx());
5314176339SValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->HasAddendum());
5414176339SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);
5514176339SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableAllocate)
56*4cb2a519SValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
5714176339SValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(a->IsAllocated());
5814176339SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableDeallocate)
5914176339SValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
6014176339SValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->IsAllocated());
6114176339SValentin Clement (バレンタイン クレメン) }
62a3ccaed3SValentin Clement (バレンタイン クレメン) 
63a3ccaed3SValentin Clement (バレンタイン クレメン) TEST(AllocatableCUFTest, DescriptorAllocationTest) {
64a3ccaed3SValentin Clement (バレンタイン クレメン)   using Fortran::common::TypeCategory;
654c1dbbe7SValentin Clement (バレンタイン クレメン)   RTNAME(CUFRegisterAllocator)();
66a3ccaed3SValentin Clement (バレンタイン クレメン)   // REAL(4), DEVICE, ALLOCATABLE :: a(:)
67a3ccaed3SValentin Clement (バレンタイン クレメン)   auto a{createAllocatable(TypeCategory::Real, 4)};
68a3ccaed3SValentin Clement (バレンタイン クレメン)   Descriptor *desc = nullptr;
69e650ac16SValentin Clement (バレンタイン クレメン)   desc = RTNAME(CUFAllocDescriptor)(a->SizeInBytes());
70a3ccaed3SValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(desc != nullptr);
71415cfaf3SValentin Clement (バレンタイン クレメン)   RTNAME(CUFFreeDescriptor)(desc);
72a3ccaed3SValentin Clement (バレンタイン クレメン) }
73