xref: /petsc/src/sys/tutorials/ex1.c (revision ffc4695bcb29f4b022f59a5fd6bc99fc280ff6d8)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Introductory example that illustrates printing.\n\n";
3c4762a1bSJed Brown 
4c4762a1bSJed Brown /*T
5c4762a1bSJed Brown    Concepts: introduction to PETSc;
6c4762a1bSJed Brown    Concepts: printing^in parallel
7c4762a1bSJed Brown    Processors: n
8c4762a1bSJed Brown T*/
9c4762a1bSJed Brown 
10c4762a1bSJed Brown 
11c4762a1bSJed Brown 
12c4762a1bSJed Brown #include <petscsys.h>
13c4762a1bSJed Brown int main(int argc,char **argv)
14c4762a1bSJed Brown {
15c4762a1bSJed Brown   PetscErrorCode ierr;
16c4762a1bSJed Brown   PetscMPIInt    rank,size;
17c4762a1bSJed Brown 
18c4762a1bSJed Brown   /*
19c4762a1bSJed Brown     Every PETSc routine should begin with the PetscInitialize() routine.
20c4762a1bSJed Brown     argc, argv - These command line arguments are taken to extract the options
21c4762a1bSJed Brown                  supplied to PETSc and options supplied to MPI.
22c4762a1bSJed Brown     help       - When PETSc executable is invoked with the option -help,
23c4762a1bSJed Brown                  it prints the various options that can be applied at
24c4762a1bSJed Brown                  runtime.  The user can use the "help" variable place
25c4762a1bSJed Brown                  additional help messages in this printout.
26c4762a1bSJed Brown   */
27c4762a1bSJed Brown   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
28c4762a1bSJed Brown 
29c4762a1bSJed Brown   /*
30c4762a1bSJed Brown      The following MPI calls return the number of processes
31c4762a1bSJed Brown      being used and the rank of this process in the group.
32c4762a1bSJed Brown    */
33*ffc4695bSBarry Smith   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr);
34*ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
35c4762a1bSJed Brown 
36c4762a1bSJed Brown   /*
37c4762a1bSJed Brown      Here we would like to print only one message that represents
38c4762a1bSJed Brown      all the processes in the group.  We use PetscPrintf() with the
39c4762a1bSJed Brown      communicator PETSC_COMM_WORLD.  Thus, only one message is
40c4762a1bSJed Brown      printed representng PETSC_COMM_WORLD, i.e., all the processors.
41c4762a1bSJed Brown   */
42c4762a1bSJed Brown   ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);CHKERRQ(ierr);
43c4762a1bSJed Brown 
44c4762a1bSJed Brown   /*
45c4762a1bSJed Brown     Here a barrier is used to separate the two program states.
46c4762a1bSJed Brown   */
47*ffc4695bSBarry Smith   ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRMPI(ierr);
48c4762a1bSJed Brown 
49c4762a1bSJed Brown   /*
50c4762a1bSJed Brown     Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF,
51c4762a1bSJed Brown     where each process is considered separately and prints independently
52c4762a1bSJed Brown     to the screen.  Thus, the output from different processes does not
53c4762a1bSJed Brown     appear in any particular order.
54c4762a1bSJed Brown   */
55c4762a1bSJed Brown 
56c4762a1bSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);CHKERRQ(ierr);
57c4762a1bSJed Brown 
58c4762a1bSJed Brown   /*
59c4762a1bSJed Brown      Always call PetscFinalize() before exiting a program.  This routine
60c4762a1bSJed Brown        - finalizes the PETSc libraries as well as MPI
61c4762a1bSJed Brown        - provides summary and diagnostic information if certain runtime
62c4762a1bSJed Brown          options are chosen (e.g., -log_view).  See PetscFinalize()
63c4762a1bSJed Brown      manpage for more information.
64c4762a1bSJed Brown   */
65c4762a1bSJed Brown   ierr = PetscFinalize();
66c4762a1bSJed Brown   return ierr;
67c4762a1bSJed Brown }
68c4762a1bSJed Brown 
69c4762a1bSJed Brown 
70c4762a1bSJed Brown /*TEST
71c4762a1bSJed Brown 
72c4762a1bSJed Brown    test:
73c4762a1bSJed Brown 
74c4762a1bSJed Brown TEST*/
75