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

Add interface to linear solvers #30

Closed
ktbolt opened this issue Apr 4, 2023 · 30 comments
Closed

Add interface to linear solvers #30

ktbolt opened this issue Apr 4, 2023 · 30 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@ktbolt
Copy link
Collaborator

ktbolt commented Apr 4, 2023

svFSIplus currently supports an interface to the Trilinos linear solver. The interface for matrix assembly looks like this

#ifdef WITH_TRILINOS
    if (eq.assmTLS) {
      trilinos_doassem_(const_cast<int&>(eNoN), const_cast<int*>(ptr.data()), lK.data(), lR.data());
    } else {
#endif
      lhsa_ns::do_assem(com_mod, eNoN, ptr, lK, lR);
#ifdef WITH_TRILINOS
    }

#endif

and is included in the assembly code for all equations (e.g., construct_fluid() for the fluid equation), about 20 files.

A better design would be to replace all of these #ifdef blocks with a call to a linear solver interface, for example

linear_solver-> assemble_equations(com_mod, eNoN, ptr, lK, lR);

where linear_solver is an object created for a specific linear solver (e.g., Trilinos, FSILS, PETSc).

@ktbolt ktbolt added the enhancement New feature or request label Apr 4, 2023
@ktbolt ktbolt self-assigned this Apr 4, 2023
@mrp089
Copy link
Member

mrp089 commented Aug 18, 2023

@yuecheng-yu and I want to implement the PETSc interface that @CZHU20 developed for svFSI in svFSIplus because we need a direct linear solver.

@ktbolt, do you already have anything that we should build on top?
@CZHU20, can we use your C code in svFSIplus?

@CZHU20
Copy link
Member

CZHU20 commented Aug 19, 2023 via email

@ktbolt
Copy link
Collaborator Author

ktbolt commented Aug 21, 2023

@mrp089 I have not spent any time thinking about this.

But off the top of my head what we need is an interface to linear solvers implemented something like this

  • abstract LinearSolver base class
  • derived classes for FSILS, Trilinos and PETSc
  • allocate a ComMod object for the appropriate linear solver set in the solver XML file
  • call this object when assembling matrices, no #ifdefs and all that

We will first need to understand what data structures are needed to interface to Trilinos and PETSc, then prototype the interface.

This was referenced Nov 15, 2023
@ktbolt
Copy link
Collaborator Author

ktbolt commented Nov 15, 2023

@mrp089 @yuecheng-yu I've seen no discussions about the design of the interface.

Possible designs, implementation strategies, prototypes, etc. should be discussed before any code is implemented.

@mrp089
Copy link
Member

mrp089 commented Nov 15, 2023

As stated above, #143 is a literal translation of the PETSc interface we have for svFSI- (like the rest of svFSI+). Once you implement the linear_solver interface you describe here, I'm happy to migrate PETSc. In the meantime, we (and others) would like to use PETSc for its wealth of linear solvers.

@ktbolt, please feel free to participate in our discussions in the lab at any time.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Nov 15, 2023

@yuecheng-yu I would like to implement the interface I outlined above. I can meet with you at any time if you would like to discuss possible designs and implementation.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Dec 8, 2023

@mrp089 I don't think anything more is going to be done with this so go ahead and do a PR for the PETSc interface, good to have this in when we release.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Dec 16, 2023

No activity here so closing.

@ktbolt ktbolt closed this as completed Dec 16, 2023
@mrp089 mrp089 added this to the G&R milestone Jan 31, 2024
@ktbolt ktbolt reopened this Feb 1, 2024
@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 1, 2024

@mrp089 I will create a linear solver interface to the svFSILS, Trilinos and PETSc linear solvers.

Shall I use your petsc_interface_30 branch for PETSc?

@mrp089
Copy link
Member

mrp089 commented Feb 1, 2024

Awesome!! Yes, that's still the most up-to-date version. There is also an example in there that uses a direct solver.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 3, 2024

I have built PETSc libs from source on a Mac.

After figuring out how to enable building with PETSc and some problems with setting PETSC_DIR I was able to compile petsc_interface_30.

I was then able to run tests/cases/fsi/pipe_3d, results seemed ok.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 3, 2024

Looking at the petsc_interface_30 I see that petsc_linear_solver.c is essentially the code written for the Fortran svFSI solver and does just a linear solve, no assembly interface like Trilinos.

