<div dir="ltr"><div><div>Ahh of course.  Thanks!<br><br></div>I had switched to PetscViewerASCIIOpen and then back to PetscViewerBinaryOpen and forgot to include the file mode.<br><br></div><div>I appreciate the rapid and useful response!<br></div><div><br></div>-Storm<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 5, 2017 at 3:21 PM, Jed Brown <span dir="ltr"><<a href="mailto:jed@jedbrown.org" target="_blank">jed@jedbrown.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Please always use "reply-all" so that your messages go to the list.<br>
This is standard mailing list etiquette.  It is important to preserve<br>
threading for people who find this discussion later and so that we do<br>
not waste our time re-answering the same questions that have already<br>
been answered in private side-conversations.  You'll likely get an<br>
answer faster that way too.<br>
<span class=""><br>
<br>
Storm Weiner <<a href="mailto:stormweiner@berkeley.edu">stormweiner@berkeley.edu</a>> writes:<br>
<br>
> Thanks,<br>
><br>
> I decided to use one of the example matrices provided on the ftp to start<br>
> with but now I have another issue.<br>
> I tried to port /mat/ex1.c to f90 but when I do<br>
><br>
>  CALL PetscViewerBinaryOpen(PETSC_<wbr>COMM_WORLD,DataFile,fd,ierr)<br>
><br>
> I get a bad file descriptor error.<br>
><br>
> A simple open() close() check immediately before PetscViewerBinaryOpen()<br>
> passes, so I know I'm using an appropriate file path.<br>
><br>
> Furthermore, the unaltered ex1.c works fine with the file I'm attempting.<br>
> Is there something special about petsc+f90 file descriptors, or did I mess<br>
> up somewhere along the way?<br>
<br>
</span>No, but Fortran doesn't warn when you pass the wrong number of<br>
parameters.  You need this.<br>
<br>
diff --git i/ex1.F90 w/ex1.F90<br>
index 049e6ae..311d6f0 100644<br>
--- i/ex1.F90<br>
+++ w/ex1.F90<br>
@@ -25,7 +25,7 @@<br>
       close(10)<br>
<br>
       write(*,*) "before viewer create"<br>
-      CALL PetscViewerBinaryOpen(PETSC_<wbr>COMM_WORLD,DataFile,fd,ierr);<wbr>CHKERRA(ierr)<br>
+      CALL PetscViewerBinaryOpen(PETSC_<wbr>COMM_WORLD,DataFile,FILE_MODE_<wbr>READ,fd,ierr);CHKERRA(ierr)<br>
<br>
<br>
       !! Load the matrix; then destroy the viewer.<br>
