-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpetsc-basic.c
More file actions
48 lines (40 loc) · 1.86 KB
/
petsc-basic.c
File metadata and controls
48 lines (40 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Basic structure of a PETSc program.
*/
#include <petsc.h>
const char *const help = "Basic PETSc program\n";
int main(int argc, char *argv[]) {
PetscErrorCode ierr;
PetscInt n = 0;
PetscReal r = 1.5;
PetscMPIInt rank, size;
/* Initialize PETSc. The first and second parameter are pointers to `argc`
and `argv`, respectively. The third is the path to the option file. The
last is a help string, which is printed when the program is executed with
the option `-help`. */
ierr = PetscInitialize(&argc, &argv, NULL, help);
/* Check the return value and quit if an error occurred. Error check is not
mandatory. */
CHKERRQ(ierr);
/* User can define a custom option. */
PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL);
PetscOptionsGetReal(NULL, NULL, "-r", &r, NULL);
/* PETSc initializes MPI automatically. Any MPI function can be used. PETSc
defines the communicator `PETSC_COMM_WORLD`, which is the set of all
processes that participate to PETSc. */
MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
MPI_Comm_size(PETSC_COMM_WORLD, &size);
/* PetscPrintf() prints to the standard output, only from the first
process. This is useful to print an information same in all processes. */
PetscPrintf(PETSC_COMM_WORLD, "n = %d, r = %lf\n", n, r);
/* PetscSynchronizedPrintf() prints to the standard output in the order of
rank. Therefore, output of the first processor precedes that of the
second and so on. */
PetscSynchronizedPrintf(PETSC_COMM_WORLD, "rank = %d, size = %d\n", rank,
size);
/* PetscSynchronizedFlush() flushes a file such as the standard input so
that every synchronized output comes before the program exits. */
PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
/* Finalize PETSc. */
PetscFinalize();
}