All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
HostTensor.cuh
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include "Tensor.cuh"
namespace faiss { namespace gpu {
template <typename T,
int Dim,
bool Contig = false,
typename IndexT = int,
template <typename U> class PtrTraits = traits::DefaultPtrTraits>
class HostTensor : public Tensor<T, Dim, Contig, IndexT, PtrTraits> {
public:
typedef IndexT IndexType;
typedef typename PtrTraits<T>::PtrType DataPtrType;
/// Default constructor
__host__ HostTensor();
/// Destructor
__host__ ~HostTensor();
/// 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.
__host__ HostTensor(Tensor<T, Dim, Contig, IndexT, PtrTraits>& t,
cudaStream_t stream);
/// Call to zero out memory
__host__ HostTensor<T, Dim, Contig, IndexT, PtrTraits>& zero();
/// Returns the maximum difference seen between two tensors
__host__ T
maxDiff(const HostTensor<T, Dim, Contig, IndexT, PtrTraits>& t) const;
/// Are the two tensors exactly equal?
__host__ bool
equal(const HostTensor<T, Dim, Contig, IndexT, PtrTraits>& t) const {
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
#include "HostTensor-inl.cuh"
faiss::gpu::Tensor::data
__host__ __device__ DataPtrType data()
Returns a raw pointer to the start of our data.
Definition: Tensor.cuh:173
faiss::gpu::HostTensor::zero
__host__ HostTensor< T, Dim, Contig, IndexT, PtrTraits > & zero()
Call to zero out memory.
Definition: HostTensor-inl.cuh:105
faiss::gpu::HostTensor::HostTensor
__host__ HostTensor()
Default constructor.
Definition: HostTensor-inl.cuh:16
faiss::gpu::Tensor::strides
__host__ __device__ const IndexT * strides() const
Returns the stride array.
Definition: Tensor.cuh:247
faiss::gpu::Tensor
Our tensor type.
Definition: Tensor.cuh:30
faiss::gpu::Tensor::sizes
__host__ __device__ const IndexT * sizes() const
Returns the size array.
Definition: Tensor.cuh:242
faiss::gpu::HostTensor
Definition: HostTensor.cuh:22
faiss::gpu::HostTensor::~HostTensor
__host__ ~HostTensor()
Destructor.
Definition: HostTensor-inl.cuh:24
faiss::gpu::HostTensor::equal
__host__ bool equal(const HostTensor< T, Dim, Contig, IndexT, PtrTraits > &t) const
Are the two tensors exactly equal?
Definition: HostTensor.cuh:66
faiss::gpu::HostTensor::maxDiff
__host__ T maxDiff(const HostTensor< T, Dim, Contig, IndexT, PtrTraits > &t) const
Returns the maximum difference seen between two tensors.
Definition: HostTensor-inl.cuh:119