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