<div dir="ltr"><div><div>Dear all, <br><br></div><span class="gmail-Apple-converted-space"></span>I have a number of small 'mpidense' matrices mij, and I want to construct them to a big 'mpidense' matrix M like this:<br><pre style="margin:0px 0px 1em;padding:5px;border-width:0px;border-style:none;border-color:-moz-use-text-color;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;word-wrap:normal;color:rgb(36,39,41);font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;background-color:rgb(239,240,241)"><code style="margin:0px;padding:0px;border-width:0px;border-style:none;border-color:-moz-use-text-color;font-size:13px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit;background-color:rgb(239,240,241)"> [ m11 m12 m13 ]
M = | m21 m22 m23 | ,
[ m31 m32 m33 ]<br></code></pre>And a short demo is below. I'm using python, but their grammar are similar. <br><pre style="background-color:rgb(248,248,255);color:rgb(0,0,0);font-family:"dejavu sans mono";font-size:9.1pt"><span style="font-weight:bold">import </span>numpy <span style="font-weight:bold">as </span>np<br><span style="font-weight:bold">from </span>petsc4py <span style="font-weight:bold">import </span>PETSc<br><span style="font-weight:bold">import </span>sys, petsc4py<br><br><br>petsc4py.init(sys.argv)<br>mSizes <span style="font-weight:bold">= </span>(<span style="color:rgb(0,153,153)">2</span>, <span style="color:rgb(0,153,153)">2</span>)<br>mij <span style="font-weight:bold">= </span>[]<br><br><span style="color:rgb(153,153,136);font-style:italic"># create sub-matrices mij<br></span><span style="font-weight:bold">for </span>i <span style="font-weight:bold">in </span><span style="color:rgb(0,134,179)">range</span>(<span style="color:rgb(0,134,179)">len</span>(mSizes))<span style="font-weight:bold">:<br></span><span style="font-weight:bold"> for </span>j <span style="font-weight:bold">in </span><span style="color:rgb(0,134,179)">range</span>(<span style="color:rgb(0,134,179)">len</span>(mSizes))<span style="font-weight:bold">:<br></span><span style="font-weight:bold"> </span>temp_m <span style="font-weight:bold">= </span>PETSc.Mat().create(<span style="color:rgb(102,0,153)">comm</span><span style="font-weight:bold">=</span>PETSc.COMM_WORLD)<br> temp_m.setSizes(((<span style="font-weight:bold">None</span>, mSizes[i]), (<span style="font-weight:bold">None</span>, mSizes[j])))<br> temp_m.setType(<span style="color:rgb(0,128,128);font-weight:bold">'mpidense'</span>)<br> temp_m.setFromOptions()<br> temp_m.setUp()<br> temp_m[<span style="font-weight:bold">:</span>, <span style="font-weight:bold">:</span>] <span style="font-weight:bold">= </span>np.random.random_sample((mSizes[i], mSizes[j]))<br> temp_m.assemble()<br> temp_m.view()<br> mij.append(temp_m)<br><br><span style="color:rgb(153,153,136);font-style:italic"># Now we have four sub-matrices. I would like to construct them into a big matrix M.<br></span>M <span style="font-weight:bold">= </span>PETSc.Mat().create(<span style="color:rgb(102,0,153)">comm</span><span style="font-weight:bold">=</span>PETSc.COMM_WORLD)<br>M.setSizes(((<span style="font-weight:bold">None</span>, np.sum(mSizes)), (<span style="font-weight:bold">None</span>, np.sum(mSizes))))<br>M.setType(<span style="color:rgb(0,128,128);font-weight:bold">'mpidense'</span>)<br>M.setFromOptions()<br>M.setUp()<br>mLocations <span style="font-weight:bold">= </span>np.insert(np.cumsum(mSizes), <span style="color:rgb(0,153,153)">0</span>, <span style="color:rgb(0,153,153)">0</span>) <span style="color:rgb(153,153,136);font-style:italic"># mLocations = [0, mSizes]<br></span><span style="font-weight:bold">for </span>i <span style="font-weight:bold">in </span><span style="color:rgb(0,134,179)">range</span>(<span style="color:rgb(0,134,179)">len</span>(mSizes))<span style="font-weight:bold">:<br></span><span style="font-weight:bold"> for </span>j <span style="font-weight:bold">in </span><span style="color:rgb(0,134,179)">range</span>(<span style="color:rgb(0,134,179)">len</span>(mSizes))<span style="font-weight:bold">:<br></span><span style="font-weight:bold"> </span>temp_m <span style="font-weight:bold">= </span>mij[i<span style="font-weight:bold">*</span><span style="color:rgb(0,134,179)">len</span>(mSizes)<span style="font-weight:bold">+</span>j].getDenseArray()<br> <span style="font-weight:bold">for </span>k <span style="font-weight:bold">in </span><span style="color:rgb(0,134,179)">range</span>(temp_m.shape[<span style="color:rgb(0,153,153)">0</span>])<span style="font-weight:bold">:<br></span><span style="font-weight:bold"> </span>M.setValues(mLocations[i]<span style="font-weight:bold">+</span>k, np.arange(mLocations[j],mLocations[j<span style="font-weight:bold">+</span><span style="color:rgb(0,153,153)">1</span>],<span style="color:rgb(102,0,153)">dtype</span><span style="font-weight:bold">=</span><span style="color:rgb(0,128,128);font-weight:bold">'int32'</span>), temp_m[k, <span style="font-weight:bold">:</span>])<br>M.assemble()<br>M.view()</pre>The code works well in a single cup, but give wrong answer for 2 and more cores. <br><br></div>Thanks. <br><div>2016-09-17</div><span><span><span><div>Best, </div><div>Regards,</div><div>Zhang Ji </div><div>Beijing Computational Science Research Center </div><div>E-mail: <a target="_blank" href="mailto:gotofd@gmail.com">gotofd@gmail.com</a><br><br><br></div></span></span></span></div>