• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include <complex>
27 
28 #include "tensorflow/core/platform/logging.h"
29 #include "tensorflow/stream_executor/gpu/gpu_types.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
40 // device memory.
41 template <typename T>
GpuMemory(const DeviceMemory<T> & mem)42 const T* GpuMemory(const DeviceMemory<T>& mem) {
43   return static_cast<const T*>(mem.opaque());
44 }
45 
46 // Converts a (non-const) DeviceMemory pointer reference to its underlying typed
47 // pointer in CUDA device memory.
48 template <typename T>
GpuMemoryMutable(DeviceMemory<T> * mem)49 T* GpuMemoryMutable(DeviceMemory<T>* mem) {
50   return static_cast<T*>(mem->opaque());
51 }
52 
53 static_assert(
54     sizeof(std::complex<float>) == sizeof(GpuComplexType),
55     "std::complex<float> and GpuComplexType should have the same size");
56 static_assert(offsetof(GpuComplexType, x) == 0,
57               "The real part of GpuComplexType should appear first.");
58 static_assert(
59     sizeof(std::complex<double>) == sizeof(GpuDoubleComplexType),
60     "std::complex<double> and GpuDoubleComplexType should have the same "
61     "size");
62 static_assert(offsetof(GpuDoubleComplexType, x) == 0,
63               "The real part of GpuDoubleComplexType should appear first.");
64 
65 // Type traits to get CUDA complex types from std::complex<>.
66 
67 template <typename T>
68 struct GpuComplexT {
69   typedef T type;
70 };
71 
72 template <>
73 struct GpuComplexT<std::complex<float>> {
74   typedef GpuComplexType type;
75 };
76 
77 template <>
78 struct GpuComplexT<std::complex<double>> {
79   typedef GpuDoubleComplexType type;
80 };
81 
82 // Converts pointers of std::complex<> to pointers of
83 // GpuComplexType/GpuDoubleComplexType. No type conversion for non-complex
84 // types.
85 
86 template <typename T>
87 inline const typename GpuComplexT<T>::type* GpuComplex(const T* p) {
88   auto* result = reinterpret_cast<const typename GpuComplexT<T>::type*>(p);
89   CHECK_EQ(reinterpret_cast<uintptr_t>(p) % alignof(decltype(*result)), 0)
90       << "Source pointer is not aligned by " << alignof(decltype(*result));
91   return result;
92 }
93 
94 template <typename T>
95 inline typename GpuComplexT<T>::type* GpuComplex(T* p) {
96   auto* result = reinterpret_cast<typename GpuComplexT<T>::type*>(p);
97   CHECK_EQ(reinterpret_cast<uintptr_t>(p) % alignof(decltype(*result)), 0)
98       << "Source pointer is not aligned by " << alignof(decltype(*result));
99   return result;
100 }
101 
102 // Converts values of std::complex<float/double> to values of
103 // GpuComplexType/GpuDoubleComplexType.
104 inline GpuComplexType GpuComplexValue(std::complex<float> val) {
105   return {val.real(), val.imag()};
106 }
107 
108 inline GpuDoubleComplexType GpuComplexValue(std::complex<double> val) {
109   return {val.real(), val.imag()};
110 }
111 
112 }  // namespace gpu
113 }  // namespace stream_executor
114 
115 #endif  // TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_HELPERS_H_
116