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 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   }
memcpyDefaultDevice24   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void* dst, const void* src, size_t n) const {
25     ::memcpy(dst, src, n);
26   }
memcpyHostToDeviceDefaultDevice27   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(void* dst, const void* src, size_t n) const {
28     memcpy(dst, src, n);
29   }
memcpyDeviceToHostDefaultDevice30   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(void* dst, const void* src, size_t n) const {
31     memcpy(dst, src, n);
32   }
memsetDefaultDevice33   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void* buffer, int c, size_t n) const {
34     ::memset(buffer, c, n);
35   }
36 
numThreadsDefaultDevice37   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t numThreads() const {
38 #ifndef __CUDA_ARCH__
39     // Running on the host CPU
40     return 1;
41 #else
42     // Running on a CUDA device
43     return 32;
44 #endif
45   }
46 
firstLevelCacheSizeDefaultDevice47   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t firstLevelCacheSize() const {
48 #ifndef __CUDA_ARCH__
49     // Running on the host CPU
50     return l1CacheSize();
51 #else
52     // Running on a CUDA device, return the amount of shared memory available.
53     return 48*1024;
54 #endif
55   }
56 
lastLevelCacheSizeDefaultDevice57   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t lastLevelCacheSize() const {
58 #ifndef __CUDA_ARCH__
59     // Running single threaded on the host CPU
60     return l3CacheSize();
61 #else
62     // Running on a CUDA device
63     return firstLevelCacheSize();
64 #endif
65   }
66 
majorDeviceVersionDefaultDevice67   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
68 #ifndef __CUDA_ARCH__
69     // Running single threaded on the host CPU
70     // Should return an enum that encodes the ISA supported by the CPU
71     return 1;
72 #else
73     // Running on a CUDA device
74     return __CUDA_ARCH__ / 100;
75 #endif
76   }
77 };
78 
79 }  // namespace Eigen
80 
81 #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_DEFAULT_H
82