Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
DeviceMemory.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9
10 #include "DeviceMemory.h"
11 #include "DeviceUtils.h"
12 #include "../../FaissAssert.h"
13
14 namespace faiss { namespace gpu {
15
16 DeviceMemoryReservation::DeviceMemoryReservation()
17 : state_(NULL),
18 device_(0),
19 data_(NULL),
20 size_(0),
21 stream_(0) {
22 }
23
24 DeviceMemoryReservation::DeviceMemoryReservation(DeviceMemory* state,
25 int device,
26 void* p,
27 size_t size,
28 cudaStream_t stream)
29 : state_(state),
30 device_(device),
31 data_(p),
32 size_(size),
33 stream_(stream) {
34 }
35
36 DeviceMemoryReservation::DeviceMemoryReservation(
37 DeviceMemoryReservation&& m) noexcept {
38 if (data_) {
39 FAISS_ASSERT(state_);
40 state_->returnAllocation(*this);
41 }
42
43 state_ = m.state_;
44 device_ = m.device_;
45 data_ = m.data_;
46 size_ = m.size_;
47 stream_ = m.stream_;
48
49 m.data_ = NULL;
50 }
51
52 DeviceMemoryReservation::~DeviceMemoryReservation() {
53 if (data_) {
54 FAISS_ASSERT(state_);
55 state_->returnAllocation(*this);
56 }
57
58 data_ = NULL;
59 }
60
61 DeviceMemoryReservation&
62 DeviceMemoryReservation::operator=(DeviceMemoryReservation&& m) {
63 if (data_) {
64 FAISS_ASSERT(state_);
65 state_->returnAllocation(*this);
66 }
67
68 state_ = m.state_;
69 device_ = m.device_;
70 data_ = m.data_;
71 size_ = m.size_;
72 stream_ = m.stream_;
73
74 m.data_ = NULL;
75
76 return *this;
77 }
78
79 DeviceMemory::~DeviceMemory() {
80 }
81
82 } } // namespace