[petsc-users] From CSR format to MatSetValues

Matthew Knepley knepley at gmail.com
Fri Jun 8 07:20:22 CDT 2012


On Fri, Jun 8, 2012 at 2:31 PM, w_ang_temp <w_ang_temp at 163.com> wrote:

> 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.
>

Almost certainly, this is not your problem. If anything is slow, its a
problem with preallocation.


>     So is there a better way to do this?
>

You can go directly to the data structure in serial, but then you give up
on parallelism.

    Matt


>     Thank you.
>                                       Jim
>
>
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120608/08c971a5/attachment.html>


More information about the petsc-users mailing list