Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
StandardGpuResources.h
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the CC-by-NC license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 #pragma once
12
13 #include "GpuResources.h"
14 #include "utils/StackDeviceMemory.h"
15 #include "utils/DeviceUtils.h"
16 #include <unordered_map>
17 #include <vector>
18
19 namespace faiss { namespace gpu {
20
21 /// Default implementation of GpuResources that allocates a cuBLAS
22 /// stream and 2 streams for use, as well as temporary memory
23 class StandardGpuResources : public GpuResources {
24 public:
25 StandardGpuResources();
26
27 ~StandardGpuResources() override;
28
29 /// Disable allocation of temporary memory; all temporary memory
30 /// requests will call cudaMalloc / cudaFree at the point of use
31 void noTempMemory();
32
33 /// Specify that we wish to use a certain fixed size of memory on
34 /// all devices as temporary memory
35 void setTempMemory(size_t size);
36
37 /// Specify that we wish to use a certain fraction of memory on
38 /// all devices as temporary memory
39 void setTempMemoryFraction(float fraction);
40
41 /// Set amount of pinned memory to allocate, for async GPU <-> CPU
42 /// transfers
43 void setPinnedMemory(size_t size);
44
45 /// Called to change the stream for work ordering
46 void setDefaultStream(int device, cudaStream_t stream);
47
48 /// Called to change the work ordering streams to the null stream
49 /// for all devices
50 void setDefaultNullStreamAllDevices();
51
52 public:
53 /// Internal system calls
54 void initializeForDevice(int device) override;
55
56 cublasHandle_t getBlasHandle(int device) override;
57
58 cudaStream_t getDefaultStream(int device) override;
59
60 std::vector<cudaStream_t> getAlternateStreams(int device) override;
61
62 DeviceMemory& getMemoryManager(int device) override;
63
64 std::pair<void*, size_t> getPinnedMemory() override;
65
66 cudaStream_t getAsyncCopyStream(int device) override;
67
68 private:
69 /// Our default stream that work is ordered on, one per each device
70 std::unordered_map<int, cudaStream_t> defaultStreams_;
71
72 /// This contains particular streams as set by the user for
73 /// ordering, if any
74 std::unordered_map<int, cudaStream_t> userDefaultStreams_;
75
76 /// Other streams we can use, per each device
77 std::unordered_map<int, std::vector<cudaStream_t> > alternateStreams_;
78
79 /// Async copy stream to use for GPU <-> CPU pinned memory copies
80 std::unordered_map<int, cudaStream_t> asyncCopyStreams_;
81
82 /// cuBLAS handle for each device
83 std::unordered_map<int, cublasHandle_t> blasHandles_;
84
85 /// Temporary memory provider, per each device
86 std::unordered_map<int, std::unique_ptr<StackDeviceMemory> > memory_;
87
88 /// Pinned memory allocation for use with this GPU
89 void* pinnedMemAlloc_;
90 size_t pinnedMemAllocSize_;
91
92 /// By default, we reserve this fraction of memory on all devices
93 float tempMemFraction_;
94
95 /// Another option is to use a specified amount of memory on all
96 /// devices
97 size_t tempMemSize_;
98
99 /// Whether we look at tempMemFraction_ or tempMemSize_
100 bool useFraction_;
101
102 /// Amount of pinned memory we should allocate
103 size_t pinnedMemSize_;
104 };
105
106 } } // namespace
faiss::gpu::StandardGpuResources::setDefaultStream
void setDefaultStream(int device, cudaStream_t stream)
Called to change the stream for work ordering.
Definition: StandardGpuResources.cpp:103
faiss::gpu::StandardGpuResources::getBlasHandle
cublasHandle_t getBlasHandle(int device) override
Returns the cuBLAS handle that we use for the given device.
Definition: StandardGpuResources.cpp:205
faiss::gpu::StandardGpuResources::setTempMemoryFraction
void setTempMemoryFraction(float fraction)
Definition: StandardGpuResources.cpp:87
faiss::gpu::StandardGpuResources::noTempMemory
void noTempMemory()
Definition: StandardGpuResources.cpp:76
faiss::gpu::StandardGpuResources::initializeForDevice
void initializeForDevice(int device) override
Internal system calls.
Definition: StandardGpuResources.cpp:122
faiss::gpu::StandardGpuResources::getAsyncCopyStream
cudaStream_t getAsyncCopyStream(int device) override
Returns the stream on which we perform async CPU &lt;-&gt; GPU copies.
Definition: StandardGpuResources.cpp:233
faiss::gpu::StandardGpuResources::getMemoryManager
DeviceMemory & getMemoryManager(int device) override
Returns the temporary memory manager for the given device.
Definition: StandardGpuResources.cpp:222
faiss::gpu::GpuResources
Definition: GpuResources.h:23
faiss::gpu::StandardGpuResources::setTempMemory
void setTempMemory(size_t size)
Definition: StandardGpuResources.cpp:81
faiss::gpu::StandardGpuResources
Definition: StandardGpuResources.h:23
faiss::gpu::StandardGpuResources::setPinnedMemory
void setPinnedMemory(size_t size)
Definition: StandardGpuResources.cpp:94
faiss::gpu::StandardGpuResources::getDefaultStream
cudaStream_t getDefaultStream(int device) override
Definition: StandardGpuResources.cpp:211
faiss::gpu::StandardGpuResources::setDefaultNullStreamAllDevices
void setDefaultNullStreamAllDevices()
Definition: StandardGpuResources.cpp:115
faiss::gpu::DeviceMemory
Manages temporary memory allocations on a GPU device.
Definition: DeviceMemory.h:46
faiss::gpu::StandardGpuResources::getPinnedMemory
std::pair< void *, size_t > getPinnedMemory() override
Returns the available CPU pinned memory buffer.
Definition: StandardGpuResources.cpp:228
faiss::gpu::StandardGpuResources::getAlternateStreams
std::vector< cudaStream_t > getAlternateStreams(int device) override
Returns the set of alternative streams that we use for the given device.
Definition: StandardGpuResources.cpp:217