<div dir="ltr">Hi Barry, <div><br></div><div>In the original <span style="font-size:12.8px">MatMPIAIJSetPreallocation(), PETSc determines column index when preallocating the matrix: </span>cols[cnt++] = l + nc*(slot + ii + gnx*jj + gnx*gny*kk); If I understand correctly, while PETSc allocates the matrix the value of cnt is important, entries value of the cols[] array is not.</div><div><br></div><div>I am using MatSetValuesStencil() to put values in to the matrix.</div><div><br></div><div>I think my problem is fitting well with the Box Stencil (3 dof) of PETSc, am I right?</div><div><br></div><div>Many thanks,</div><div><br></div><div>Pham</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 28, 2015 at 9:05 AM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Oct 27, 2015, at 8:55 PM, Sang pham van <<a href="mailto:pvsang002@gmail.com">pvsang002@gmail.com</a>> wrote:<br>
><br>
> Hi Matt,<br>
><br>
> Can you please answer my second question about the way PESCs understand the column indies? :<br>
><br>
> One more question I have is that, when preallocating for a node, as you can see in my below code (based on original PETSc code), since I have 3 equations, I preallocated 3 rows (in rows[l] array), each row has a number of columns index, all column indices of the three rows are stored in the cols[] array. At this point I don't understand how PETSc know in that cols[] arrays which column indices belonging to the first(second/third) row? Please help me to understand this.<br>
<br>
</span> The preallocation doesn't indicate which columns will have nonzeros, just the NUMBER of columns that will have nonzeros, it is only when values are actually entered into the matrix that the nonzero columns are determined.<br>
<br>
Also note that MatSetValuesLocal() will NOT work for your "dense rows", it only works for values that fit into the natural stencil of the matrix as defined with the DMDA. To put values into the dense rows you will need to call MatSetValues(), not MatSetValuesLocal().<br>
<br>
You are trying to do something that PETSc DMDA was not designed for so it may take a little work to accomplish what you want.<br>
<div class="HOEnZb"><div class="h5"><br>
Barry<br>
<br>
><br>
> Thank you.<br>
><br>
> Pham<br>
><br>
><br>
> On Wed, Oct 28, 2015 at 8:48 AM, Matthew Knepley <<a href="mailto:knepley@gmail.com">knepley@gmail.com</a>> wrote:<br>
> On Tue, Oct 27, 2015 at 8:18 PM, Sang pham van <<a href="mailto:pvsang002@gmail.com">pvsang002@gmail.com</a>> wrote:<br>
> Hi Barry,<br>
><br>
> I made my function for preallocating the DM matrix. I am using immersed boundary method to solve a problem of solid mechanics (3D, linear elasticity).<br>
> At every solid nodes, I have 3 unknowns for displacement in 3 directions (nc=3), my DM has box stencil. Thus, at a node I have 3 equations, for each equation I preallocate 27*3 = 81 entries.<br>
> When running the code, I got many of the following error when setting values into the preallocated matrix:<br>
><br>
> You might have memory corruption. Run with valgrind.<br>
><br>
> Thanks,<br>
><br>
> Matt<br>
><br>
> [0]PETSC ERROR: --------------------- Error Message ------------------------------------<br>
> [0]PETSC ERROR: Argument out of range!<br>
> [0]PETSC ERROR: Local index 1496271904 too large 121500 (max) at 0!<br>
> [0]PETSC ERROR: ------------------------------------------------------------------------<br>
> [0]PETSC ERROR: Petsc Release Version 3.4.5, Jun, 29, 2014<br>
> [0]PETSC ERROR: See docs/changes/index.html for recent updates.<br>
> [0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.<br>
> [0]PETSC ERROR: See docs/index.html for manual pages.<br>
> [0]PETSC ERROR: ------------------------------------------------------------------------<br>
> [0]PETSC ERROR: ./sasMAIN on a arch-linux-cxx-opt named sang by sang Wed Oct 28 07:34:37 2015<br>
> [0]PETSC ERROR: Libraries linked from /home/sang/petsc/petsc-3.4.5/arch-linux-cxx-opt/lib<br>
> [0]PETSC ERROR: Configure run at Thu Sep 3 23:04:15 2015<br>
> [0]PETSC ERROR: Configure options --download-mpich=1 --with-scalar-type=real --with-clanguage=cxx --download-mumps=1 --download-blacs=1 --download-parmetis=1 --download-scalapack=1 --with-debugging=0 --download-hypre=1 --with-fc=gfortran --download-metis=1 -download-cmake=1 --download-f-blas-lapack=1<br>
> [0]PETSC ERROR: ------------------------------------------------------------------------<br>
> [0]PETSC ERROR: ISLocalToGlobalMappingApply() line 444 in /home/sang/petsc/petsc-3.4.5/src/vec/is/utils/isltog.c<br>
> [0]PETSC ERROR: MatSetValuesLocal() line 1968 in /home/sang/petsc/petsc-3.4.5/src/mat/interface/matrix.c<br>
> [0]PETSC ERROR: MatSetValuesStencil() line 1339 in /home/sang/petsc/petsc-3.4.5/src/mat/interface/matrix.c<br>
><br>
> Can you please explain to me what is possible reason for this error? it looks like there is problem with the mapping from LocaltoGobal index, but that part I just copy from the original code.<br>
> One more question I have is that, when preallocating for a node, as you can see in my below code (based on original PETSc code), since I have 3 equations, I preallocated 3 rows (in rows[l] array), each row has a number of columns index, all column indices of the three rows are stored in the cols[] array. At this point I don't understand how PETSc know in that cols[] arrays which column indices belonging to the first(second/third) row? Please help me to understand this.<br>
><br>
> Thank you very much.<br>
> Pham<br>
><br>
> Below are my code for preallocating the DM matrix;<br>
><br>
> PetscErrorCode sasMatVecPetsc::DMCreateMatrix_DA_3d_MPIAIJ_pvs(DM da,Mat pMat, sasSmesh* mesh,sasVector<int>& solidcells,sasVector<int>& isolidcellnearBI,sasVector<int>& solidcellnearBI,sasVector<int>& forcing_or_ghost_cells)<br>
> {<br>
> PetscErrorCode ierr;<br>
> PetscInt xs,ys,nx,ny,i,j,slot,gxs,gys,gnx,gny;<br>
> PetscInt m,n,dim,s,*cols = NULL,k,nc,*rows = NULL,col,cnt,l,p,*dnz = NULL,*onz = NULL;<br>
> PetscInt istart,iend,jstart,jend,kstart,kend,zs,nz,gzs,gnz,ii,jj,kk;<br>
> MPI_Comm comm;<br>
> PetscScalar *values;<br>
> DMDABoundaryType bx,by,bz;<br>
> ISLocalToGlobalMapping ltog,ltogb;<br>
> DMDAStencilType st;<br>
><br>
> PetscFunctionBegin;<br>
><br>
> ierr = DMDAGetInfo(da,&dim,&m,&n,&p,0,0,0,&nc,&s,&bx,&by,&bz,&st);CHKERRQ(ierr); // nc = 3<br>
> col = 2*s + 1;<br>
> ierr = DMDAGetCorners(da,&xs,&ys,&zs,&nx,&ny,&nz);CHKERRQ(ierr);<br>
> ierr = DMDAGetGhostCorners(da,&gxs,&gys,&gzs,&gnx,&gny,&gnz);CHKERRQ(ierr);<br>
> ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr);<br>
> ierr = PetscMalloc2(nc,PetscInt,&rows,col*col*col*nc*nc,PetscInt,&cols);CHKERRQ(ierr);<br>
> ierr = DMGetLocalToGlobalMapping(da,<og);CHKERRQ(ierr);<br>
> ierr = MatPreallocateInitialize(comm,nc*nx*ny*nz,nc*nx*ny*nz,dnz,onz);CHKERRQ(ierr);<br>
> nCells = mesh->nCells;<br>
> sasVector<int> cell_type(mesh->nCells,0);<br>
> for(int i=0;i<solidcells.N;i++) cell_type[solidcells[i]] = 1;<br>
> for(int i=0;i<forcing_or_ghost_cells.N;i++) cell_type[forcing_or_ghost_cells[i]] = 2;<br>
> for(i = 0;i<nCells;i++)<br>
> {<br>
> slot = mesh->Cells_ijk[i].i - gxs + gnx*(mesh->Cells_ijk[i].j - gys) + gnx*gny*(mesh->Cells_ijk[i].k - gzs);<br>
> cnt = 0;<br>
> if(cell_type[i]==0) /// fluid cells<br>
> {<br>
> for (l=0; l<nc; l++)<br>
> {<br>
> cols[cnt++] = l + nc*slot;<br>
> rows[l] = l + nc*slot;<br>
> }<br>
> }<br>
><br>
> if(cell_type[i]==1) /// solid cells:<br>
> {<br>
> for (l=0; l<nc; l++)<br>
> {<br>
> for (int ll=0; ll<nc; ll++)<br>
> for (ii=-1; ii<2; ii++) {<br>
> for (jj=-1; jj<2; jj++) {<br>
> for (kk=-1; kk<2; kk++) {<br>
> cols[cnt++] = ll + nc*(slot + ii + gnx*jj + gnx*gny*kk);<br>
> }<br>
> }<br>
> }<br>
> rows[l] = l + nc*(slot);<br>
> }<br>
> }<br>
> if(cell_type[i]!=2)<br>
> ierr = MatPreallocateSetLocal(ltog,nc,rows,ltog,cnt,cols,dnz,onz);CHKERRQ(ierr);<br>
> }<br>
> MatCreate(comm,&pMat);<br>
> MatSetSizes(pMat,nc*gnx*gny*gnz,nc*gnx*gny*gnz,nc*gnx*gny*gnz,nc*gnx*gny*gnz);<br>
> MatSetType(pMat,MATAIJ);<br>
> ierr = MatSetBlockSize(pMat,nc);CHKERRQ(ierr);<br>
> ierr = MatSeqAIJSetPreallocation(pMat,0,dnz);CHKERRQ(ierr);<br>
> ierr = MatMPIAIJSetPreallocation(pMat,0,dnz,0,onz);CHKERRQ(ierr);<br>
> ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);<br>
> ierr = MatSetLocalToGlobalMapping(pMat,ltog,ltog);CHKERRQ(ierr);<br>
><br>
> ierr = PetscFree2(rows,cols);CHKERRQ(ierr);<br>
><br>
> PetscFunctionReturn(0);<br>
> }<br>
><br>
> On Sun, Oct 25, 2015 at 12:46 AM, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
><br>
> > On Oct 24, 2015, at 1:43 AM, Sang pham van <<a href="mailto:pvsang002@gmail.com">pvsang002@gmail.com</a>> wrote:<br>
> ><br>
> > Thank you, Dave,<br>
> ><br>
> > Can I just call My_DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J, void* my_context) when I need to create my matrix, and forget about DMDASetGetMatrix()?<br>
><br>
> You can do that if that is all you need. In some contexts when something else in PETSc needs to create a specific matrix (like with geometric multigrid) then that code calls DMCreateMatrix() which would then use your creation routine.<br>
><br>
> So, in summary you can start by just calling your own routine and convert it to use DMSetGetMatrix() later if you need to.<br>
><br>
> Barry<br>
><br>
> ><br>
> > Thank you.<br>
> ><br>
> > Pham<br>
> ><br>
> > On Sat, Oct 24, 2015 at 1:33 PM, Dave May <<a href="mailto:dave.mayhem23@gmail.com">dave.mayhem23@gmail.com</a>> wrote:<br>
> > If you need to access a user defined context from within your CreateMatrix function, you can attach it to the DM via PetscObjectCompose<br>
> ><br>
> > <a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscObjectCompose.html" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscObjectCompose.html</a><br>
> ><br>
> > If your context is not a petsc object, you can use PetscContainerCreate(),<br>
> ><br>
> > <a href="http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscContainerCreate.html#PetscContainerCreate" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscContainerCreate.html#PetscContainerCreate</a><br>
> ><br>
> > You would then set your user context pointer inside the container and then use PetscObjectCompose() to attach the container to the DM<br>
> ><br>
> > Thanks,<br>
> > Dave<br>
> ><br>
> ><br>
> > On 24 October 2015 at 06:04, Sang pham van <<a href="mailto:pvsang002@gmail.com">pvsang002@gmail.com</a>> wrote:<br>
> > Hi Barry,<br>
> ><br>
> > The routine DMCreateMatrix_DA_3d_MPIAIJ has 2 input arguments (DM da, Mat J), the function pointer in DMDASetGetMatrix() only accept function with that two arguments.<br>
> > As you suggested, I am writing a routine (based on DMCreateMatrix_DA_3d_MPIAIJ()) to preallocate the matrix in the way I wish, to do that I think It needs to have one more input argument: My_DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J, void* my_context). But DMDASetGetMatrix() will not accept pointer of this function. Please give a suggestion to overcome this.<br>
> ><br>
> > Thank you.<br>
> ><br>
> > Pham<br>
> ><br>
> ><br>
> > On Fri, Sep 4, 2015 at 1:24 AM, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
> ><br>
> > Look at DMCreateMatrix_DA_2d_MPIAIJ (or the 3d version if working in 3d) You need to copy this routine and add whatever additional preallocation information you need. Then call DMDASetGetMatrix() so that the DM will use your routine to create the matrix for you.<br>
> ><br>
> > Barry<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > > On Sep 3, 2015, at 11:28 AM, Sang pham van <<a href="mailto:pvsang002@gmail.com">pvsang002@gmail.com</a>> wrote:<br>
> > ><br>
> > > Hi,<br>
> > ><br>
> > > I am using DMCreateMatrix to create matrix from a existed DM object and defined stencil.<br>
> > > In my code, boundary nodes need to involve many inner nodes, thus matrix rows corresponding to boundary nodes are almost dense. How can I tell petsc that those rows need to be preallocated with more entries? I don't want to use MatMPIAIJSetPreallocation() since advantages of DM might be lost.<br>
> > ><br>
> > > Many thanks.<br>
> > ><br>
> > > Sam.<br>
> > ><br>
> > ><br>
> ><br>
> ><br>
> ><br>
> ><br>
><br>
><br>
><br>
><br>
><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>
><br>
<br>
</div></div></blockquote></div><br></div>