Assembling a matrix
Barry Smith
bsmith at mcs.anl.gov
Fri Dec 18 18:12:44 CST 2009
The issue is that the data structures that PETSc uses to store the
sparse matrices are not designed to allow changing the matrix size,
thus if you have the matrix A as a PETSc sparse matrix creating B
means copying the entire matrix, thus two copies of the matrix, more
memory usage. Now if memory is not an issue for you, the copying can
be done efficiently order nz work. I can send you code for the
sequential case.
If you don't want two copies of the matrix for memory reasons,
could you "lie" to the part of the code that generates A and have it
create the A with an extra row and column that it simply does not use?
So in the MatSetSizes() you just set it 1 larger then it is. Then call
the MatSetValues() to fill up A then call the MatSetValues() to put in
the v and c part without calling MatAssemblyBegin/End() in between and
you will have an efficient code in both time and memory usage.
Barry
On Dec 18, 2009, at 5:59 PM, Douglas Arnold wrote:
> Anirban Chatterjee asked how to assemble an (n+1)x(n+1) sparse matrix
>
> B = [A v;
> v^T c]
>
> from an nxn sparse matrix A, a vector v, and a scalar c. Matt
> Knepley and Barry Smith advised against it.
>
> I have pretty much the same problem and I want to make sure I
> understand the situation. In my case, the matrix A comes to me
> from another application that I would rather not fiddle with.
> The rank of A is n-1 and v is a vector in its nullspace, c=0. I would
> like to solve a system with the matrix B by a direct solver, and
> so I think I need to assemble it. Do I understand correctly that
> there is no efficient way to do this in PETSc? Even in a sequential
> computation?
>
> -- Doug
>
>
>> Where will the "new" row live? On the last process, why that one?
>>
>> Does the matrix A have to be completely assembled before the v is
>> computed? Or can the entries of A be computed at the same time as
>> the v?
>>
>> PETSc doesn't have code to efficiently dynamically change the size
>> of a matrix. Thus generating a new matrix from a given sparse matrix
>> plus a new row requires a complete copy of the starting matrix into
>> the new matrix data structure. If you assemble the entire matrix
>> together without first generating the smaller matrix A then it is
>> easy; just generate the preallocation structure of A with the one
>> additional row/column information and call MatSetValues() to put in
>> the parts of A and the parts of v.
>>
>> Barry
>>
>> On Dec 8, 2009, at 4:05 PM, Anirban Chatterjee wrote:
>>
>>> Hi,
>>>
>>> I have an nxn matrix A, a column vector v and a scalar c. I want to
>>> assemble a matrix B as follows,
>>> B = [A v;
>>> v^T c]
>>> Can someone tell me the easiest and most efficient way to do this.
>>>
>>> --Anirban
More information about the petsc-users
mailing list