11 namespace faiss {
namespace gpu {
13 template <
typename T,
int Dim,
bool Contig,
14 typename IndexT,
template <
typename U>
class PtrTraits>
17 Tensor<T, Dim, Contig, IndexT, PtrTraits>(),
18 state_(AllocState::NotOwner) {
21 template <
typename T,
int Dim,
bool Contig,
22 typename IndexT,
template <
typename U>
class PtrTraits>
25 if (state_ == AllocState::Owner) {
26 FAISS_ASSERT(this->data_ !=
nullptr);
28 this->data_ =
nullptr;
32 template <
typename T,
int Dim,
bool Contig,
33 typename IndexT,
template <
typename U>
class PtrTraits>
36 const IndexT sizes[Dim]) :
37 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, sizes),
38 state_(AllocState::Owner) {
41 FAISS_ASSERT(this->
data_ !=
nullptr);
44 template <
typename T,
int Dim,
bool Contig,
45 typename IndexT,
template <
typename U>
class PtrTraits>
48 std::initializer_list<IndexT> sizes) :
49 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, sizes),
50 state_(AllocState::Owner) {
52 FAISS_ASSERT(this->
data_ !=
nullptr);
55 template <
typename T,
int Dim,
bool Contig,
56 typename IndexT,
template <
typename U>
class PtrTraits>
60 const IndexT sizes[Dim]) :
61 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes),
62 state_(AllocState::NotOwner) {
65 template <
typename T,
int Dim,
bool Contig,
66 typename IndexT,
template <
typename U>
class PtrTraits>
70 std::initializer_list<IndexT> sizes) :
71 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes),
72 state_(AllocState::NotOwner) {
75 template <
typename T,
int Dim,
bool Contig,
76 typename IndexT,
template <
typename U>
class PtrTraits>
80 const IndexT sizes[Dim],
81 const IndexT strides[Dim]) :
82 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes, strides),
83 state_(AllocState::NotOwner) {
86 template <
typename T,
int Dim,
bool Contig,
87 typename IndexT,
template <
typename U>
class PtrTraits>
91 cudaStream_t stream) :
92 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, t.sizes(), t.strides()),
93 state_(AllocState::Owner) {
102 template <
typename T,
int Dim,
bool Contig,
103 typename IndexT,
template <
typename U>
class PtrTraits>
107 FAISS_ASSERT(this->isContiguous());
109 if (this->data_ !=
nullptr) {
110 memset(this->data_, 0, this->getSizeInBytes());
116 template <
typename T,
int Dim,
bool Contig,
117 typename IndexT,
template <
typename U>
class PtrTraits>
121 auto size = this->numElements();
124 FAISS_ASSERT(size > 0);
127 auto a = this->data();
130 T maxDiff = a[0] - b[0];
132 maxDiff = maxDiff < 0 ? maxDiff * (T) -1 : maxDiff;
134 for (IndexT i = 1; i < size; ++i) {
135 auto diff = a[i] - b[i];
137 diff = diff < 0 ? diff * (T) -1 : diff;
138 if (diff > maxDiff) {
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
DataPtrType data_
Raw pointer to where the tensor data begins.
__host__ HostTensor< T, Dim, Contig, IndexT, PtrTraits > & zero()
Call to zero out memory.
__host__ HostTensor()
Default constructor.
__host__ __device__ IndexT numElements() const
__host__ void copyFrom(Tensor< T, Dim, Contig, IndexT, PtrTraits > &t, cudaStream_t stream)
Copies a tensor into ourselves; sizes must match.
__host__ ~HostTensor()
Destructor.
__host__ __device__ bool isContiguous() const
__host__ T maxDiff(const HostTensor< T, Dim, Contig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.