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