Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
HostTensor.cuh
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 
9 #pragma once
10 
11 #include "Tensor.cuh"
12 
13 namespace faiss { namespace gpu {
14 
15 template <typename T,
16  int Dim,
17  bool InnerContig = false,
18  typename IndexT = int,
19  template <typename U> class PtrTraits = traits::DefaultPtrTraits>
20 class HostTensor : public Tensor<T, Dim, InnerContig, IndexT, PtrTraits> {
21  public:
22  typedef IndexT IndexType;
23  typedef typename PtrTraits<T>::PtrType DataPtrType;
24 
25  /// Default constructor
26  __host__ HostTensor();
27 
28  /// Destructor
29  __host__ ~HostTensor();
30 
31  /// Constructs a tensor of the given size, allocating memory for it
32  /// locally
33  __host__ HostTensor(const IndexT sizes[Dim]);
34  __host__ HostTensor(std::initializer_list<IndexT> sizes);
35 
36  /// Constructs a tensor of the given size and stride, referencing a
37  /// memory region we do not own
38  __host__ HostTensor(DataPtrType data,
39  const IndexT sizes[Dim]);
40  __host__ HostTensor(DataPtrType data,
41  std::initializer_list<IndexT> sizes);
42 
43  /// Constructs a tensor of the given size and stride, referencing a
44  /// memory region we do not own
45  __host__ HostTensor(DataPtrType data,
46  const IndexT sizes[Dim],
47  const IndexT strides[Dim]);
48 
49  /// Copies a tensor into ourselves, allocating memory for it
50  /// locally. If the tensor is on the GPU, then we will copy it to
51  /// ourselves wrt the given stream.
53  cudaStream_t stream);
54 
55  /// Call to zero out memory
57 
58  /// Returns the maximum difference seen between two tensors
59  __host__ T
61 
62  /// Are the two tensors exactly equal?
63  __host__ bool
65  return (maxDiff(t) == (T) 0);
66  }
67 
68  private:
69  enum AllocState {
70  /// This tensor itself owns the memory, which must be freed via
71  /// cudaFree
72  Owner,
73 
74  /// This tensor itself is not an owner of the memory; there is
75  /// nothing to free
76  NotOwner,
77  };
78 
79  AllocState state_;
80 };
81 
82 } } // namespace
83 
84 #include "HostTensor-inl.cuh"
__host__ ~HostTensor()
Destructor.
__host__ HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > & zero()
Call to zero out memory.
__host__ __device__ const IndexT * sizes() const
Returns the size array.
Definition: Tensor.cuh:243
__host__ HostTensor()
Default constructor.
__host__ __device__ const IndexT * strides() const
Returns the stride array.
Definition: Tensor.cuh:248
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:174
Our tensor type.
Definition: Tensor.cuh:28
__host__ bool equal(const HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > &t) const
Are the two tensors exactly equal?
Definition: HostTensor.cuh:64
__host__ T maxDiff(const HostTensor< T, Dim, InnerContig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.