Skip to content

Commit

Permalink
Continue refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Sep 6, 2023
1 parent 63be05e commit 748edf5
Show file tree
Hide file tree
Showing 6 changed files with 945 additions and 1,011 deletions.
21 changes: 12 additions & 9 deletions librapid/include/librapid/array/arrayContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ namespace librapid {
struct IsArrayContainer : std::false_type {};

template<typename ShapeType, typename StorageScalar>
struct IsArrayContainer<array::ArrayContainer<ShapeType, StorageScalar>> : std::true_type {};
struct IsArrayContainer<array::ArrayContainer<ShapeType, StorageScalar>> : std::true_type {
};

LIBRAPID_DEFINE_AS_TYPE(typename StorageScalar,
array::ArrayContainer<Shape COMMA StorageScalar>);
Expand Down Expand Up @@ -765,12 +766,10 @@ namespace librapid {
ArrayContainer<ShapeType_, StorageType_>::packet(size_t index) const -> Packet {
auto ptr = LIBRAPID_ASSUME_ALIGNED(m_storage.begin());

#if defined(LIBRAPID_NATIVE_ARCH) && !defined(LIBRAPID_OSX)
// On MacOS (and other platforms??) we cannot use aligned loads in arrays due to one
// annoying edge case. Normally, all SIMD loads will be aligned to a 64-byte boundary.
// Say, however, this array is a sub-array of a larger array. If the outer dimension
// of the larger array does not result in a 64-byte alignment, the data of *this* array
// will not be correctly aligned, hence causing a segfault.
#if defined(LIBRAPID_NATIVE_ARCH)
LIBRAIPD_ASSERT(ptr % Packet::size == 0,
"ArrayContainer::packet called on unaligned storage");

return xsimd::load_aligned(ptr + index);
#else
return xsimd::load_unaligned(ptr + index);
Expand All @@ -786,10 +785,14 @@ namespace librapid {
template<typename ShapeType_, typename StorageType_>
LIBRAPID_ALWAYS_INLINE void
ArrayContainer<ShapeType_, StorageType_>::writePacket(size_t index, const Packet &value) {
auto ptr = LIBRAPID_ASSUME_ALIGNED(m_storage.begin());

#if defined(LIBRAPID_NATIVE_ARCH)
value.store_aligned(m_storage.begin() + index);
LIBRAIPD_ASSERT(ptr % Packet::size == 0,
"ArrayContainer::packet called on unaligned storage");
value.store_aligned(ptr + index);
#else
value.store_unaligned(m_storage.begin() + index);
value.store_unaligned(ptr + index);
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions librapid/include/librapid/array/assignOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ namespace librapid {
::librapid::detail::LibRapidType::Scalar,
int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto dataSourceExtractor(const T &obj) {
return obj.storage().begin().get();
return obj.storage().begin();
}

template<typename T, typename std::enable_if_t<typetraits::TypeInfo<T>::type ==
Expand Down Expand Up @@ -518,7 +518,7 @@ namespace librapid {
std::make_index_sequence<argSize>(),
filename,
kernelName,
reinterpret_cast<Scalar *>(lhs.storage().begin().get()),
reinterpret_cast<Scalar *>(lhs.storage().begin()),
function);
}
} // namespace detail
Expand Down
4 changes: 2 additions & 2 deletions librapid/include/librapid/array/linalg/transpose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ namespace librapid {
else {
if (m_inputShape.ndim() == 2) {
int64_t blockSize = global::cacheLineSize / sizeof(Scalar);
auto *__restrict outPtr = out.storage().begin().get();
auto *__restrict inPtr = m_array.storage().begin().get();
auto *__restrict outPtr = out.storage().begin();
auto *__restrict inPtr = m_array.storage().begin();
detail::cuda::transposeImpl(
outPtr, inPtr, m_inputShape[0], m_inputShape[1], m_alpha, blockSize);
} else {
Expand Down
19 changes: 4 additions & 15 deletions librapid/include/librapid/array/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ namespace librapid {
/// Free a Storage object
~Storage();

/// \brief Set this storage object to reference the same data as \p other
/// \param other Storage object to reference
void set(const Storage &other);

/// \brief Return a Storage object on the host with the same data as this Storage object
/// (mainly for use with CUDA or OpenCL)
/// \return
Expand Down Expand Up @@ -174,7 +170,7 @@ namespace librapid {
template<typename P>
LIBRAPID_ALWAYS_INLINE void initData(P begin, SizeType size);

#if defined(LIBRAPID_NATIVE_ARCH) && !defined(LIBRAPID_APPLE)
#if defined(LIBRAPID_NATIVE_ARCH)
alignas(LIBRAPID_MEM_ALIGN) Pointer m_begin = nullptr;
#else
Pointer m_begin = nullptr; // Pointer to the beginning of the data
Expand Down Expand Up @@ -475,7 +471,7 @@ namespace librapid {
if (this != &other) {
size_t oldSize = m_size;
m_size = other.m_size;
if (other.m_size == m_size) LIBRAPID_UNLIKELY {
if (other.m_size != m_size) LIBRAPID_UNLIKELY {
if (m_ownsData) LIBRAPID_LIKELY {
// Reallocate
detail::safeDeallocate(m_begin, oldSize);
Expand All @@ -494,7 +490,7 @@ namespace librapid {
}

template<typename T>
Storage<T> &Storage<T>::operator=(Storage &&other) noexcept {
auto Storage<T>::operator=(Storage &&other) noexcept -> Storage & {
if (this != &other) {
m_begin = std::move(other.m_begin);
m_size = std::move(other.m_size);
Expand All @@ -517,6 +513,7 @@ namespace librapid {
void Storage<T>::initData(P begin, P end) {
m_size = static_cast<SizeType>(std::distance(begin, end));
m_begin = detail::safeAllocate<T>(m_size);
m_ownsData = true;
auto thisBegin = LIBRAPID_ASSUME_ALIGNED(m_begin);
auto otherBegin = LIBRAPID_ASSUME_ALIGNED(begin);
detail::fastCopy(thisBegin, otherBegin, m_size);
Expand All @@ -528,14 +525,6 @@ namespace librapid {
initData(begin, begin + size);
}

// template<typename T>
// void Storage<T>::set(const Storage<T> &other) {
// // We can simply copy the shared pointers across
// m_begin = other.m_begin;
// m_size = other.m_size;
// m_ownsData = other.m_ownsData;
// }

template<typename T>
auto Storage<T>::toHostStorage() const -> Storage {
return copy();
Expand Down
Loading

0 comments on commit 748edf5

Please sign in to comment.