4
$\begingroup$

I created a binary matrix using the following loop in c

char filename[6];
filename=sprintf(filename,"file01");
moutput=fopen(filename,"wb+");
for (i=0; i<2*m+1; i++){
      for (j=0; j<2*n+1; j++) {
            r=rmin+(rmax-rmin)*GetRand(N);
            fwrite(&r, sizeof(double), 1, moutput);
      }
}
fclose(moutput);

where r is a uniformly distributed pseudorandom double precision number between $rmin$ and $rmax$. I verified that it wrote the values correctly.

So, I am using this tutorial as a guideline to load "file01" as a binary matrix. I ran the following script:

make ex1
./ex1 -f0 file01

However, it does not seem to work. I get an error saying:

[0]PETSC ERROR: Unexpected data in file!
[0]PETSC ERROR: not matrix object in file!

Is the problem that I wrote the binary file initially or that I'm using the wrong example to load this particular binary file?

$\endgroup$
5
  • $\begingroup$ Out of curiosity, since none of our resident PETSc experts have weighed in, have you tried cross-posting to a PETSc mailing list to see what their "official answer" is? I'm a little surprised that this is a problem, speaking as someone who hasn't touched PETSc (yet). $\endgroup$ Commented Feb 15, 2012 at 17:51
  • $\begingroup$ @GeoffOxberry: I think I will consult the developers of PETSc, and perhaps they can provide better insight. $\endgroup$
    – Paul
    Commented Feb 15, 2012 at 18:35
  • $\begingroup$ For the record, I think your question is fine; like I said, I'm just surprised that this format issue is even a problem. $\endgroup$ Commented Feb 15, 2012 at 20:25
  • $\begingroup$ @GeoffOxberry: I got this response from Matt Knepley: You are missing the leading integer identifying it as a matrix. See the code for the full format petsc.cs.iit.edu/petsc/petsc-dev/annotate/d927f8909be6/src/mat/… ... $\endgroup$
    – Paul
    Commented Feb 15, 2012 at 21:45
  • $\begingroup$ Thanks! I can talk to Matt about posting something if he has time. If he doesn't post something in the next couple of days, could you post his response under your own name (properly attributing him, of course)? $\endgroup$ Commented Feb 15, 2012 at 22:03

3 Answers 3

5
$\begingroup$

Okay, the full format of the binary file is here, and reduces to:

  • int id: MAT_FILE_CLASSID
  • int nr: number of rows
  • int nc: number of columns
  • int nnz: total number of nonzeros
  • int[nr] nz: number nonzeros in each row
  • int[nnz] j: column indices of all nonzeros (starting index is zero)
  • PetscScalar[nnz] a: values of all nonzeros

You can also use bin/matlab/PetscBinaryWrite.m to write a Matlab matrix to PETSc binary format.

$\endgroup$
1
  • $\begingroup$ To summarize, when I write a binary file that will be opened by PETSc's MatLoad function, I should write each of these integer values in the order specified above. Then I can write the actual non-zero values of each row should be written in double format. Is this correct? $\endgroup$
    – Paul
    Commented Mar 4, 2012 at 15:19
4
$\begingroup$

I had the same problem before. My solution is quite simple. Just use the I/O functions from your language, e.g. fopen, fseek, fread, fclose in C, for each processor. I assume that you enclose your codes by PetscInitialize()/PetscFinalize() and run the executable by mpirun. So before I/O in the codes you would first decide the locations of the data that each processor would read (for example, from file size and the rank of this processor). You might assemble Vec's to save your data into petsc format. But I found VecLoad does not allow one to decide local (processor) sizes. I also tried MPI I/O functions but failed to get correct values (maybe due to data types).

$\endgroup$
1
$\begingroup$

I've never used PETSc before, but it seems to me that you're just writing the binary file in your own format, rather than what PETSc expects.

Here's an example of generating a matrix, writing it to a binary file using PETSc, and reading the matrix back in: http://www.mcs.anl.gov/petsc/petsc-current/src/mat/examples/tests/ex31.c.html

$\endgroup$
2
  • $\begingroup$ You're probably right, James... I can't seem to find any information about how the binary file should be formatted in PETSc $\endgroup$
    – Paul
    Commented Feb 12, 2012 at 21:45
  • $\begingroup$ The example provided above creates a matrix in parallel with P processors and then writes the matrix in binary format. I am curious to know whether it will be read correctly next time with lets say P+3 processors. $\endgroup$
    – user1007
    Commented Feb 19, 2012 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.