My plan is to

  1. learn how to use PETSc
  2. implement an interface for svFSILS, Trilinos and PETSc
  3. rewrite the PETSc interface code in petsc_linear_solver.c
  4. add the new interface to the svFSIplus code removing all of the #ifdefs and such
  5. Add XML commands to select a solver and read solver configuration files
  6. add CMake commands to select building with PETSc (like Trilinos)

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 6, 2024

I've incorporated the PETSc interface into a branch of the latest svFSIplus code, compiled and tested, works.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 8, 2024

I've created a LinearAlgebra base class that provides an interface to numerical linear algebra packages, just PETSc for now though.

The PetscLinearAlgebra class derived fromLinearAlgebra provides an interface to PETSc, contains a private PetscImpl class hiding all of the PETSc data structures and functions.

A PETSc interface is created like so

auto linear_algebra = LinearAlgebraFactory::create_interface(LinearAlgebraType::petsc);
linear_algebra->initialize(simulation->com_mod);
.
.
.
linear_algebra->solve(com_mod, eq, incL, res);

All of the PETSc-dependent code has been moved to petsc_impl.h,cpp which is then conditionally included into PetscLinearAlgebra. I also removed class plsType from ComMod.h.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 8, 2024

The PETSc interface petsc_linear_solver.c is a C code interface to Fortran: declared extern functions with underscores, passed scalars as pointers, etc., no attempt was made to make it a C++ code integrated with svFSIplus (e.g. use enum classes defined in consts.h).

I've cleaned up all of this so now the code looks like it is actually part of svFSIplus.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 9, 2024

I've added fsils and trilinos classes derived from LinearAlgebra to provide interfaces to FSILS and Trilinos linear algebra packages, tested both on fsi/pipe_3d and results look good.

All Trilinos solve code has been moved into trilinos_impl.h,cpp, removed tslType from ComMod.

I have not yet implemented using Trilinos for assembly and preconditioning, can be used with FSILS solve it seems, need to understand how these guys work together.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 15, 2024

I needed to reorganized the code a bit to get things to work in parallel, fsils works and trilinos works, still some things to do for petsc.

You can now replace

    // Assembly
#ifdef WITH_TRILINOS
    if (eq.assmTLS) {
      if (cPhys == Equation_ustruct) {
        throw std::runtime_error("[construct_fsi] Cannot assemble USTRUCT using Trilinos");
      }
      trilinos_doassem_(const_cast<int&>(eNoN), ptr.data(), lK.data(), lR.data());
    } else {
#endif
      if (cPhys == Equation_ustruct) {
        throw std::runtime_error("[construct_fsi] USTRUCT_DOASSEM not implemented");
      } else {
        lhsa_ns::do_assem(com_mod, eNoN, ptr, lK, lR);
      }
#ifdef WITH_TRILINOS
    }
#endif

with

    // Assembly
    eq.linear_algebra->assemble(com_mod, eNoN, ptr, lK, lR);

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 21, 2024

It just hit me that the petsc_interface_30 implementation hard-codes using PETSc for all equations. However, Trilinos or FSILS can be specified for each equation.

It would be good to have PETSc work the same way.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 23, 2024

I've modified the PETSc interface to support using PETSc on a per equation basis.

I've also added error checking for all of the restrictions for preconditioning and assembly.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 23, 2024

@MatteoSalvador @mrp089 I am going to remove support for the Preconditioner parameter under the LS section, for example

   <LS type="CG" >
      <Preconditioner> FSILS </Preconditioner>
      <Tolerance> 1e-4 </Tolerance>
   </LS>

will no longer be valid, a Linear_algebra section is required to set the Preconditioner

     <Linear_algebra type="fsils" >
        <Preconditioner> fsils </Preconditioner>
     </Linear_algebra>

I would also like to make the Linear_algebra section required, currently if it is not there then the linear algebra by default is fsils.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Feb 28, 2024

I've added error checking and changed the LinearAlgebra classes a bit to reproduce what all the ifdefs were doing.

I will now replace the remaining #ifdef WITH_TRILINOS statements and test.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Mar 1, 2024

I've replace the remaining #ifdef WITH_TRILINOS statements and have tested on various svFSI-Test problems, seems to work.

