xref: /llvm-project/flang/unittests/Runtime/CUDA/Allocatable.cpp (revision 4cb2a519db10f54815c8a4ccd5accbedc1cdfd07)
1cdf447baSValentin Clement (バレンタイン クレメン) //===-- flang/unittests/Runtime/Allocatable.cpp ------------------*- C++-*-===//
2cdf447baSValentin Clement (バレンタイン クレメン) //
3cdf447baSValentin Clement (バレンタイン クレメン) // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cdf447baSValentin Clement (バレンタイン クレメン) // See https://llvm.org/LICENSE.txt for license information.
5cdf447baSValentin Clement (バレンタイン クレメン) // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cdf447baSValentin Clement (バレンタイン クレメン) //
7cdf447baSValentin Clement (バレンタイン クレメン) //===----------------------------------------------------------------------===//
8cdf447baSValentin Clement (バレンタイン クレメン) 
9cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocatable.h"
10cdf447baSValentin Clement (バレンタイン クレメン) #include "gtest/gtest.h"
11cdf447baSValentin Clement (バレンタイン クレメン) #include "../../../runtime/terminator.h"
12cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Common/Fortran.h"
13cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/allocator.h"
14cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/common.h"
15cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/descriptor.h"
16cdf447baSValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocator-registry.h"
17cdf447baSValentin Clement (バレンタイン クレメン) 
18cdf447baSValentin Clement (バレンタイン クレメン) #include "cuda_runtime.h"
19cdf447baSValentin Clement (バレンタイン クレメン) 
20cdf447baSValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime;
21cdf447baSValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime::cuda;
22cdf447baSValentin Clement (バレンタイン クレメン) 
23cdf447baSValentin Clement (バレンタイン クレメン) static OwningPtr<Descriptor> createAllocatable(
24cdf447baSValentin Clement (バレンタイン クレメン)     Fortran::common::TypeCategory tc, int kind, int rank = 1) {
25cdf447baSValentin Clement (バレンタイン クレメン)   return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
26cdf447baSValentin Clement (バレンタイン クレメン)       CFI_attribute_allocatable);
27cdf447baSValentin Clement (バレンタイン クレメン) }
28cdf447baSValentin Clement (バレンタイン クレメン) 
29cdf447baSValentin Clement (バレンタイン クレメン) TEST(AllocatableCUFTest, SimpleDeviceAllocatable) {
30cdf447baSValentin Clement (バレンタイン クレメン)   using Fortran::common::TypeCategory;
31cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(CUFRegisterAllocator)();
32cdf447baSValentin Clement (バレンタイン クレメン)   // REAL(4), DEVICE, ALLOCATABLE :: a(:)
33cdf447baSValentin Clement (バレンタイン クレメン)   auto a{createAllocatable(TypeCategory::Real, 4)};
34cdf447baSValentin Clement (バレンタイン クレメン)   a->SetAllocIdx(kDeviceAllocatorPos);
35cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_EQ((int)kDeviceAllocatorPos, a->GetAllocIdx());
36cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->HasAddendum());
37cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableSetBounds)(*a, 0, 1, 10);
38cdf447baSValentin Clement (バレンタイン クレメン) 
39cdf447baSValentin Clement (バレンタイン クレメン)   // Emulate a device descriptor for the purpose of unit testing part of the
40cdf447baSValentin Clement (バレンタイン クレメン)   // code.
41cdf447baSValentin Clement (バレンタイン クレメン)   Descriptor *device_desc;
42cdf447baSValentin Clement (バレンタイン クレメン)   CUDA_REPORT_IF_ERROR(cudaMalloc(&device_desc, a->SizeInBytes()));
43cdf447baSValentin Clement (バレンタイン クレメン) 
44cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableAllocate)
45*4cb2a519SValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
46cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(a->IsAllocated());
47cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(CUFDescriptorSync)(device_desc, a.get(), __FILE__, __LINE__);
48cdf447baSValentin Clement (バレンタイン クレメン)   cudaDeviceSynchronize();
49cdf447baSValentin Clement (バレンタイン クレメン) 
50cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_EQ(cudaSuccess, cudaGetLastError());
51cdf447baSValentin Clement (バレンタイン クレメン) 
52cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableDeallocate)
53cdf447baSValentin Clement (バレンタイン クレメン)   (*a, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
54cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_FALSE(a->IsAllocated());
55cdf447baSValentin Clement (バレンタイン クレメン) 
56cdf447baSValentin Clement (バレンタイン クレメン)   RTNAME(CUFDescriptorSync)(device_desc, a.get(), __FILE__, __LINE__);
57cdf447baSValentin Clement (バレンタイン クレメン)   cudaDeviceSynchronize();
58cdf447baSValentin Clement (バレンタイン クレメン) 
59cdf447baSValentin Clement (バレンタイン クレメン)   EXPECT_EQ(cudaSuccess, cudaGetLastError());
60cdf447baSValentin Clement (バレンタイン クレメン) }
61