Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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