[petsc-users] From CSR format to MatSetValues
w_ang_temp
w_ang_temp at 163.com
Fri Jun 8 01:31:21 CDT 2012
Hello,
When solving a linear system Ax=b, the first step is assigning values to
matrix A. In my program, the subroutine PETSCSOLVE, which is used to slove Ax=b
with PETSc, gets the CSR format matrix(values, columns, rowIndex) to set values
to PETSc Mat A.
The variables 'values'、'columns'、'rowIndex' belong to the main function.
They are used to represent a matrix in CSR format. The following table describes
the arrays in terms of the values, row, and column positions of the non-zero
elements in a sparse matrix.
values: A real or complex array that contains the non-zero elements of a
sparse matrix. The non-zero elements are mapped into the values
array using the row-major upper triangular storage mapping described
above.
columns: Element i of the integer array columns is the number of the column
that contains the i-th element in the values array.
rowIndex: Element j of the integer array rowIndex gives the index of the
element in the values array that is first non-zero element in a row j.
codes:
---------Set Values to A From CSR Format(values,rowIndex,columns)-------------------
!values:Non-Symmetric Matrix
ione = 1
do 10,II=Istart+1,Iend
!non-zero numbers of this row
rowNum=rowIndex(II+1)-rowIndex(II)
do 11,JJ=1,rowNum
!elemnt index of the values/columns
kValNn=rowIndex(II)+JJ-1
!column index of this element
nCol=columns(kValNn)-1
!Setdata:II-RowIndex,nCol-ColIndex
nRow=II-1
call MatSetValues(A,ione,nRow,ione,nCol,values(kValNn),INSERT_VALUES,ierr)
11 continue
10 continue
--------------------------------------------------------------------------------------
As we can see, it has to loop a number of times because it can only set one data per
time. And this leads to low efficiency.
So is there a better way to do this?
Thank you.
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120608/c3d62f2b/attachment-0001.html>
More information about the petsc-users
mailing list