xref: /aosp_15_r20/external/eigen/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceDefault.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <[email protected]>
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 namespace Eigen {
15 
16 // Default device for the machine (typically a single cpu core)
17 struct DefaultDevice {
allocateDefaultDevice18   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate(size_t num_bytes) const {
19     return internal::aligned_malloc(num_bytes);
20   }
deallocateDefaultDevice21   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate(void* buffer) const {
22     internal::aligned_free(buffer);
23   }
allocate_tempDefaultDevice24     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate_temp(size_t num_bytes) const {
25     return allocate(num_bytes);
26   }
deallocate_tempDefaultDevice27   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate_temp(void* buffer) const {
28     deallocate(buffer);
29   }
memcpyDefaultDevice30   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void* dst, const void* src, size_t n) const {
31     ::memcpy(dst, src, n);
32   }
memcpyHostToDeviceDefaultDevice33   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(void* dst, const void* src, size_t n) const {
34     memcpy(dst, src, n);
35   }
memcpyDeviceToHostDefaultDevice36   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(void* dst, const void* src, size_t n) const {
37     memcpy(dst, src, n);
38   }
memsetDefaultDevice39   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void* buffer, int c, size_t n) const {
40     ::memset(buffer, c, n);
41   }
42   template<typename Type>
getDefaultDevice43   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Type get(Type data) const {
44     return data;
45   }
46 
numThreadsDefaultDevice47   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t numThreads() const {
48 #if !defined(EIGEN_GPU_COMPILE_PHASE)
49     // Running on the host CPU
50     return 1;
51 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
52     // Running on a HIP device
53     return 64;
54 #else
55     // Running on a CUDA device
56     return 32;
57 #endif
58   }
59 
firstLevelCacheSizeDefaultDevice60   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t firstLevelCacheSize() const {
61 #if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
62     // Running on the host CPU
63     return l1CacheSize();
64 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
65     // Running on a HIP device
66     return 48*1024; // FIXME : update this number for HIP
67 #else
68     // Running on a CUDA device, return the amount of shared memory available.
69     return 48*1024;
70 #endif
71   }
72 
lastLevelCacheSizeDefaultDevice73   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t lastLevelCacheSize() const {
74 #if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
75     // Running single threaded on the host CPU
76     return l3CacheSize();
77 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
78     // Running on a HIP device
79     return firstLevelCacheSize(); // FIXME : update this number for HIP
80 #else
81     // Running on a CUDA device
82     return firstLevelCacheSize();
83 #endif
84   }
85 
majorDeviceVersionDefaultDevice86   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
87 #if !defined(EIGEN_GPU_COMPILE_PHASE)
88     // Running single threaded on the host CPU
89     // Should return an enum that encodes the ISA supported by the CPU
90     return 1;
91 #elif defined(EIGEN_HIP_DEVICE_COMPILE)
92     // Running on a HIP device
93     // return 1 as major for HIP
94     return 1;
95 #else
96     // Running on a CUDA device
97     return EIGEN_CUDA_ARCH / 100;
98 #endif
99   }
100 };
101 
102 }  // namespace Eigen
103 
104 #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_DEFAULT_H
105