|
| 1 | +// Copyright (C) The DDC development team, see COPYRIGHT.md file |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <complex> |
| 8 | +#include <cstddef> |
| 9 | +#include <filesystem> |
| 10 | +#include <iosfwd> |
| 11 | +#include <string> |
| 12 | +#include <type_traits> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include <Kokkos_Core.hpp> |
| 17 | + |
| 18 | +namespace ddc::detail { |
| 19 | + |
| 20 | +enum class NpyByteOrder : char { little_endian = '<', big_endian = '>', not_applicable = '|' }; |
| 21 | + |
| 22 | +NpyByteOrder get_byte_order(std::size_t itemsize) noexcept; |
| 23 | + |
| 24 | +enum class NpyKind : char { |
| 25 | + boolean = 'b', |
| 26 | + signed_int = 'i', |
| 27 | + unsigned_int = 'u', |
| 28 | + floating_point = 'f', |
| 29 | + complex = 'c', |
| 30 | + other = 'V', |
| 31 | +}; |
| 32 | + |
| 33 | +struct NpyDtype |
| 34 | +{ |
| 35 | + NpyByteOrder byte_order; |
| 36 | + NpyKind kind; |
| 37 | + std::size_t itemsize; // in bytes |
| 38 | + |
| 39 | + std::string str() const; |
| 40 | +}; |
| 41 | + |
| 42 | +template <typename T> |
| 43 | +NpyDtype convert_to_npy_dtype() |
| 44 | +{ |
| 45 | + std::size_t const itemsize = sizeof(T); |
| 46 | + NpyByteOrder const byte_order = get_byte_order(itemsize); |
| 47 | + NpyKind kind; |
| 48 | + |
| 49 | + if constexpr (std::is_same_v<T, bool>) { |
| 50 | + // ── Single-byte / untyped ───────────────────────────────────────── |
| 51 | + kind = NpyKind::boolean; |
| 52 | + } else if constexpr (std::is_same_v<T, std::byte>) { |
| 53 | + // std::byte → raw byte buffer, no arithmetic meaning |
| 54 | + kind = NpyKind::other; |
| 55 | + } else if constexpr (std::is_same_v<T, char>) { |
| 56 | + // char is a distinct type; its signedness is implementation-defined |
| 57 | + kind = std::is_signed_v<char> ? NpyKind::signed_int : NpyKind::unsigned_int; |
| 58 | + } else if constexpr ( |
| 59 | + std::is_same_v<T, std::complex<float>> || std::is_same_v<T, std::complex<double>>) { |
| 60 | + // ── Complex ─────────────────────────────────────────────────────── |
| 61 | + // NumPy 'c' dtype stores interleaved real+imag, same layout as std::complex |
| 62 | + kind = NpyKind::complex; |
| 63 | + } else if constexpr (std::is_floating_point_v<T>) { |
| 64 | + // ── Floating-point ──────────────────────────────────────────────── |
| 65 | + static_assert( |
| 66 | + !std::is_same_v<T, long double>, |
| 67 | + "long double is platform-specific (80/96/128-bit); cast to double first."); |
| 68 | + kind = NpyKind::floating_point; |
| 69 | + } else if constexpr (std::is_signed_v<T>) { |
| 70 | + // ── Integers ────────────────────────────────────────────────────── |
| 71 | + kind = NpyKind::signed_int; |
| 72 | + } else if constexpr (std::is_unsigned_v<T>) { |
| 73 | + kind = NpyKind::unsigned_int; |
| 74 | + } else { |
| 75 | + static_assert(sizeof(T) == 0, "Unsupported type for NpyDtype::of<T>()"); |
| 76 | + } |
| 77 | + |
| 78 | + return {.byte_order = byte_order, .kind = kind, .itemsize = itemsize}; |
| 79 | +} |
| 80 | + |
| 81 | +struct NpyArrayView |
| 82 | +{ |
| 83 | + void const* data; |
| 84 | + NpyDtype dtype; |
| 85 | + std::vector<std::size_t> shape; |
| 86 | + bool fortran_order; |
| 87 | +}; |
| 88 | + |
| 89 | +void save_npy(std::ostream& os, NpyArrayView const& view); |
| 90 | + |
| 91 | +void save_npy(std::filesystem::path const& filename, NpyArrayView const& view); |
| 92 | + |
| 93 | +} // namespace ddc::detail |
| 94 | + |
| 95 | +namespace ddc::experimental { |
| 96 | + |
| 97 | +template <typename T, typename Extents, typename Layout, typename Accessor> |
| 98 | +void save_npy(std::ostream& os, Kokkos::mdspan<T, Extents, Layout, Accessor> const& mds) |
| 99 | +{ |
| 100 | + static_assert( |
| 101 | + std::is_same_v<Layout, Kokkos::layout_left> |
| 102 | + || std::is_same_v<Layout, Kokkos::layout_right>, |
| 103 | + "save_npy: only contiguous layouts supported."); |
| 104 | + static_assert( |
| 105 | + std::is_same_v<Accessor, Kokkos::default_accessor<T>> |
| 106 | + || std::is_same_v<Accessor, Kokkos::default_accessor<T const>>, |
| 107 | + "save_npy: non-host-accessible accessor. Use create_mirror_view + deep_copy first."); |
| 108 | + |
| 109 | + std::vector<std::size_t> shape(Extents::rank()); |
| 110 | + for (std::size_t i = 0; i < Extents::rank(); ++i) { |
| 111 | + shape[i] = mds.extent(i); |
| 112 | + } |
| 113 | + |
| 114 | + ddc::detail::save_npy( |
| 115 | + os, |
| 116 | + ddc::detail::NpyArrayView { |
| 117 | + .data = mds.data_handle(), |
| 118 | + .dtype = ddc::detail::convert_to_npy_dtype<std::remove_const_t<T>>(), |
| 119 | + .shape = std::move(shape), |
| 120 | + .fortran_order = std::is_same_v<Layout, Kokkos::layout_left>, |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +template <typename T, typename Extents, typename Layout, typename Accessor> |
| 125 | +void save_npy( |
| 126 | + std::filesystem::path const& filename, |
| 127 | + Kokkos::mdspan<T, Extents, Layout, Accessor> const& mds) |
| 128 | +{ |
| 129 | + static_assert( |
| 130 | + std::is_same_v<Layout, Kokkos::layout_left> |
| 131 | + || std::is_same_v<Layout, Kokkos::layout_right>, |
| 132 | + "save_npy: only contiguous layouts supported."); |
| 133 | + static_assert( |
| 134 | + std::is_same_v<Accessor, Kokkos::default_accessor<T>> |
| 135 | + || std::is_same_v<Accessor, Kokkos::default_accessor<T const>>, |
| 136 | + "save_npy: non-host-accessible accessor. Use create_mirror_view + deep_copy first."); |
| 137 | + |
| 138 | + std::vector<std::size_t> shape(Extents::rank()); |
| 139 | + if constexpr (Extents::rank() > 0) { |
| 140 | + for (std::size_t i = 0; i < Extents::rank(); ++i) { |
| 141 | + shape[i] = mds.extent(i); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + ddc::detail::save_npy( |
| 146 | + filename, |
| 147 | + ddc::detail::NpyArrayView { |
| 148 | + .data = mds.data_handle(), |
| 149 | + .dtype = ddc::detail::convert_to_npy_dtype<std::remove_const_t<T>>(), |
| 150 | + .shape = std::move(shape), |
| 151 | + .fortran_order = std::is_same_v<Layout, Kokkos::layout_left>, |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace ddc::experimental |
0 commit comments