And I added a check for the ustruct equation that requires fsils assembly.

I've also added support for a couple of PETSc preconditioners (jacobi and rcs).

@ktbolt
Copy link
Collaborator Author

ktbolt commented Mar 2, 2024

I've made all of the changes I think we need for now, all code for each of the linear algebra interfaces is mostly encapsulated in classes. There is still a bit of assembly code spread around for the ustruct equation but that is confined to a couple of files.

The PreconditionerType and other related data structures (e.g. preconditioner_name_to_type) defined in consts.h is not very pleasing, would like to have separate enums for each linear algebra interface but I will leave that for another day.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Mar 2, 2024

@MatteoSalvador @mrp089 I was thinking to update all of the svFSIplus/tests/cases files within my current branch so they will work with the code changes.

Shall I do that or should I create another Issue and branch for the file changes?

@MatteoSalvador
Copy link
Collaborator

Thanks @ktbolt! I think you can update the tests within the same PR/issue, as this is strongly connected to the linear algebra modifications

@mrp089
Copy link
Member

mrp089 commented Mar 4, 2024

@ktbolt, yes, please update the tests so they work with your code changes (otherwise we can't merge it).

How will we test the PETSc and Trilinos interfaces on GitHub runners? I see two options:

  1. Build PETSc / Trilinos on each test machine (doable with our current setup, but pipelines will run for hours)
  2. Create our own runners from a Docker image with pre-installed PETSc / Trilinos (quicker, but a lot to set up)

@ktbolt
Copy link
Collaborator Author

ktbolt commented Mar 5, 2024

@mrp089

  1. Why would the pipelines run for hours?

  2. Building Docker containers would be a good idea. We could then use those containers on an HPC cluster.

We do need to monitor memory and execute time, make sure those don't change significantly with code modifications.

@mrp089
Copy link
Member

mrp089 commented Mar 5, 2024

In my experience, building PETSc or Trilinos took a long time. But if you have a minimal configuration that builds quickly everything that we need for svFSI and nothing more, that would be great!

In this case, maybe add the build instructions as separate bash configure scripts that are then called in .github/workflows/test.yml. Then people can execute the same PETSc/Trilinos build instructions independently of the GitHub runners.

GitHub's limit is six hours. We can set our own time limit with timeout-minutes.

@ktbolt
Copy link
Collaborator Author

ktbolt commented Mar 5, 2024

I didn't think PETSc took so long to build but maybe (probably) I was not building it in all of its glory.

I built Trilinos according to the svFSI instructions. The primary problem I had there was getting mumps installed.

dcodoni added a commit to dcodoni/svMultiPhysics that referenced this issue Jun 13, 2024
…Vascular#30)

- created dockerfiles to build Docker containers with pre-compiled Trilinos and PETSc libraries in Ubuntu-20.04 and Ubuntu-22.04 (addresses issue SimVascular#211)
- added test cases for fluid and fsi problems to test Trilinos and PETSc linear solvers and preconditioners
- updated the workflow file test_ubuntu.yml to include PETSc and Trilinos tests for continuous integration (CI)
dcodoni added a commit to dcodoni/svMultiPhysics that referenced this issue Jun 13, 2024
…imVascular#211)

- Trilinos and PETSc interfaces added by @ktbolt
- created dockerfiles to build Docker containers with pre-compiled Trilinos and PETSc libraries in Ubuntu-20.04 and Ubuntu-22.04
- added test cases for fluid and fsi problems to test Trilinos and PETSc linear solvers and preconditioners
- updated the workflow file test_ubuntu.yml to include PETSc and Trilinos tests for continuous integration (CI)
MatteoSalvador pushed a commit that referenced this issue Jun 13, 2024
- Trilinos and PETSc interfaces added by @ktbolt
- created dockerfiles to build Docker containers with pre-compiled Trilinos and PETSc libraries in Ubuntu-20.04 and Ubuntu-22.04
- added test cases for fluid and fsi problems to test Trilinos and PETSc linear solvers and preconditioners
- updated the workflow file test_ubuntu.yml to include PETSc and Trilinos tests for continuous integration (CI)
@ktbolt
Copy link
Collaborator Author

ktbolt commented Aug 27, 2024

Merged into main.

@ktbolt ktbolt closed this as completed Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants