feat(debugUtils): added more sparse matrix debug utilities
This commit is contained in:
@@ -8,6 +8,11 @@
|
||||
#include "mfem.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <tuple>
|
||||
#include <ranges>
|
||||
|
||||
/**
|
||||
* @brief Saves an mfem::SparseMatrix to a custom compact binary file (.csrbin).
|
||||
@@ -29,14 +34,7 @@
|
||||
* - J array (int64_t * NNZ): CSR Column Indices
|
||||
* - Data array (double * NNZ): CSR Non-zero values
|
||||
*/
|
||||
bool saveSparseMatrixBinary(const mfem::SparseMatrix& mat, const std::string& filename) {
|
||||
std::ofstream outfile(filename, std::ios::binary | std::ios::trunc);
|
||||
if (!outfile) {
|
||||
std::cerr << "Error: Cannot open file for writing: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
void write_sparse_matrix(const mfem::SparseMatrix &mat, std::ostream &outfile) {
|
||||
// --- Get Data Pointers and Dimensions from MFEM Matrix ---
|
||||
const int* mfem_I = mat.GetI();
|
||||
const int* mfem_J = mat.GetJ();
|
||||
@@ -86,6 +84,17 @@ bool saveSparseMatrixBinary(const mfem::SparseMatrix& mat, const std::string& fi
|
||||
|
||||
outfile.write(reinterpret_cast<const char*>(mfem_data), data_count * sizeof(double));
|
||||
if (!outfile) throw std::runtime_error("Error writing Data array.");
|
||||
}
|
||||
|
||||
bool saveSparseMatrixBinary(const mfem::SparseMatrix& mat, const std::string& filename) {
|
||||
std::ofstream outfile(filename, std::ios::binary | std::ios::trunc);
|
||||
if (!outfile) {
|
||||
std::cerr << "Error: Cannot open file for writing: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
write_sparse_matrix(mat, outfile);
|
||||
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
@@ -163,4 +172,33 @@ void writeDenseMatrixToCSV(const std::string &filename, int precision, const mfe
|
||||
writeDenseMatrixToCSV(filename, precision, mat);
|
||||
}
|
||||
|
||||
void saveBlockFormToBinary(std::vector<mfem::SparseMatrix *> &block_diags, std::vector<std::array<int, 2>> block, std::string filename) {
|
||||
// First write a magic number and version
|
||||
|
||||
// --- Open the file ---
|
||||
std::ofstream outfile(filename, std::ios::binary | std::ios::trunc);
|
||||
if (!outfile) {
|
||||
std::cerr << "Error: Cannot open file for writing: " << filename << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Write Header ---
|
||||
const char magic[4] = {'B', 'L', 'C', 'K'};
|
||||
const char datastart[9] = {'D', 'A', 'T', 'A', 'S', 'T', 'A', 'R', 'T'};
|
||||
const char dataend[7] = {'D', 'A', 'T', 'A', 'E', 'N', 'D'};
|
||||
const uint8_t size = block_diags.size();
|
||||
|
||||
outfile.write(reinterpret_cast<const char*>(&magic), 4);
|
||||
outfile.write(reinterpret_cast<const char*>(&size), sizeof(size));
|
||||
|
||||
for (const auto&& [block_diag, blockIDs] : std::views::zip(block_diags, block)) {
|
||||
// Write the sparse matrix data
|
||||
outfile.write(reinterpret_cast<const char*>(&datastart), 9);
|
||||
outfile.write(reinterpret_cast<const char*>(&blockIDs), sizeof(blockIDs));
|
||||
write_sparse_matrix(*block_diag, outfile);
|
||||
outfile.write(reinterpret_cast<const char*>(&dataend), 7);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //MFEM_SMOUT_H
|
||||
|
||||
@@ -34,3 +34,6 @@ package-dir = {"" = "src"}
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["src"]
|
||||
|
||||
[project.scripts]
|
||||
smanalyze = "SSEDebug.smRead.cli.interface:inspectSMMat"
|
||||
@@ -0,0 +1,31 @@
|
||||
import argparse
|
||||
|
||||
def inspectSMMat():
|
||||
parser = argparse.ArgumentParser(description="Inspect SM matrix file")
|
||||
parser.add_argument("filename", type=str, help="Path to the SM matrix file")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
with open(args.filename, 'rb') as f:
|
||||
magic = f.read(4)
|
||||
if magic == b'BLCK':
|
||||
print(f"{args.filename} is a valid block form SM matrix file.")
|
||||
from SSEDebug.smRead.smread import loadBlockMatrix as matreader
|
||||
if magic == b"CSRB":
|
||||
print(f"{args.filename} is a valid CSR form SM matrix file.")
|
||||
from SSEDebug.smRead.smread import loadSparseMatrix as matreader
|
||||
else:
|
||||
raise ValueError(f"Unknown file format: {magic}")
|
||||
|
||||
sm = matreader(args.filename)
|
||||
from SSEDebug.smRead import analyze_sparse_matrix
|
||||
analyze_sparse_matrix(sm)
|
||||
|
||||
except ValueError as e:
|
||||
print(f"Invalid file format: {e}")
|
||||
except FileNotFoundError:
|
||||
print(f"File not found: {args.filename}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
finally:
|
||||
print("Finished inspecting the SM matrix file.")
|
||||
@@ -7,7 +7,7 @@ import scipy.sparse.linalg as spla # For matrix norm
|
||||
import time
|
||||
import os
|
||||
|
||||
def loadSparseMatrixBinary(filename):
|
||||
def loadSparseMatrixBinary(f):
|
||||
"""
|
||||
Loads a sparse matrix from the custom binary format (.csrbin).
|
||||
|
||||
@@ -27,7 +27,6 @@ def loadSparseMatrixBinary(filename):
|
||||
EXPECTED_VERSION = 1
|
||||
|
||||
try:
|
||||
with open(filename, 'rb') as f:
|
||||
# --- Read Header ---
|
||||
magic = f.read(4)
|
||||
if magic != EXPECTED_MAGIC:
|
||||
@@ -76,11 +75,6 @@ def loadSparseMatrixBinary(filename):
|
||||
if Data_array.size != data_count:
|
||||
raise ValueError(f"Error reading Data array. Expected {data_count} elements, read {Data_array.size}. File truncated or corrupt?")
|
||||
|
||||
# --- Check for extra data ---
|
||||
extra_data = f.read()
|
||||
if extra_data:
|
||||
print(f"Warning: {len(extra_data)} extra bytes found at the end of the file.")
|
||||
|
||||
|
||||
# --- Construct SciPy CSR Matrix ---
|
||||
sparse_matrix = sp.csr_matrix((Data_array, J_array, I_array), shape=(height, width))
|
||||
@@ -91,10 +85,65 @@ def loadSparseMatrixBinary(filename):
|
||||
|
||||
return sparse_matrix
|
||||
|
||||
except FileNotFoundError:
|
||||
raise IOError(f"Error: File not found at {filename}")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"An error occurred while reading {filename}: {e}")
|
||||
raise RuntimeError(f"An error occurred while reading: {e}")
|
||||
|
||||
def loadSparseMatrix(filename):
|
||||
"""
|
||||
Loads a sparse matrix from the custom binary format (.csrbin).
|
||||
|
||||
Args:
|
||||
filename (str): The path to the .csrbin file.
|
||||
|
||||
Returns:
|
||||
scipy.sparse.csr_matrix: The loaded sparse matrix.
|
||||
|
||||
Raises:
|
||||
ValueError: If the file format is incorrect or sizes don't match.
|
||||
IOError: If the file cannot be read.
|
||||
"""
|
||||
with open(filename, 'rb') as f:
|
||||
# Check magic number
|
||||
magic = f.read(4)
|
||||
if magic != b'CSRB':
|
||||
raise ValueError(f"Invalid magic number. Expected 'CSRB', got {magic}")
|
||||
|
||||
# Read the rest of the file
|
||||
f.seek(0, 0)
|
||||
sm = loadSparseMatrixBinary(f)
|
||||
|
||||
return sm
|
||||
def loadBlockMatrix(filename):
|
||||
smList = list()
|
||||
with open(filename, 'rb') as f:
|
||||
f.seek(0, 2)
|
||||
fileSize = f.tell()
|
||||
f.seek(0, 0)
|
||||
magic = f.read(4)
|
||||
if magic != b'BLCK':
|
||||
raise ValueError(f"Invalid magic number. Expected 'BLCK'. got {magic}")
|
||||
size = struct.unpack('<B', f.read(1))[0]
|
||||
print(f"Size: {size}")
|
||||
while f.tell() < fileSize:
|
||||
dataStartCard = f.read(9)
|
||||
if dataStartCard != b'DATASTART':
|
||||
raise ValueError(f"Invalid data start card. Expected 'DATASTART' Got {dataStartCard}.")
|
||||
blockId = struct.unpack(f'<ii', f.read(8))
|
||||
sm = loadSparseMatrixBinary(f)
|
||||
smList.append((sm, blockId))
|
||||
# unpack 2 ints as the block id
|
||||
dataEndCard = f.read(7)
|
||||
if dataEndCard != b'DATAEND':
|
||||
raise ValueError(f"Invalid data end card. Expected 'DATAEND'. Got {dataEndCard}.")
|
||||
outArray = np.empty(shape=(size, size), dtype=np.object_)
|
||||
|
||||
for sm, blockId in smList:
|
||||
if blockId[0] >= size or blockId[1] >= size:
|
||||
raise ValueError(f"Block ID {blockId} out of range. Size: {size}")
|
||||
outArray[blockId[0], blockId[1]] = sm
|
||||
|
||||
# Check if all blocks are filled
|
||||
return sp.bmat(outArray, format='csr')
|
||||
|
||||
|
||||
def analyze_sparse_matrix(sp_mat):
|
||||
@@ -109,10 +158,6 @@ def analyze_sparse_matrix(sp_mat):
|
||||
print("Sparse Matrix Analysis Report")
|
||||
print("-" * 50)
|
||||
|
||||
if not isinstance(sp_mat, sp.spmatrix):
|
||||
print("Error: Input is not a SciPy sparse matrix.")
|
||||
return
|
||||
|
||||
rows, cols = sp_mat.shape
|
||||
print(f"Size (Shape): {rows} rows x {cols} columns")
|
||||
|
||||
@@ -129,8 +174,8 @@ def analyze_sparse_matrix(sp_mat):
|
||||
else:
|
||||
sparsity = 1.0
|
||||
|
||||
print(f"Non-zero elements (NNZ): {nnz}")
|
||||
print(f"Total elements: {total_elements}")
|
||||
print(f"Non-zero elements (NNZ): {nnz} (~{nnz*8/(1024**2):.2f} MB)")
|
||||
print(f"Total elements: {total_elements} (~{total_elements*8/(1024**3):.2f} GB)")
|
||||
print(f"Sparsity: {sparsity:.6%} (percentage of zeros)")
|
||||
|
||||
if nnz == 0:
|
||||
@@ -225,6 +270,14 @@ def load_and_analyze_sparse_matrix(filename: str):
|
||||
sm = loadSparseMatrixBinary(filename)
|
||||
analyze_sparse_matrix(sm)
|
||||
|
||||
def compute_frobenius_distance(sparseMat):
|
||||
identityMat = sp.eye(sparseMat.shape[0], sparseMat.shape[1], format='csr')
|
||||
diffMat = sparseMat - identityMat
|
||||
normDistance = np.sqrt(diffMat.data.dot(diffMat.data))
|
||||
frobNormIdentity = np.sqrt(identityMat.shape[0])
|
||||
|
||||
return normDistance/frobNormIdentity
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Simple tool to get some statistics about a sparse matrix from mfem")
|
||||
parser.add_argument("path", help="path to the output file", type=str)
|
||||
|
||||
Reference in New Issue
Block a user