<div dir="auto"><div>Cc'ing petsc-dev<br><div class="gmail_quote">---------- Messaggio inoltrato ----------<br>Da: "Stefano Zampini" <<a href="mailto:stefano.zampini@gmail.com">stefano.zampini@gmail.com</a>><br>Data: 12 Gen 2018 9:32 PM<br>Oggetto: Re: MatDestroySubMatrices_SeqAIJ and reference counting<br>A: "Hong" <<a href="mailto:hzhang@mcs.anl.gov">hzhang@mcs.anl.gov</a>><br>Cc: <br><br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div class="quoted-text"><blockquote type="cite"><div>Sorry, I do not understand your question. Where comes matrix B?</div></blockquote><div><br></div></div><div>It’s not important where matrix B comes from. The error is reproducible by just increasing the reference count of any of the submats. See below the patch for ex4.c</div><div class="quoted-text"><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Can you give me an example of this error?</div></div></div></div></blockquote><div><br></div></div>here you have<br><div><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><b>diff --git a/src/mat/examples/tests/ex4.c b/src/mat/examples/tests/ex4.c</b></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><b>index 0555a54d9d..1d1961e4fa 100644</b></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><b>--- a/src/mat/examples/tests/ex4.c</b></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><b>+++ b/src/mat/examples/tests/ex4.c</b></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures;color:#33bbc8">@@ -68,7 +68,9 @@</span><span style="font-variant-ligatures:no-common-ligatures"> int main(int argc,char **argv)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   ierr = MatView(submat,sviewer);<wbr>CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   ierr = PetscViewerRestoreSubViewer(<wbr>PETSC_VIEWER_STDOUT_WORLD,<wbr>PETSC_COMM_SELF,&sviewer);<wbr>CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   ierr = PetscViewerFlush(PETSC_VIEWER_<wbr>STDOUT_WORLD);CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38);background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">+  ierr = PetscObjectReference((<wbr>PetscObject)submat);CHKERRQ(<wbr>ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   ierr = MatDestroySubMatrices(1,&<wbr>submatrices);CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38);background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">+  ierr = MatDestroy(&submat);CHKERRQ(<wbr>ierr);</span></div><p style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:16px"><span style="font-variant-ligatures:no-common-ligatures"> </span><br class="m_326818260560541686webkit-block-placeholder"></p><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   /* Form submatrix with rows 2-4 and all columns */</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">   ierr   = ISDestroy(&icol);CHKERRQ(ierr)<wbr>;</span></div><div class="elided-text"><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Hong</div><div class="gmail_quote"><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hong,<div><br></div><div>can you explain what’s the rationale behind calling explicitly the Layout and header destroy for the submit case in the loop here <a href="https://bitbucket.org/petsc/petsc/src/ac3af1b492556bac9856a6aee1c73992bd0b1779/src/mat/impls/aij/seq/aij.c?at=master&fileviewer=file-view-default#aij.c-2531" target="_blank">https://bitbucket.org/pet<wbr>sc/petsc/src/ac3af1b492556bac9<wbr>856a6aee1c73992bd0b1779/src/<wbr>mat/impls/aij/seq/aij.c?at=<wbr>master&fileviewer=file-view-<wbr>default#aij.c-2531</a></div><div><br></div><div>A code like this fails since you don’t take into account reference counting on the submatrices.</div><div><br></div><div>MatCreateSubMatrices(A,….&subm<wbr>ats)</div><div>PetscObjectCompose(B,”_XXXX”,s<wbr>ubmats[0);</div><div>…..</div><div>MatDestroySubMatrices(..,subma<wbr>ts);</div><div>MatDestroy(B); //Error, corrupt argument when trying to destroy composed objects</div><div><br></div><div>Here is a representative valgrind stack trace</div><div><br></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133== Invalid read of size 4</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    at 0x100368F63: PetscCheckPointer (/Users/szampini/software/pets<wbr>c/src/sys/error/checkptr.c:<wbr>108)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1001ACA9F: PetscObjectDereference (/Users/szampini/software/pets<wbr>c/src/sys/objects/inherit.c:<wbr>564)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10019AC07: PetscObjectListDestroy (/Users/szampini/software/pets<wbr>c/src/sys/objects/olist.c:154)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1001A8176: PetscHeaderDestroy_Private (/Users/szampini/software/pets<wbr>c/src/sys/objects/inherit.c:<wbr>115)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100626CAD: MatDestroy (/Users/szampini/software/pets<wbr>c/src/mat/interface/matrix.c:<wbr>1237)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">==90133==  Address 0x106c4c320 is 1,632 bytes inside a block of size 4,420 free'd</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    at 0x10014C9F3: free (in /usr/local/Cellar/valgrind/3.1<wbr>3.0/lib/valgrind/vgpreload_mem<wbr>check-amd64-darwin.so)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100229B7A: PetscFreeAlign (/Users/szampini/software/pets<wbr>c/src/sys/memory/mal.c:88)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10022D771: PetscTrFreeDefault (/Users/szampini/software/pets<wbr>c/src/sys/memory/mtr.c:309)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100A7034D: MatDestroySubMatrices_SeqAIJ (/Users/szampini/software/pets<wbr>c/src/mat/impls/aij/seq/aij.c:<wbr>2549)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1006260BD: MatDestroySubMatrices (/Users/szampini/software/pets<wbr>c/src/mat/interface/matrix.c:<wbr>6957)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1006D82EC: _DestroyContainer</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1001AFA31: PetscContainerDestroy (/Users/szampini/software/pets<wbr>c/src/sys/objects/inherit.c:<wbr>899)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1001ACBE5: PetscObjectDereference (/Users/szampini/software/pets<wbr>c/src/sys/objects/inherit.c:<wbr>566)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10019AC07: PetscObjectListDestroy (/Users/szampini/software/pets<wbr>c/src/sys/objects/olist.c:154)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1001A8176: PetscHeaderDestroy_Private (/Users/szampini/software/pets<wbr>c/src/sys/objects/inherit.c:<wbr>115)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100626CAD: MatDestroy (/Users/szampini/software/pets<wbr>c/src/mat/interface/matrix.c:<wbr>1237)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">==90133==  Block was alloc'd at</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    at 0x10014C616: malloc (in /usr/local/Cellar/valgrind/3.1<wbr>3.0/lib/valgrind/vgpreload_mem<wbr>check-amd64-darwin.so)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x1002299AC: PetscMallocAlign (/Users/szampini/software/pets<wbr>c/src/sys/memory/mal.c:48)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10022CA73: PetscTrMallocDefault (/Users/szampini/software/pets<wbr>c/src/sys/memory/mtr.c:183)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10022B328: PetscMallocA (/Users/szampini/software/pets<wbr>c/src/sys/memory/mal.c:396)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100C12E05: MatCreate (/Users/szampini/software/pets<wbr>c/src/mat/utils/gcreate.c:89)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100B839F6: MatCreateSubMatrices_MPIAIJ_Lo<wbr>cal (/Users/szampini/software/pets<wbr>c/src/mat/impls/aij/mpi/mpiov.<wbr>c:2554)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x100B7DEE7: MatCreateSubMatrices_MPIAIJ (/Users/szampini/software/pets<wbr>c/src/mat/impls/aij/mpi/mpiov.<wbr>c:2038)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">==90133==    by 0x10066703A: MatCreateSubMatrices (/Users/szampini/software/pets<wbr>c/src/mat/interface/matrix.c:<wbr>6806)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><br></div></div></div></blockquote></div><br></div></div>
</div></blockquote></div></div><br></div></blockquote></div><br></div></div>