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 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 namespace faiss { namespace gpu {
12
13 template <typename T, int Dim, bool InnerContig,
14 typename IndexT, template <typename U> class PtrTraits>
15 __host__
16 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor() :
17 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(),
18 state_(AllocState::NotOwner) {
19 }
20
21 template <typename T, int Dim, bool InnerContig,
22 typename IndexT, template <typename U> class PtrTraits>
23 __host__
24 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::~HostTensor() {
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 InnerContig,
33 typename IndexT, template <typename U> class PtrTraits>
34 __host__
35 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
36 const IndexT sizes[Dim]) :
37 Tensor<T, Dim, InnerContig, 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 InnerContig,
45 typename IndexT, template <typename U> class PtrTraits>
46 __host__
47 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
48 std::initializer_list<IndexT> sizes) :
49 Tensor<T, Dim, InnerContig, 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 InnerContig,
56 typename IndexT, template <typename U> class PtrTraits>
57 __host__
58 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
59 DataPtrType data,
60 const IndexT sizes[Dim]) :
61 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
62 state_(AllocState::NotOwner) {
63 }
64
65 template <typename T, int Dim, bool InnerContig,
66 typename IndexT, template <typename U> class PtrTraits>
67 __host__
68 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
69 DataPtrType data,
70 std::initializer_list<IndexT> sizes) :
71 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes),
72 state_(AllocState::NotOwner) {
73 }
74
75 template <typename T, int Dim, bool InnerContig,
76 typename IndexT, template <typename U> class PtrTraits>
77 __host__
78 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
79 DataPtrType data,
80 const IndexT sizes[Dim],
81 const IndexT strides[Dim]) :
82 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>(data, sizes, strides),
83 state_(AllocState::NotOwner) {
84 }
85
86 template <typename T, int Dim, bool InnerContig,
87 typename IndexT, template <typename U> class PtrTraits>
88 __host__
89 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::HostTensor(
90 Tensor<T, Dim, InnerContig, IndexT, PtrTraits>& t,
91 cudaStream_t stream) :
92 Tensor<T, Dim, InnerContig, 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 InnerContig,
103 typename IndexT, template <typename U> class PtrTraits>
104 __host__ HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>&
105 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::zero() {
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 InnerContig,
117 typename IndexT, template <typename U> class PtrTraits>
118 __host__ T
119 HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>::maxDiff(
120 const HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>& t) const {
121 auto size = this->numElements();
122
123 FAISS_ASSERT(size == t.numElements());
124 FAISS_ASSERT(size > 0);
125
126 if (InnerContig) {
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
faiss::gpu::Tensor::numElements
__host__ __device__ size_t numElements() const
Definition: Tensor-inl.cuh:352
faiss::gpu::HostTensor::~HostTensor
__host__ ~HostTensor()
Destructor.
Definition: HostTensor-inl.cuh:24
faiss::gpu::HostTensor::zero
__host__ HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > & zero()
Call to zero out memory.
Definition: HostTensor-inl.cuh:105
faiss::gpu::Tensor::data_
DataPtrType data_
Raw pointer to where the tensor data begins.
Definition: Tensor.cuh:346
faiss::gpu::Tensor::isContiguous
__host__ __device__ bool isContiguous() const
Definition: Tensor-inl.cuh:365
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:101
faiss::gpu::HostTensor::HostTensor
__host__ HostTensor()
Default constructor.
Definition: HostTensor-inl.cuh:16
faiss::gpu::Tensor::data
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:178
faiss::gpu::Tensor
Our tensor type.
Definition: Tensor.cuh:30
faiss::gpu::HostTensor
Definition: HostTensor.cuh:22
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:119