10#if defined(EIGEN_USE_GPU) && !defined(EIGEN_CXX11_TENSOR_TENSOR_DEVICE_GPU_H)
11#define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_GPU_H
20static const int kGpuScratchSize = 1024;
24class StreamInterface {
26 virtual ~StreamInterface() {}
28 virtual const gpuStream_t& stream()
const = 0;
29 virtual const gpuDeviceProp_t& deviceProperties()
const = 0;
32 virtual void* allocate(
size_t num_bytes)
const = 0;
33 virtual void deallocate(
void* buffer)
const = 0;
36 virtual void* scratchpad()
const = 0;
42 virtual unsigned int* semaphore()
const = 0;
45class GpuDeviceProperties {
47 GpuDeviceProperties() :
48 initialized_(false), first_(true), device_properties_(nullptr) {}
50 ~GpuDeviceProperties() {
51 if (device_properties_) {
52 delete[] device_properties_;
57 return device_properties_[device];
72 if (first_.exchange(
false)) {
75 gpuError_t status = gpuGetDeviceCount(&num_devices);
76 if (status != gpuSuccess) {
77 std::cerr <<
"Failed to get the number of GPU devices: "
78 << gpuGetErrorString(status)
80 gpu_assert(status == gpuSuccess);
82 device_properties_ =
new gpuDeviceProp_t[num_devices];
83 for (
int i = 0;
i < num_devices; ++
i) {
84 status = gpuGetDeviceProperties(&device_properties_[
i],
i);
85 if (status != gpuSuccess) {
86 std::cerr <<
"Failed to initialize GPU device #"
89 << gpuGetErrorString(status)
91 gpu_assert(status == gpuSuccess);
95 std::atomic_thread_fence(std::memory_order_release);
99 while (!initialized_) {
100 std::atomic_thread_fence(std::memory_order_acquire);
101 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
108 volatile bool initialized_;
109 std::atomic<bool> first_;
110 gpuDeviceProp_t* device_properties_;
114 static GpuDeviceProperties* deviceProperties =
new GpuDeviceProperties();
115 if (!deviceProperties->isInitialized()) {
116 deviceProperties->initialize();
118 return *deviceProperties;
122 return GetGpuDeviceProperties().get(device);
125static const gpuStream_t default_stream = gpuStreamDefault;
127class GpuStreamDevice :
public StreamInterface {
130 GpuStreamDevice() : stream_(&default_stream), scratch_(NULL), semaphore_(NULL) {
131 gpuGetDevice(&device_);
134 GpuStreamDevice(
int device) : stream_(&default_stream), device_(device), scratch_(NULL), semaphore_(NULL) {}
139 GpuStreamDevice(
const gpuStream_t* stream,
int device = -1)
140 : stream_(stream), device_(device), scratch_(NULL), semaphore_(NULL) {
142 gpuGetDevice(&device_);
145 gpuError_t err = gpuGetDeviceCount(&num_devices);
147 gpu_assert(err == gpuSuccess);
148 gpu_assert(device < num_devices);
153 virtual ~GpuStreamDevice() {
155 deallocate(scratch_);
159 const gpuStream_t& stream()
const {
return *stream_; }
160 const gpuDeviceProp_t& deviceProperties()
const {
161 return GetGpuDeviceProperties(device_);
163 virtual void* allocate(
size_t num_bytes)
const {
164 gpuError_t err = gpuSetDevice(device_);
166 gpu_assert(err == gpuSuccess);
168 err = gpuMalloc(&result, num_bytes);
169 gpu_assert(err == gpuSuccess);
170 gpu_assert(result != NULL);
173 virtual void deallocate(
void* buffer)
const {
174 gpuError_t err = gpuSetDevice(device_);
176 gpu_assert(err == gpuSuccess);
177 gpu_assert(buffer != NULL);
178 err = gpuFree(buffer);
179 gpu_assert(err == gpuSuccess);
182 virtual void* scratchpad()
const {
183 if (scratch_ == NULL) {
184 scratch_ = allocate(kGpuScratchSize +
sizeof(
unsigned int));
189 virtual unsigned int* semaphore()
const {
190 if (semaphore_ == NULL) {
191 char* scratch =
static_cast<char*
>(scratchpad()) + kGpuScratchSize;
192 semaphore_ =
reinterpret_cast<unsigned int*
>(scratch);
193 gpuError_t err = gpuMemsetAsync(semaphore_, 0,
sizeof(
unsigned int), *stream_);
195 gpu_assert(err == gpuSuccess);
201 const gpuStream_t* stream_;
203 mutable void* scratch_;
204 mutable unsigned int* semaphore_;
210 explicit GpuDevice(
const StreamInterface* stream) : stream_(stream), max_blocks_(INT_MAX) {
213 explicit GpuDevice(
const StreamInterface* stream,
int num_blocks) : stream_(stream), max_blocks_(num_blocks) {
218 return stream_->stream();
222 return stream_->allocate(num_bytes);
226 stream_->deallocate(buffer);
230 return stream_->allocate(num_bytes);
234 stream_->deallocate(buffer);
237 template<
typename Type>
243 return stream_->scratchpad();
247 return stream_->semaphore();
251#ifndef EIGEN_GPU_COMPILE_PHASE
252 gpuError_t err = gpuMemcpyAsync(dst, src,
n, gpuMemcpyDeviceToDevice,
255 gpu_assert(err == gpuSuccess);
260 eigen_assert(
false &&
"The default device should be used instead to generate kernel code");
266 gpuMemcpyAsync(dst, src,
n, gpuMemcpyHostToDevice, stream_->stream());
268 gpu_assert(err == gpuSuccess);
273 gpuMemcpyAsync(dst, src,
n, gpuMemcpyDeviceToHost, stream_->stream());
275 gpu_assert(err == gpuSuccess);
279#ifndef EIGEN_GPU_COMPILE_PHASE
280 gpuError_t err = gpuMemsetAsync(buffer,
c,
n, stream_->stream());
282 gpu_assert(err == gpuSuccess);
284 eigen_assert(
false &&
"The default device should be used instead to generate kernel code");
301 return firstLevelCacheSize();
305#ifndef EIGEN_GPU_COMPILE_PHASE
306 gpuError_t err = gpuStreamSynchronize(stream_->stream());
307 if (err != gpuSuccess) {
308 std::cerr <<
"Error detected in GPU stream: "
309 << gpuGetErrorString(err)
311 gpu_assert(err == gpuSuccess);
314 gpu_assert(
false &&
"The default device should be used instead to generate kernel code");
319 return stream_->deviceProperties().multiProcessorCount;
322 return stream_->deviceProperties().maxThreadsPerBlock;
325 return stream_->deviceProperties().maxThreadsPerMultiProcessor;
328 return stream_->deviceProperties().sharedMemPerBlock;
331 return stream_->deviceProperties().major;
334 return stream_->deviceProperties().minor;
343 inline bool ok()
const {
345 gpuError_t error = gpuStreamQuery(stream_->stream());
346 return (error == gpuSuccess) || (error == gpuErrorNotReady);
353 const StreamInterface* stream_;
357#if defined(EIGEN_HIPCC)
359#define LAUNCH_GPU_KERNEL(kernel, gridsize, blocksize, sharedmem, device, ...) \
360 hipLaunchKernelGGL(kernel, dim3(gridsize), dim3(blocksize), (sharedmem), (device).stream(), __VA_ARGS__); \
361 gpu_assert(hipGetLastError() == hipSuccess);
365#define LAUNCH_GPU_KERNEL(kernel, gridsize, blocksize, sharedmem, device, ...) \
366 (kernel) <<< (gridsize), (blocksize), (sharedmem), (device).stream() >>> (__VA_ARGS__); \
367 gpu_assert(cudaGetLastError() == cudaSuccess);
373static EIGEN_DEVICE_FUNC inline void setGpuSharedMemConfig(gpuSharedMemConfig config) {
374#ifndef EIGEN_GPU_COMPILE_PHASE
375 gpuError_t status = gpuDeviceSetSharedMemConfig(config);
377 gpu_assert(status == gpuSuccess);
int n
Definition BiCGSTAB_simple.cpp:1
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define EIGEN_ALWAYS_INLINE
Definition Macros.h:932
#define EIGEN_UNUSED_VARIABLE(var)
Definition Macros.h:1076
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define eigen_assert(x)
Definition Macros.h:1037
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
int data[]
Definition Map_placement_new.cpp:1
Scalar Scalar * c
Definition benchVecAdd.cpp:17
Type
Definition Constants.h:471
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Container::iterator get(Container &c, Position position)
Definition stdlist_overload.cpp:29