# How do I read a binary file into PETSc as a matrix?

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?

• 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). – Geoff Oxberry Feb 15 '12 at 17:51
• @GeoffOxberry: I think I will consult the developers of PETSc, and perhaps they can provide better insight. – Paul Feb 15 '12 at 18:35
• For the record, I think your question is fine; like I said, I'm just surprised that this format issue is even a problem. – Geoff Oxberry Feb 15 '12 at 20:25
• @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/… ... – Paul Feb 15 '12 at 21:45
• 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)? – Geoff Oxberry Feb 15 '12 at 22:03

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.

• 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? – Paul Mar 4 '12 at 15:19

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

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

• You're probably right, James... I can't seem to find any information about how the binary file should be formatted in PETSc – Paul Feb 12 '12 at 21:45
• 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. – user1007 Feb 19 '12 at 7:34