2017-02-23 06:26:44 +08:00
|
|
|
/**
|
2019-05-28 22:17:22 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-02-23 06:26:44 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2017-02-23 06:26:44 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
#include <faiss/gpu/utils/Tensor.cuh>
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
namespace faiss { namespace gpu {
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
int Dim,
|
2017-11-22 21:11:28 +08:00
|
|
|
bool InnerContig = false,
|
2017-02-23 06:26:44 +08:00
|
|
|
typename IndexT = int,
|
|
|
|
template <typename U> class PtrTraits = traits::DefaultPtrTraits>
|
2017-11-22 21:11:28 +08:00
|
|
|
class HostTensor : public Tensor<T, Dim, InnerContig, IndexT, PtrTraits> {
|
2017-02-23 06:26:44 +08:00
|
|
|
public:
|
|
|
|
typedef IndexT IndexType;
|
|
|
|
typedef typename PtrTraits<T>::PtrType DataPtrType;
|
|
|
|
|
|
|
|
/// Default constructor
|
|
|
|
__host__ HostTensor();
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
__host__ ~HostTensor();
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
/// Move constructor
|
|
|
|
__host__ HostTensor(HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>&& t);
|
|
|
|
|
|
|
|
/// Move assignment
|
|
|
|
__host__ HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>&
|
|
|
|
operator=(HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>&& t);
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
/// Constructs a tensor of the given size, allocating memory for it
|
|
|
|
/// locally
|
|
|
|
__host__ HostTensor(const IndexT sizes[Dim]);
|
|
|
|
__host__ HostTensor(std::initializer_list<IndexT> sizes);
|
|
|
|
|
|
|
|
/// Constructs a tensor of the given size and stride, referencing a
|
|
|
|
/// memory region we do not own
|
|
|
|
__host__ HostTensor(DataPtrType data,
|
|
|
|
const IndexT sizes[Dim]);
|
|
|
|
__host__ HostTensor(DataPtrType data,
|
|
|
|
std::initializer_list<IndexT> sizes);
|
|
|
|
|
|
|
|
/// Constructs a tensor of the given size and stride, referencing a
|
|
|
|
/// memory region we do not own
|
|
|
|
__host__ HostTensor(DataPtrType data,
|
|
|
|
const IndexT sizes[Dim],
|
|
|
|
const IndexT strides[Dim]);
|
|
|
|
|
|
|
|
/// Copies a tensor into ourselves, allocating memory for it
|
|
|
|
/// locally. If the tensor is on the GPU, then we will copy it to
|
|
|
|
/// ourselves wrt the given stream.
|
2017-11-22 21:11:28 +08:00
|
|
|
__host__ HostTensor(Tensor<T, Dim, InnerContig, IndexT, PtrTraits>& t,
|
2017-02-23 06:26:44 +08:00
|
|
|
cudaStream_t stream);
|
|
|
|
|
|
|
|
/// Call to zero out memory
|
2017-11-22 21:11:28 +08:00
|
|
|
__host__ HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>& zero();
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
/// Returns the maximum difference seen between two tensors
|
|
|
|
__host__ T
|
2017-11-22 21:11:28 +08:00
|
|
|
maxDiff(const HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>& t) const;
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
/// Are the two tensors exactly equal?
|
|
|
|
__host__ bool
|
2017-11-22 21:11:28 +08:00
|
|
|
equal(const HostTensor<T, Dim, InnerContig, IndexT, PtrTraits>& t) const {
|
2017-02-23 06:26:44 +08:00
|
|
|
return (maxDiff(t) == (T) 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum AllocState {
|
|
|
|
/// This tensor itself owns the memory, which must be freed via
|
|
|
|
/// cudaFree
|
|
|
|
Owner,
|
|
|
|
|
|
|
|
/// This tensor itself is not an owner of the memory; there is
|
|
|
|
/// nothing to free
|
|
|
|
NotOwner,
|
|
|
|
};
|
|
|
|
|
|
|
|
AllocState state_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
#include <faiss/gpu/utils/HostTensor-inl.cuh>
|