Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorDeviceDefault.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_DEVICE_DEFAULT_H
12#define EIGEN_TENSOR_TENSOR_DEVICE_DEFAULT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19// Default device for the machine (typically a single cpu core)
20struct DefaultDevice {
21 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate(size_t num_bytes) const {
22 return internal::aligned_malloc(num_bytes);
23 }
24 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate(void* buffer) const { internal::aligned_free(buffer); }
25 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* allocate_temp(size_t num_bytes) const { return allocate(num_bytes); }
26 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void deallocate_temp(void* buffer) const { deallocate(buffer); }
27 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void* dst, const void* src, size_t n) const {
28 ::memcpy(dst, src, n);
29 }
30 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(void* dst, const void* src, size_t n) const {
31 memcpy(dst, src, n);
32 }
33 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(void* dst, const void* src, size_t n) const {
34 memcpy(dst, src, n);
35 }
36 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void* buffer, int c, size_t n) const { ::memset(buffer, c, n); }
37 template <typename T>
38 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void fill(T* begin, T* end, const T& value) const {
39#ifdef EIGEN_GPU_COMPILE_PHASE
40 // std::fill is not a device function, so resort to simple loop.
41 for (T* it = begin; it != end; ++it) {
42 *it = value;
43 }
44#else
45 std::fill(begin, end, value);
46#endif
47 }
48 template <typename Type>
49 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Type get(Type data) const {
50 return data;
51 }
52
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t numThreads() const {
54#if !defined(EIGEN_GPU_COMPILE_PHASE)
55 // Running on the host CPU
56 return 1;
57#elif defined(EIGEN_HIP_DEVICE_COMPILE)
58 // Running on a HIP device
59 return 64;
60#else
61 // Running on a CUDA device
62 return 32;
63#endif
64 }
65
66 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t firstLevelCacheSize() const {
67#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
68 // Running on the host CPU
69 return l1CacheSize();
70#elif defined(EIGEN_HIP_DEVICE_COMPILE)
71 // Running on a HIP device
72 return 48 * 1024; // FIXME: Update this number for HIP.
73#else
74 // Running on a CUDA device, return the amount of shared memory available.
75 return 48 * 1024;
76#endif
77 }
78
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t lastLevelCacheSize() const {
80#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(SYCL_DEVICE_ONLY)
81 // Running single threaded on the host CPU
82 return l3CacheSize();
83#elif defined(EIGEN_HIP_DEVICE_COMPILE)
84 // Running on a HIP device
85 return firstLevelCacheSize(); // FIXME: Update this number for HIP.
86#else
87 // Running on a CUDA device
88 return firstLevelCacheSize();
89#endif
90 }
91
92 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void synchronize() const {
93 // Nothing. Default device operations are synchronous.
94 }
95
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
97#if !defined(EIGEN_GPU_COMPILE_PHASE)
98 // Running single threaded on the host CPU
99 // Should return an enum that encodes the ISA supported by the CPU
100 return 1;
101#elif defined(EIGEN_HIP_DEVICE_COMPILE)
102 // Running on a HIP device
103 // return 1 as major for HIP
104 return 1;
105#else
106 // Running on a CUDA device
107 return EIGEN_CUDA_ARCH / 100;
108#endif
109 }
110};
111
112} // namespace Eigen
113
114#endif // EIGEN_TENSOR_TENSOR_DEVICE_DEFAULT_H
Namespace containing all symbols from the Eigen library.