mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-29 23:34:12 +08:00
Marked a few functions as EIGEN_DEVICE_FUNC to enable the use of tensors in cuda kernels.
This commit is contained in:
parent
fefec723aa
commit
f669f5656a
@ -143,8 +143,8 @@ inline void* handmade_aligned_realloc(void* ptr, std::size_t size, std::size_t =
|
|||||||
*** Implementation of generic aligned realloc (when no realloc can be used)***
|
*** Implementation of generic aligned realloc (when no realloc can be used)***
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
void* aligned_malloc(std::size_t size);
|
EIGEN_DEVICE_FUNC void* aligned_malloc(std::size_t size);
|
||||||
void aligned_free(void *ptr);
|
EIGEN_DEVICE_FUNC void aligned_free(void *ptr);
|
||||||
|
|
||||||
/** \internal
|
/** \internal
|
||||||
* \brief Reallocates aligned memory.
|
* \brief Reallocates aligned memory.
|
||||||
@ -185,33 +185,33 @@ inline void* generic_aligned_realloc(void* ptr, size_t size, size_t old_size)
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#ifdef EIGEN_NO_MALLOC
|
#ifdef EIGEN_NO_MALLOC
|
||||||
inline void check_that_malloc_is_allowed()
|
EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
|
||||||
{
|
{
|
||||||
eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
|
eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
|
||||||
}
|
}
|
||||||
#elif defined EIGEN_RUNTIME_NO_MALLOC
|
#elif defined EIGEN_RUNTIME_NO_MALLOC
|
||||||
inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
|
EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
|
||||||
{
|
{
|
||||||
static bool value = true;
|
static bool value = true;
|
||||||
if (update == 1)
|
if (update == 1)
|
||||||
value = new_value;
|
value = new_value;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
|
EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
|
||||||
inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
|
EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
|
||||||
inline void check_that_malloc_is_allowed()
|
EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
|
||||||
{
|
{
|
||||||
eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
|
eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
inline void check_that_malloc_is_allowed()
|
EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
|
||||||
{}
|
{}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 or 32 bytes alignment depending on the requirements.
|
/** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 or 32 bytes alignment depending on the requirements.
|
||||||
* On allocation error, the returned pointer is null, and std::bad_alloc is thrown.
|
* On allocation error, the returned pointer is null, and std::bad_alloc is thrown.
|
||||||
*/
|
*/
|
||||||
inline void* aligned_malloc(size_t size)
|
EIGEN_DEVICE_FUNC inline void* aligned_malloc(size_t size)
|
||||||
{
|
{
|
||||||
check_that_malloc_is_allowed();
|
check_that_malloc_is_allowed();
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ inline void* aligned_malloc(size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** \internal Frees memory allocated with aligned_malloc. */
|
/** \internal Frees memory allocated with aligned_malloc. */
|
||||||
inline void aligned_free(void *ptr)
|
EIGEN_DEVICE_FUNC inline void aligned_free(void *ptr)
|
||||||
{
|
{
|
||||||
#if !EIGEN_ALIGN
|
#if !EIGEN_ALIGN
|
||||||
std::free(ptr);
|
std::free(ptr);
|
||||||
@ -298,12 +298,12 @@ inline void* aligned_realloc(void *ptr, size_t new_size, size_t old_size)
|
|||||||
/** \internal Allocates \a size bytes. If Align is true, then the returned ptr is 16-byte-aligned.
|
/** \internal Allocates \a size bytes. If Align is true, then the returned ptr is 16-byte-aligned.
|
||||||
* On allocation error, the returned pointer is null, and a std::bad_alloc is thrown.
|
* On allocation error, the returned pointer is null, and a std::bad_alloc is thrown.
|
||||||
*/
|
*/
|
||||||
template<bool Align> inline void* conditional_aligned_malloc(size_t size)
|
template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(size_t size)
|
||||||
{
|
{
|
||||||
return aligned_malloc(size);
|
return aligned_malloc(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline void* conditional_aligned_malloc<false>(size_t size)
|
template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(size_t size)
|
||||||
{
|
{
|
||||||
check_that_malloc_is_allowed();
|
check_that_malloc_is_allowed();
|
||||||
|
|
||||||
@ -314,12 +314,12 @@ template<> inline void* conditional_aligned_malloc<false>(size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** \internal Frees memory allocated with conditional_aligned_malloc */
|
/** \internal Frees memory allocated with conditional_aligned_malloc */
|
||||||
template<bool Align> inline void conditional_aligned_free(void *ptr)
|
template<bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void *ptr)
|
||||||
{
|
{
|
||||||
aligned_free(ptr);
|
aligned_free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline void conditional_aligned_free<false>(void *ptr)
|
template<> EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void *ptr)
|
||||||
{
|
{
|
||||||
std::free(ptr);
|
std::free(ptr);
|
||||||
}
|
}
|
||||||
@ -341,7 +341,7 @@ template<> inline void* conditional_aligned_realloc<false>(void* ptr, size_t new
|
|||||||
/** \internal Destructs the elements of an array.
|
/** \internal Destructs the elements of an array.
|
||||||
* The \a size parameters tells on how many objects to call the destructor of T.
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
||||||
*/
|
*/
|
||||||
template<typename T> inline void destruct_elements_of_array(T *ptr, size_t size)
|
template<typename T> EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T *ptr, size_t size)
|
||||||
{
|
{
|
||||||
// always destruct an array starting from the end.
|
// always destruct an array starting from the end.
|
||||||
if(ptr)
|
if(ptr)
|
||||||
@ -351,7 +351,7 @@ template<typename T> inline void destruct_elements_of_array(T *ptr, size_t size)
|
|||||||
/** \internal Constructs the elements of an array.
|
/** \internal Constructs the elements of an array.
|
||||||
* The \a size parameter tells on how many objects to call the constructor of T.
|
* The \a size parameter tells on how many objects to call the constructor of T.
|
||||||
*/
|
*/
|
||||||
template<typename T> inline T* construct_elements_of_array(T *ptr, size_t size)
|
template<typename T> EIGEN_DEVICE_FUNC inline T* construct_elements_of_array(T *ptr, size_t size)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
EIGEN_TRY
|
EIGEN_TRY
|
||||||
@ -371,7 +371,7 @@ template<typename T> inline T* construct_elements_of_array(T *ptr, size_t size)
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
|
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
|
||||||
{
|
{
|
||||||
if(size > size_t(-1) / sizeof(T))
|
if(size > size_t(-1) / sizeof(T))
|
||||||
throw_std_bad_alloc();
|
throw_std_bad_alloc();
|
||||||
@ -381,7 +381,7 @@ EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
|
|||||||
* On allocation error, the returned pointer is undefined, but a std::bad_alloc is thrown.
|
* On allocation error, the returned pointer is undefined, but a std::bad_alloc is thrown.
|
||||||
* The default constructor of T is called.
|
* The default constructor of T is called.
|
||||||
*/
|
*/
|
||||||
template<typename T> inline T* aligned_new(size_t size)
|
template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(size_t size)
|
||||||
{
|
{
|
||||||
check_size_for_overflow<T>(size);
|
check_size_for_overflow<T>(size);
|
||||||
T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
|
T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
|
||||||
@ -396,7 +396,7 @@ template<typename T> inline T* aligned_new(size_t size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, bool Align> inline T* conditional_aligned_new(size_t size)
|
template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(size_t size)
|
||||||
{
|
{
|
||||||
check_size_for_overflow<T>(size);
|
check_size_for_overflow<T>(size);
|
||||||
T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
|
T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
|
||||||
@ -414,7 +414,7 @@ template<typename T, bool Align> inline T* conditional_aligned_new(size_t size)
|
|||||||
/** \internal Deletes objects constructed with aligned_new
|
/** \internal Deletes objects constructed with aligned_new
|
||||||
* The \a size parameters tells on how many objects to call the destructor of T.
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
||||||
*/
|
*/
|
||||||
template<typename T> inline void aligned_delete(T *ptr, size_t size)
|
template<typename T> EIGEN_DEVICE_FUNC inline void aligned_delete(T *ptr, size_t size)
|
||||||
{
|
{
|
||||||
destruct_elements_of_array<T>(ptr, size);
|
destruct_elements_of_array<T>(ptr, size);
|
||||||
aligned_free(ptr);
|
aligned_free(ptr);
|
||||||
@ -423,13 +423,13 @@ template<typename T> inline void aligned_delete(T *ptr, size_t size)
|
|||||||
/** \internal Deletes objects constructed with conditional_aligned_new
|
/** \internal Deletes objects constructed with conditional_aligned_new
|
||||||
* The \a size parameters tells on how many objects to call the destructor of T.
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
||||||
*/
|
*/
|
||||||
template<typename T, bool Align> inline void conditional_aligned_delete(T *ptr, size_t size)
|
template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T *ptr, size_t size)
|
||||||
{
|
{
|
||||||
destruct_elements_of_array<T>(ptr, size);
|
destruct_elements_of_array<T>(ptr, size);
|
||||||
conditional_aligned_free<Align>(ptr);
|
conditional_aligned_free<Align>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
|
template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
|
||||||
{
|
{
|
||||||
check_size_for_overflow<T>(new_size);
|
check_size_for_overflow<T>(new_size);
|
||||||
check_size_for_overflow<T>(old_size);
|
check_size_for_overflow<T>(old_size);
|
||||||
@ -452,7 +452,7 @@ template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pt
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
|
template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(size_t size)
|
||||||
{
|
{
|
||||||
if(size==0)
|
if(size==0)
|
||||||
return 0; // short-cut. Also fixes Bug 884
|
return 0; // short-cut. Also fixes Bug 884
|
||||||
@ -495,7 +495,7 @@ template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, bool Align> inline void conditional_aligned_delete_auto(T *ptr, size_t size)
|
template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T *ptr, size_t size)
|
||||||
{
|
{
|
||||||
if(NumTraits<T>::RequireInitialization)
|
if(NumTraits<T>::RequireInitialization)
|
||||||
destruct_elements_of_array<T>(ptr, size);
|
destruct_elements_of_array<T>(ptr, size);
|
||||||
|
@ -350,7 +350,7 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_> >
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void resize(const array<Index, NumIndices>& dimensions)
|
EIGEN_DEVICE_FUNC void resize(const array<Index, NumIndices>& dimensions)
|
||||||
{
|
{
|
||||||
std::size_t i;
|
std::size_t i;
|
||||||
Index size = Index(1);
|
Index size = Index(1);
|
||||||
@ -367,7 +367,7 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_> >
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize(const DSizes<Index, NumIndices>& dimensions) {
|
EIGEN_DEVICE_FUNC void resize(const DSizes<Index, NumIndices>& dimensions) {
|
||||||
array<Index, NumIndices> dims;
|
array<Index, NumIndices> dims;
|
||||||
for (std::size_t i = 0; i < NumIndices; ++i) {
|
for (std::size_t i = 0; i < NumIndices; ++i) {
|
||||||
dims[i] = dimensions[i];
|
dims[i] = dimensions[i];
|
||||||
|
@ -275,7 +275,7 @@ struct DSizes : array<DenseIndex, NumDims> {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DSizes& operator = (const array<DenseIndex, NumDims>& other) {
|
EIGEN_DEVICE_FUNC DSizes& operator = (const array<DenseIndex, NumDims>& other) {
|
||||||
*static_cast<Base*>(this) = other;
|
*static_cast<Base*>(this) = other;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -112,9 +112,9 @@ class TensorStorage<T, NumIndices_, Dynamic, Options_, typename internal::gen_nu
|
|||||||
void swap(Self_& other)
|
void swap(Self_& other)
|
||||||
{ std::swap(m_data,other.m_data); std::swap(m_dimensions,other.m_dimensions); }
|
{ std::swap(m_data,other.m_data); std::swap(m_dimensions,other.m_dimensions); }
|
||||||
|
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const DSizes<DenseIndex, NumIndices_>& dimensions() const {return m_dimensions;}
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const DSizes<DenseIndex, NumIndices_>& dimensions() const {return m_dimensions;}
|
||||||
|
|
||||||
void resize(DenseIndex size, const array<DenseIndex, NumIndices_>& nbDimensions)
|
EIGEN_DEVICE_FUNC void resize(DenseIndex size, const array<DenseIndex, NumIndices_>& nbDimensions)
|
||||||
{
|
{
|
||||||
const DenseIndex currentSz = internal::array_prod(m_dimensions);
|
const DenseIndex currentSz = internal::array_prod(m_dimensions);
|
||||||
if(size != currentSz)
|
if(size != currentSz)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user