12 namespace faiss {
namespace gpu {
14 template <
typename T,
int Dim,
bool Contig,
15 typename IndexT,
template <
typename U>
class PtrTraits>
18 Tensor<T, Dim, Contig, IndexT, PtrTraits>(),
19 state_(AllocState::NotOwner) {
22 template <
typename T,
int Dim,
bool Contig,
23 typename IndexT,
template <
typename U>
class PtrTraits>
26 if (state_ == AllocState::Owner) {
27 FAISS_ASSERT(this->data_ !=
nullptr);
29 this->data_ =
nullptr;
33 template <
typename T,
int Dim,
bool Contig,
34 typename IndexT,
template <
typename U>
class PtrTraits>
37 const IndexT sizes[Dim]) :
38 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, sizes),
39 state_(AllocState::Owner) {
42 FAISS_ASSERT(this->
data_ !=
nullptr);
45 template <
typename T,
int Dim,
bool Contig,
46 typename IndexT,
template <
typename U>
class PtrTraits>
49 std::initializer_list<IndexT> sizes) :
50 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, sizes),
51 state_(AllocState::Owner) {
53 FAISS_ASSERT(this->
data_ !=
nullptr);
56 template <
typename T,
int Dim,
bool Contig,
57 typename IndexT,
template <
typename U>
class PtrTraits>
61 const IndexT sizes[Dim]) :
62 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes),
63 state_(AllocState::NotOwner) {
66 template <
typename T,
int Dim,
bool Contig,
67 typename IndexT,
template <
typename U>
class PtrTraits>
71 std::initializer_list<IndexT> sizes) :
72 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes),
73 state_(AllocState::NotOwner) {
76 template <
typename T,
int Dim,
bool Contig,
77 typename IndexT,
template <
typename U>
class PtrTraits>
81 const IndexT sizes[Dim],
82 const IndexT strides[Dim]) :
83 Tensor<T, Dim, Contig, IndexT, PtrTraits>(data, sizes, strides),
84 state_(AllocState::NotOwner) {
87 template <
typename T,
int Dim,
bool Contig,
88 typename IndexT,
template <
typename U>
class PtrTraits>
92 cudaStream_t stream) :
93 Tensor<T, Dim, Contig, IndexT, PtrTraits>(nullptr, t.sizes(), t.strides()),
94 state_(AllocState::Owner) {
103 template <
typename T,
int Dim,
bool Contig,
104 typename IndexT,
template <
typename U>
class PtrTraits>
108 FAISS_ASSERT(this->isContiguous());
110 if (this->data_ !=
nullptr) {
111 memset(this->data_, 0, this->getSizeInBytes());
117 template <
typename T,
int Dim,
bool Contig,
118 typename IndexT,
template <
typename U>
class PtrTraits>
122 auto size = this->numElements();
125 FAISS_ASSERT(size > 0);
128 auto a = this->data();
131 T maxDiff = a[0] - b[0];
133 maxDiff = maxDiff < 0 ? maxDiff * (T) -1 : maxDiff;
135 for (IndexT i = 1; i < size; ++i) {
136 auto diff = a[i] - b[i];
138 diff = diff < 0 ? diff * (T) -1 : diff;
139 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.