Skip to content

Commit 7e059ff

Browse files
committed
Some updates to the interface
1 parent f5feb6c commit 7e059ff

File tree

10 files changed

+330
-301
lines changed

10 files changed

+330
-301
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Lanczos solver,
22
Computes the matrix-vector product sqrt(M)·v using a recursive algorithm.
3-
For that, it requires a functor in which the () operator takes an output real* array and an input real* (both in device memory if compiled in CUDA mode or host memory otherwise) as:
3+
For that, it requires a functor with a "dot" function that takes an output real* array and an input real* (both in device memory if compiled in CUDA mode or host memory otherwise) as:
44
```c++
5-
inline void operator()(real* in_v, real * out_Mv);
5+
virtual void dot(real* in_v, real * out_Mv) override;
66
```
7+
The functor must inherit ```lanczos::MatrixDot``` (see example.cu).
78
This function must fill "out" with the result of performing the M·v dot product- > out = M·a_v.
89
If M has size NxN and the cost of the dot product is O(M). The total cost of the algorithm is O(m·M). Where m << N.
910
If M·v performs a dense M-V product, the cost of the algorithm would be O(m·N^2).
1011
11-
This is a header-only library, although a shared library can be
12+
This is a header-only library, although a shared library can be compiled instead.
1213
1314
## Usage:
1415
@@ -19,8 +20,8 @@ Let us go through the remaining one, a GPU-only example.
1920
2021
Create the module:
2122
```c++
22-
real tolerance = 1e-6;
23-
lanczos::Solver lanczos(tolerance);
23+
24+
lanczos::Solver lanczos;
2425
```
2526
Write a functor that computes the product between the original matrix and a given vector, "v":
2627
```c++
@@ -30,7 +31,7 @@ struct DiagonalMatrix: public lanczos::MatrixDot{
3031
int size;
3132
DiagonalMatrix(int size): size(size){}
3233

33-
void operator()(real* v, real* Mv){
34+
void dot(real* v, real* Mv) override{
3435
//An example diagonal matrix
3536
for(int i=0; i<size; i++){
3637
Mv[i] = 2*v[i];
@@ -54,10 +55,14 @@ Provide the solver with an instance of the functor and the target vector:
5455
DiagonalMatrix dot(size);
5556
//Call the solver
5657
real* d_result = thrust::raw_pointer_cast(result.data());
57-
real* d_v = thrust::raw_pointer_cast(v.data());
58-
int numberIterations = lanczos.solve(dot, d_result, d_v, size);
58+
real* d_v = thrust::raw_pointer_cast(v.data());
59+
real tolerance = 1e-6;
60+
int numberIterations = lanczos.run(dot, d_result, d_v, tolerance, size);
61+
int iterations = 100;
62+
int residual = lanczos.runIterations(dot, d_result, d_v, iterations, size);
5963
```
60-
The solve function returns the number of iterations that were needed to achieve the requested accuracy.
64+
The run function returns the number of iterations that were needed to achieve the requested accuracy.
65+
The runIterations returns the residual after the requested iterations.
6166

6267
## Other functions:
6368

example.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ struct DiagonalMatrix: public lanczos::MatrixDot{
3636
int main(){
3737
{
3838
//Initialize the solver
39-
real tolerance = 1e-6;
40-
lanczos::Solver lanczos(tolerance);
39+
lanczos::Solver lanczos;
4140
int size = 10;
4241
//A vector filled with 1.
4342
//Lanczos defines this type for convenience. It will be a thrust::device_vector if CUDA_ENABLED is defined and an std::vector otherwise
@@ -48,7 +47,7 @@ int main(){
4847
//A functor that multiplies by a diagonal matrix
4948
DiagonalMatrix dot(size);
5049
//Call the solver
51-
int numberIterations = lanczos.solve(dot, result.data(), v.data(), size);
50+
int numberIterations = lanczos.run(dot, result.data(), v.data(), tolerance, size);
5251
std::cout<<"Solved after "<<numberIterations<< " iterations"<<std::endl;
5352
//Now result is filled with sqrt(M)*v = sqrt(2)*[1,1,1...1]
5453
std::cout<<"Result: ";for(int i = 0; i<10; i++) std::cout<<result[i]<<" "; std::cout<<std::endl;

example.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main(){
3737
{
3838
//Initialize the solver
3939
real tolerance = 1e-6;
40-
lanczos::Solver lanczos(tolerance);
40+
lanczos::Solver lanczos;
4141
int size = 10;
4242
//A vector filled with 1.
4343
//Lanczos defines this type for convenience. It will be a thrust::device_vector if CUDA_ENABLED is defined and an std::vector otherwise
@@ -50,7 +50,7 @@ int main(){
5050
//Call the solver
5151
real* d_result = lanczos::detail::getRawPointer(result);
5252
real* d_v = lanczos::detail::getRawPointer(v);
53-
int numberIterations = lanczos.solve(dot, d_result, d_v, size);
53+
int numberIterations = lanczos.run(dot, d_result, d_v, tolerance, size);
5454
std::cout<<"Solved after "<<numberIterations<< " iterations"<<std::endl;
5555
//Now result is filled with sqrt(M)*v = sqrt(2)*[1,1,1...1]
5656
std::cout<<"Result: ";for(int i = 0; i<10; i++) std::cout<<result[i]<<" "; std::cout<<std::endl;

0 commit comments

Comments
 (0)