<div class="gmail_quote">On Sun, Feb 13, 2011 at 23:37, Gaurish Telang <span dir="ltr">&lt;<a href="mailto:gaurish108@gmail.com">gaurish108@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div id=":3u">I have a text file containing the non-zero entries of a sparse matrix of dimension 2683x1274, stored in the form (row, column, element) i.e. [i, j, element] format. <br></div></blockquote><div><br></div><div>
This is a horrible format and can not scale. If it becomes a performance issue, change the format.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div id=":3u">
<br>That is ALL the information I have regarding the matrix. <br>
<br>However when pre-allocating memory with  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]),  the parameters nz and nnz need to be known,<br><br>nz=number of nonzeros per row (same for all rows)
<br>nnz=array containing the number of nonzeros in the various rows 
(possibly different for each row) or <a href="http://www.mcs.anl.gov/petsc/petsc-2/snapshots/petsc-dev/docs/manualpages/Sys/PETSC_NULL.html#PETSC_NULL" target="_blank">PETSC_NULL</a><br><br>which i do not know for my matrix, ( unless I resort to using MATLAB. ). <br>

<br>Does that mean I have to set nz= 1274 (the length of a row) and nnz=PETSC_NULL ?</div></blockquote><div><br></div><div>No, read the file twice. The first time through, just count the number of nonzeros (per row), then set preallocation with the correct size, and finally read the file a second time calling MatSetValue() for each entry.</div>
<div><br></div><div>For a matrix this small, you could just read it in without preallocating, but that will get too expensive quickly if you increase the matrix size.</div></div>