<div dir="ltr"><div class="gmail_default" style="font-family:comic sans ms,sans-serif">Thanks Karl!</div><div class="gmail_default" style="font-family:comic sans ms,sans-serif"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 27, 2018 at 2:09 PM, Karl Rupp <span dir="ltr"><<a href="mailto:rupp@iue.tuwien.ac.at" target="_blank">rupp@iue.tuwien.ac.at</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Fazlul,<br>
<br>
Pass the command line option -log_view to get a detailed breakdown of time spent in various code sections. For details, consult the manual.<br>
<br>
Best regards,<br>
Karli<span class=""><br>
<br>
On 07/27/2018 01:47 PM, Fazlul Huq wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
Thanks Matthew.<br>
<br>
By the way, how can I find the execution time of the processor?<br>
<br>
<br>
<br></span><span class="">
On Thu, Jul 19, 2018 at 7:34 PM, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a> <mailto:<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>>> wrote:<br>
<br>
On Thu, Jul 19, 2018 at 1:44 PM Fazlul Huq <<a href="mailto:huq2090@gmail.com" target="_blank">huq2090@gmail.com</a><br></span><div><div class="h5">
<mailto:<a href="mailto:huq2090@gmail.com" target="_blank">huq2090@gmail.com</a>>> wrote:<br>
<br>
Hello all,<br>
<br>
I can run the following code with this command showing output on<br>
the console: ./poisson_m -ksp_view_solution<br>
<br>
<br>
What is the question? To put that ASCII output in a file, use<br>
<br>
-ksp_view_solution :sol.txt<br>
<br>
There is a chapter in the manual on viewing.<br>
<br>
Thanks,<br>
<br>
Matt<br>
<br>
The code is:<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>
Include "petscksp.h" so that we can use KSP solvers. Note<br>
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<br>
subspace methods<br>
petscviewer.h - viewers petscpc.h -<br>
preconditioners<br>
<br>
Note: The corresponding uniprocessor example is ex1.c<br>
*/<br>
#include <petscksp.h><br>
<br>
int main(int argc,char **args)<br>
{<br>
Vec x, b, u; /* approx solution, RHS,<br>
exact solution */<br>
Mat A; /* linear system matrix */<br>
KSP ksp; /* linear solver context */<br>
PC pc; /* preconditioner context */<br>
PetscReal norm,tol=1000.*PETSC_MACHINE_E<wbr>PSILON; /* norm<br>
of solution error */<br>
PetscErrorCode ierr;<br>
PetscInt i,n = 99,col[3],its,rstart,rend,nloc<wbr>al;<br>
PetscScalar one = 1.0,hundredth = 0.001,leftbc =<br>
10.001,rightbc = 15.001,value[3];<br>
<br>
ierr = PetscInitialize(&argc,&args,(c<wbr>har*)0,help);if (ierr)<br>
return ierr;<br>
ierr = PetscOptionsGetInt(NULL,NULL,"<wbr>-n",&n,NULL);CHKERRQ(ierr);<br>
<br>
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - -<br>
Compute the matrix and right-hand-side vector that define<br>
the linear system, Ax = b.<br>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -<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<br>
decide how<br>
many elements of the vector are stored on each processor.<br>
The second<br>
argument to VecSetSizes() below causes PETSc to decide.<br>
*/<br>
ierr = VecCreate(PETSC_COMM_WORLD,&x)<wbr>;CHKERRQ(ierr);<br>
ierr = VecSetSizes(x,PETSC_DECIDE,n);<wbr>CHKERRQ(ierr);<br>
ierr = VecSetFromOptions(x);CHKERRQ(i<wbr>err);<br>
ierr = VecDuplicate(x,&b);CHKERRQ(ier<wbr>r);<br>
ierr = VecDuplicate(x,&u);CHKERRQ(ier<wbr>r);<br>
<br>
/* Identify the starting and ending mesh points on each<br>
processor for the interior part of the mesh. We let PETSc<br>
decide<br>
above. */<br>
<br>
ierr = VecGetOwnershipRange(x,&rstart<wbr>,&rend);CHKERRQ(ierr);<br>
ierr = VecGetLocalSize(x,&nlocal);CHK<wbr>ERRQ(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<br>
for details.<br>
<br>
We pass in nlocal as the "local" size of the matrix to<br>
force it<br>
to have the same parallel layout as the vector created above.<br>
*/<br>
ierr = MatCreate(PETSC_COMM_WORLD,&A)<wbr>;CHKERRQ(ierr);<br>
ierr = MatSetSizes(A,nlocal,nlocal,n,<wbr>n);CHKERRQ(ierr);<br>
ierr = MatSetFromOptions(A);CHKERRQ(i<wbr>err);<br>
ierr = MatSetUp(A);CHKERRQ(ierr);<br>
<br>
/*<br>
Assemble matrix.<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;<br>
value[1] = -1.0;<br>
ierr =<br>
MatSetValues(A,1,&i,2,col,valu<wbr>e,INSERT_VALUES);CHKERRQ(ierr)<wbr>;<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;<br>
value[1] = 2.0;<br>
ierr =<br>
MatSetValues(A,1,&i,2,col,valu<wbr>e,INSERT_VALUES);CHKERRQ(ierr)<wbr>;<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 =<br>
MatSetValues(A,1,&i,3,col,valu<wbr>e,INSERT_VALUES);CHKERRQ(ierr)<wbr>;<br>
}<br>
<br>
/* Assemble the matrix */<br>
ierr = MatAssemblyBegin(A,MAT_FINAL_A<wbr>SSEMBLY);CHKERRQ(ierr);<br>
ierr = MatAssemblyEnd(A,MAT_FINAL_ASS<wbr>EMBLY);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,<br>
INSERT_VALUES);CHKERRQ(ierr);<br>
for (i=1; i<n-1; i++){<br>
ierr = VecSetValues(b, 1, &i, &hundredth,<br>
INSERT_VALUES);CHKERRQ(ierr);<br>
}<br>
i=n-1;<br>
ierr = VecSetValues(b, 1, &i, &rightbc,<br>
INSERT_VALUES);CHKERRQ(ierr);<br>
ierr = VecAssemblyBegin(b);CHKERRQ(ie<wbr>rr);<br>
ierr = VecAssemblyEnd(b);CHKERRQ(ierr<wbr>);<br>
<br>
// ierr = MatMult(A,u,b);CHKERRQ(ierr);<br>
<br>
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - -<br>
Create the linear solver and set various options<br>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - - */<br>
/*<br>
Create linear solver context<br>
*/<br>
ierr = KSPCreate(PETSC_COMM_WORLD,&ks<wbr>p);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);CHKER<wbr>RQ(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<wbr>);<br>
ierr = PCSetType(pc,PCJACOBI);CHKERRQ<wbr>(ierr);<br>
ierr =<br>
KSPSetTolerances(ksp,1.e-7,PET<wbr>SC_DEFAULT,PETSC_DEFAULT,PETSC<wbr>_DEFAULT);CHKERRQ(ierr);<br>
<br>
/*<br>
Set runtime options, e.g.,<br>
-ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol<br>
<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<wbr>(ierr);<br>
<br>
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - -<br>
Solve the linear system<br>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - - */<br>
/*<br>
Solve linear system<br>
*/<br>
ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr<wbr>);<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_STDOU<wbr>T_WORLD);CHKERRQ(ierr);<br>
<br>
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - -<br>
Check solution and clean up<br>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>
- - - - - */<br>
/*<br>
Check the error<br>
*/<br>
ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr<wbr>);<br>
ierr = VecNorm(x,NORM_2,&norm);CHKERR<wbr>Q(ierr);<br>
ierr = KSPGetIterationNumber(ksp,&its<wbr>);CHKERRQ(ierr);<br>
if (norm > tol) {<br>
ierr = PetscPrintf(PETSC_COMM_WORLD,"<wbr>Norm of error %g,<br>
Iterations %D\n",(double)norm,its);CHKERR<wbr>Q(ierr);<br>
}<br>
<br>
/*<br>
Free work space. All PETSc objects should be destroyed<br>
when they<br>
are no longer needed.<br>
*/<br>
ierr = VecDestroy(&x);CHKERRQ(ierr); ierr =<br>
VecDestroy(&u);CHKERRQ(ierr);<br>
ierr = VecDestroy(&b);CHKERRQ(ierr); ierr =<br>
MatDestroy(&A);CHKERRQ(ierr);<br>
ierr = KSPDestroy(&ksp);CHKERRQ(ierr)<wbr>;<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<br>
runtime<br>
options are chosen (e.g., -log_view).<br>
*/<br>
ierr = PetscFinalize();<br>
return ierr;<br>
}<br>
<br>
<br>
Now I am trying to save this output in a file (say in a .csv<br>
file) at runtime along with execution time but I can't.<br>
<br>
Any suggestion please!<br>
<br>
Sincerely,<br>
Huq<br>
<br>
-- <br>
Fazlul Huq<br>
Graduate Research Assistant<br>
Department of Nuclear, Plasma & Radiological Engineering (NPRE)<br>
University of Illinois at Urbana-Champaign (UIUC)<br></div></div>
E-mail: <a href="mailto:huq2090@gmail.com" target="_blank">huq2090@gmail.com</a> <mailto:<a href="mailto:huq2090@gmail.com" target="_blank">huq2090@gmail.com</a>><span class=""><br>
<br>
<br>
<br>
-- What most experimenters take for granted before they begin their<br>
experiments is infinitely more interesting than any results to which<br>
their experiments lead.<br>
-- Norbert Wiener<br>
<br>
<a href="https://www.cse.buffalo.edu/~knepley/" rel="noreferrer" target="_blank">https://www.cse.buffalo.edu/~k<wbr>nepley/</a><br></span>
<<a href="http://www.caam.rice.edu/%7Emk51/" rel="noreferrer" target="_blank">http://www.caam.rice.edu/%7Em<wbr>k51/</a>><br>
<br>
<br>
</blockquote>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="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>