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