<div><div class="h5"><br>
<br>
> The complete error message is:<br>
><br>
><br>
> $ ./ex1 -f $PWD/testmat<br>
>  before initialize<br>
>  DataFile=<br>
>  /usr/workspace/wsa/weiner6/<wbr>MPI_sandbox/Minimal/testmat<br>
><br>
><br>
>  before test open/close<br>
>  before viewer create<br>
> [0]PETSC ERROR:<br>
> ------------------------------<wbr>------------------------------<wbr>------------<br>
> [0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation,<br>
> probably memory access out of range<br>
> [0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger<br>
> [0]PETSC ERROR: or see<br>
> <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/<wbr>documentation/faq.html#<wbr>valgrind</a><br>
> [0]PETSC ERROR: or try <a href="http://valgrind.org" rel="noreferrer" target="_blank">http://valgrind.org</a> on GNU/linux and Apple Mac OS X<br>
> to find memory corruption errors<br>
> [0]PETSC ERROR: likely location of problem given in stack below<br>
> [0]PETSC ERROR: ---------------------  Stack Frames<br>
> ------------------------------<wbr>------<br>
> [0]PETSC ERROR: Note: The EXACT line numbers in the stack are not available,<br>
> [0]PETSC ERROR:       INSTEAD the line number of the start of the function<br>
> [0]PETSC ERROR:       is given.<br>
> [0]PETSC ERROR: --------------------- Error Message<br>
> ------------------------------<wbr>------------------------------<wbr>--<br>
> [0]PETSC ERROR: Signal received<br>
> [0]PETSC ERROR: See <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/<wbr>documentation/faq.html</a> for<br>
> trouble shooting.<br>
> [0]PETSC ERROR: Petsc Release Version 3.8.0, unknown<br>
> [0]PETSC ERROR: ./ex1 on a arch-linux2-c-debug named borax2 by weiner6 Thu<br>
> Oct  5 14:48:33 2017<br>
> [0]PETSC ERROR: Configure options --with-scalar-type=complex<br>
> --with-valgrind-dir=/usr/bin/<br>
> [0]PETSC ERROR: #1 User provided function() line 0 in  unknown file<br>
> application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0<br>
> [unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=59<br>
> :<br>
> system msg for write_line failure : Bad file descriptor<br>
><br>
> -Storm<br>
><br>
> P.S.  Should I be using reply-all to in these email threads?<br>
><br>
> On Thu, Oct 5, 2017 at 2:10 PM, Jed Brown <<a href="mailto:jed@jedbrown.org">jed@jedbrown.org</a>> wrote:<br>
><br>
>> <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html#sparse-" rel="noreferrer" target="_blank">http://www.mcs.anl.gov/petsc/<wbr>documentation/faq.html#sparse-</a><br>
>> matrix-ascii-format<br>
>><br>
>> Better, read it using Python on MATLAB and write it (using the provided<br>
>> PETSc scripts) in PETSc binary format.  Parallel IO with ASCII files is<br>
>> a dead end.<br>
>><br>
>> Storm Weiner <<a href="mailto:stormweiner@berkeley.edu">stormweiner@berkeley.edu</a>> writes:<br>
>><br>
>> > Hey there,<br>
>> ><br>
>> > I'm working through some basic examples, and I want to read in a test<br>
>> > matrix I have stored in a (row, column, value) ASCII file.   How can I<br>
>> read<br>
>> > this in using PETSc?<br>
>> ><br>
>> ><br>
>> > I found the routine PetscViewerASCIIOpen and PetscViewerASCIIRead  but<br>
>> I'm<br>
>> > not sure how to tell it that what I have is actually a matrix.  In this<br>
>> > example, I know the matrix, so I can specify how many entries to read,<br>
>> but<br>
>> > is there a way to just read "the whole file"?<br>
>> ><br>
>> > What's the preferred PETSc way to do this?<br>
>> ><br>
>> > I'm using F90 by the way.<br>
>> ><br>
>> ><br>
>> > -Storm<br>
>><br>
</div></div>>        Program Main<br>
>        use PETSCMAT<br>
>        implicit NONE<br>
> #include <petsc/finclude/petscmat.h><br>
><br>
><br>
>       Mat             ::  A<br>
>       PetscViewer     ::  fd<br>
>       character(100)   ::  DataFile    ! input file name<br>
>       IS              ::  isrow,iscol            ! row and column permutations<br>
>       PetscErrorCode  ::  ierr<br>
>       MatOrderingType :: rtype = MATORDERINGRCM<br>
>       PetscBool       :: flg,PetscPreLoad = PETSC_FALSE<br>
><br>
>       write(*,*) "before initialize"<br>
>       call PETSCinitialize(PETSC_NULL_<wbr>CHARACTER,ierr)<br>
><br>
>       call PetscOptionsGetString(petsc_<wbr>NULL_OPTIONS,PETSC_NULL_<wbr>CHARACTER,"-f",DataFile,flg,<wbr>ierr)<br>
>       CHKERRA(ierr)<br>
>       write(*,*) "DataFile=",DataFile<br>
>      !!Open ascii file.  Note that we use FILE_MODE_READ to indicate<br>
>      !!reading from this file.<br>
>       write(*,*) "before test open/close"<br>
>       open(10, FILE=DataFile)<br>
>       close(10)<br>
><br>
>       write(*,*) "before viewer create"<br>
>       CALL PetscViewerBinaryOpen(PETSC_<wbr>COMM_WORLD,DataFile,fd,ierr);<wbr>CHKERRA(ierr)<br>
><br>
><br>
>       !! Load the matrix; then destroy the viewer.<br>
>       write(*,*) "before matcreate"<br>
>       call  MatCreate(PETSC_COMM_WORLD,A,<wbr>ierr);CHKERRA(ierr);<br>
>       write(*,*) "before matsettype"<br>
>       call  MatSetType(A,MATSEQAIJ,ierr);<wbr>CHKERRA(ierr);<br>
>       write(*,*) "before matload"<br>
>       call  MatLoad(A,fd,ierr);CHKERRA(<wbr>ierr);<br>
>       write(*,*) "before destroyviewer"<br>
>       call  PetscViewerDestroy(fd,ierr);<wbr>CHKERRA(ierr);<br>
><br>
><br>
><br>
>       call  MatGetOrdering(A,rtype,isrow,<wbr>iscol,ierr);CHKERRA(ierr);<br>
><br>
><br>
>      !! All PETSc objects should be destroyed when they are no longer needed.<br>
>       call MatDestroy(A,ierr);CHKERRA(<wbr>ierr);<br>
>       call ISDestroy(isrow,ierr);CHKERRA(<wbr>ierr);<br>
>       call ISDestroy(iscol,ierr);CHKERRA(<wbr>ierr);<br>
>       write(*,*) isrow,iscol<br>
>       call PetscFinalize(ierr)<br>
>       stop<br>
>       end program Main<br>
</blockquote></div><br></div>