Skip to content

archivesdj/Amgcl.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Amgcl.Net

Amgcl Wrapper for .NET

AMGCL is a header-only C++ library for solving large sparse linear systems with algebraic multigrid (AMG) method. It is super fast and beautiful algorithm. I want to use it in my .NET code. So I made this wapper code using net6.0 as base .NET runtime framework.

Quick start

C# Code Examples

This test code use AmgclSharp library that is wrapper of lib\amgcl.h.

List<int> ptr = new();
List<int> col = new();
List<double> val = new();
List<double> rhs = new();

int n = sample_problem(64, val, col, ptr, rhs);

Amg amg = new();

amg.ParamsCreate();

amg.ParamsSetInt("precond.coarse_enough", 1000);
amg.ParamsSetString("precond.coarsening.type", "smoothed_aggregation");
amg.ParamsSetFloat("precond.coarsening.aggr.eps_strong", 1e-3f);
amg.ParamsSetString("precond.relax.type", "spai0");

amg.ParamsSetString("solver.type", "bicgstabl");
amg.ParamsSetInt("solver.L", 1);
amg.ParamsSetInt("solver.maxiter", 100);

amg.SolverCreate(n, ptr.ToArray(), col.ToArray(), val.ToArray());

amg.ParamsDestroy();

double[] x = new double[n];
amg.SolverSolve(rhs.ToArray(), x);

Console.WriteLine($"Iterations: {amg.Iterations}");
Console.WriteLine($"Error: {amg.Residual}");

amg.SolverDestroy();

Original C++ Code

call_lib.cpp of using lib/amgcl.h

std::vector<int>    ptr;
std::vector<int>    col;
std::vector<double> val;
std::vector<double> rhs;

int n = sample_problem(128l, val, col, ptr, rhs);

amgclHandle prm = amgcl_params_create();

amgcl_params_seti(prm, "precond.coarse_enough", 1000);
amgcl_params_sets(prm, "precond.coarsening.type", "smoothed_aggregation");
amgcl_params_setf(prm, "precond.coarsening.aggr.eps_strong", 1e-3f);
amgcl_params_sets(prm, "precond.relax.type", "spai0");

amgcl_params_sets(prm, "solver.type", "bicgstabl");
amgcl_params_seti(prm, "solver.L", 1);
amgcl_params_seti(prm, "solver.maxiter", 100);

amgclHandle solver = amgcl_solver_create(
    n, ptr.data(), col.data(), val.data(), prm
    );

amgcl_params_destroy(prm);

std::vector<double> x(n, 0);
conv_info cnv = amgcl_solver_solve(solver, rhs.data(), x.data());

std::cout << "Iterations: " << cnv.iterations << std::endl
    << "Error:      " << cnv.residual   << std::endl;

amgcl_solver_destroy(solver);