- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Everyone!
I am trying to read/write binary data to files in FORTRAN using the Intel compiler that will be read/write with Matlab. I have successfully done this however the runtime for large files is slow. A small example of my code for writting a binary file is,
real*8 a(10)
open(unit = 1, file = 'foo.dat', form = 'unformatted', recordtype = 'stream')
do i = 1,10
write(1) a(10)
end do
I believe that I am being slowed down by constantly fetching the A data in memory? Is there a way I can write the vector A as is done in C using FWRITE where one specifies the number of bytes and the loop is eliminated?
I also need to write COMPLEX data into the output. I am currently doing the following (Matlab stores real() and imag() data in separate vectors),
complex*16 b(10)
do i = 1,10
write(1) real(b(i))
end do
do i = 1,20
write(1) imag(b(i))
end do
Is there a way this can be done using a type of STRIDE variable as opposed to storing the real and imaginary parts in separate vectors? Since in FORTRAN, the real/imaginary parts are interlaced, is there a way of writing every other value? For example, if we were to write out B,
real(b(1)) imag(b(1)) real(b(2)) imag(b(2)) ...
byte chunk 1 byte chunk 2 byte chunk 3 byte chunk 4 .....
I would want to write byte chunk 1,3,5, ... for the reals and chunk 2,4,6,... for the imaginary data.
Thank you very much for your time. Any help would be greatly appreciated.
Sincerely,
David
I am trying to read/write binary data to files in FORTRAN using the Intel compiler that will be read/write with Matlab. I have successfully done this however the runtime for large files is slow. A small example of my code for writting a binary file is,
real*8 a(10)
open(unit = 1, file = 'foo.dat', form = 'unformatted', recordtype = 'stream')
do i = 1,10
write(1) a(10)
end do
I believe that I am being slowed down by constantly fetching the A data in memory? Is there a way I can write the vector A as is done in C using FWRITE where one specifies the number of bytes and the loop is eliminated?
I also need to write COMPLEX data into the output. I am currently doing the following (Matlab stores real() and imag() data in separate vectors),
complex*16 b(10)
do i = 1,10
write(1) real(b(i))
end do
do i = 1,20
write(1) imag(b(i))
end do
Is there a way this can be done using a type of STRIDE variable as opposed to storing the real and imaginary parts in separate vectors? Since in FORTRAN, the real/imaginary parts are interlaced, is there a way of writing every other value? For example, if we were to write out B,
real(b(1)) imag(b(1)) real(b(2)) imag(b(2)) ...
byte chunk 1 byte chunk 2 byte chunk 3 byte chunk 4 .....
I would want to write byte chunk 1,3,5, ... for the reals and chunk 2,4,6,... for the imaginary data.
Thank you very much for your time. Any help would be greatly appreciated.
Sincerely,
David
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
write(1) A(1:10)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - david.sallngc.com
...
real*8 a(10)
open(unit = 1, file = 'foo.dat', form = 'unformatted', recordtype = 'stream')
do i = 1,10
write(1) a(10)
end do
I believe that I am being slowed down by constantly fetching the A data in memory? Is there a way I can write the vector A as is done in C using FWRITE where one specifies the number of bytes and the loop is eliminated?
real*8 a(10)
open(unit = 1, file = 'foo.dat', form = 'unformatted', recordtype = 'stream')
do i = 1,10
write(1) a(10)
end do
I believe that I am being slowed down by constantly fetching the A data in memory? Is there a way I can write the vector A as is done in C using FWRITE where one specifies the number of bytes and the loop is eliminated?
Instead of Fortran WRITE, use the Win32 API function WriteFile:
lret = WriteFile(handl, LOC(A), 10*SIZEOF(A(1)), NULL, os)
This does exactly what you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jimdempseyatthecove
write(1) A(1:10)
Thanks Jim. I can not believe that is all that is necessary!
How about if my vector is complex? Is there a way to write all REAL's and IMAG's separately usig some type of indexing?
Thanks again.
Sincerely,
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Paul Curtis
Instead of Fortran WRITE, use the Win32 API function WriteFile:
lret = WriteFile(handl, LOC(A), 10*SIZEOF(A(1)), NULL, os)
This does exactly what you want.
Thanks for the reply. I need code that will run on both Windows 32-bit and 64-bit as well as a 64-bit Linux. Even though WriteFile looks just like fwrite, I need something more portable.
Thanks again.
Sincerely,
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - david.sallngc.com
How about if my vector is complex? Is there a way to write all REAL's and IMAG's separately usig some type of indexing?
David,
write(1) (real(a(i)),i=1,10), (aimag(a(j)),j=1,10)
should work
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can open the file ACCESS='STREAM' and just write a stream of bytes. This is standard F2003.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page