Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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__
15 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor() :
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__
23 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::~HostTensor() {
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__
34 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
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__
46 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
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__
57 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
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__
67 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
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__
77 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
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__
88 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
89 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>& t,
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>
103 __host__ HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>&
104 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::zero() {
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
118 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::maxDiff(
119 const HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>& t) const {
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
faiss::gpu::Tensor::numElements
__host__ __device__ size_t numElements() const
Definition: Tensor-inl.cuh:387
faiss::gpu::HostTensor::~HostTensor
__host__ ~HostTensor()
Destructor.
Definition: HostTensor-inl.cuh:23
faiss::gpu::HostTensor::zero
__host__ HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > & zero()
Call to zero out memory.
Definition: HostTensor-inl.cuh:104
faiss::gpu::Tensor::data_
DataPtrType data_
Raw pointer to where the tensor data begins.
Definition: Tensor.cuh:343
faiss::gpu::Tensor::isContiguous
__host__ __device__ bool isContiguous() const
Definition: Tensor-inl.cuh:400
faiss::gpu::Tensor::copyFrom
__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
faiss::gpu::HostTensor::HostTensor
__host__ HostTensor()
Default constructor.
Definition: HostTensor-inl.cuh:15
faiss::gpu::Tensor::data
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:175
faiss::gpu::Tensor
Our tensor type.
Definition: Tensor.cuh:29
faiss::gpu::HostTensor
Definition: HostTensor.cuh:21
faiss::gpu::HostTensor::maxDiff
__host__ T maxDiff(const HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.
Definition: HostTensor-inl.cuh:118