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 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 #include "DeviceMemory.h"
12 #include "DeviceUtils.h"
13 #include "../../FaissAssert.h"
14
15 namespace faiss { namespace gpu {
16
17 DeviceMemoryReservation::DeviceMemoryReservation()
18 : state_(NULL),
19 device_(0),
20 data_(NULL),
21 size_(0),
22 stream_(0) {
23 }
24
25 DeviceMemoryReservation::DeviceMemoryReservation(DeviceMemory* state,
26 int device,
27 void* p,
28 size_t size,
29 cudaStream_t stream)
30 : state_(state),
31 device_(device),
32 data_(p),
33 size_(size),
34 stream_(stream) {
35 }
36
37 DeviceMemoryReservation::DeviceMemoryReservation(
38 DeviceMemoryReservation&& m) noexcept {
39 if (data_) {
40 FAISS_ASSERT(state_);
41 state_->returnAllocation(*this);
42 }
43
44 state_ = m.state_;
45 device_ = m.device_;
46 data_ = m.data_;
47 size_ = m.size_;
48 stream_ = m.stream_;
49
50 m.data_ = NULL;
51 }
52
53 DeviceMemoryReservation::~DeviceMemoryReservation() {
54 if (data_) {
55 FAISS_ASSERT(state_);
56 state_->returnAllocation(*this);
57 }
58
59 data_ = NULL;
60 }
61
62 DeviceMemoryReservation&
63 DeviceMemoryReservation::operator=(DeviceMemoryReservation&& m) {
64 if (data_) {
65 FAISS_ASSERT(state_);
66 state_->returnAllocation(*this);
67 }
68
69 state_ = m.state_;
70 device_ = m.device_;
71 data_ = m.data_;
72 size_ = m.size_;
73 stream_ = m.stream_;
74
75 m.data_ = NULL;
76
77 return *this;
78 }
79
80 DeviceMemory::~DeviceMemory() {
81 }
82
83 } } // namespace