Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/actions/setup_linux/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ runs:
sudo ln -fs "$(which clang-tidy-22)" "/usr/local/bin/clang-tidy"
echo "::endgroup::"

- name: Install and configure gcc-14
- name: Install and configure gcc-16
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GCC 14 and 15 with the ubuntu package did not have mdspan.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we may face CI error when ubuntu PPA dies again, it seems that GCC 16 is necessary.

shell: bash
run: |
echo "::group::Install and configure gcc-14"
echo "::group::Install and configure gcc-16"
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt-get -qqy update
sudo apt-get -qy install gcc-14 g++-14
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 100
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-14 100
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-14 100
sudo apt-get -qy install gcc-16 g++-16
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-16 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-16 100
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-16 100
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-16 100
echo "::endgroup::"

- name: install qt
Expand Down
56 changes: 55 additions & 1 deletion cpp/modmesh/buffer/SimpleArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@
#include <modmesh/toggle/RadixTree.hpp>

#include <algorithm>
#include <array>
#include <concepts>
#include <format>
#include <functional>
#include <limits>
#include <mdspan>
#include <numeric>
#include <span>
#include <stdexcept>

#ifdef _MSC_VER
Expand Down Expand Up @@ -1946,6 +1949,57 @@ class SimpleArray
template <typename... Args>
value_type * vptr(Args... args) { return m_body + buffer_offset(m_stride, args...); }

std::span<value_type> as_span()
{
if (!is_c_contiguous())
{
throw std::runtime_error("SimpleArray::as_span: array is not C-contiguous");
}
return std::span<value_type>(data(), size());
}
std::span<value_type const> as_span() const
{
if (!is_c_contiguous())
{
throw std::runtime_error("SimpleArray::as_span: array is not C-contiguous");
}
return std::span<value_type const>(data(), size());
}

template <size_t N>
std::mdspan<value_type, std::dextents<size_t, N>> as_mdspan()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-dimensional view.

{
if (ndim() != N)
{
throw std::out_of_range(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime shape check.

std::format("SimpleArray::as_mdspan: rank {} does not match ndim() {}", N, ndim()));
}
if (!is_c_contiguous())
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw if the buffer is not C-contiguous.

{
throw std::runtime_error("SimpleArray::as_mdspan: array is not C-contiguous");
}
std::array<size_t, N> exts;
for (size_t i = 0; i < N; ++i) { exts[i] = shape(i); }
Comment on lines +1975 to +1982
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may check whether SimpleArray is contiguous. The size of shape may not equal to the size of data().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added above.

return std::mdspan<value_type, std::dextents<size_t, N>>(data(), exts);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use data(). Disregard ghost information.

}

template <size_t N>
std::mdspan<value_type const, std::dextents<size_t, N>> as_mdspan() const
{
if (ndim() != N)
{
throw std::out_of_range(
std::format("SimpleArray::as_mdspan: rank {} does not match ndim() {}", N, ndim()));
}
if (!is_c_contiguous())
{
throw std::runtime_error("SimpleArray::as_mdspan: array is not C-contiguous");
}
std::array<size_t, N> exts;
for (size_t i = 0; i < N; ++i) { exts[i] = shape(i); }
return std::mdspan<value_type const, std::dextents<size_t, N>>(data(), exts);
}

/* Backdoor */
value_type const & data(size_t it) const { return data()[it]; }
value_type & data(size_t it) { return data()[it]; }
Expand Down Expand Up @@ -2423,4 +2477,4 @@ class SimpleArrayPlex

} /* end namespace modmesh */

/* vim: set et ts=4 sw=4: */
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
1 change: 1 addition & 0 deletions gtests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_executable(
test_nopython_transform.cpp
test_nopython_rtree.cpp
test_nopython_formatter.cpp
test_nopython_mdspan.cpp
${MODMESH_TOGGLE_SOURCES}
${MODMESH_BUFFER_SOURCES}
${MODMESH_SERIALIZATION_SOURCES}
Expand Down
Loading
Loading