xref: /llvm-project/flang/unittests/Runtime/CUDA/Memory.cpp (revision 4cb2a519db10f54815c8a4ccd5accbedc1cdfd07)
1fa627d98SValentin Clement (バレンタイン クレメン) //===-- flang/unittests/Runtime/Memory.cpp -----------------------*- C++-*-===//
2fa627d98SValentin Clement (バレンタイン クレメン) //
3fa627d98SValentin Clement (バレンタイン クレメン) // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fa627d98SValentin Clement (バレンタイン クレメン) // See https://llvm.org/LICENSE.txt for license information.
5fa627d98SValentin Clement (バレンタイン クレメン) // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fa627d98SValentin Clement (バレンタイン クレメン) //
7fa627d98SValentin Clement (バレンタイン クレメン) //===----------------------------------------------------------------------===//
8fa627d98SValentin Clement (バレンタイン クレメン) 
9fa627d98SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/memory.h"
10fa627d98SValentin Clement (バレンタイン クレメン) #include "gtest/gtest.h"
11fa627d98SValentin Clement (バレンタイン クレメン) #include "../../../runtime/terminator.h"
1232473864SValentin Clement (バレンタイン クレメン) #include "../tools.h"
13fa627d98SValentin Clement (バレンタイン クレメン) #include "flang/Common/Fortran.h"
1432473864SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/allocator.h"
15fa627d98SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/common.h"
1632473864SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/CUDA/descriptor.h"
1732473864SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocatable.h"
1832473864SValentin Clement (バレンタイン クレメン) #include "flang/Runtime/allocator-registry.h"
19fa627d98SValentin Clement (バレンタイン クレメン) 
20fa627d98SValentin Clement (バレンタイン クレメン) #include "cuda_runtime.h"
21fa627d98SValentin Clement (バレンタイン クレメン) 
2232473864SValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime;
23fa627d98SValentin Clement (バレンタイン クレメン) using namespace Fortran::runtime::cuda;
24fa627d98SValentin Clement (バレンタイン クレメン) 
25fa627d98SValentin Clement (バレンタイン クレメン) TEST(MemoryCUFTest, SimpleAllocTramsferFree) {
26fa627d98SValentin Clement (バレンタイン クレメン)   int *dev = (int *)RTNAME(CUFMemAlloc)(
27fa627d98SValentin Clement (バレンタイン クレメン)       sizeof(int), kMemTypeDevice, __FILE__, __LINE__);
28fa627d98SValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(dev != 0);
29fa627d98SValentin Clement (バレンタイン クレメン)   int host = 42;
30fa627d98SValentin Clement (バレンタイン クレメン)   RTNAME(CUFDataTransferPtrPtr)
31fa627d98SValentin Clement (バレンタイン クレメン)   ((void *)dev, (void *)&host, sizeof(int), kHostToDevice, __FILE__, __LINE__);
32fa627d98SValentin Clement (バレンタイン クレメン)   host = 0;
33fa627d98SValentin Clement (バレンタイン クレメン)   RTNAME(CUFDataTransferPtrPtr)
34fa627d98SValentin Clement (バレンタイン クレメン)   ((void *)&host, (void *)dev, sizeof(int), kDeviceToHost, __FILE__, __LINE__);
35fa627d98SValentin Clement (バレンタイン クレメン)   EXPECT_EQ(42, host);
36fa627d98SValentin Clement (バレンタイン クレメン)   RTNAME(CUFMemFree)((void *)dev, kMemTypeDevice, __FILE__, __LINE__);
37fa627d98SValentin Clement (バレンタイン クレメン) }
3832473864SValentin Clement (バレンタイン クレメン) 
3932473864SValentin Clement (バレンタイン クレメン) static OwningPtr<Descriptor> createAllocatable(
4032473864SValentin Clement (バレンタイン クレメン)     Fortran::common::TypeCategory tc, int kind, int rank = 1) {
4132473864SValentin Clement (バレンタイン クレメン)   return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
4232473864SValentin Clement (バレンタイン クレメン)       CFI_attribute_allocatable);
4332473864SValentin Clement (バレンタイン クレメン) }
4432473864SValentin Clement (バレンタイン クレメン) 
4532473864SValentin Clement (バレンタイン クレメン) TEST(MemoryCUFTest, CUFDataTransferDescDesc) {
4632473864SValentin Clement (バレンタイン クレメン)   using Fortran::common::TypeCategory;
4732473864SValentin Clement (バレンタイン クレメン)   RTNAME(CUFRegisterAllocator)();
4832473864SValentin Clement (バレンタイン クレメン)   // INTEGER(4), DEVICE, ALLOCATABLE :: a(:)
4932473864SValentin Clement (バレンタイン クレメン)   auto dev{createAllocatable(TypeCategory::Integer, 4)};
5032473864SValentin Clement (バレンタイン クレメン)   dev->SetAllocIdx(kDeviceAllocatorPos);
5132473864SValentin Clement (バレンタイン クレメン)   EXPECT_EQ((int)kDeviceAllocatorPos, dev->GetAllocIdx());
5232473864SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableSetBounds)(*dev, 0, 1, 10);
5332473864SValentin Clement (バレンタイン クレメン)   RTNAME(AllocatableAllocate)
54*4cb2a519SValentin Clement (バレンタイン クレメン)   (*dev, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
5532473864SValentin Clement (バレンタイン クレメン)   EXPECT_TRUE(dev->IsAllocated());
5632473864SValentin Clement (バレンタイン クレメン) 
5732473864SValentin Clement (バレンタイン クレメン)   // Create temp array to transfer to device.
5832473864SValentin Clement (バレンタイン クレメン)   auto x{MakeArray<TypeCategory::Integer, 4>(std::vector<int>{10},
5932473864SValentin Clement (バレンタイン クレメン)       std::vector<int32_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})};
60308c0074SValentin Clement   RTNAME(CUFDataTransferDescDesc)
61308c0074SValentin Clement   (dev.get(), x.get(), kHostToDevice, __FILE__, __LINE__);
6232473864SValentin Clement (バレンタイン クレメン) 
6332473864SValentin Clement (バレンタイン クレメン)   // Retrieve data from device.
6432473864SValentin Clement (バレンタイン クレメン)   auto host{MakeArray<TypeCategory::Integer, 4>(std::vector<int>{10},
6532473864SValentin Clement (バレンタイン クレメン)       std::vector<int32_t>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
6632473864SValentin Clement (バレンタイン クレメン)   RTNAME(CUFDataTransferDescDesc)
6732473864SValentin Clement (バレンタイン クレメン)   (host.get(), dev.get(), kDeviceToHost, __FILE__, __LINE__);
6832473864SValentin Clement (バレンタイン クレメン) 
6932473864SValentin Clement (バレンタイン クレメン)   for (unsigned i = 0; i < 10; ++i) {
7032473864SValentin Clement (バレンタイン クレメン)     EXPECT_EQ(*host->ZeroBasedIndexedElement<std::int32_t>(i), (std::int32_t)i);
7132473864SValentin Clement (バレンタイン クレメン)   }
7232473864SValentin Clement (バレンタイン クレメン) }
73