Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
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).

share|improve this answer

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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