<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=Windows-1252">
<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:Aptos;
panose-1:2 11 0 4 2 2 2 2 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:11.0pt;
font-family:"Aptos",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#467886;
text-decoration:underline;}
p.p1, li.p1, div.p1
{mso-style-name:p1;
margin:0cm;
background:#FCF4DC;
font-size:13.5pt;
font-family:Menlo;
color:#536870;}
p.p2, li.p2, div.p2
{mso-style-name:p2;
margin:0cm;
background:#FCF4DC;
font-size:13.5pt;
font-family:Menlo;
color:#C20CC3;}
span.s1
{mso-style-name:s1;
color:#C20CC3;}
span.s3
{mso-style-name:s3;
background:#E0E409;}
span.s4
{mso-style-name:s4;
color:#BA6018;}
span.s5
{mso-style-name:s5;
color:#536870;}
span.s2
{mso-style-name:s2;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:11.0pt;
mso-ligatures:none;
mso-fareast-language:EN-US;}
@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="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal" style="margin-bottom:12.0pt">Hi,<br>
I am trying to view a matrix object when running a job in parallel. The application is BOUT++ (<a href="https://urldefense.us/v3/__https://boutproject.github.io/__;!!G_uCfscf7eWS!Z-f_HrmUaFAwAK4e2pBspL2oRFtfq9O3GtRCoS_Nb1e9y5j2Vjq6THJTnoot7XiMeRAxlNhskW0QrMV7x_v_VsBXVAsGHRrIifxc$">https://boutproject.github.io/</a>) which uses petsc as solver.
<br>
While the option -mat_view gives a jumbled output on STDOUT from across the processors, redirecting output to a specific file results in race condition. The only option that works for me is
<i>-mat_view binary</i> with <i>-viewer_binary_filename <name></i><b>. </b>I am processing this binary file with petsc4py as follows:<b><o:p></o:p></b></p>
<p class="p1"><span class="s1">from</span><span class="s2"> </span><span class="s3">petsc</span><span class="s2">4py
</span><span class="s1">import</span><span class="s2"> PETSc</span></p>
<p class="p1"><span class="s1">import</span><span class="s2"> numpy </span><span class="s4">as</span><span class="s2"> np</span></p>
<p class="p1"><span class="s1">import</span><span class="s2"> matplotlib.pyplot </span>
<span class="s4">as</span><span class="s2"> plt</span></p>
<p class="p2"><span class="s2">import</span><span class="s5"> glob<o:p></o:p></span></p>
<p class="p2"><span class="s5"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB">def</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">read_binary</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(filename):</span><span style="font-size:13.5pt;font-family:Menlo;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> viewer = PETSc.Viewer().createMPIIO(filename,
</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">"r"</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">, comm=PETSc.COMM_WORLD)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> A = PETSc.Mat().load(viewer)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> A.assemble()<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">print</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">"Matrix
type:"</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">, A.getType())<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">print</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">"Matrix
size:"</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">, A.getSize())<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> info = A.getInfo()<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">print</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">"Number
of nonzeros:"</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">, info[</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">'nz_used'</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">])</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">print</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">"Nonzeros
allocated:"</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">, info[</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">'nz_allocated'</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">])</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="p1"> m, n = A.getSize()<o:p></o:p></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> dense_mat = np.zeros((m, n), dtype=np.float64)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> </span><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB">
for</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> i
</span><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB">in</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">range</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(</span><span style="font-size:13.5pt;font-family:Menlo;color:#AF2017;mso-ligatures:none;mso-fareast-language:EN-GB">m</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">):</span><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> cols, vals = A.getRow(i)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> dense_mat[i, cols] = vals<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
<p class="p1"> rstart, rend = A.getOwnershipRange()<o:p></o:p></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> rows, cols = [], []<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB">for</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> i
</span><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB">in</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">range</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(rstart, rend):<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> cols_i, _ = A.getRow(i)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> rows.extend([i] *
</span><span style="font-size:13.5pt;font-family:Menlo;color:#27A7B5;mso-ligatures:none;mso-fareast-language:EN-GB">len</span><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">(cols_i))<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> cols.extend(cols_i)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> rows = np.array(rows)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> cols = np.array(cols)<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB"> return dense_mat, rows, cols<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#BA6018;mso-ligatures:none;mso-fareast-language:EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:#FCF4DC"><span style="font-size:13.5pt;font-family:Menlo;color:#536870;mso-ligatures:none;mso-fareast-language:EN-GB">A, rows, cols = read_binary(“matrix”)<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This yields accurate number of non-zeros and matrix sparsity. However, the dense matrix turns out to be a null matrix. I wonder what is going wrong here.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thank you, best regards</p>
<p class="MsoNormal">Arun Pillai</p>
</div>
</body>
</html>