Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

BACKSPACE error 23

Hanbing_P_
Beginner
4,120 Views

       I have a program for processing data of engineering which is coded in Fortran 90. An error occurs with the 'BACKSPACE' state in the program, but I have no idea about how to fix it.

      At the beginning of the process, some temp data  has been written into a scratch 'unformatted' file,and  then I use 'BACKSPACE' to read it out in reverse. When the file pointer  almost approaches the beginning of the scratch file, an error occurs with the 'BACKSPACE' , the return value of 'IOSTAT=23' and 'IOMSG='B'' are output ! But,I do not know what this error code means.

      And there is another fact should be mentioned, I think. I have two sets of data ,nothing big difference exists between them but the amount.When I process the set of data with smaller amount, no error occurs with 'BACKSPACE'.Otherwise, it does as the way I just described.Surely, when larger amount of data is processed, larger the scratch file will be, I do not whether it matters!

     So,could anyone give me some tips about how to fix it ? Thanks a lot !

0 Kudos
25 Replies
Anthony_Richards
New Contributor I
3,575 Views
   

Rules and Behavior

Use the BACKSPACE statement with files connected for sequential access.

BACKSPACE cannot be used to skip over records that have been written using list-directed or namelist formatting.

The I/O unit number must specify an open file on disk or magnetic tape.

Backspacing from the current record n is performed by rewinding to the start of the file and then performing n-1 successive READs to reach the previous record.

A BACKSPACE statement must not be specified for a file that is open for direct or append access, because n is not available to the Fortran I/O system.

If a file is already positioned at the beginning of a file, a BACKSPACE statement has no effect.

If the file is positioned between the last record and the end-of-file record, BACKSPACE positions the file at the start of the last record.

0 Kudos
andrew_4619
Honored Contributor III
3,575 Views

You can see the error codes here https://software.intel.com/en-us/node/510890

but in this case it won't help a great deal. Using backspace in this way doesn't sound like a great way of doing it. is this dataset large >1000Mb? Creating (using allocate) a temporary  data structure and saving the data might be a better easier and much faster way. The scheme you have sound like the way we used to do things years back when memory was a very limited  resource.

 

 

0 Kudos
Steven_L_Intel1
Employee
3,575 Views

IOMSG='B'? Something's not right there. Is this a program and dataset you can provide to us?

I agree that BACKSPACE is not the best way of handling this sort of thing. If the data you're writing is all the same length, you might consider using direct access and REC= to position.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

app4619 wrote:

You can see the error codes here https://software.intel.com/en-us/node/510890

but in this case it won't help a great deal. Using backspace in this way doesn't sound like a great way of doing it. is this dataset large >1000Mb? Creating (using allocate) a temporary  data structure and saving the data might be a better easier and much faster way. The scheme you have sound like the way we used to do things years back when memory was a very limited  resource.

 

 

Yes! The scratch file may be several GBs! So,I can't just keep it in the memory!

0 Kudos
Hanbing_P_
Beginner
3,575 Views

Steve Lionel (Intel) wrote:

IOMSG='B'? Something's not right there. Is this a program and dataset you can provide to us?

I agree that BACKSPACE is not the best way of handling this sort of thing. If the data you're writing is all the same length, you might consider using direct access and REC= to position.

Sorry,I can't provide neither the program nor the dataset to you because of some special considerations!

More than one type of  record that should be written into the scratch file, so I do not use a direct access file!  

0 Kudos
Arjen_Markus
Honored Contributor II
3,575 Views

In that case, a file opened for "stream access" would be the way to go. Such files have no inherent structure but allow you to reposition the reading/writing position at will. You are best off with an unformatted file.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

arjenmarkus wrote:

In that case, a file opened for "stream access" would be the way to go. Such files have no inherent structure but allow you to reposition the reading/writing position at will. You are best off with an unformatted file.

Yes, you are right! Maybe I can try other ways to do the same thing. But, what't more,I want to find out the reason of the occurrence of such error!

0 Kudos
Steven_L_Intel1
Employee
3,575 Views

How large are the individual records? Unfortunately, this particular error can have multiple causes. It might be that if you call GETLASTERROR()  from module IFPORT after you get the backspace error, the code may provide further details.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

Steve Lionel (Intel) wrote:

IOMSG='B'? Something's not right there. Is this a program and dataset you can provide to us?

I agree that BACKSPACE is not the best way of handling this sort of thing. If the data you're writing is all the same length, you might consider using direct access and REC= to position.

Although,I can not provide the original code, but some code that will reproduce the similar error.

program test
integer(4) :: lfn,i,n,ierr
character(2) :: cid
lfn=13
open (lfn,FILE='tmpcid',FORM='unformatted',RECL=8040000,convert='big_endian') 
write(lfn) '00'
do i=1,4000000
write(lfn) 'ab'
enddo
n=0
do while (.TRUE.)
    n=n+1
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n=',n
        call exit(1)
    endif
    read(lfn,iostat=ierr) cid
    if (ierr.ne.0) then
        write(*,*) 'Error:read n=',n
        call exit(1)
    endif
    if(cid.eq.'00') exit
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n=',n
        call exit(1)
    endif
enddo
close(lfn)
stop 
end program test

The output is "

Error:backspace n=   3216026

"

That is exactly the error I meet in my program. Why this error came out ?

0 Kudos
mecej4
Honored Contributor III
3,575 Views

