Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solver doesn't provide way to retrieve iteration counts #366

Open
ScottMacLachlan opened this issue Dec 12, 2022 · 8 comments
Open

Solver doesn't provide way to retrieve iteration counts #366

ScottMacLachlan opened this issue Dec 12, 2022 · 8 comments
Assignees

Comments

@ScottMacLachlan
Copy link
Contributor

MultilevelSolver computes an iteration count while it iterates, but that count is not stored after the solve, for a later query. It would be nice if this had a simple routine to store and access iteration counts.

@lukeolson
Copy link
Member

This could be embedded in info (which is fairly thin right now, and modeled after the solvers in scipy.sparse).

There are two other options. The first is to pass res=[] to residuals=res and count the length. The other is with a callback:

def mysolver(A, b):
    its = 0
    def cb(x):
        nonlocal its
        its += 1
    res = []
    ml = pyamg.smoothed_aggregation_solver(A)
    x, info = ml.solve(b, callback=cb, return_info=True)
    # x, info = sp.sparse.linalg.cg(A, b, callback=cb)
    return x, info, its

A = pyamg.gallery.poisson((100, 100), format='csr')
b = np.random.rand(A.shape[0])
x, info, its = mysolver(A, b)

That said, I like the idea of more complete info to avoid a callback (complex) or a residual list (costly).

@lukeolson lukeolson self-assigned this Dec 12, 2022
@ScottMacLachlan
Copy link
Contributor Author

Yeah - the callback is the clear workaround, but having either a more complete info, or stashing the iteration count in self so that it can be retrieved after the solve would be preferable... Thanks!

@bensworth
Copy link
Collaborator

Ya I agree this is worth adding; also adding average and final convegence factor for most recent solve would be nice. @lukeolson thoughts? I have the same little code block I copy to all my scripts to get this info

@lukeolson
Copy link
Member

An easy thing to do is to change the definition of info. (but this would be inconsistent with scipy and others)

Would we want additional info or just iterations?

@bensworth
Copy link
Collaborator

I vote at least include information on convergence, trivial cost and more useful in my mind than any downsides of being inconsistent with scipy. We could also just include the operator and cycle complexity in info too, so you get a pretty complete picture of your solver in the info struct

@jbschroder
Copy link
Member

jbschroder commented Dec 13, 2022 via email

@bensworth
Copy link
Collaborator

@jbschroder what do you mean by multiple definitions? There's only one way to define the convergence factor over some number of iterations. The only question is which residual you use and which iterations you use I think, but which residual should be a function of your outer solver (GMRES, FP, etc.) anyways?

I always have a little code that prints something like this at the end of my scripts (the alignment is right in terminal):

Ruge Stuben
	Operator complexity         =  1.17
	Cycle complexity            =  2.33
	Most recent iteration count =  8
	Average convergence factor  =  0.06
	Final convergence factor    =  0.05

I think it would be nice to automate this with a function, maybe save these numbers in .info and have some form of print function to print like this. Thoughts?

@bensworth
Copy link
Collaborator

The CC could just be included in the current output w/ operator and grid complexity. But we could have a different output that tells about the solve, even as default at the end of the solve function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants