|
| 1 | +//===-- runtime/CUDA/allocatable.cpp --------------------------------------===// |
| 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 "flang/Runtime/CUDA/allocatable.h" |
| 10 | +#include "../stat.h" |
| 11 | +#include "../terminator.h" |
| 12 | +#include "flang/Runtime/CUDA/common.h" |
| 13 | +#include "flang/Runtime/CUDA/descriptor.h" |
| 14 | +#include "flang/Runtime/allocatable.h" |
| 15 | +#include "llvm/Support/ErrorHandling.h" |
| 16 | + |
| 17 | +#include "cuda_runtime.h" |
| 18 | + |
| 19 | +namespace Fortran::runtime::cuda { |
| 20 | + |
| 21 | +extern "C" { |
| 22 | +RT_EXT_API_GROUP_BEGIN |
| 23 | + |
| 24 | +int RTDEF(CUFAllocatableAllocate)(Descriptor &desc, bool hasStat, |
| 25 | + const Descriptor *errMsg, const char *sourceFile, int sourceLine) { |
| 26 | + if (desc.HasAddendum()) { |
| 27 | + Terminator terminator{sourceFile, sourceLine}; |
| 28 | + // TODO: This require a bit more work to set the correct type descriptor |
| 29 | + // address |
| 30 | + terminator.Crash( |
| 31 | + "not yet implemented: CUDA descriptor allocation with addendum"); |
| 32 | + } |
| 33 | + // Perform the standard allocation. |
| 34 | + int stat{RTNAME(AllocatableAllocate)( |
| 35 | + desc, hasStat, errMsg, sourceFile, sourceLine)}; |
| 36 | +#ifndef RT_DEVICE_COMPILATION |
| 37 | + // Descriptor synchronization is only done when the allocation is done |
| 38 | + // from the host. |
| 39 | + if (stat == StatOk) { |
| 40 | + void *deviceAddr{ |
| 41 | + RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)}; |
| 42 | + RTNAME(CUFDescriptorSync) |
| 43 | + ((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine); |
| 44 | + } |
| 45 | +#endif |
| 46 | + return stat; |
| 47 | +} |
| 48 | + |
| 49 | +int RTDEF(CUFAllocatableDeallocate)(Descriptor &desc, bool hasStat, |
| 50 | + const Descriptor *errMsg, const char *sourceFile, int sourceLine) { |
| 51 | + // Perform the standard allocation. |
| 52 | + int stat{RTNAME(AllocatableDeallocate)( |
| 53 | + desc, hasStat, errMsg, sourceFile, sourceLine)}; |
| 54 | +#ifndef RT_DEVICE_COMPILATION |
| 55 | + // Descriptor synchronization is only done when the deallocation is done |
| 56 | + // from the host. |
| 57 | + if (stat == StatOk) { |
| 58 | + void *deviceAddr{ |
| 59 | + RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)}; |
| 60 | + RTNAME(CUFDescriptorSync) |
| 61 | + ((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine); |
| 62 | + } |
| 63 | +#endif |
| 64 | + return stat; |
| 65 | +} |
| 66 | + |
| 67 | +RT_EXT_API_GROUP_END |
| 68 | + |
| 69 | +} // extern "C" |
| 70 | + |
| 71 | +} // namespace Fortran::runtime::cuda |
0 commit comments