Guest User

util.cpp

a guest
Mar 24th, 2026
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #pragma once
  2. #include <Eigen/Sparse>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <fast_matrix_market/app/Eigen.hpp>
  7. #include <numeric>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <filesystem>
  11.  
  12. /**
  13.  * CUSTOM BINARY CSR FORMAT
  14.  *
  15.  * TODO : explain the "why" behind this format (performance, parallelization, etc.)
  16.  *
  17.  * LAYOUT IN MEMORY
  18.  * (With 4 Byte StorageIndex=int and 8 Byte Scalar=double)
  19.  * Data is stored in three consecutive sections without any padding:
  20.  * * [1] HEADER
  21.  * - Rows        (4 Bytes)
  22.  * - Cols        (4 Bytes)
  23.  * - NNZ         (4 Bytes)
  24.  * * [2] STRUCTURE
  25.  * - Row-Starts (4 Bytes * [Rows + 1])
  26.  * - Col-ID     (4 Bytes * NNZ)
  27.  * * [3] VALUES
  28.  * - Numbers    (8 Bytes * NNZ)
  29.  */
  30.  
  31. // Data type for CustomSparseMatrix entries.
  32. using Scalar = double;
  33. // Data type for CustomSparseMatrix indexing. This type needs to be large enough to hold the total number of NNZ.
  34. using StorageIndex = int;
  35. using CustomSparseMatrix = Eigen::SparseMatrix<Scalar, Eigen::RowMajor, StorageIndex>;
  36. using CustomVector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
  37.  
  38. // Write a .mtx matrix file from mtx_path to a custom binary CSR format file at bin_path.
  39. // Returns 0 on success, 1 on failure.
  40. inline int write_binary_matrix(const std::string &mtx_path, const std::string &bin_path, CustomSparseMatrix &out_mat)
  41. {
  42.     std::ifstream f(mtx_path);
  43.     if (!f)
  44.     {
  45.         std::cerr << "Cannot open MTX file: " << mtx_path << "\n";
  46.         return 1;
  47.     }
  48.     fast_matrix_market::read_matrix_market_eigen(f, out_mat);
  49.  
  50.     out_mat.makeCompressed();
  51.  
  52.     std::ofstream out(bin_path, std::ios::binary);
  53.     if (!out)
  54.     {
  55.         std::cerr << "Cannot create binary file: " << bin_path << "\n";
  56.         return 1;
  57.     }
  58.  
  59.     // Get matrix metadata
  60.     StorageIndex rows = static_cast<StorageIndex>(out_mat.rows());
  61.     StorageIndex cols = static_cast<StorageIndex>(out_mat.cols());
  62.     StorageIndex nnz = static_cast<StorageIndex>(out_mat.nonZeros());
  63.  
  64.     // Write matrix metadata
  65.     out.write(reinterpret_cast<const char *>(&rows), sizeof(StorageIndex));
  66.     out.write(reinterpret_cast<const char *>(&cols), sizeof(StorageIndex));
  67.     out.write(reinterpret_cast<const char *>(&nnz), sizeof(StorageIndex));
  68.  
  69.     // Write CSR data
  70.     out.write(reinterpret_cast<const char *>(out_mat.outerIndexPtr()), sizeof(StorageIndex) * (rows + 1));
  71.     out.write(reinterpret_cast<const char *>(out_mat.innerIndexPtr()), sizeof(StorageIndex) * nnz);
  72.     out.write(reinterpret_cast<const char *>(out_mat.valuePtr()), sizeof(Scalar) * nnz);
  73.  
  74.     std::cout << "Matrix successfully converted: " << rows << "x" << cols
  75.               << " with " << nnz << " non-zero entries." << std::endl;
  76.  
  77.     return 0;
  78. }
  79.  
  80. // Load a custom binary CSR format matrix from bin_path and return it as CustomSparseMatrix. Optimize Linux first-touch NUMA policy if NUMA_optimize is true.
  81. // NUMA_optimize=true only works as intended if the process calling this function has a NUMA policy that allocates memory locally (e.g., not interleaved,etc.)
  82. inline CustomSparseMatrix load_binary_matrix(const std::string &bin_path, bool NUMA_optimize)
  83. {
  84.     std::ifstream in(bin_path, std::ios::binary);
  85.     if (!in)
  86.     {
  87.         std::cerr << "Cannot open file: " << bin_path << "\n";
  88.         exit(1);
  89.     }
  90.  
  91.     StorageIndex rows, cols, nnz;
  92.     in.read(reinterpret_cast<char *>(&rows), sizeof(StorageIndex));
  93.     in.read(reinterpret_cast<char *>(&cols), sizeof(StorageIndex));
  94.     in.read(reinterpret_cast<char *>(&nnz), sizeof(StorageIndex));
  95.  
  96.     CustomSparseMatrix A(rows, cols);
  97.     A.resizeNonZeros(nnz);
  98.  
  99.     StorageIndex *outer = A.outerIndexPtr();
  100.     StorageIndex *inner = A.innerIndexPtr();
  101.     Scalar *vals = A.valuePtr();
  102.  
  103.     if (NUMA_optimize)
  104.     {
  105. #pragma omp parallel
  106.         {
  107. #pragma omp for schedule(static)
  108.             for (StorageIndex i = 0; i <= rows; ++i)
  109.             {
  110.                 outer[i] = 0;
  111.             }
  112.  
  113. #pragma omp for schedule(static)
  114.             for (StorageIndex i = 0; i < nnz; ++i)
  115.             {
  116.                 inner[i] = 0;
  117.                 vals[i] = 0.0;
  118.             }
  119.         }
  120.     }
  121.  
  122.     in.read(reinterpret_cast<char *>(outer), sizeof(StorageIndex) * (rows + 1));
  123.     in.read(reinterpret_cast<char *>(inner), sizeof(StorageIndex) * nnz);
  124.     in.read(reinterpret_cast<char *>(vals), sizeof(Scalar) * nnz);
  125.  
  126.     in.close();
  127.     return A;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment