<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 14, 2018, at 7:16 AM, Hong <<a href="mailto:hzhang@mcs.anl.gov" class="">hzhang@mcs.anl.gov</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Stefano,</div><div class="">MatCreateSubMatrices() must be destroyed by MatDestroySubMatrices()</div><div class="">because all submatrices in the array share common internal data structure for reuse. </div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">+ ierr = PetscObjectReference((PetscObj<wbr class="">ect)submat);CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Only </span><span style="font-variant-ligatures:no-common-ligatures" class="">submat = </span><span style="color:rgb(80,0,80);font-variant-ligatures:no-common-ligatures" class="">subma</span><wbr style="color:rgb(80,0,80);font-variant-ligatures:no-common-ligatures" class=""><span style="color:rgb(80,0,80);font-variant-ligatures:no-common-ligatures" class="">trices[0] adds reference count.</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38)" class=""><br class=""></div><div style="color:rgb(80,0,80);margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = MatDestroySubMatrices(1,&subma<wbr class="">trices);CHKERRQ(ierr);</span></div><div style="color:rgb(80,0,80);margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Now, the internal common data structure is destroyed.</span></div><div style="color:rgb(80,0,80);margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(52,188,38)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">+ ierr = MatDestroy(&submat);CHKERRQ(ie<wbr class="">rr);</span></div></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">I guess submat becomes an orphan, it cannot be destroyed using previous ops->destroy routine.</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Shall we allow user to pick a single matrix out of submatrices array, and use/destroy it as a regular single matrix?</span></div><div class=""><br class=""></div></div></div></blockquote><div><br class=""></div><div>I accept the fact that the array of submatrices obtained by MatCreateSubMatrices shall be destroyed by MatDestroySubMatrices.</div><div>However, not being able to take reference on one of the submats is not properly PETSc philosophy :-), as the Mats returned do not behave like proper Mat objects</div><div>How difficult is to allow this? and what is the common data structure the submats share?</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">Hong<br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Jan 13, 2018 at 1:09 PM, Stefano Zampini <span dir="ltr" class=""><<a href="mailto:stefano.zampini@gmail.com" target="_blank" class="">stefano.zampini@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto" class=""><div class="">Cc'ing petsc-dev<br class=""><div class="gmail_quote">---------- Messaggio inoltrato ----------<br class="">Da: "Stefano Zampini" <<a href="mailto:stefano.zampini@gmail.com" target="_blank" class="">stefano.zampini@gmail.com</a>><br class="">Data: 12 Gen 2018 9:32 PM<br class="">Oggetto: Re: MatDestroySubMatrices_SeqAIJ and reference counting<br class="">A: "Hong" <<a href="mailto:hzhang@mcs.anl.gov" target="_blank" class="">hzhang@mcs.anl.gov</a>><br class="">Cc: <br class=""><div class=""><div class="gmail-h5"><br type="attribution" class=""><blockquote class="gmail-m_-5358193299216387954quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class=""><div class="gmail-m_-5358193299216387954quoted-text"><blockquote type="cite" class=""><div class="">Sorry, I do not understand your question. Where comes matrix B?</div></blockquote><div class=""><br class=""></div></div><div class="">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="gmail-m_-5358193299216387954quoted-text"><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">Can you give me an example of this error?</div></div></div></div></blockquote><div class=""><br class=""></div></div>here you have<br class=""><div class=""><br class=""></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><b class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><b class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><b class="">--- 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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><b class="">+++ 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)" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(51,187,200)" class="">@@ -68,7 +68,9 @@</span><span style="font-variant-ligatures:no-common-ligatures" class=""> 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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = MatView(submat,sviewer);CHKERR<wbr class="">Q(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = PetscViewerRestoreSubViewer(PE<wbr class="">TSC_VIEWER_STDOUT_WORLD,PETSC_<wbr class="">COMM_SELF,&sviewer);CHKERRQ(<wbr class="">ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = PetscViewerFlush(PETSC_VIEWER_<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">+ ierr = PetscObjectReference((PetscObj<wbr class="">ect)submat);CHKERRQ(ierr);</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = MatDestroySubMatrices(1,&subma<wbr class="">trices);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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">+ ierr = MatDestroy(&submat);CHKERRQ(ie<wbr class="">rr);</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 16px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="gmail-m_-5358193299216387954m_326818260560541686webkit-block-placeholder"></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> /* 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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> ierr = ISDestroy(&icol);CHKERRQ(ierr)<wbr class="">;</span></div><div class="gmail-m_-5358193299216387954elided-text"><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">Hong</div><div class="gmail_quote"><br class=""></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word" class="">Hong,<div class=""><br class=""></div><div class="">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" class="">https://bitbucket.org/pet<wbr class="">sc/petsc/src/ac3af1b492556bac9<wbr class="">856a6aee1c73992bd0b1779/src/ma<wbr class="">t/impls/aij/seq/aij.c?at=maste<wbr class="">r&fileviewer=file-view-default<wbr class="">#aij.c-2531</a></div><div class=""><br class=""></div><div class="">A code like this fails since you don’t take into account reference counting on the submatrices.</div><div class=""><br class=""></div><div class="">MatCreateSubMatrices(A,….&subm<wbr class="">ats)</div><div class="">PetscObjectCompose(B,”_XXXX”,s<wbr class="">ubmats[0);</div><div class="">…..</div><div class="">MatDestroySubMatrices(..,subma<wbr class="">ts);</div><div class="">MatDestroy(B); //Error, corrupt argument when trying to destroy composed objects</div><div class=""><br class=""></div><div class="">Here is a representative valgrind stack trace</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== at 0x100368F63: PetscCheckPointer (/Users/szampini/software/pets<wbr class="">c/src/sys/error/checkptr.c:108<wbr class="">)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1001ACA9F: PetscObjectDereference (/Users/szampini/software/pets<wbr class="">c/src/sys/objects/inherit.c:56<wbr class="">4)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10019AC07: PetscObjectListDestroy (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1001A8176: PetscHeaderDestroy_Private (/Users/szampini/software/pets<wbr class="">c/src/sys/objects/inherit.c:11<wbr class="">5)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100626CAD: MatDestroy (/Users/szampini/software/pets<wbr class="">c/src/mat/interface/matrix.c:1<wbr class="">237)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class="">==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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== at 0x10014C9F3: free (in /usr/local/Cellar/valgrind/3.1<wbr class="">3.0/lib/valgrind/vgpreload_mem<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100229B7A: PetscFreeAlign (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10022D771: PetscTrFreeDefault (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100A7034D: MatDestroySubMatrices_SeqAIJ (/Users/szampini/software/pets<wbr class="">c/src/mat/impls/aij/seq/aij.c:<wbr class="">2549)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1006260BD: MatDestroySubMatrices (/Users/szampini/software/pets<wbr class="">c/src/mat/interface/matrix.c:6<wbr class="">957)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1001AFA31: PetscContainerDestroy (/Users/szampini/software/pets<wbr class="">c/src/sys/objects/inherit.c:89<wbr class="">9)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1001ACBE5: PetscObjectDereference (/Users/szampini/software/pets<wbr class="">c/src/sys/objects/inherit.c:56<wbr class="">6)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10019AC07: PetscObjectListDestroy (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1001A8176: PetscHeaderDestroy_Private (/Users/szampini/software/pets<wbr class="">c/src/sys/objects/inherit.c:11<wbr class="">5)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100626CAD: MatDestroy (/Users/szampini/software/pets<wbr class="">c/src/mat/interface/matrix.c:1<wbr class="">237)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class="">==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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== at 0x10014C616: malloc (in /usr/local/Cellar/valgrind/3.1<wbr class="">3.0/lib/valgrind/vgpreload_mem<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x1002299AC: PetscMallocAlign (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10022CA73: PetscTrMallocDefault (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10022B328: PetscMallocA (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100C12E05: MatCreate (/Users/szampini/software/pets<wbr class="">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)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100B839F6: MatCreateSubMatrices_MPIAIJ_Lo<wbr class="">cal (/Users/szampini/software/pets<wbr class="">c/src/mat/impls/aij/mpi/mpiov.<wbr class="">c:2554)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x100B7DEE7: MatCreateSubMatrices_MPIAIJ (/Users/szampini/software/pets<wbr class="">c/src/mat/impls/aij/mpi/mpiov.<wbr class="">c:2038)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">==90133== by 0x10066703A: MatCreateSubMatrices (/Users/szampini/software/pets<wbr class="">c/src/mat/interface/matrix.c:6<wbr class="">806)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><br class=""></div></div></div></blockquote></div><br class=""></div></div>
</div></blockquote></div></div><br class=""></div></blockquote></div></div></div><br class=""></div></div>
</blockquote></div><br class=""></div></div></div>
</div></blockquote></div><br class=""></body></html>