<div dir="ltr"><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Hi all,</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">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 (<a href="http://www.mcs.anl.gov/petsc/petsc-3.9/docs/manualpages/Sys/PetscTime.html#PetscTime">http://www.mcs.anl.gov/petsc/petsc-3.9/docs/manualpages/Sys/PetscTime.html#PetscTime</a>) and trying to print time from the following code but I got error message. The code and error messages are as follows:</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Code:</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br>static char help[] = "Solves a tridiagonal linear system.\n\n";<br><br>/*T<br> Concepts: KSP^basic parallel example;<br> Processors: n<br>T*/<br><br><br><br>/*<br> Include "petscksp.h" so that we can use KSP solvers. Note that this file<br> automatically includes:<br> petscsys.h - base PETSc routines petscvec.h - vectors<br> petscmat.h - matrices<br> petscis.h - index sets petscksp.h - Krylov subspace methods<br> petscviewer.h - viewers petscpc.h - preconditioners<br><br> Note: The corresponding uniprocessor example is ex1.c<br>*/<br>#include <petscksp.h><br>#include <petsctime.h><br><br>int main(int argc,char **args)<br>{<br> Vec x, b, u; /* approx solution, RHS, exact solution */<br> Mat A; /* linear system matrix */<br> KSP ksp; /* linear solver context */<br> PC pc; /* preconditioner context */<br> // PetscTime time;<br> PetscReal v,norm,tol=1000.*PETSC_MACHINE_EPSILON; /* norm of solution error */<br> PetscErrorCode ierr;<br> PetscInt i,n = 99,col[3],its,rstart,rend,nlocal;<br> PetscScalar one = 1.0,hundredth = 0.001,leftbc = 10.001,rightbc = 15.001,value[3];<br><br> ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;<br> ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr);<br><br> /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br> Compute the matrix and right-hand-side vector that define<br> the linear system, Ax = b.<br> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */<br><br> /*<br> Create vectors. Note that we form 1 vector from scratch and<br> then duplicate as needed. For this simple case let PETSc decide how<br> many elements of the vector are stored on each processor. The second<br> argument to VecSetSizes() below causes PETSc to decide.<br> */<br> ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);<br> ierr = VecSetSizes(x,PETSC_DECIDE,n);CHKERRQ(ierr);<br> ierr = VecSetFromOptions(x);CHKERRQ(ierr);<br> ierr = VecDuplicate(x,&b);CHKERRQ(ierr);<br> ierr = VecDuplicate(x,&u);CHKERRQ(ierr);<br><br> /* Identify the starting and ending mesh points on each<br> processor for the interior part of the mesh. We let PETSc decide<br> above. */<br><br> ierr = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);<br> ierr = VecGetLocalSize(x,&nlocal);CHKERRQ(ierr);<br><br> /*<br> Create matrix. When using MatCreate(), the matrix format can<br> be specified at runtime.<br><br> Performance tuning note: For problems of substantial size,<br> preallocation of matrix memory is crucial for attaining good<br> performance. See the matrix chapter of the users manual for details.<br><br> We pass in nlocal as the "local" size of the matrix to force it<br> to have the same parallel layout as the vector created above.<br> */<br> ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);<br> ierr = MatSetSizes(A,nlocal,nlocal,n,n);CHKERRQ(ierr);<br> ierr = MatSetFromOptions(A);CHKERRQ(ierr);<br> ierr = MatSetUp(A);CHKERRQ(ierr);<br><br> /*<br> Assemble matrix.<br><br> The linear system is distributed across the processors by<br> chunks of contiguous rows, which correspond to contiguous<br> sections of the mesh on which the problem is discretized.<br> For matrix assembly, each processor contributes entries for<br> the part that it owns locally.<br> */<br><br><br> if (!rstart) {<br> rstart = 1;<br> i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;<br> ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);<br> }<br> if (rend == n) {<br> rend = n-1;<br> i = n-1; col[0] = n-2; col[1] = n-1; value[0] = -1.0; value[1] = 2.0;<br> ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);<br> }<br><br> /* Set entries corresponding to the mesh interior */<br> value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;<br> for (i=rstart; i<rend; i++) {<br> col[0] = i-1; col[1] = i; col[2] = i+1;<br> ierr = MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr);<br> }<br><br> /* Assemble the matrix */<br> ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br> ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br><br> /*<br> Set exact solution; then compute right-hand-side vector.<br> */<br> ierr = VecSet(u,one);CHKERRQ(ierr);<br><br> i=0;<br> ierr = VecSetValues(b, 1, &i, &leftbc, INSERT_VALUES);CHKERRQ(ierr);<br> for (i=1; i<n-1; i++){<br> ierr = VecSetValues(b, 1, &i, &hundredth, INSERT_VALUES);CHKERRQ(ierr);<br> }<br> i=n-1;<br> ierr = VecSetValues(b, 1, &i, &rightbc, INSERT_VALUES);CHKERRQ(ierr);<br> ierr = VecAssemblyBegin(b);CHKERRQ(ierr);<br> ierr = VecAssemblyEnd(b);CHKERRQ(ierr);<br><br> // ierr = MatMult(A,u,b);CHKERRQ(ierr);<br><br> /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br> Create the linear solver and set various options<br> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */<br> /*<br> Create linear solver context<br> */<br> ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);<br><br> /*<br> Set operators. Here the matrix that defines the linear system<br> also serves as the preconditioning matrix.<br> */<br> ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);<br><br> /*<br> Set linear solver defaults for this problem (optional).<br> - By extracting the KSP and PC contexts from the KSP context,<br> we can then directly call any KSP and PC routines to set<br> various options.<br> - The following four statements are optional; all of these<br> parameters could alternatively be specified at runtime via<br> KSPSetFromOptions();<br> */<br> ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);<br> ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr);<br> ierr = KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);<br><br> /*<br> Set runtime options, e.g.,<br> -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol><br> These options will override those specified above as long as<br> KSPSetFromOptions() is called _after_ any other customization<br> routines.<br> */<br> ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);<br><br> /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br> Solve the linear system<br> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */<br> /*<br> Solve linear system<br> */<br> ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);<br><br> /*<br> View solver info; we could instead use the option -ksp_view to<br> print this info to the screen at the conclusion of KSPSolve().<br> */<br> ierr = KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);<br><br> /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br> Check solution and clean up<br> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */<br> /*<br> Check the error<br> */<br> ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);<br> ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);<br> ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);<br> if (norm > tol) {<br> ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);CHKERRQ(ierr);<br> }<br><br> // if (!PETSC_TRUE) {<br> // ierr = PetscViewer viewer;CHKERRQ(ierr);<br> // ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, "Amat.m", &viewer);CHKERRQ(ierr);<br> // ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);<br> // ierr = MatView(A,viewer);CHKERRQ(ierr);<br> // ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);<br> // ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);<br> // }<br>ierr = PetscTime(PetscLogDouble *v);CHKERRQ(ierr);<br>ierr = PetscLogDouble v;CHKERRQ(ierr);<br>ierr = PetscTime(&v);CHKERRQ(ierr);<br>ierr = printf(MPI_Wtime, "Time for operation %g\n",(double)v);<br><br> /*<br> Free work space. All PETSc objects should be destroyed when they<br> are no longer needed.<br> */<br> ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr);<br> ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr);<br> ierr = KSPDestroy(&ksp);CHKERRQ(ierr);<br><br> /*<br> Always call PetscFinalize() before exiting a program. This routine<br> - finalizes the PETSc libraries as well as MPI<br> - provides summary and diagnostic information if certain runtime<br> options are chosen (e.g., -log_view).<br> */<br> ierr = PetscFinalize();<br> return ierr;<br>}<br><br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Error Message: <br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">jimmy@dave:~/projects/petsc/problems/ksp$ make poisson_m<br>/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<br>/home/jimmy/projects/petsc/problems/ksp/poisson_m.c: In function ‘main’:<br>/home/jimmy/projects/petsc/problems/ksp/poisson_m.c:200:18: error: expected expression before ‘PetscLogDouble’<br> ierr = PetscTime(PetscLogDouble *v);CHKERRQ(ierr);<br> ^~~~~~~~~~~~~~<br>/home/jimmy/projects/petsc/problems/ksp/poisson_m.c:201:8: error: expected expression before ‘PetscLogDouble’<br> ierr = PetscLogDouble v;CHKERRQ(ierr);<br> ^~~~~~~~~~~~~~<br>/home/jimmy/projects/petsc/problems/ksp/poisson_m.c:203:15: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types]<br> ierr = printf(MPI_Wtime, "Time for operation %g\n",(double)v);<br> ^~~~~~~~~<br>In file included from /home/jimmy/projects/petsc/include/petscsys.h:175:0,<br> from /home/jimmy/projects/petsc/include/petscis.h:7,<br> from /home/jimmy/projects/petsc/include/petscvec.h:9,<br> from /home/jimmy/projects/petsc/include/petscmat.h:6,<br> from /home/jimmy/projects/petsc/include/petscpc.h:6,<br> from /home/jimmy/projects/petsc/include/petscksp.h:6,<br> from /home/jimmy/projects/petsc/problems/ksp/poisson_m.c:21:<br>/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘double (*)(void)’<br> extern int printf (const char *__restrict __format, ...);<br> ^~~~~~<br>/home/jimmy/projects/petsc/lib/petsc/conf/rules:359: recipe for target 'poisson_m.o' failed<br>make: *** [poisson_m.o] Error 1<br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Thanks in advance.</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br></div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Sincerely,</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Huq<br clear="all"></div><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><br><div><font face="comic sans ms, sans-serif">Fazlul Huq</font></div><div><font face="comic sans ms, sans-serif">Graduate Research Assistant</font></div><div><font face="comic sans ms, sans-serif">Department of Nuclear, Plasma & Radiological Engineering (NPRE)</font></div><div><font face="comic sans ms, sans-serif">University of Illinois at Urbana-Champaign (UIUC)</font></div><div><font face="comic sans ms, sans-serif">E-mail: <a href="mailto:huq2090@gmail.com" target="_blank">huq2090@gmail.com</a></font></div></div></div></div></div></div></div>
</div>