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 #include <petscsys.h> 14 int main(int argc,char **argv) 15 { 16 PetscErrorCode ierr; 17 PetscMPIInt rank,size; 18 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 ierr = PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr; 29 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 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr); 35 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 36 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 ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);CHKERRQ(ierr); 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 ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank);CHKERRQ(ierr); 55 ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank);CHKERRQ(ierr); 56 ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);CHKERRQ(ierr); 57 /* 58 Here a barrier is used to separate the two states. 59 */ 60 ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRMPI(ierr); 61 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 ierr = PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);CHKERRQ(ierr); 68 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_view). 74 See the PetscFinalize() manpage for more information. 75 */ 76 ierr = PetscFinalize(); 77 return ierr; 78 } 79 80 /*TEST 81 82 test: 83 84 TEST*/ 85