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