Saltar al contenido principal

PETSc Overview

Introduction to PETSc Fortran Framework

What is PETSc?

PETSc, the Portable Extensible Toolkit for Scientific Computing, is a suite of data structures and routines for the scalable parallel solution of scientific applications modeled by partial differential equations (PDEs). It provides efficient and flexible tools for the parallel solution of large-scale linear and nonlinear systems arising from a wide range of application areas.

In this tutorial, we will focus on the Fortran interface of PETSc and explore its history, features, and provide several examples to demonstrate its capabilities.

History of PETSc

PETSc was originally developed at Argonne National Laboratory in the 1990s by Satish Balay, William Gropp, Lois Curfman McInnes, and Barry Smith. Since then, it has evolved into a widely used and respected toolkit within the scientific computing community.

Features of PETSc Fortran Framework

  1. Efficient Parallel Computing: PETSc provides parallel linear and nonlinear solvers that can be used on a wide range of parallel architectures, including distributed and shared-memory systems.
  2. Data Structures: PETSc offers a variety of data structures, such as vectors and matrices, designed specifically for parallel computing. These data structures are optimized for memory usage and computational efficiency.
  3. Scalability: PETSc is designed to scale efficiently to large parallel systems. It employs advanced algorithms and techniques to ensure optimal performance.
  4. Customizable Solver Options: PETSc allows users to customize solver options to meet their specific needs. Users can choose from a wide range of solvers and preconditioners, and fine-tune their performance through various parameters and settings.
  5. Flexibility: PETSc is highly flexible and can be used with a variety of programming languages, including Fortran, C, and C++. This allows users to leverage existing code and libraries while taking advantage of PETSc's advanced parallel computing capabilities.

PETSc Fortran Examples

Example 1: Creating and Manipulating Vectors

program petsc_example
use petsc
implicit none

type (Vec) :: x
PetscErrorCode :: ierr

call VecCreate(PETSC_COMM_WORLD, x, ierr)
call VecSetSizes(x, PETSC_DECIDE, 5, ierr)
call VecSetFromOptions(x)

! Set the values of the vector
call VecSet(x, 1.0, ierr)

! Scale the vector by a constant
call VecScale(x, 2.0, ierr)

! Get the local array of the vector
PetscScalar, dimension(:), allocatable :: array
PetscInt :: n, i

call VecGetLocalSize(x, n, ierr)
allocate(array(n))
call VecGetArray(x, array, ierr)

do i = 1, n
print *, array(i)
end do

call VecRestoreArray(x, array, ierr)
call VecDestroy(x, ierr)

end program petsc_example

Output:

 2.0
2.0
2.0
2.0
2.0

Explanation: In this example, we create a vector x using VecCreate and set its size to 5 using VecSetSizes. We then set the vector values to 1 using VecSet and scale the vector by 2 using VecScale. Finally, we retrieve the local array of the vector using VecGetArray and print its values.

Example 2: Creating and Manipulating Matrices

program petsc_example
use petsc
implicit none

type (Mat) :: A
PetscErrorCode :: ierr

call MatCreate(PETSC_COMM_WORLD, A, ierr)
call MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, 5, 5, ierr)
call MatSetFromOptions(A)
call MatSetUp(A, ierr)

! Set the values of the matrix
PetscInt, dimension(3) :: rows = [1, 2, 3]
PetscInt, dimension(3) :: cols = [1, 2, 3]
PetscScalar, dimension(3) :: values = [1.0, 2.0, 3.0]

call MatSetValues(A, 3, rows, 3, cols, values, INSERT_VALUES, ierr)

! Assemble the matrix
call MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY, ierr)
call MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY, ierr)

! Get the local portion of the matrix
PetscInt :: m, n, i, j
PetscScalar :: val

call MatGetLocalSize(A, m, n, ierr)

do i = 1, m
do j = 1, n
call MatGetValues(A, 1, i, 1, j, val, ierr)
print *, val
end do
end do

call MatDestroy(A, ierr)

end program petsc_example

Output:

 1.0
2.0
3.0

Explanation: In this example, we create a matrix A using MatCreate and set its size to 5x5 using MatSetSizes. We then set the values of the matrix using MatSetValues and assemble the matrix using MatAssemblyBegin and MatAssemblyEnd. Finally, we retrieve the local portion of the matrix using MatGetLocalSize and MatGetValues, and print its values.

Conclusion

PETSc's Fortran framework provides a powerful set of tools for parallel scientific computing. Its efficient parallel solvers, flexible data structures, and customizable options make it a valuable resource for researchers and developers working on large-scale scientific applications. With its extensive documentation and active community, PETSc continues to evolve and improve, making it an essential toolkit in the field of scientific computing.

For more information and detailed documentation, visit the official PETSc website: https://www.mcs.anl.gov/petsc/