[petsc-users] Getting time

Matthew Knepley knepley at gmail.com
Fri Jul 20 13:29:59 CDT 2018


On Fri, Jul 20, 2018 at 2:27 PM Fazlul Huq <huq2090 at gmail.com> wrote:

> Hi all,
>
> I am trying to get how much time does is take to run the code. I looked at
> manual (scetion 13.6) as well as here (
> http://www.mcs.anl.gov/petsc/petsc-3.9/docs/manualpages/Sys/PetscTime.html#PetscTime)
> and trying to print time from the following code but I got error message.
> The code and error messages are as follows:
>
> Code:
>
> static char help[] = "Solves a tridiagonal linear system.\n\n";
>
> /*T
>    Concepts: KSP^basic parallel example;
>    Processors: n
> T*/
>
>
>
> /*
>   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
>   automatically includes:
>      petscsys.h       - base PETSc routines   petscvec.h - vectors
>      petscmat.h - matrices
>      petscis.h     - index sets            petscksp.h - Krylov subspace
> methods
>      petscviewer.h - viewers               petscpc.h  - preconditioners
>
>   Note:  The corresponding uniprocessor example is ex1.c
> */
> #include <petscksp.h>
> #include <petsctime.h>
>
> int main(int argc,char **args)
> {
>   Vec            x, b, u;          /* approx solution, RHS, exact solution
> */
>   Mat            A;                /* linear system matrix */
>   KSP            ksp;              /* linear solver context */
>   PC             pc;               /* preconditioner context */
>   // PetscTime      time;
>   PetscReal      v,norm,tol=1000.*PETSC_MACHINE_EPSILON;  /* norm of
> solution error */
>   PetscErrorCode ierr;
>   PetscInt       i,n = 99,col[3],its,rstart,rend,nlocal;
>   PetscScalar    one = 1.0,hundredth = 0.001,leftbc = 10.001,rightbc =
> 15.001,value[3];
>
>   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
>   ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr);
>
>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>          Compute the matrix and right-hand-side vector that define
>          the linear system, Ax = b.
>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
>
>   /*
>      Create vectors.  Note that we form 1 vector from scratch and
>      then duplicate as needed. For this simple case let PETSc decide how
>      many elements of the vector are stored on each processor. The second
>      argument to VecSetSizes() below causes PETSc to decide.
>   */
>   ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);
>   ierr = VecSetSizes(x,PETSC_DECIDE,n);CHKERRQ(ierr);
>   ierr = VecSetFromOptions(x);CHKERRQ(ierr);
>   ierr = VecDuplicate(x,&b);CHKERRQ(ierr);
>   ierr = VecDuplicate(x,&u);CHKERRQ(ierr);
>
>   /* Identify the starting and ending mesh points on each
>      processor for the interior part of the mesh. We let PETSc decide
>      above. */
>
>   ierr = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
>   ierr = VecGetLocalSize(x,&nlocal);CHKERRQ(ierr);
>
>   /*
>      Create matrix.  When using MatCreate(), the matrix format can
>      be specified at runtime.
>
>      Performance tuning note:  For problems of substantial size,
>      preallocation of matrix memory is crucial for attaining good
>      performance. See the matrix chapter of the users manual for details.
>
>      We pass in nlocal as the "local" size of the matrix to force it
>      to have the same parallel layout as the vector created above.
>   */
>   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
>   ierr = MatSetSizes(A,nlocal,nlocal,n,n);CHKERRQ(ierr);
>   ierr = MatSetFromOptions(A);CHKERRQ(ierr);
>   ierr = MatSetUp(A);CHKERRQ(ierr);
>
>   /*
>      Assemble matrix.
>
>      The linear system is distributed across the processors by
>      chunks of contiguous rows, which correspond to contiguous
>      sections of the mesh on which the problem is discretized.
>      For matrix assembly, each processor contributes entries for
>      the part that it owns locally.
>   */
>
>
>   if (!rstart) {
>     rstart = 1;
>     i      = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
>     ierr   = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
>   }
>   if (rend == n) {
>     rend = n-1;
>     i    = n-1; col[0] = n-2; col[1] = n-1; value[0] = -1.0; value[1] =
> 2.0;
>     ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
>   }
>
>   /* Set entries corresponding to the mesh interior */
>   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
>   for (i=rstart; i<rend; i++) {
>     col[0] = i-1; col[1] = i; col[2] = i+1;
>     ierr   = MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr);
>   }
>
>   /* Assemble the matrix */
>   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
>   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
>
>   /*
>      Set exact solution; then compute right-hand-side vector.
>   */
>   ierr = VecSet(u,one);CHKERRQ(ierr);
>
>   i=0;
>     ierr = VecSetValues(b, 1, &i, &leftbc, INSERT_VALUES);CHKERRQ(ierr);
>   for (i=1; i<n-1; i++){
>     ierr = VecSetValues(b, 1, &i, &hundredth, INSERT_VALUES);CHKERRQ(ierr);
>   }
>   i=n-1;
>     ierr = VecSetValues(b, 1, &i, &rightbc, INSERT_VALUES);CHKERRQ(ierr);
>     ierr = VecAssemblyBegin(b);CHKERRQ(ierr);
>     ierr = VecAssemblyEnd(b);CHKERRQ(ierr);
>
>   // ierr = MatMult(A,u,b);CHKERRQ(ierr);
>
>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>                 Create the linear solver and set various options
>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
>   /*
>      Create linear solver context
>   */
>   ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
>
>   /*
>      Set operators. Here the matrix that defines the linear system
>      also serves as the preconditioning matrix.
>   */
>   ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
>
>   /*
>      Set linear solver defaults for this problem (optional).
>      - By extracting the KSP and PC contexts from the KSP context,
>        we can then directly call any KSP and PC routines to set
>        various options.
>      - The following four statements are optional; all of these
>        parameters could alternatively be specified at runtime via
>        KSPSetFromOptions();
>   */
>   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
>   ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr);
>   ierr =
> KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
>
>   /*
>     Set runtime options, e.g.,
>         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
>     These options will override those specified above as long as
>     KSPSetFromOptions() is called _after_ any other customization
>     routines.
>   */
>   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
>
>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>                       Solve the linear system
>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
>   /*
>      Solve linear system
>   */
>   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
>
>   /*
>      View solver info; we could instead use the option -ksp_view to
>      print this info to the screen at the conclusion of KSPSolve().
>   */
>   ierr = KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
>
>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>                       Check solution and clean up
>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
>   /*
>      Check the error
>   */
>   ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);
>   ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
>   ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
>   if (norm > tol) {
>     ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations
> %D\n",(double)norm,its);CHKERRQ(ierr);
>   }
>
>   // if (!PETSC_TRUE) {
>   // ierr = PetscViewer viewer;CHKERRQ(ierr);
>   // ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, "Amat.m",
> &viewer);CHKERRQ(ierr);
>   // ierr = PetscViewerPushFormat(viewer,
> PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
>   // ierr = MatView(A,viewer);CHKERRQ(ierr);
>   // ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
>   // ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
>   // }
> ierr = PetscTime(PetscLogDouble *v);CHKERRQ(ierr);
>

