Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
HostTensor-inl.cuh
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 namespace faiss { namespace gpu {
11 
12 template <typename T, int Dim, bool InnerContig,
13  typename IndexT, template <typename U> class PtrTraits>
14 __host__
16  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(),
17  state_(AllocState::NotOwner) {
18 }
19 
20 template <typename T, int Dim, bool InnerContig,
21  typename IndexT, template <typename U> class PtrTraits>
22 __host__
24  if (state_ == AllocState::Owner) {
25  FAISS_ASSERT(this->data_ != nullptr);
26  delete[] this->data_;
27  this->data_ = nullptr;
28  }
29 }
30 
31 template <typename T, int Dim, bool InnerContig,
32  typename IndexT, template <typename U> class PtrTraits>
33 __host__
35  const IndexT sizes[Dim]) :
36  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, sizes),
37  state_(AllocState::Owner) {
38 
39  this->data_ = new T[this->numElements()];
40  FAISS_ASSERT(this->data_ != nullptr);
41 }
42 
43 template <typename T, int Dim, bool InnerContig,
44  typename IndexT, template <typename U> class PtrTraits>
45 __host__
47  std::initializer_list<IndexT> sizes) :
48  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, sizes),
49  state_(AllocState::Owner) {
50  this->data_ = new T[this->numElements()];
51  FAISS_ASSERT(this->data_ != nullptr);
52 }
53 
54 template <typename T, int Dim, bool InnerContig,
55  typename IndexT, template <typename U> class PtrTraits>
56 __host__
58  DataPtrType data,
59  const IndexT sizes[Dim]) :
60  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
61  state_(AllocState::NotOwner) {
62 }
63 
64 template <typename T, int Dim, bool InnerContig,
65  typename IndexT, template <typename U> class PtrTraits>
66 __host__
68  DataPtrType data,
69  std::initializer_list<IndexT> sizes) :
70  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
71  state_(AllocState::NotOwner) {
72 }
73 
74 template <typename T, int Dim, bool InnerContig,
75  typename IndexT, template <typename U> class PtrTraits>
76 __host__
78  DataPtrType data,
79  const IndexT sizes[Dim],
80  const IndexT strides[Dim]) :
81  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes, strides),
82  state_(AllocState::NotOwner) {
83 }
84 
85 template <typename T, int Dim, bool InnerContig,
86  typename IndexT, template <typename U> class PtrTraits>
87 __host__
90  cudaStream_t stream) :
91  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, t.sizes(), t.strides()),
92  state_(AllocState::Owner) {
93  // Only contiguous arrays handled for now
94  FAISS_ASSERT(t.isContiguous());
95 
96  this->data_ = new T[t.numElements()];
97  this->copyFrom(t, stream);
98 }
99 
100 /// Call to zero out memory
101 template <typename T, int Dim, bool InnerContig,
102  typename IndexT, template <typename U> class PtrTraits>
105  // Region must be contiguous
106  FAISS_ASSERT(this->isContiguous());
107 
108  if (this->data_ != nullptr) {
109  memset(this->data_, 0, this->getSizeInBytes());
110  }
111 
112  return *this;
113 }
114 
115 template <typename T, int Dim, bool InnerContig,
116  typename IndexT, template <typename U> class PtrTraits>
117 __host__ T
120  auto size = this->numElements();
121 
122  FAISS_ASSERT(size == t.numElements());
123  FAISS_ASSERT(size > 0);
124 
125  if (InnerContig) {
126  auto a = this->data();
127  auto b = t.data();
128 
129  T maxDiff = a[0] - b[0];
130  // FIXME: type-specific abs()
131  maxDiff = maxDiff < 0 ? maxDiff * (T) -1 : maxDiff;
132 
133  for (IndexT i = 1; i < size; ++i) {
134  auto diff = a[i] - b[i];
135  // FIXME: type-specific abs
136  diff = diff < 0 ? diff * (T) -1 : diff;
137  if (diff > maxDiff) {
138  maxDiff = diff;
139  }
140  }
141 
142  return maxDiff;
143  } else {
144  // non-contiguous
145  // FIXME
146  FAISS_ASSERT(false);
147  return (T) 0;
148  }
149 }
150 
151 } } // namespace
__host__ __device__ size_t numElements() const
Definition: Tensor-inl.cuh:387
__host__ ~HostTensor()
Destructor.
__host__ HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > & zero()
Call to zero out memory.
DataPtrType data_
Raw pointer to where the tensor data begins.
Definition: Tensor.cuh:343
__host__ __device__ bool isContiguous() const
Definition: Tensor-inl.cuh:400
__host__ void copyFrom(Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &t, cudaStream_t stream)
Copies a tensor into ourselves; sizes must match.
Definition: Tensor-inl.cuh:131
__host__ HostTensor()
Default constructor.
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:175
Our tensor type.
Definition: Tensor.cuh:29
__host__ T maxDiff(const HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.