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_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
22 #define TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
23
24 #include <stddef.h>
25 #include <complex>
26
27 #include "tensorflow/stream_executor/gpu/gpu_types.h"
28
29 namespace stream_executor {
30
31 template <typename ElemT>
32 class DeviceMemory;
33
34 namespace gpu {
35
36 // Converts a const DeviceMemory reference to its underlying typed pointer in
37 // CUDA
38 // device memory.
39 template <typename T>
GpuMemory(const DeviceMemory<T> & mem)40 const T* GpuMemory(const DeviceMemory<T>& mem) {
41 return static_cast<const T*>(mem.opaque());
42 }
43
44 // Converts a (non-const) DeviceMemory pointer reference to its underlying typed
45 // pointer in CUDA device memory.
46 template <typename T>
GpuMemoryMutable(DeviceMemory<T> * mem)47 T* GpuMemoryMutable(DeviceMemory<T>* mem) {
48 return static_cast<T*>(mem->opaque());
49 }
50
51 static_assert(
52 sizeof(std::complex<float>) == sizeof(GpuComplexType),
53 "std::complex<float> and GpuComplexType should have the same size");
54 static_assert(offsetof(GpuComplexType, x) == 0,
55 "The real part of GpuComplexType should appear first.");
56 static_assert(
57 sizeof(std::complex<double>) == sizeof(GpuDoubleComplexType),
58 "std::complex<double> and GpuDoubleComplexType should have the same "
59 "size");
60 static_assert(offsetof(GpuDoubleComplexType, x) == 0,
61 "The real part of GpuDoubleComplexType should appear first.");
62
63 // Type traits to get CUDA complex types from std::complex<>.
64
65 template <typename T>
66 struct GpuComplexT {
67 typedef T type;
68 };
69
70 template <>
71 struct GpuComplexT<std::complex<float>> {
72 typedef GpuComplexType type;
73 };
74
75 template <>
76 struct GpuComplexT<std::complex<double>> {
77 typedef GpuDoubleComplexType type;
78 };
79
80 // Converts pointers of std::complex<> to pointers of
81 // GpuComplexType/GpuDoubleComplexType. No type conversion for non-complex
82 // types.
83
84 template <typename T>
85 inline const typename GpuComplexT<T>::type* GpuComplex(const T* p) {
86 return reinterpret_cast<const typename GpuComplexT<T>::type*>(p);
87 }
88
89 template <typename T>
90 inline typename GpuComplexT<T>::type* GpuComplex(T* p) {
91 return reinterpret_cast<typename GpuComplexT<T>::type*>(p);
92 }
93
94 // Converts values of std::complex<float/double> to values of
95 // GpuComplexType/GpuDoubleComplexType.
96 inline GpuComplexType GpuComplexValue(std::complex<float> val) {
97 return {val.real(), val.imag()};
98 }
99
100 inline GpuDoubleComplexType GpuComplexValue(std::complex<double> val) {
101 return {val.real(), val.imag()};
102 }
103
104 } // namespace gpu
105 } // namespace stream_executor
106
107 #endif // TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
108