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