1) This is the function declaration, not a call

2) Use -log_view, not explicit timers

   Matt


> ierr = PetscLogDouble v;CHKERRQ(ierr);
> ierr = PetscTime(&v);CHKERRQ(ierr);
> ierr = printf(MPI_Wtime, "Time for operation %g\n",(double)v);
>
>   /*
>      Free work space.  All PETSc objects should be destroyed when they
>      are no longer needed.
>   */
>   ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr);
>   ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr);
>   ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
>
>   /*
>      Always call PetscFinalize() before exiting a program.  This routine
>        - finalizes the PETSc libraries as well as MPI
>        - provides summary and diagnostic information if certain runtime
>          options are chosen (e.g., -log_view).
>   */
>   ierr = PetscFinalize();
>   return ierr;
> }
>
> Error Message:
> jimmy at dave:~/projects/petsc/problems/ksp$ make poisson_m
> /home/jimmy/projects/petsc/arch-linux2-c-debug/bin/mpicc -o poisson_m.o -c
> -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas
> -fstack-protector -fvisibility=hidden -g3
> -I/home/jimmy/projects/petsc/include
> -I/home/jimmy/projects/petsc/arch-linux2-c-debug/include
> `pwd`/poisson_m.c
> /home/jimmy/projects/petsc/problems/ksp/poisson_m.c: In function ‘main’:
> /home/jimmy/projects/petsc/problems/ksp/poisson_m.c:200:18: error:
> expected expression before ‘PetscLogDouble’
>  ierr = PetscTime(PetscLogDouble *v);CHKERRQ(ierr);
>                   ^~~~~~~~~~~~~~
> /home/jimmy/projects/petsc/problems/ksp/poisson_m.c:201:8: error: expected
> expression before ‘PetscLogDouble’
>  ierr = PetscLogDouble v;CHKERRQ(ierr);
>         ^~~~~~~~~~~~~~
> /home/jimmy/projects/petsc/problems/ksp/poisson_m.c:203:15: warning:
> passing argument 1 of ‘printf’ from incompatible pointer type
> [-Wincompatible-pointer-types]
>  ierr = printf(MPI_Wtime, "Time for operation %g\n",(double)v);
>                ^~~~~~~~~
> In file included from /home/jimmy/projects/petsc/include/petscsys.h:175:0,
>                  from /home/jimmy/projects/petsc/include/petscis.h:7,
>                  from /home/jimmy/projects/petsc/include/petscvec.h:9,
>                  from /home/jimmy/projects/petsc/include/petscmat.h:6,
>                  from /home/jimmy/projects/petsc/include/petscpc.h:6,
>                  from /home/jimmy/projects/petsc/include/petscksp.h:6,
>                  from
> /home/jimmy/projects/petsc/problems/ksp/poisson_m.c:21:
> /usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but
> argument is of type ‘double (*)(void)’
>  extern int printf (const char *__restrict __format, ...);
>             ^~~~~~
> /home/jimmy/projects/petsc/lib/petsc/conf/rules:359: recipe for target
> 'poisson_m.o' failed
> make: *** [poisson_m.o] Error 1
>
> Thanks in advance.
>
> Sincerely,
> Huq
>
> --
>
> Fazlul Huq
> Graduate Research Assistant
> Department of Nuclear, Plasma & Radiological Engineering (NPRE)
> University of Illinois at Urbana-Champaign (UIUC)
> E-mail: huq2090 at gmail.com
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.caam.rice.edu/~mk51/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20180720/8084a21e/attachment-0001.html>


More information about the petsc-users mailing list