Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuResources.h
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 
9 #pragma once
10 
11 #include "utils/DeviceMemory.h"
12 #include <cuda_runtime.h>
13 #include <cublas_v2.h>
14 #include <utility>
15 #include <vector>
16 
17 namespace faiss { namespace gpu {
18 
19 /// Base class of GPU-side resource provider; hides provision of
20 /// cuBLAS handles, CUDA streams and a temporary memory manager
21 class GpuResources {
22  public:
23  virtual ~GpuResources();
24 
25  /// Call to pre-allocate resources for a particular device. If this is
26  /// not called, then resources will be allocated at the first time
27  /// of demand
28  virtual void initializeForDevice(int device) = 0;
29 
30  /// Returns the cuBLAS handle that we use for the given device
31  virtual cublasHandle_t getBlasHandle(int device) = 0;
32 
33  /// Returns the stream that we order all computation on for the
34  /// given device
35  virtual cudaStream_t getDefaultStream(int device) = 0;
36 
37  /// Returns the set of alternative streams that we use for the given device
38  virtual std::vector<cudaStream_t> getAlternateStreams(int device) = 0;
39 
40  /// Returns the temporary memory manager for the given device
41  virtual DeviceMemory& getMemoryManager(int device) = 0;
42 
43  /// Returns the available CPU pinned memory buffer
44  virtual std::pair<void*, size_t> getPinnedMemory() = 0;
45 
46  /// Returns the stream on which we perform async CPU <-> GPU copies
47  virtual cudaStream_t getAsyncCopyStream(int device) = 0;
48 
49  /// Calls getBlasHandle with the current device
50  cublasHandle_t getBlasHandleCurrentDevice();
51 
52  /// Calls getDefaultStream with the current device
53  cudaStream_t getDefaultStreamCurrentDevice();
54 
55  /// Synchronizes the CPU with respect to the default stream for the
56  /// given device
57  // equivalent to cudaDeviceSynchronize(getDefaultStream(device))
58  void syncDefaultStream(int device);
59 
60  /// Calls syncDefaultStream for the current device
62 
63  /// Calls getAlternateStreams for the current device
64  std::vector<cudaStream_t> getAlternateStreamsCurrentDevice();
65 
66  /// Calls getMemoryManager for the current device
68 
69  /// Calls getAsyncCopyStream for the current device
70  cudaStream_t getAsyncCopyStreamCurrentDevice();
71 };
72 
73 } } // namespace
virtual void initializeForDevice(int device)=0
cudaStream_t getDefaultStreamCurrentDevice()
Calls getDefaultStream with the current device.
virtual cublasHandle_t getBlasHandle(int device)=0
Returns the cuBLAS handle that we use for the given device.
virtual cudaStream_t getDefaultStream(int device)=0
cublasHandle_t getBlasHandleCurrentDevice()
Calls getBlasHandle with the current device.
DeviceMemory & getMemoryManagerCurrentDevice()
Calls getMemoryManager for the current device.
void syncDefaultStreamCurrentDevice()
Calls syncDefaultStream for the current device.
cudaStream_t getAsyncCopyStreamCurrentDevice()
Calls getAsyncCopyStream for the current device.
std::vector< cudaStream_t > getAlternateStreamsCurrentDevice()
Calls getAlternateStreams for the current device.
virtual cudaStream_t getAsyncCopyStream(int device)=0
Returns the stream on which we perform async CPU &lt;-&gt; GPU copies.
void syncDefaultStream(int device)
virtual DeviceMemory & getMemoryManager(int device)=0
Returns the temporary memory manager for the given device.
Manages temporary memory allocations on a GPU device.
Definition: DeviceMemory.h:44
virtual std::pair< void *, size_t > getPinnedMemory()=0
Returns the available CPU pinned memory buffer.
virtual std::vector< cudaStream_t > getAlternateStreams(int device)=0
Returns the set of alternative streams that we use for the given device.