[petsc-users] How to create a local to global mapping and construct matrix correctly

Ji Zhang gotofd at gmail.com
Fri Sep 16 11:03:54 CDT 2016


Dear all,

I have a number of small 'mpidense' matrices mij, and I want to construct
them to a big 'mpidense' matrix M like this:

     [  m11  m12  m13  ]
M =  |  m21  m22  m23  |   ,
     [  m31  m32  m33  ]

And a short demo is below. I'm using python, but their grammar are similar.

import numpy as np
from petsc4py import PETSc
import sys, petsc4py


petsc4py.init(sys.argv)
mSizes = (2, 2)
mij = []

# create sub-matrices mij
for i in range(len(mSizes)):
    for j in range(len(mSizes)):
        temp_m = PETSc.Mat().create(comm=PETSc.COMM_WORLD)
        temp_m.setSizes(((None, mSizes[i]), (None, mSizes[j])))
        temp_m.setType('mpidense')
        temp_m.setFromOptions()
        temp_m.setUp()
        temp_m[:, :] = np.random.random_sample((mSizes[i], mSizes[j]))
        temp_m.assemble()
        temp_m.view()
        mij.append(temp_m)

# Now we have four sub-matrices. I would like to construct them into a
big matrix M.
M = PETSc.Mat().create(comm=PETSc.COMM_WORLD)
M.setSizes(((None, np.sum(mSizes)), (None, np.sum(mSizes))))
M.setType('mpidense')
M.setFromOptions()
M.setUp()
mLocations = np.insert(np.cumsum(mSizes), 0, 0)    # mLocations = [0, mSizes]
for i in range(len(mSizes)):
    for j in range(len(mSizes)):
        temp_m = mij[i*len(mSizes)+j].getDenseArray()
        for k in range(temp_m.shape[0]):
            M.setValues(mLocations[i]+k,
np.arange(mLocations[j],mLocations[j+1],dtype='int32'), temp_m[k, :])
M.assemble()
M.view()

The code works well in a single cup, but give wrong answer for 2 and more
cores.

Thanks.
2016-09-17
Best,
Regards,
Zhang Ji
Beijing Computational Science Research Center
E-mail: gotofd at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20160917/42e8c23f/attachment.html>


More information about the petsc-users mailing list