TensorDeviceDefault.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_DEVICE_DEFAULT_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_DEFAULT_H
12 
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 // Default device for the machine (typically a single cpu core)
19 struct DefaultDevice {
20  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate(size_t num_bytes) const {
21  return internal::aligned_malloc(num_bytes);
22  }
23  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate(void* buffer) const {
24  internal::aligned_free(buffer);
25  }
26  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate_temp(size_t num_bytes) const {
27  return allocate(num_bytes);
28  }
29  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate_temp(void* buffer) const {
30  deallocate(buffer);
31  }
32  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void* dst, const void* src, size_t n) const {
33  ::memcpy(dst, src, n);
34  }
35  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(void* dst, const void* src, size_t n) const {
36  memcpy(dst, src, n);
37  }
38  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(void* dst, const void* src, size_t n) const {
39  memcpy(dst, src, n);
40  }
41  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void* buffer, int c, size_t n) const {
42  ::memset(buffer, c, n);
43  }
44  template<typename T>
45  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void fill(T* begin, T* end, const T& value) const {
46 #ifdef EIGEN_GPU_COMPILE_PHASE
47  // std::fill is not a device function, so resort to simple loop.
48  for (T* it = begin; it != end; ++it) {
49  *it = value;
50  }
51 #else
52  std::fill(begin, end, value);
53 #endif
54  }
55  template<typename Type>
56  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Type get(Type data) const {
57  return data;
58  }
59 
60  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t numThreads() const {
61 #if !defined(EIGEN_GPU_COMPILE_PHASE)
62  // Running on the host CPU
63  return 1;
64 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
65  // Running on a HIP device
66  return 64;
67 #else
68  // Running on a CUDA device
69  return 32;
70 #endif
71  }
72 
73  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t firstLevelCacheSize() const {
74 #if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
75  // Running on the host CPU
76  return l1CacheSize();
77 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
78  // Running on a HIP device
79  return 48*1024; // FIXME : update this number for HIP
80 #else
81  // Running on a CUDA device, return the amount of shared memory available.
82  return 48*1024;
83 #endif
84  }
85 
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t lastLevelCacheSize() const {
87 #if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
88  // Running single threaded on the host CPU
89  return l3CacheSize();
90 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
91  // Running on a HIP device
92  return firstLevelCacheSize(); // FIXME : update this number for HIP
93 #else
94  // Running on a CUDA device
95  return firstLevelCacheSize();
96 #endif
97  }
98 
99  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void synchronize() const {
100  // Nothing. Default device operations are synchronous.
101  }
102 
103  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
104 #if !defined(EIGEN_GPU_COMPILE_PHASE)
105  // Running single threaded on the host CPU
106  // Should return an enum that encodes the ISA supported by the CPU
107  return 1;
108 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
109  // Running on a HIP device
110  // return 1 as major for HIP
111  return 1;
112 #else
113  // Running on a CUDA device
114  return EIGEN_CUDA_ARCH / 100;
115 #endif
116  }
117 };
118 
119 } // namespace Eigen
120 
121 #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_DEFAULT_H
int n
Array33i c
#define EIGEN_DEVICE_FUNC
int data[]
static const lastp1_t end
Type
void * aligned_malloc(std::size_t size)
void aligned_free(void *ptr)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::ptrdiff_t l1CacheSize()
std::ptrdiff_t l3CacheSize()
void * allocate_temp(size_t num_bytes) const
void deallocate_temp(void *buffer) const
void memcpyDeviceToHost(void *dst, const void *src, size_t n) const
void memcpyHostToDevice(void *dst, const void *src, size_t n) const
void memset(void *buffer, int c, size_t n) const
size_t lastLevelCacheSize() const
Type get(Type data) const
void deallocate(void *buffer) const
void memcpy(void *dst, const void *src, size_t n) const
void * allocate(size_t num_bytes) const
size_t firstLevelCacheSize() const
void fill(T *begin, T *end, const T &value) const