xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/stream_executor/gpu/gpu_helpers.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // Common helper functions used for dealing with CUDA API datatypes.
17 //
18 // These are typically placed here for use by multiple source components (for
19 // example, BLAS and executor components).
20 
21 #ifndef TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
22 #define TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
23 
24 #include <stddef.h>
25 
26 #include <complex>
27 
28 #include "tensorflow/compiler/xla/stream_executor/gpu/gpu_types.h"
29 #include "tensorflow/core/platform/logging.h"
30 
31 namespace stream_executor {
32 
33 template <typename ElemT>
34 class DeviceMemory;
35 
36 namespace gpu {
37 
38 // Converts a const DeviceMemory reference to its underlying typed pointer in
39 // CUDA device memory.
40 template <typename T>
GpuMemory(const DeviceMemory<T> & mem)41 const T* GpuMemory(const DeviceMemory<T>& mem) {
42   return static_cast<const T*>(mem.opaque());
43 }
44 
45 // Converts a (non-const) DeviceMemory pointer reference to its underlying typed
46 // pointer in CUDA device memory.
47 template <typename T>
GpuMemoryMutable(DeviceMemory<T> * mem)48 T* GpuMemoryMutable(DeviceMemory<T>* mem) {
49   return static_cast<T*>(mem->opaque());
50 }
51 
52 static_assert(
53     sizeof(std::complex<float>) == sizeof(GpuComplexType),
54     "std::complex<float> and GpuComplexType should have the same size");
55 static_assert(offsetof(GpuComplexType, x) == 0,
56               "The real part of GpuComplexType should appear first.");
57 static_assert(
58     sizeof(std::complex<double>) == sizeof(GpuDoubleComplexType),
59     "std::complex<double> and GpuDoubleComplexType should have the same "
60     "size");
61 static_assert(offsetof(GpuDoubleComplexType, x) == 0,
62               "The real part of GpuDoubleComplexType should appear first.");
63 
64 // Type traits to get CUDA complex types from std::complex<>.
65 
66 template <typename T>
67 struct GpuComplexT {
68   typedef T type;
69 };
70 
71 template <>
72 struct GpuComplexT<std::complex<float>> {
73   typedef GpuComplexType type;
74 };
75 
76 template <>
77 struct GpuComplexT<std::complex<double>> {
78   typedef GpuDoubleComplexType type;
79 };
80 
81 // Converts pointers of std::complex<> to pointers of
82 // GpuComplexType/GpuDoubleComplexType. No type conversion for non-complex
83 // types.
84 
85 template <typename T>
86 inline const typename GpuComplexT<T>::type* GpuComplex(const T* p) {
87   auto* result = reinterpret_cast<const typename GpuComplexT<T>::type*>(p);
88   CHECK_EQ(reinterpret_cast<uintptr_t>(p) % alignof(decltype(*result)), 0)
89       << "Source pointer is not aligned by " << alignof(decltype(*result));
90   return result;
91 }
92 
93 template <typename T>
94 inline typename GpuComplexT<T>::type* GpuComplex(T* p) {
95   auto* result = reinterpret_cast<typename GpuComplexT<T>::type*>(p);
96   CHECK_EQ(reinterpret_cast<uintptr_t>(p) % alignof(decltype(*result)), 0)
97       << "Source pointer is not aligned by " << alignof(decltype(*result));
98   return result;
99 }
100 
101 // Converts values of std::complex<float/double> to values of
102 // GpuComplexType/GpuDoubleComplexType.
103 inline GpuComplexType GpuComplexValue(std::complex<float> val) {
104   return {val.real(), val.imag()};
105 }
106 
107 inline GpuDoubleComplexType GpuComplexValue(std::complex<double> val) {
108   return {val.real(), val.imag()};
109 }
110 
111 }  // namespace gpu
112 }  // namespace stream_executor
113 
114 #endif  // TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
115