That is a nice, short reproducer! I can confirm that the bug occurs with IFort 15.0.2.179, 32- and 64-bit, on Windows 8.1-64. A couple of comments: 

  • since you did not specify direct access in the open statement (understandable, since direct access precludes BACKSPACE), the file has variable length records, with a maximum length of 8040000 X 4 bytes, with the default unit reclen=4 bytes. If you had specified /assume:byterecl, the maximum record size would have been 8040000 bytes. However, you write records that are all just two bytes long, so the RECLEN= specification has no effect on the output.
  • If the compiler option /assume:byterecl is specified, the program stops with n = 804104. Does your large program exhibit similar behavior?
  • The bug persists if the specifiers RECL= and CONVERT= are removed. The error then occurs with n = 820, whether are not /assume:byterecl is specified. With these changes, the reproducer runs and exhibits the bug with a run time of less than a second. For the convenience of Intel personnel, I show the modified code below.

​The bug is absent in Intel Fortran 7.0, but can be seen in 11.1.070 and later versions.

program test
integer(4) :: lfn,i,n,ierr
character(2) :: cid
lfn=13
open (lfn,FILE='tmpcid',FORM='unformatted')
write(lfn) '00'
do i=1,1000
   write(lfn) 'ab'
enddo
n=0
do while (.TRUE.)
    n=n+1
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n, ierr = ',n,ierr
        call exit(1)
    endif
    read(lfn,iostat=ierr) cid
    if (ierr.ne.0) then
        write(*,*) 'Error:read n, ierr = ',n,ierr
        call exit(1)
    endif
    if(cid.eq.'00') exit
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n,ierr = ',n,ierr
        call exit(1)
    endif
enddo
write(*,*)'n = ',n
close(lfn)
stop
end program test

 

0 Kudos
Hanbing_P_
Beginner
3,575 Views

mecej4 wrote:

That is a nice, short reproducer! I can confirm that the bug occurs with IFort 15.0.2.179, 32- and 64-bit, on Windows 8.1-64. A couple of comments: 

  • since you did not specify direct access in the open statement, the file has variable length records, with a maximum length of 8040000 X 4 bytes, with the default unit reclen=4 bytes. If you had specified /assume:byterecl, the maximum record size would have been 8040000 bytes. However, you write records that are all just two bytes long, so the RECLEN= specification has no effect on the output.
  • If the compiler option /assume:byterecl is specified, the program stops with n = 804104. Does your large program exhibit similar behavior?
  • The bug persists if the specifiers RECL= and CONVERT= are removed. The error then occurs with n = 820, whether are not /assume:byterecl is specified. With these changes, the reproducer runs and exhibits the bug with a run time of less than a second. For the convenience of Intel personnel, I show the modified code below.
program test
integer(4) :: lfn,i,n,ierr
character(2) :: cid
lfn=13
open (lfn,FILE='tmpcid',FORM='unformatted')
write(lfn) '00'
do i=1,1000
   write(lfn) 'ab'
enddo
n=0
do while (.TRUE.)
    n=n+1
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n, ierr = ',n,ierr
        call exit(1)
    endif
    read(lfn,iostat=ierr) cid
    if (ierr.ne.0) then
        write(*,*) 'Error:read n, ierr = ',n,ierr
        call exit(1)
    endif
    if(cid.eq.'00') exit
    backspace(lfn,iostat=ierr)
    if (ierr.ne.0) then
        write(*,*) 'Error:backspace n,ierr = ',n,ierr
        call exit(1)
    endif
enddo
write(*,*)'n = ',n
close(lfn)
stop

 

So , a very strange bug ,isn't it ? Thanks for your test!

0 Kudos
Hanbing_P_
Beginner
3,575 Views
Did anyone ever come cross the same bug?
0 Kudos
Steven_L_Intel1
Employee
3,575 Views

Thanks! We'll check it out.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

Steve Lionel (Intel) wrote:

Thanks! We'll check it out.

I am exactly the guy who should say ' thanks' to you and your colleagues for your kindness help ! 

And I am looking forward to hearing from you soon ,because the bug has upset me for a long time. Thanks to you and your colleagues again! 

0 Kudos
Steven_L_Intel1
Employee
3,575 Views

I have escalated this as issue DPD200369151 and will update this thread as there is news. Regarding the IOMSG returning just 'B', this is probably due to the variable you specified for IOMSG being a single character (or displaying only one character), since what it does return is the full "BACKSPACE error" message text with the file name. I did add a display of GETLASTERROR() and it showed 0, indicating that it wasn't a file system error but rather something going wrong in the Fortran library.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

Steve Lionel (Intel) wrote:

I have escalated this as issue DPD200369151 and will update this thread as there is news. Regarding the IOMSG returning just 'B', this is probably due to the variable you specified for IOMSG being a single character (or displaying only one character), since what it does return is the full "BACKSPACE error" message text with the file name. I did add a display of GETLASTERROR() and it showed 0, indicating that it wasn't a file system error but rather something going wrong in the Fortran library.

So, is this a bug of the FORTRAN library or the compiler ?

0 Kudos
Steven_L_Intel1
Employee
3,575 Views

It is a bug in the Intel Fortran run-time library that implements BACKSPACE, not in the compiler itself.

0 Kudos
Hanbing_P_
Beginner
3,575 Views

Steve Lionel (Intel) wrote:

It is a bug in the Intel Fortran run-time library that implements BACKSPACE, not in the compiler itself.

Thanks!

0 Kudos
Steven_L_Intel1
Employee
3,575 Views

The developers report that if you add BLOCKSIZE=4000 (or anything above 2048) then the program will work. I tried it and it indeed does, so there's a workaround for you.

0 Kudos
Hanbing_P_
Beginner
3,350 Views

Steve Lionel (Intel) wrote:

The developers report that if you add BLOCKSIZE=4000 (or anything above 2048) then the program will work. I tried it and it indeed does, so there's a workaround for you.

Thanks for your reply! I'll try it in my program. And, if it is OK, could you tell more information about that error? Such as how it happens,and how can I avoid its occurrence again ?  

0 Kudos
Reply