Actual source code: ex2.c
petsc-3.6.4 2016-04-12
2: static char help[] = "Synchronized printing.\n\n";
4: /*T
5: Concepts: petsc^introduction
6: Concepts: printing^synchronized
7: Concepts: printing^in parallel
8: Concepts: printf^synchronized
9: Concepts: printf^in parallel
10: Processors: n
11: T*/
13: #include <petscsys.h>
14: int main(int argc,char **argv)
15: {
17: PetscMPIInt rank,size;
19: /*
20: Every PETSc program should begin with the PetscInitialize() routine.
21: argc, argv - These command line arguments are taken to extract the options
22: supplied to PETSc and options supplied to MPI.
23: help - When PETSc executable is invoked with the option -help,
24: it prints the various options that can be applied at
25: runtime. The user can use the "help" variable place
26: additional help messages in this printout.
27: */
28: PetscInitialize(&argc,&argv,NULL,help);
30: /*
31: The following MPI calls return the number of processes
32: being used and the rank of this process in the group.
33: */
34: MPI_Comm_size(PETSC_COMM_WORLD,&size);
35: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
37: /*
38: Here we would like to print only one message that represents
39: all the processes in the group. We use PetscPrintf() with the
40: communicator PETSC_COMM_WORLD. Thus, only one message is
41: printed representing PETSC_COMM_WORLD, i.e., all the processors.
42: */
43: PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);
44: /*
45: Here we would like to print info from each process, such that
46: output from process "n" appears after output from process "n-1".
47: To do this we use a combination of PetscSynchronizedPrintf() and
48: PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
49: All the processes print the message, one after another.
50: PetscSynchronizedFlush() indicates that the current process in the
51: given communicator has concluded printing, so that the next process
52: in the communicator can begin printing to the screen.
53: */
54: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank);
55: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank);
56: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
57: /*
58: Here a barrier is used to separate the two states.
59: */
60: MPI_Barrier(PETSC_COMM_WORLD);
62: /*
63: Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
64: (where each process is considered separately). Thus, this time the
65: output from different processes does not appear in any particular order.
66: */
67: PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);
69: /*
70: Always call PetscFinalize() before exiting a program. This routine
71: - finalizes the PETSc libraries as well as MPI
72: - provides summary and diagnostic information if certain runtime
73: options are chosen (e.g., -log_summary).
74: See the PetscFinalize() manpage for more information.
75: */
76: PetscFinalize();
77: return 0;
78: }