Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <omp.h>
- #include <Eigen/Core>
- #include <Eigen/Sparse>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <chrono>
- #include <numeric>
- #include <filesystem>
- #include <sys/resource.h>
- #include <cstdint>
- #include "util.hpp"
- #include "util_perf.hpp"
- void spmv_csr(int rows, const int *row_ptr, const int *col_idx, const Scalar *values, const CustomVector &x, CustomVector &y)
- {
- // Parallelize over rows
- #pragma omp parallel for schedule(static)
- for (int i = 0; i < rows; i++)
- {
- Scalar sum = 0;
- for (int j = row_ptr[i]; j < row_ptr[i + 1]; j++)
- sum += values[j] * x[col_idx[j]];
- y[i] = sum;
- }
- }
- int main(int argc, char **argv)
- {
- if (argc < 4)
- {
- std::cerr << "Usage: " << argv[0] << " <matrix.bin> <iterations> <cores> \n";
- return 1;
- }
- std::string matrix_full_path = argv[1];
- int max_iterations = std::stoi(argv[2]);
- int num_cores = std::stoi(argv[3]);
- std::string matrix_basename = std::filesystem::path(matrix_full_path).filename().string();
- srand(42);
- omp_set_num_threads(num_cores);
- auto io_start = std::chrono::high_resolution_clock::now();
- CustomSparseMatrix A = load_binary_matrix(matrix_full_path, 0);
- auto io_end = std::chrono::high_resolution_clock::now();
- double io_elapsed = std::chrono::duration<double>(io_end - io_start).count();
- CustomVector x(A.cols());
- CustomVector y(A.rows());
- y.setZero();
- x.setRandom();
- auto *row_ptr = A.outerIndexPtr();
- auto *col_idx = A.innerIndexPtr();
- auto *values = A.valuePtr();
- struct IterData
- {
- double elapsed;
- double gflops;
- };
- std::vector<IterData> iter_results;
- iter_results.reserve(max_iterations);
- for (int iter = 0; iter < max_iterations; ++iter)
- {
- double t_start = omp_get_wtime();
- spmv_csr(A.rows(), row_ptr, col_idx, values, x, y);
- double t_end = omp_get_wtime();
- double elapsed = t_end - t_start;
- iter_results.push_back({elapsed, (2.0 * A.nonZeros()) / (elapsed * 1e9)});
- }
- double total_spmv_time = 0;
- for (const auto &res : iter_results)
- total_spmv_time += res.elapsed;
- double avg_gflops = (2.0 * A.nonZeros() * max_iterations) / (total_spmv_time * 1e9);
- std::cout << matrix_basename << ", cores: "
- << num_cores << ", iter: "
- << max_iterations << ", io: "
- << io_elapsed << ", spmv runtime: "
- << total_spmv_time << ", avg gflops: "
- << avg_gflops << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment