[petsc-users] Reading in a matrix from ASCII file

Jed Brown jed at jedbrown.org
Thu Oct 5 17:21:17 CDT 2017


Please always use "reply-all" so that your messages go to the list.
This is standard mailing list etiquette.  It is important to preserve
threading for people who find this discussion later and so that we do
not waste our time re-answering the same questions that have already
been answered in private side-conversations.  You'll likely get an
answer faster that way too.


Storm Weiner <stormweiner at berkeley.edu> writes:

> Thanks,
>
> I decided to use one of the example matrices provided on the ftp to start
> with but now I have another issue.
> I tried to port /mat/ex1.c to f90 but when I do
>
>  CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,DataFile,fd,ierr)
>
> I get a bad file descriptor error.
>
> A simple open() close() check immediately before PetscViewerBinaryOpen()
> passes, so I know I'm using an appropriate file path.
>
> Furthermore, the unaltered ex1.c works fine with the file I'm attempting.
> Is there something special about petsc+f90 file descriptors, or did I mess
> up somewhere along the way?

No, but Fortran doesn't warn when you pass the wrong number of
parameters.  You need this.

diff --git i/ex1.F90 w/ex1.F90
index 049e6ae..311d6f0 100644
--- i/ex1.F90
+++ w/ex1.F90
@@ -25,7 +25,7 @@
       close(10)
        
       write(*,*) "before viewer create" 
-      CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,DataFile,fd,ierr);CHKERRA(ierr)
+      CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,DataFile,FILE_MODE_READ,fd,ierr);CHKERRA(ierr)
 
 
       !! Load the matrix; then destroy the viewer.


> The complete error message is:
>
>
> $ ./ex1 -f $PWD/testmat
>  before initialize
>  DataFile=
>  /usr/workspace/wsa/weiner6/MPI_sandbox/Minimal/testmat
>
>
>  before test open/close
>  before viewer create
> [0]PETSC ERROR:
> ------------------------------------------------------------------------
> [0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation,
> probably memory access out of range
> [0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
> [0]PETSC ERROR: or see
> http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind
> [0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X
> to find memory corruption errors
> [0]PETSC ERROR: likely location of problem given in stack below
> [0]PETSC ERROR: ---------------------  Stack Frames
> ------------------------------------
> [0]PETSC ERROR: Note: The EXACT line numbers in the stack are not available,
> [0]PETSC ERROR:       INSTEAD the line number of the start of the function
> [0]PETSC ERROR:       is given.
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Signal received
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html for
> trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.8.0, unknown
> [0]PETSC ERROR: ./ex1 on a arch-linux2-c-debug named borax2 by weiner6 Thu
> Oct  5 14:48:33 2017
> [0]PETSC ERROR: Configure options --with-scalar-type=complex
> --with-valgrind-dir=/usr/bin/
> [0]PETSC ERROR: #1 User provided function() line 0 in  unknown file
> application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
> [unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=59
> :
> system msg for write_line failure : Bad file descriptor
>
> -Storm
>
> P.S.  Should I be using reply-all to in these email threads?
>
> On Thu, Oct 5, 2017 at 2:10 PM, Jed Brown <jed at jedbrown.org> wrote:
>
>> http://www.mcs.anl.gov/petsc/documentation/faq.html#sparse-
>> matrix-ascii-format
>>
>> Better, read it using Python on MATLAB and write it (using the provided
>> PETSc scripts) in PETSc binary format.  Parallel IO with ASCII files is
>> a dead end.
>>
>> Storm Weiner <stormweiner at berkeley.edu> writes:
>>
>> > Hey there,
>> >
>> > I'm working through some basic examples, and I want to read in a test
>> > matrix I have stored in a (row, column, value) ASCII file.   How can I
>> read
>> > this in using PETSc?
>> >
>> >
>> > I found the routine PetscViewerASCIIOpen and PetscViewerASCIIRead  but
>> I'm
>> > not sure how to tell it that what I have is actually a matrix.  In this
>> > example, I know the matrix, so I can specify how many entries to read,
>> but
>> > is there a way to just read "the whole file"?
>> >
>> > What's the preferred PETSc way to do this?
>> >
>> > I'm using F90 by the way.
>> >
>> >
>> > -Storm
>>
>        Program Main
>        use PETSCMAT
>        implicit NONE
> #include <petsc/finclude/petscmat.h>
>
>
>       Mat             ::  A                      
>       PetscViewer     ::  fd                    
>       character(100)   ::  DataFile    ! input file name 
>       IS              ::  isrow,iscol            ! row and column permutations 
>       PetscErrorCode  ::  ierr
>       MatOrderingType :: rtype = MATORDERINGRCM
>       PetscBool       :: flg,PetscPreLoad = PETSC_FALSE
>      
>       write(*,*) "before initialize" 
>       call PETSCinitialize(PETSC_NULL_CHARACTER,ierr)
>
>       call PetscOptionsGetString(petsc_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-f",DataFile,flg,ierr)
>       CHKERRA(ierr)
>       write(*,*) "DataFile=",DataFile 
>      !!Open ascii file.  Note that we use FILE_MODE_READ to indicate
>      !!reading from this file.
>       write(*,*) "before test open/close" 
>       open(10, FILE=DataFile)
>       close(10)
>        
>       write(*,*) "before viewer create" 
>       CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,DataFile,fd,ierr);CHKERRA(ierr)
>
>
>       !! Load the matrix; then destroy the viewer.
>       write(*,*) "before matcreate" 
>       call  MatCreate(PETSC_COMM_WORLD,A,ierr);CHKERRA(ierr);
>       write(*,*) "before matsettype" 
>       call  MatSetType(A,MATSEQAIJ,ierr);CHKERRA(ierr);
>       write(*,*) "before matload" 
>       call  MatLoad(A,fd,ierr);CHKERRA(ierr);
>       write(*,*) "before destroyviewer" 
>       call  PetscViewerDestroy(fd,ierr);CHKERRA(ierr);
>
>
>
>       call  MatGetOrdering(A,rtype,isrow,iscol,ierr);CHKERRA(ierr);
>
>
>      !! All PETSc objects should be destroyed when they are no longer needed.
>       call MatDestroy(A,ierr);CHKERRA(ierr);
>       call ISDestroy(isrow,ierr);CHKERRA(ierr);
>       call ISDestroy(iscol,ierr);CHKERRA(ierr);
>       write(*,*) isrow,iscol
>       call PetscFinalize(ierr)
>       stop
>       end program Main


More information about the petsc-users mailing list