<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Menlo;
panose-1:2 11 6 9 3 8 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:10.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.gmailsignatureprefix
{mso-style-name:gmail_signature_prefix;}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
p.p1, li.p1, div.p1
{mso-style-name:p1;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:#5D6C79;}
p.p2, li.p2, div.p2
{mso-style-name:p2;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:black;}
p.p3, li.p3, div.p3
{mso-style-name:p3;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:#3900A0;}
p.p4, li.p4, div.p4
{mso-style-name:p4;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:black;}
p.p5, li.p5, div.p5
{mso-style-name:p5;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:#6C36A9;}
p.p6, li.p6, div.p6
{mso-style-name:p6;
margin:0cm;
background:white;
font-size:8.5pt;
font-family:Menlo;
color:#643820;}
span.s2
{mso-style-name:s2;
color:#3900A0;}
span.s3
{mso-style-name:s3;
color:#5D6C79;}
span.s4
{mso-style-name:s4;
color:#1C00CF;}
span.s5
{mso-style-name:s5;
color:#6C36A9;}
span.s6
{mso-style-name:s6;
color:#643820;}
span.s7
{mso-style-name:s7;
color:#9B2393;}
span.s8
{mso-style-name:s8;
color:#C41A16;}
span.s1
{mso-style-name:s1;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Thank you, Matt. It took me a while but I understand how to work with submatrices.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">(Below is simple stand-alone code which assembles two submatrices into a matrix of 5 x 5, which might be of use to someone else)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Q1) Looks like I don’t need to call MatrixAssembleBegin/End of on submatrices?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Q2) Though I have preallocated A for 5 x 5 – why do I still need to call
<o:p></o:p></span></p>
<p class="p5"><span class="apple-converted-space"> </span>MatSetOption<span class="s1">(A,
</span>MAT_NEW_NONZERO_ALLOCATION_ERR<span class="s1">, </span>PETSC_FALSE<span class="s1">);</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"> else I get an error while assembling using MatSetValuesLocal()?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Q3) Am I calling MatRestoreLocalSubmatrix() at the right place and what does it do?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Q4) When assembling a large submatrix, can I call MatGetOwershipRange on the submatrix?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Best,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US">Karthik.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="p1"><span class="apple-converted-space"> </span> #include <petsc.h><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">Mat</span><span class="apple-converted-space">
</span>A;<span class="apple-converted-space"> </span><span class="s3">/* linear system matrix */</span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscInt</span>
<span class="apple-converted-space"> </span>i,j,m = <span class="s4">3</span>,n =
<span class="s4">3</span>, P = <span class="s4">5</span>, Q = <span class="s4">5</span>;<o:p></o:p></p>
<p class="p3"><span class="apple-converted-space"> </span>PetscErrorCode<span class="s1"> ierr;</span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscScalar</span><span class="apple-converted-space">
</span>v;<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// create matrix A<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>ierr = <span class="s5">
MatCreate</span>(<span class="s5">PETSC_COMM_WORLD</span>,&A);<span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>ierr = <span class="s5">
MatSetType</span>(A, <span class="s6">MATMPIAIJ</span>); <span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s1">ierr =
</span>MatSetSizes<span class="s1">(A,</span>PETSC_DECIDE<span class="s1">,</span>PETSC_DECIDE<span class="s1">,P,Q);</span><span class="s6">CHKERRQ</span><span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>ierr = <span class="s5">
MatSetFromOptions</span>(A);<span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>ierr = <span class="s5">
MatMPIAIJSetPreallocation</span>(A,<span class="s4">5</span>,<span class="s7"><b>NULL</b></span>,<span class="s4">5</span>,<span class="s7"><b>NULL</b></span>);<span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>//ierr = MatSeqAIJSetPreallocation(A,5,NULL);CHKERRQ(ierr);<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// local indices is always 0<span class="apple-converted-space">
</span>1<span class="apple-converted-space"> </span>2<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscInt</span><span class="apple-converted-space">
</span>c = <span class="s4">3</span>, global_col_indices[] = {<span class="s4">0</span>,
<span class="s4">1</span>, <span class="s4">2</span>};<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscInt</span><span class="apple-converted-space">
</span>r = <span class="s4">2</span>, global_row_indices[] = {<span class="s4">3</span>,
<span class="s4">4</span>};<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">ISLocalToGlobalMapping</span> col_mapping, row_mapping;<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span>ISLocalToGlobalMappingCreate<span class="s1">(</span>PETSC_COMM_WORLD<span class="s1">,
</span><span class="s4">1</span><span class="s1">, c, global_col_indices, </span>
PETSC_COPY_VALUES<span class="s1">, &col_mapping);</span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span>ISLocalToGlobalMappingCreate<span class="s1">(</span>PETSC_COMM_WORLD<span class="s1">,
</span><span class="s4">1</span><span class="s1">, r, global_row_indices, </span>
PETSC_COPY_VALUES<span class="s1">, &row_mapping);</span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetLocalToGlobalMapping</span>(A,row_mapping, col_mapping);<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s7"><b>for</b></span> (i=<span class="s4">0</span>; i<m; i++) {<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s7"><b>for</b></span> (j=<span class="s4">0</span>; j<n; j++) {<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>v = i*n + j + <span class="s4">
1</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">std</span>::<span class="s5">cout</span> <<
<span class="s8">"inserting value "</span> << v << <span class="s8">" into position "</span> << i <<
<span class="s8">", "</span> << j << <span class="s2">std</span>::<span class="s5">endl</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetValues</span>(A,
<span class="s4">1</span>, &i, <span class="s4">1</span>, &j, &v, <span class="s5">
INSERT_VALUES</span>); <span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>}<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>}<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// assemble matrix<o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s1">ierr =
</span>MatAssemblyBegin<span class="s1">(A,</span>MAT_FINAL_ASSEMBLY<span class="s1">);</span><span class="s6">CHKERRQ</span><span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s1">ierr =
</span>MatAssemblyEnd<span class="s1">(A,</span>MAT_FINAL_ASSEMBLY<span class="s1">);</span><span class="s6">CHKERRQ</span><span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p6"><span class="apple-converted-space"> </span><span class="s5">MatView</span><span class="s1">(A,</span>PETSC_VIEWER_STDOUT_WORLD<span class="s1">);</span>CHKERRQ<span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// Test MatGetLocalSubMatrix<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">Mat</span> SubA, SubB;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">IS</span> row_isx, col_isx;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscInt</span> row_isx_vals[<span class="s4">2</span>];<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>row_isx_vals[<span class="s4">0</span>] =
<span class="s4">0</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>row_isx_vals[<span class="s4">1</span>] =
<span class="s4">1</span>;<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscInt</span> col_isx_vals[<span class="s4">3</span>];<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>col_isx_vals[<span class="s4">0</span>] =
<span class="s4">0</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>col_isx_vals[<span class="s4">1</span>] =
<span class="s4">1</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>col_isx_vals[<span class="s4">2</span>] =
<span class="s4">2</span>;<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s6">CHKERRQ</span><span class="s1">(</span>ISCreateGeneral<span class="s1">(</span>PETSC_COMM_WORLD<span class="s1">,
</span><span class="s4">3</span><span class="s1">, col_isx_vals, </span>PETSC_COPY_VALUES<span class="s1">, &col_isx));</span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s6">CHKERRQ</span><span class="s1">(</span>ISCreateGeneral<span class="s1">(</span>PETSC_COMM_WORLD<span class="s1">,
</span><span class="s4">2</span><span class="s1">, row_isx_vals, </span>PETSC_COPY_VALUES<span class="s1">, &row_isx));</span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s6">CHKERRQ</span>(<span class="s5">MatGetLocalSubMatrix</span>(A, row_isx, col_isx, &SubA));<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// MatSetLocalToGlobalMapping(A,row_mapping, row_mapping);<o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span>MatSetOption<span class="s1">(A,
</span>MAT_NEW_NONZERO_ALLOCATION_ERR<span class="s1">, </span>PETSC_FALSE<span class="s1">);</span><o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s7"><b>int</b></span> irow =
<span class="s4">0</span>, icol = <span class="s4">0</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s2">PetscScalar</span>
<span class="apple-converted-space"> </span>val = <span class="s4">10</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetValuesLocal</span>(SubA,<span class="s4">1</span>,&irow,<span class="s4">1</span>,&icol,&val,<span class="s5">INSERT_VALUES</span>);<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>//<span class="apple-converted-space">
</span>ierr = MatAssemblyBegin(SubA,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>//<span class="apple-converted-space">
</span>ierr = MatAssemblyEnd(SubA,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetLocalToGlobalMapping</span>(A,col_mapping, row_mapping);<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s6">CHKERRQ</span>(<span class="s5">MatGetLocalSubMatrix</span>(A, col_isx, row_isx, &SubB));<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>irow = <span class="s4">
0</span>, icol = <span class="s4">0</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetValuesLocal</span>(SubB,<span class="s4">1</span>,&irow,<span class="s4">1</span>,&icol,&val,<span class="s5">INSERT_VALUES</span>);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>irow = <span class="s4">
2</span>, icol = <span class="s4">0</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetValuesLocal</span>(SubB,<span class="s4">1</span>,&irow,<span class="s4">1</span>,&icol,&val,<span class="s5">INSERT_VALUES</span>);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>irow = <span class="s4">
2</span>, icol = <span class="s4">1</span>;<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatSetValuesLocal</span>(SubB,<span class="s4">1</span>,&irow,<span class="s4">1</span>,&icol,&val,<span class="s5">INSERT_VALUES</span>);<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// ierr = MatAssemblyBegin(SubB,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// ierr = MatAssemblyEnd(SubB,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s1">ierr =
</span>MatAssemblyBegin<span class="s1">(A,</span>MAT_FINAL_ASSEMBLY<span class="s1">);</span><span class="s6">CHKERRQ</span><span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p5"><span class="apple-converted-space"> </span><span class="s1">ierr =
</span>MatAssemblyEnd<span class="s1">(A,</span>MAT_FINAL_ASSEMBLY<span class="s1">);</span><span class="s6">CHKERRQ</span><span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p6"><span class="apple-converted-space"> </span><span class="s5">MatView</span><span class="s1">(A,</span>PETSC_VIEWER_STDOUT_WORLD<span class="s1">);</span>CHKERRQ<span class="s1">(ierr);</span><o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p4"><span class="apple-converted-space"> </span><o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatRestoreLocalSubMatrix</span>(A, row_isx, col_isx, &SubA);<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span><span class="s5">MatRestoreLocalSubMatrix</span>(A, col_isx, row_isx, &SubB);<o:p></o:p></p>
<p class="p4"><o:p> </o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>// cleanup<o:p></o:p></p>
<p class="p2"><span class="apple-converted-space"> </span>ierr = <span class="s5">
MatDestroy</span>(&A); <span class="s6">CHKERRQ</span>(ierr);<o:p></o:p></p>
<p class="p1"><span class="apple-converted-space"> </span>ierr = PetscFinalize();<o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">Matthew Knepley <knepley@gmail.com><br>
<b>Date: </b>Thursday, 20 April 2023 at 11:37<br>
<b>To: </b>Chockalingam, Karthikeyan (STFC,DL,HC) <karthikeyan.chockalingam@stfc.ac.uk><br>
<b>Cc: </b>petsc-users@mcs.anl.gov <petsc-users@mcs.anl.gov><br>
<b>Subject: </b>Re: [petsc-users] question about MatSetLocalToGlobalMapping<o:p></o:p></span></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">On Thu, Apr 20, 2023 at 6:13 AM Karthikeyan Chockalingam - STFC UKRI via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:<o:p></o:p></span></p>
</div>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">Hello,<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">I created a new thread, thought would it be more appropriate (and is a continuation of my previous post). I want to construct the below K matrix (which
is composed of submatrices) <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">K = [A P^T</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> P 0]</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Where K is of type MatMPIAIJ. I first constructed the top left [A] using MatSetValues().</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Now, I would like to construct the bottom left [p] and top right [p^T]
</span><span style="font-size:11.0pt">using MatSetValuesLocal().<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">To use MatSetValuesLocal(), I first have to create a local-to-global mapping using ISLocalToGlobalMappingCreate. I have created two mapping row_mapping
and column_mapping.<o:p></o:p></span></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">I do not understand why they are not the same map. Maybe I was unclear before. It looks like you have two fields, say phi and lambda, where lambda is a Lagrange multiplier imposing some constraint. Then you
get a saddle point like this. You can imagine matrices<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> (phi, phi) --> A<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> (phi, lambda) --> P^T<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> (lambda, phi) --> P<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">So you make a L2G map for the phi field and the lambda field. Oh, you are calling them row and col map, but they are my phi and lambda<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">maps. I do not like the row and col names since in P they reverse.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Q1) At what point should I declare
</span><span style="font-size:11.0pt">MatSetLocalToGlobalMapping – is it just before I use MatSetValuesLocal()?<o:p></o:p></span></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">Okay, it is good you are asking this because my thinking was somewhat confused. I think the precise steps are:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> 1) Create the large saddle point matrix K<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> 1a) We must call <a href="https://petsc.org/main/manualpages/Mat/MatSetLocalToGlobalMapping/">https://petsc.org/main/manualpages/Mat/MatSetLocalToGlobalMapping/</a> on it. In the simplest case, this just
maps<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> the local rows numbers [0, Nrows) to the global rows numbers [rowStart, rowStart + Nrows).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> 2) To form each piece:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> 2a) Extract that block using <a href="https://petsc.org/main/manualpages/Mat/MatGetLocalSubMatrix/">https://petsc.org/main/manualpages/Mat/MatGetLocalSubMatrix/</a><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> This gives back a Mat object that you subsequently restore using <a href="https://petsc.org/main/manualpages/Mat/MatRestoreLocalSubMatrix/">https://petsc.org/main/manualpages/Mat/MatRestoreLocalSubMatrix/</a><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> 2b) Insert values using <a href="https://petsc.org/main/manualpages/Mat/MatSetValuesLocal/">
https://petsc.org/main/manualpages/Mat/MatSetValuesLocal/</a><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> The local indices used for insertion here are indices relative to the block itself, and the L2G map for this matrix<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> has been rewritten to insert into that block in the larger matrix. Thus this looks like just calling MatSetValuesLocal()<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> on the smaller matrix block, but inserts correctly into the larger matrix.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">Therefore, the code you write code in 2) could work equally well making the large matrix from 1), or independent smaller matrix blocks.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">Does this make sense?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> Thanks,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> Matt<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">I will use MatSetLocalToGlobalMapping(K, row_mapping, column_mapping) to build
<span style="color:black">the bottom left [P].</span><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Q2) Can now I reset the mapping as
</span><span style="font-size:11.0pt">MatSetLocalToGlobalMapping(K, column_mapping, row_mapping) to build
<span style="color:black">the top right [P^T]? </span><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">Many thanks!<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Kind regards,</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black">Karthik.</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:black"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:#212121"> </span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> <o:p></o:p></span></p>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt"><br clear="all">
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<p class="MsoNormal"><span class="gmailsignatureprefix"><span style="font-size:11.0pt">--
</span></span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">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<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt"><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>