<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Jul 12, 2018 at 1:00 AM Fazlul Huq <<a href="mailto:huq2090@gmail.com">huq2090@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="auto"><div dir="ltr"><div style="font-family:comic sans ms,sans-serif">Hi,</div><div style="font-family:comic sans ms,sans-serif">I am trying to solve this problem with the following code: <br></div><div style="font-family:comic sans ms,sans-serif"><br>static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";<br><br>/*T<br> Concepts: KSP^solving a system of linear equations<br> Processors: 1<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 parallel example is ex23.c<br>*/<br>#include <petscksp.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> PetscReal norm; /* norm of solution error */<br> PetscErrorCode ierr;<br> PetscInt i,n = 10,col[3],its;<br> PetscMPIInt size;<br> // PetscScalar zero = 0;<br> PetscScalar hundredth = 0.01,value[3];<br> PetscScalar one = 1.0;<br> PetscScalar leftbc = 10.01;<br> PetscScalar rightbc = 15.01;<br> PetscBool nonzeroguess = PETSC_FALSE,changepcside = PETSC_FALSE;<br><br> ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;<br> ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);<br> if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");<br> ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr);<br> ierr = PetscOptionsGetBool(NULL,NULL,"-nonzero_guess",&nonzeroguess,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> Create vectors. Note that we form 1 vector from scratch and<br> then duplicate as needed.<br> */<br> ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);<br> ierr = PetscObjectSetName((PetscObject) x, "Solution");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> i=1;<br> ierr = VecSetValues(b, 1, &i, &leftbc, INSERT_VALUES);CHKERRQ(ierr);<br> for (i=2; i<n-1; i++){<br> ierr = VecSetValues(b, n-2, &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> /*<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> ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);<br> ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);CHKERRQ(ierr);<br> ierr = MatSetFromOptions(A);CHKERRQ(ierr);<br> ierr = MatSetUp(A);CHKERRQ(ierr);<br><br> /*<br> Assemble matrix<br> */<br> value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;<br> for (i=1; i<n-1; 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> i = n - 1; col[0] = n - 2; col[1] = n - 1;<br> ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);<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> 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><br><br> ierr = VecSet(u,one);CHKERRQ(ierr);<br> // ierr = MatMult(A,u,b);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, n-2, &i, &hundredth, INSERT_VALUES);CHKERRQ(ierr);<br></div></div></div></div></blockquote><div><br></div><div>What is the line above intended to do? You are telling it to take n-2 values, but only giving it</div><div>one value (hundreth) so it is indexing far outside the array.</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="auto"><div dir="ltr"><div style="font-family:comic sans ms,sans-serif"> }<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> Test if you can change the KSP side and type after they have been previously set<br> */<br> ierr = PetscOptionsGetBool(NULL,NULL,"-change_pc_side",&changepcside,NULL);CHKERRQ(ierr);<br> if (changepcside) {<br> ierr = KSPSetUp(ksp);CHKERRQ(ierr);<br> ierr = PetscOptionsInsertString(NULL,"-ksp_norm_type unpreconditioned -ksp_pc_side right");CHKERRQ(ierr);<br> ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);<br> ierr = KSPSetUp(ksp);CHKERRQ(ierr);<br> }<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-5,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> if (nonzeroguess) {<br> PetscScalar p = .5;<br> ierr = VecSet(x,p);CHKERRQ(ierr);<br> ierr = KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);CHKERRQ(ierr);<br> }<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> ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);CHKERRQ(ierr);<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 style="font-family:comic sans ms,sans-serif">But I got the following error message: <br></div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif">[0]PETSC ERROR: ------------------------------------------------------------------------<br>[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range<br>[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger<br>[0]PETSC ERROR: or see <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind</a><br>[0]PETSC ERROR: or try <a href="http://valgrind.org" rel="noreferrer" target="_blank">http://valgrind.org</a> on GNU/linux and Apple Mac OS X to find memory corruption errors<br>[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run <br>[0]PETSC ERROR: to get more information on the crash.<br>[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------<br>[0]PETSC ERROR: Signal received<br>[0]PETSC ERROR: See <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/documentation/faq.html</a> for trouble shooting.<br>[0]PETSC ERROR: Petsc Release Version 3.8.3, Dec, 09, 2017 <br>[0]PETSC ERROR: ./ex1 on a arch-linux2-c-opt named huq2090 by huq2090 Wed Jul 11 23:48:10 2018<br>[0]PETSC ERROR: Configure options --prefix=/opt/moose/petsc-3.8.3/mpich-3.2_gcc-7.3.0-opt --download-hypre=1 --with-ssl=0 --with-debugging=no --with-pic=1 --with-shared-libraries=1 --with-cc=mpicc --with-cxx=mpicxx --with-fc=mpif90 --download-fblaslapack=1 --download-metis=1 --download-parmetis=1 --download-superlu_dist=1 --download-mumps=1 --download-scalapack=1 -CC=mpicc -CXX=mpicxx -FC=mpif90 -F77=mpif77 -F90=mpif90 -CFLAGS="-fPIC -fopenmp" -CXXFLAGS="-fPIC -fopenmp" -FFLAGS="-fPIC -fopenmp" -FCFLAGS="-fPIC -fopenmp" -F90FLAGS="-fPIC -fopenmp" -F77FLAGS="-fPIC -fopenmp"<br>[0]PETSC ERROR: #1 User provided function() line 0 in unknown file<br>application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0<br>[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=59<br>:<br>system msg for write_line failure : Bad file descriptor<br></div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif">And again, when I tried to solve the same code with another pc, I got different error message attached herewith as an attachment.</div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif">Can you please suggest me about what to do.</div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif">Thanks.</div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif">Best, <br></div><div style="font-family:comic sans ms,sans-serif">Huq<br></div><div style="font-family:comic sans ms,sans-serif"><br></div><div style="font-family:comic sans ms,sans-serif"><br><br></div><br>-- <br><div class="m_-4525730291042851516m_-3015437439651453309m_8985794460961685802gmail_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" rel="noreferrer" target="_blank">huq2090@gmail.com</a></font></div></div></div></div></div></div></div>
</div></div>
</div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.caam.rice.edu/~mk51/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div>