1 | //===-- lib/cuda/pointer.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 "flang/Runtime/CUDA/pointer.h" |
10 | #include "flang-rt/runtime/assign-impl.h" |
11 | #include "flang-rt/runtime/descriptor.h" |
12 | #include "flang-rt/runtime/stat.h" |
13 | #include "flang-rt/runtime/terminator.h" |
14 | #include "flang/Runtime/CUDA/descriptor.h" |
15 | #include "flang/Runtime/CUDA/memmove-function.h" |
16 | #include "flang/Runtime/pointer.h" |
17 | |
18 | #include "cuda_runtime.h" |
19 | |
20 | namespace Fortran::runtime::cuda { |
21 | |
22 | extern "C" { |
23 | RT_EXT_API_GROUP_BEGIN |
24 | |
25 | int RTDEF(CUFPointerAllocate)(Descriptor &desc, int64_t *stream, bool *pinned, |
26 | bool hasStat, const Descriptor *errMsg, const char *sourceFile, |
27 | int sourceLine) { |
28 | if (desc.HasAddendum()) { |
29 | Terminator terminator{sourceFile, sourceLine}; |
30 | // TODO: This require a bit more work to set the correct type descriptor |
31 | // address |
32 | terminator.Crash( |
33 | "not yet implemented: CUDA descriptor allocation with addendum" ); |
34 | } |
35 | // Perform the standard allocation. |
36 | int stat{ |
37 | RTNAME(PointerAllocate)(desc, hasStat, errMsg, sourceFile, sourceLine)}; |
38 | if (pinned) { |
39 | // Set pinned according to stat. More infrastructre is needed to set it |
40 | // closer to the actual allocation call. |
41 | *pinned = (stat == StatOk); |
42 | } |
43 | return stat; |
44 | } |
45 | |
46 | int RTDEF(CUFPointerAllocateSync)(Descriptor &desc, int64_t *stream, |
47 | bool *pinned, bool hasStat, const Descriptor *errMsg, |
48 | const char *sourceFile, int sourceLine) { |
49 | int stat{RTNAME(CUFPointerAllocate)( |
50 | desc, stream, pinned, hasStat, errMsg, sourceFile, sourceLine)}; |
51 | #ifndef RT_DEVICE_COMPILATION |
52 | // Descriptor synchronization is only done when the allocation is done |
53 | // from the host. |
54 | if (stat == StatOk) { |
55 | void *deviceAddr{ |
56 | RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)}; |
57 | RTNAME(CUFDescriptorSync) |
58 | ((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine); |
59 | } |
60 | #endif |
61 | return stat; |
62 | } |
63 | |
64 | int RTDEF(CUFPointerAllocateSource)(Descriptor &pointer, |
65 | const Descriptor &source, int64_t *stream, bool *pinned, bool hasStat, |
66 | const Descriptor *errMsg, const char *sourceFile, int sourceLine) { |
67 | int stat{RTNAME(CUFPointerAllocate)( |
68 | pointer, stream, pinned, hasStat, errMsg, sourceFile, sourceLine)}; |
69 | if (stat == StatOk) { |
70 | Terminator terminator{sourceFile, sourceLine}; |
71 | Fortran::runtime::DoFromSourceAssign( |
72 | pointer, source, terminator, &MemmoveHostToDevice); |
73 | } |
74 | return stat; |
75 | } |
76 | |
77 | int RTDEF(CUFPointerAllocateSourceSync)(Descriptor &pointer, |
78 | const Descriptor &source, int64_t *stream, bool *pinned, bool hasStat, |
79 | const Descriptor *errMsg, const char *sourceFile, int sourceLine) { |
80 | int stat{RTNAME(CUFPointerAllocateSync)( |
81 | pointer, stream, pinned, hasStat, errMsg, sourceFile, sourceLine)}; |
82 | if (stat == StatOk) { |
83 | Terminator terminator{sourceFile, sourceLine}; |
84 | Fortran::runtime::DoFromSourceAssign( |
85 | pointer, source, terminator, &MemmoveHostToDevice); |
86 | } |
87 | return stat; |
88 | } |
89 | |
90 | RT_EXT_API_GROUP_END |
91 | |
92 | } // extern "C" |
93 | |
94 | } // namespace Fortran::runtime::cuda |
95 | |