<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I pulled the most recent version of petsc-dev and added superlu_dist.  The only case that fails is spooles on 1 processor with an mpiaij matrix created with the Create/SetSizes/... paradigm.  I would suspect something in spooles wrapper code. <div><div><br></div><div>code allocating mat:</div><div>MatCreate(Py_PETSC_COMM_WORLD,&self->m);<br>MatSetSizes(self->m,n,n,N,N);<br>MatSetType(self->m,MATMPIAIJ);<br><br></div><div>results:</div><div>-pc_type lu -pc_factor_mat_solver_packages spooles -ksp_monitor_true_residual (spooles on 1 proc)</div><div> 0 KSP preconditioned resid norm 2.284405915285e+01 true resid norm 1.062801511746e+01 ||Ae||/||Ax|| 1.000000000000e+00<br> 1 KSP preconditioned resid norm 3.936838941525e-01 true resid norm 1.050257318153e+01 ||Ae||/||Ax|| 9.881970495395e-01<br> 2 KSP preconditioned resid norm 7.332823321496e-14 true resid norm 2.408053447453e-13 ||Ae||/||Ax|| 2.265760276816e-14<br><br>-pc_type lu -pc_factor_mat_solver_packages spooles -ksp_monitor_true_residual (spooleson 8 procs)</div><div>0 KSP preconditioned resid norm 2.284405915285e+01 true resid norm 1.062801511746e+01 ||Ae||/||Ax|| 1.000000000000e+00<br> 1 KSP preconditioned resid norm 2.898588418444e-14 true resid norm 1.045214942604e-13 ||Ae||/||Ax|| 9.834526306678e-15<br><br>-pc_type lu -pc_factor_mat_solver_packages superlu_dist -ksp_monitor_true_residual</div><div>0 KSP preconditioned resid norm 2.284405915285e+01 true resid norm 1.062801511746e+01 ||Ae||/||Ax|| 1.000000000000e+00<br> 1 KSP preconditioned resid norm 2.917052183641e-14 true resid norm 1.299023464819e-13 ||Ae||/||Ax|| 1.222263471084e-14<br></div><div><br></div><div>code allocating mat:</div><div><br></div><div>MatCreateMPIAIJ(Py_PETSC_COMM_WORLD,n,n,N,N,1,PETSC_NULL,max_nNeighbors,PETSC_NULL,&self->m);</div><div><br></div><div>results:</div><div><div>-pc_type lu -pc_factor_mat_solver_packages spooles -ksp_monitor_true_residual (spooles on 1 proc)</div> 0 KSP preconditioned resid norm 2.284405915285e+01 true resid norm 1.062801511746e+01 ||Ae||/||Ax|| 1.000000000000e+00<br> 1 KSP preconditioned resid norm 3.093811333624e-14 true resid norm 1.046870732638e-13 ||Ae||/||Ax|| 9.850105791801e-15<br><div><div><br></div><div>On Oct 29, 2009, at 1:50 PM, Matthew Knepley wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">On Thu, Oct 29, 2009 at 1:46 PM, Chris Kees <span dir="ltr"><<a href="mailto:christopher.e.kees@usace.army.mil">christopher.e.kees@usace.army.mil</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Mark, Barry,<br> <br> Thanks for the help.  For now I'm sticking with the approach of copying the csr matrix and using the csr data structures to do the preallocations. I'll eventually get around to writing the code for assembling directly into the petsc matrix.  I have two more questions.<br> <br> 1) On 1 processor, the linear solver is not converging in 1 iteration using -pc_type lu -pc_factor_mat_solver_package spooles. It converges in one iteration in parallel or on a single processor if I set -mat_type seqaij. Also, if I go back to the old way of constructing the matrix it DOES converge in 1 iteration on 1 processor. That is, if I replace the MatCreate/MatSetSizes/...  calls from the earlier email, with what I had originally:<br> <br> MatCreateMPIAIJ(Py_PETSC_COMM_WORLD,n,n,N,N,1,PETSC_NULL,max_nNeighbors,PETSC_NULL,&self->m);<br> <br> then the solver converges in 1 iteration as expected.  This wouldn't be a big deal except that we're trying to verify that our quadratic finite elements are working, and the behavior for p1 and p2 elements is different. The above observations are for p2, while all the cases work as expected for p1. Should we just not be using mpiaij at all on 1 processor?<br> </blockquote><div><br>No, you have a problem.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> 2) In the code snippet I took from the petsc example there are two preallocation calls (see below). Is that correct, or should I be checking the matrix type and calling only the preallocation function for the actual matrix type? i.e. something like<br> <br> MatCreate(...<br> MatSetSizes(...<br> MatSetFromOptions(...<br> MatGetType(...<br> if type==mpiaij: MatMPIAIJSetPreallocationCSR(...<br></blockquote><div><br>It does not hurt to call them all.<br><br>  Matt<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Chris<br> <br> On Oct 21, 2009, at 11:50 AM, Mark F. Adams wrote:<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Chris, just a note,<br> <br> Perhaps I missed something here but do something similar to you, eg overlapping partitions, and PETSc is setup very well to be intrusive in your code (I sometimes write a little 'addvalue' wrapper) and assemble your FE matrices directly into a global matrix.  You use the (global) MatSetValues(...) and set the matrix option MAT_IGNORE_OFF_PROC_ENTRIES (or something like that) so that these off processor matrix entries, which are computed redundantly, are thrown away and you get the correct matrix.<br> <br> As Barry said you don't need exact non-zero counts to allocate storage in PETSC matrices, just don't underestimate.<br> <br> Mark<br> <br> On Oct 21, 2009, at 9:16 AM, Barry Smith wrote:<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <br> On Oct 20, 2009, at 10:13 PM, Chris Kees wrote:<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Thanks. Just to make sure I'm following, when I create the matrix I do:<br> <br>   MatCreate(PETSC_COMM_WORLD,&self->m);<br>   MatSetSizes(self->m,n,n,N,N);<br>   MatSetType(self->m,MATMPIAIJ);<br>   MatSetFromOptions(self->m);<br>   MatSeqAIJSetPreallocationCSR(self->m,SMP(L)->A.rowptr,SMP(L)->A.colind,(double*)(SMP(L)->A.nzval));<br>   MatMPIAIJSetPreallocationCSR(self->m,SMP(L)->A.rowptr,SMP(L)->A.colind,(double*)(SMP(L)->A.nzval));<br> <br> and then I copy the matrix at each Newton iteration using the code in the first message below.  You said "...PreallocationCSR() does the copy very efficiently", but I don't think I'm supposed to repeatedly call it, right?  Looking at the output some more it does seem like building the preconditioner initially is taking much more time than the initial pass through MatSetValues.<br> </blockquote> <br>  If the matrix nonzero structure stays the same then yes you need call MatMPIAIJSetPreallocationCSR() only once and use the MatSetValues() calls each Newton step to update the nonzeros.<br> <br>  Barry<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <br> Chris<br> <br> On Oct 20, 2009, at 8:32 PM, Barry Smith wrote:<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <br> Here's the deal. In parallel PETSc does not use a single CSR matrix on each process to hold the MPIAIJ matrix. Hence if you store the matrix on a process as a single CSR matrix then it has to be copied into the PETSc datastructure. MatMPIAIJSetPreallocationCSR() does the copy very efficiently. But you are right, until you free YOUR rowpt[], colind[] and a[] there are two copies of the matrix.<br> One could argue that this is ok, because anyways any preconditioner worth anything will take up at least that much memory later anyways.  For example, if you use the PETSc default preconditioner, block Jacobi with ILU(0) on each block it will take pretty much the same amount of memory.<br> <br> For people who use SOR or some other preconditioner that requires very little memory this is not satisfactory because they feel they could run larger problems without the copy.<br> <br> I strongly recommend you use MatMPIAIJSetPreallocationCSR() and live with any memory limitation. The reason is that doing more than that has orders of magnitude more pain with generally little payoff.<br> If you like pain then you can<br> 1) change your code to not build directly into CSR, instead call MatSetValuesLocal() as you compute the entries. With this you need to figure out the preallocation which can be painful code.<br> 2) write an entire matrix class that stores the matrix like you store it, not like you store it. This is a huge project, since you have to write your own matrix-vector, ILU factorization etc.<br> <br> Barry<br> <br> <br> <br> On Oct 20, 2009, at 8:04 PM, Chris Kees wrote:<br> <br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Hi,<br> <br> Our unstructured finite element code does a fairly standard overlapping decomposition that allows it to calculate all the non-zero column entries for the rows owned by the processor (rows 0...ldim-1 in the local numbering).  We assemble them into a local CSR matrix and then copy them into a PETSc matrix like this:<br> <br> for (int i=0;i<ldim;i++)<br> {<br> irow[0] = i;<br> MatSetValuesLocal(PETSCMAT(par_L),1,irow,rowptr[i+1]-rowptr[i],&colind[rowptr[i]],&a[rowptr[i]],INSERT_VALUES);<br> }<br> <br> where rowptr and colind are the CSR indexing arrays in the local numbering.<br> <br> I would like to eliminate the duplicate memory (and the copy above). Is there a recommended way to let PETSc reference array 'a' directly or a way to get rid of 'a' and translate our CSR assembly routines to use low level PETSc data structures?<br> <br> So far I've added MatMPIAIJSetPreallocationCSR to try to speed up the first solve, but the documentation is clear that 'a' is not a pointer to the AIJ storage so I still need the copy. I tried using MatCreateMPIAIJWithSplitArrays with the 'o' arrays set to zero but I got indexing errors, and the documentation really seems to imply that the local matrix is really not a standard CSR anyway. If that's true then it seems like the options I have are to write a shell matrix for parallel CSR storage or write some new assembly routines that use MatSetValuesLocal. I'm more concerned about the memory than the overhead of MatSetValuesLocal, but it would certainly be easier on me to use the CSR-based assembly routines we already have.<br> <br> Thanks,<br> Chris<br> <br> <br> <br> </blockquote> <br> </blockquote> <br> </blockquote> <br> <br> </blockquote> <br> </blockquote> <br> </blockquote></div><br><br clear="all"><br>-- <br>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<br> </blockquote></div><br></div></div></body></html>