# Efficiency of developing PDE solvers using sparse matrices versus loops

I am new to solving PDEs, but have been looking at different implementations of finite difference and finite volume schemes. One thing I have noticed in different implementations is that some implementations use sparse matrices while other implementations--of the same algorithms--use basic loops.

I can understand that for some solvers, using a matrix is necessary, such as implicit solvers that have a linear solve step. Also, some of the confusion may stem from the fact that elliptic pdes are presented first. Since linear elliptic pdes are solved using differentiation matrices, this might explain some the attention on sparse matrices.

I was trying to understand which approach was more efficient--sparse matrices or basic loops? If the goal of practicing writing PDE solvers is to subsequently write codes for real problems, then I would like to learn best practices for writing efficient solvers.

Can anyone tell me which method, sparse matrices or loops, is more efficient method when dealing with larger problems with fine discretizations? Of larger problems we run into issues like breaking up the problem into separate pieces that can be parallelized. We also run into memory issues, etc. I have not seen any of these issues addressed in the simple demos I have found.

I am not sure if the specific language makes a difference, but I generally write in Julia.

• It depends on what solvers you use and the problem size. For multidimensional problems with <250k unknowns, explicitly forming the matrix and using a sparse direct solver is often the most efficient approach. For larger problems of >1M unknowns and especially in 3D, you'll want iterative solvers for which the ability to perform matrix-vector multiplication is more important than having the matrix itself. (Search for "matrix-free".) You need to know about both and you should choose a software framework that makes it easy to switch. May 20, 2022 at 0:54
• @DanielShapero YES, this is very helpful. This is the kind of information I was looking for. You are correct that I will always need to work through a framework, and that the way I write the solver will depend on how the framework handles solvers. But at least I know that when I write practice solvers, I should write a version for <250k and for >1M. This really helps to narrow down how I will write the basic scaffolding code. Thanks again. May 20, 2022 at 1:42