|
| 1 | +// Copyright (c) Sleipnir contributors |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include <bit> |
| 10 | +#include <fstream> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | + |
| 14 | +#include <Eigen/SparseCore> |
| 15 | + |
| 16 | +namespace slp { |
| 17 | + |
| 18 | +/// Writes the sparsity pattern of a sparse matrix to a file. |
| 19 | +/// |
| 20 | +/// Each file represents the sparsity pattern of one matrix over time. <a |
| 21 | +/// href="https://github.com/SleipnirGroup/Sleipnir/blob/main/tools/spy.py">spy.py</a> |
| 22 | +/// can display it as an animation. |
| 23 | +/// |
| 24 | +/// The file starts with the following header: |
| 25 | +/// <ol> |
| 26 | +/// <li>Plot title (length as a little-endian int32, then characters)</li> |
| 27 | +/// <li>Row label (length as a little-endian int32, then characters)</li> |
| 28 | +/// <li>Column label (length as a little-endian int32, then characters)</li> |
| 29 | +/// </ol> |
| 30 | +/// |
| 31 | +/// Then, each sparsity pattern starts with: |
| 32 | +/// <ol> |
| 33 | +/// <li>Number of coordinates as a little-endian int32</li> |
| 34 | +/// </ol> |
| 35 | +/// |
| 36 | +/// followed by that many coordinates in the following format: |
| 37 | +/// <ol> |
| 38 | +/// <li>Row index as a little-endian int32</li> |
| 39 | +/// <li>Column index as a little-endian int32</li> |
| 40 | +/// <li>Sign as a character ('+' for positive, '-' for negative, or '0' for |
| 41 | +/// zero)</li> |
| 42 | +/// </ol> |
| 43 | +/// |
| 44 | +/// @tparam Scalar Scalar type. |
| 45 | +template <typename Scalar> |
| 46 | +class Spy { |
| 47 | + public: |
| 48 | + /// Constructs a Spy instance. |
| 49 | + /// |
| 50 | + /// @param filename The filename. |
| 51 | + /// @param title Plot title. |
| 52 | + /// @param row_label Row label. |
| 53 | + /// @param col_label Column label. |
| 54 | + /// @param rows The sparse matrix's number of rows. |
| 55 | + /// @param cols The sparse matrix's number of columns. |
| 56 | + Spy(std::string_view filename, std::string_view title, |
| 57 | + std::string_view row_label, std::string_view col_label, int rows, |
| 58 | + int cols) |
| 59 | + : m_file{std::string{filename}, std::ios::binary} { |
| 60 | + // Write title |
| 61 | + write32le(title.size()); |
| 62 | + m_file.write(title.data(), title.size()); |
| 63 | + |
| 64 | + // Write row label |
| 65 | + write32le(row_label.size()); |
| 66 | + m_file.write(row_label.data(), row_label.size()); |
| 67 | + |
| 68 | + // Write column label |
| 69 | + write32le(col_label.size()); |
| 70 | + m_file.write(col_label.data(), col_label.size()); |
| 71 | + |
| 72 | + // Write row and column counts |
| 73 | + write32le(rows); |
| 74 | + write32le(cols); |
| 75 | + } |
| 76 | + |
| 77 | + /// Adds a matrix to the file. |
| 78 | + /// |
| 79 | + /// @param mat The matrix. |
| 80 | + void add(const Eigen::SparseMatrix<Scalar>& mat) { |
| 81 | + // Write number of coordinates |
| 82 | + write32le(mat.nonZeros()); |
| 83 | + |
| 84 | + // Write coordinates |
| 85 | + for (int k = 0; k < mat.outerSize(); ++k) { |
| 86 | + for (typename Eigen::SparseMatrix<Scalar>::InnerIterator it{mat, k}; it; |
| 87 | + ++it) { |
| 88 | + write32le(it.row()); |
| 89 | + write32le(it.col()); |
| 90 | + if (it.value() > Scalar(0)) { |
| 91 | + m_file << '+'; |
| 92 | + } else if (it.value() < Scalar(0)) { |
| 93 | + m_file << '-'; |
| 94 | + } else { |
| 95 | + m_file << '0'; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private: |
| 102 | + std::ofstream m_file; |
| 103 | + |
| 104 | + /// Writes a 32-bit signed integer to the file as little-endian. |
| 105 | + /// |
| 106 | + /// @param num A 32-bit signed integer. |
| 107 | + void write32le(int32_t num) { |
| 108 | + if constexpr (std::endian::native != std::endian::little) { |
| 109 | + // Manual byteswap for 32-bit integer |
| 110 | + num = ((num & 0xFF000000) >> 24) | |
| 111 | + ((num & 0x00FF0000) >> 8) | |
| 112 | + ((num & 0x0000FF00) << 8) | |
| 113 | + ((num & 0x000000FF) << 24); |
| 114 | + } |
| 115 | + m_file.write(reinterpret_cast<char*>(&num), sizeof(num)); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace slp |
| 120 | + |
| 121 | +#endif |
0 commit comments