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