Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
HostTensor-inl.cuh
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 
9 namespace faiss { namespace gpu {
10 
11 template <typename T, int Dim, bool InnerContig,
12  typename IndexT, template <typename U> class PtrTraits>
13 __host__
15  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(),
16  state_(AllocState::NotOwner) {
17 }
18 
19 template <typename T, int Dim, bool InnerContig,
20  typename IndexT, template <typename U> class PtrTraits>
21 __host__
23  if (state_ == AllocState::Owner) {
24  FAISS_ASSERT(this->data_ != nullptr);
25  delete[] this->data_;
26  this->data_ = nullptr;
27  }
28 }
29 
30 template <typename T, int Dim, bool InnerContig,
31  typename IndexT, template <typename U> class PtrTraits>
32 __host__
34  const IndexT sizes[Dim]) :
35  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, sizes),
36  state_(AllocState::Owner) {
37 
38  this->data_ = new T[this->numElements()];
39  FAISS_ASSERT(this->data_ != nullptr);
40 }
41 
42 template <typename T, int Dim, bool InnerContig,
43  typename IndexT, template <typename U> class PtrTraits>
44 __host__
46  std::initializer_list<IndexT> sizes) :
47  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, sizes),
48  state_(AllocState::Owner) {
49  this->data_ = new T[this->numElements()];
50  FAISS_ASSERT(this->data_ != nullptr);
51 }
52 
53 template <typename T, int Dim, bool InnerContig,
54  typename IndexT, template <typename U> class PtrTraits>
55 __host__
57  DataPtrType data,
58  const IndexT sizes[Dim]) :
59  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
60  state_(AllocState::NotOwner) {
61 }
62 
63 template <typename T, int Dim, bool InnerContig,
64  typename IndexT, template <typename U> class PtrTraits>
65 __host__
67  DataPtrType data,
68  std::initializer_list<IndexT> sizes) :
69  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
70  state_(AllocState::NotOwner) {
71 }
72 
73 template <typename T, int Dim, bool InnerContig,
74  typename IndexT, template <typename U> class PtrTraits>
75 __host__
77  DataPtrType data,
78  const IndexT sizes[Dim],
79  const IndexT strides[Dim]) :
80  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes, strides),
81  state_(AllocState::NotOwner) {
82 }
83 
84 template <typename T, int Dim, bool InnerContig,
85  typename IndexT, template <typename U> class PtrTraits>
86 __host__
89  cudaStream_t stream) :
90  Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(nullptr, t.sizes(), t.strides()),
91  state_(AllocState::Owner) {
92  // Only contiguous arrays handled for now
93  FAISS_ASSERT(t.isContiguous());
94 
95  this->data_ = new T[t.numElements()];
96  this->copyFrom(t, stream);
97 }
98 
99 /// Call to zero out memory
100 template <typename T, int Dim, bool InnerContig,
101  typename IndexT, template <typename U> class PtrTraits>
104  // Region must be contiguous
105  FAISS_ASSERT(this->isContiguous());
106 
107  if (this->data_ != nullptr) {
108  memset(this->data_, 0, this->getSizeInBytes());
109  }
110 
111  return *this;
112 }
113 
114 template <typename T, int Dim, bool InnerContig,
115  typename IndexT, template <typename U> class PtrTraits>
116 __host__ T
119  auto size = this->numElements();
120 
121  FAISS_ASSERT(size == t.numElements());
122  FAISS_ASSERT(size > 0);
123 
124  if (InnerContig) {
125  auto a = this->data();
126  auto b = t.data();
127 
128  T maxDiff = a[0] - b[0];
129  // FIXME: type-specific abs()
130  maxDiff = maxDiff < 0 ? maxDiff * (T) -1 : maxDiff;
131 
132  for (IndexT i = 1; i < size; ++i) {
133  auto diff = a[i] - b[i];
134  // FIXME: type-specific abs
135  diff = diff < 0 ? diff * (T) -1 : diff;
136  if (diff > maxDiff) {
137  maxDiff = diff;
138  }
139  }
140 
141  return maxDiff;
142  } else {
143  // non-contiguous
144  // FIXME
145  FAISS_ASSERT(false);
146  return (T) 0;
147  }
148 }
149 
150 } } // namespace
__host__ __device__ size_t numElements() const
Definition: Tensor-inl.cuh:386
__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:342
__host__ __device__ bool isContiguous() const
Definition: Tensor-inl.cuh:399
__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:130
__host__ HostTensor()
Default constructor.
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:174
Our tensor type.
Definition: Tensor.cuh:28
__host__ T maxDiff(const HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.