Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F91624377
cg_main.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Nov 12, 20:40
Size
1 KB
Mime Type
text/x-c
Expires
Thu, Nov 14, 20:40 (2 d)
Engine
blob
Format
Raw Data
Handle
22295319
Attached To
R12662 PHPC-graded_hw_1
cg_main.cc
View Options
#include "cg.hh"
#include <chrono>
#include <iostream>
using clk = std::chrono::high_resolution_clock;
using second = std::chrono::duration<double>;
using time_point = std::chrono::time_point<clk>;
/*
Implementation of a simple CG solver using matrix in the mtx format (Matrix
market) Any matrix in that format can be used to test the code
*/
int main(int argc, char ** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [martix-market-filename]"
<< std::endl;
return 1;
}
CGSolver solver;
solver.read_matrix(argv[1]);
int n = solver.n();
int m = solver.m();
double h = 1. / n;
solver.init_source_term(h);
std::vector<double> x_d(n);
std::fill(x_d.begin(), x_d.end(), 0.);
std::cout << "Call CG dense on matrix size " << m << " x " << n << ")"
<< std::endl;
auto t1 = clk::now();
solver.solve(x_d);
second elapsed = clk::now() - t1;
std::cout << "Time for CG (dense solver) = " << elapsed.count() << " [s]\n";
CGSolverSparse sparse_solver;
sparse_solver.read_matrix(argv[1]);
sparse_solver.init_source_term(h);
std::vector<double> x_s(n);
std::fill(x_s.begin(), x_s.end(), 0.);
std::cout << "Call CG sparse on matrix size " << m << " x " << n << ")"
<< std::endl;
t1 = clk::now();
sparse_solver.solve(x_s);
elapsed = clk::now() - t1;
std::cout << "Time for CG (sparse solver) = " << elapsed.count() << " [s]\n";
return 0;
}
Event Timeline
Log In to Comment