Skip to content

Commit

Permalink
Small changes. not sure if this does anything
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Aug 17, 2023
1 parent 6fde8b5 commit 98c3768
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 232 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(CMAKE_CXX_STANDARD 23)
endif ()

# LibRapid requires C++17 or later
#if (CMAKE_CXX_STANDARD LESS 17)
# message(FATAL_ERROR "LibRapid requires C++17 or later")
#endif ()
# LibRapid requires C++20 or later
if (CMAKE_CXX_STANDARD LESS 20)
message(FATAL_ERROR "LibRapid requires C++20 or later")
endif ()

# Extract version information
file(READ "version.txt" ver)
Expand Down
2 changes: 2 additions & 0 deletions librapid/include/librapid/array/assignOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ namespace librapid {
const int64_t size = function.shape().size();
const int64_t vectorSize = size - (size % packetWidth);

LIBRAPID_ASSUME(vectorSize % packetWidth == 0);

// Ensure the function can actually be assigned to the array container
// static_assert(
// typetraits::IsSame<Scalar, typename std::decay_t<decltype(function)>::Scalar>,
Expand Down
43 changes: 27 additions & 16 deletions librapid/include/librapid/array/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace librapid {
LIBRAPID_ALWAYS_INLINE void resizeImpl(SizeType newSize);

#if defined(LIBRAPID_NATIVE_ARCH) && !defined(LIBRAPID_APPLE)
alignas(LIBRAPID_DEFAULT_MEM_ALIGN) Pointer m_begin = nullptr;
alignas(LIBRAPID_MEM_ALIGN) Pointer m_begin = nullptr;
#else
Pointer m_begin = nullptr; // Pointer to the beginning of the data
#endif
Expand Down Expand Up @@ -303,7 +303,7 @@ namespace librapid {

private:
#if defined(LIBRAPID_NATIVE_ARCH) && !defined(LIBRAPID_APPLE)
alignas(LIBRAPID_DEFAULT_MEM_ALIGN) std::array<Scalar, Size> m_data;
alignas(LIBRAPID_MEM_ALIGN) std::array<Scalar, Size> m_data;
#else
// No memory alignment on Apple platforms or if it is disabled
std::array<Scalar, Size> m_data;
Expand Down Expand Up @@ -335,18 +335,21 @@ namespace librapid {
/// \param size The number of elements of type \p in the memory block
template<typename T>
void safeDeallocate(T *ptr, size_t size) {
auto ptr_ = LIBRAPID_ASSUME_ALIGNED(ptr);
LIBRAPID_ASSUME(ptr_ != nullptr);
LIBRAPID_ASSUME(size > 0);
if constexpr (!std::is_trivially_destructible_v<T>) {
for (size_t i = 0; i < size; ++i) { ptr[i].~T(); }
for (size_t i = 0; i < size; ++i) { ptr_[i].~T(); }
}

#if defined(LIBRAPID_BLAS_MKLBLAS)
mkl_free(ptr);
mkl_free(ptr_);
#elif defined(LIBRAPID_APPLE)
free(ptr);
free(ptr_);
#elif defined(LIBRAPID_NATIVE_ARCH) && defined(LIBRAPID_MSVC)
_aligned_free(ptr);
_aligned_free(ptr_);
#else
free(ptr);
free(ptr_);
#endif
}

Expand All @@ -370,25 +373,29 @@ namespace librapid {
#elif defined(LIBRAPID_APPLE)
// Use posix_memalign
void *_ptr;
auto err = posix_memalign(&_ptr, global::memoryAlignment, size * sizeof(T));
auto err = posix_memalign(&_ptr, LIBRAPID_MEM_ALIGN, size * sizeof(T));
LIBRAPID_ASSERT(err == 0, "posix_memalign failed with error code {}", err);
auto ptr = static_cast<RawPointer>(_ptr);
#elif defined(LIBRAPID_MSVC) || defined(LIBRAPID_MINGW)
auto ptr =
static_cast<RawPointer>(_aligned_malloc(size * sizeof(T), global::memoryAlignment));
static_cast<RawPointer>(_aligned_malloc(size * sizeof(T), LIBRAPID_MEM_ALIGN));
#else
auto ptr = static_cast<RawPointer>(
std::aligned_alloc(global::memoryAlignment, size * sizeof(T)));
auto ptr =
static_cast<RawPointer>(std::aligned_alloc(LIBRAPID_MEM_ALIGN, size * sizeof(T)));
#endif

LIBRAPID_ASSERT(
ptr != nullptr, "Failed to allocate {} bytes of memory", size * sizeof(T));

// If the type cannot be trivially constructed, we need to
// initialize each value

auto ptr_ = LIBRAPID_ASSUME_ALIGNED(ptr);
LIBRAPID_ASSUME(ptr_ != nullptr);
LIBRAPID_ASSUME(size > 0);
if constexpr (!typetraits::TriviallyDefaultConstructible<T>::value &&
!std::is_array<T>::value) {
for (RawPointer p = ptr; p != ptr + size; ++p) { new (p) T(); }
for (RawPointer p = ptr_; p != ptr_ + size; ++p) { new (p) T(); }
}

return Pointer(ptr, [size](RawPointer ptr) { safeDeallocate(ptr, size); });
Expand Down Expand Up @@ -552,17 +559,19 @@ namespace librapid {
m_size = static_cast<SizeType>(std::distance(begin, end));
m_begin = detail::safeAllocate<T>(m_size);

auto thisBegin = LIBRAPID_ASSUME_ALIGNED(m_begin.get());
auto otherBegin = LIBRAPID_ASSUME_ALIGNED(begin);
if constexpr (typetraits::TypeInfo<T>::canMemcpy) {
if constexpr (typetraits::TriviallyDefaultConstructible<T>::value) {
// Use a slightly faster memcpy if the type is trivially default constructible
std::uninitialized_copy(begin, end, m_begin.get());
std::uninitialized_copy(otherBegin, end, thisBegin);
} else {
// Otherwise, use the standard copy algorithm
std::copy(begin, end, m_begin.get());
}
} else {
// Since we can't memcpy, we have to copy each element individually
for (SizeType i = 0; i < m_size; ++i) { m_begin.get()[i] = begin[i]; }
for (SizeType i = 0; i < m_size; ++i) { thisBegin[i] = otherBegin[i]; }
}
}

Expand Down Expand Up @@ -633,15 +642,17 @@ namespace librapid {
m_begin = detail::safeAllocate<T>(newSize);
m_size = newSize;

auto oldBeginAligned = LIBRAPID_ASSUME_ALIGNED(oldBegin);
auto beginAligned = LIBRAPID_ASSUME_ALIGNED(m_begin.get());
// Copy the data across
if constexpr (typetraits::TriviallyDefaultConstructible<T>::value) {
// Use a slightly faster memcpy if the type is trivially default constructible
std::uninitialized_copy(
oldBegin.get(), oldBegin.get() + ::librapid::min(oldSize, newSize), m_begin.get());
oldBeginAligned, oldBeginAligned + ::librapid::min(oldSize, newSize), beginAligned);
} else {
// Otherwise, use the standard copy algorithm
std::copy(
oldBegin.get(), oldBegin.get() + ::librapid::min(oldSize, newSize), m_begin.get());
oldBeginAligned, oldBeginAligned + ::librapid::min(oldSize, newSize), beginAligned);
}
}

Expand Down
Loading

0 comments on commit 98c3768

Please sign in to comment.