[Fortran] How to do I/O jobs and computing jobs at the same time

In summary: You can also use threads, but you need to be very careful about thread safety. You also need to be careful about data corruption. You might consider using a double buffer technique.
  • #1
xuphys
7
0
It takes a lot of time for my Fortran program to do I/O jobs. Is there any way to do I/O jobs while at the same time it is doing computing jobs? To do I/O jobs and computing jobs at the same time would save a lot of time.

Thanks.
 
Technology news on Phys.org
  • #2
Run the I/O-intensive and CPU-intensive aspects of your application in different threads of execution. There are Fortran wrappers for the POSIX threads library (but pthreads entails running on a Unix-like system). Another option is OpenMP.

Whenever you use threads you need to be concerned with one thread corrupting another. Your Fortran library for example may or may not be thread safe. (C as-is also has this problem, except that large chunks of the C library must be thread safe to be POSIX compliant. Similar concerns exist on and are addressed in the Windows C standard libraries with respect to the Windows threading mechanisms.)

Your program almost certainly is not thread safe as-is. Common blocks are notoriously unsafe. You will need to modify your program to make sure that (for example) the input thread is not writing input data to the same structure or common block that your compute thread is reading. You do not want your compute thread operating on an input buffer that contains some data from input record A and some from record A+1. Output has similar problems with corruption. Guarding against corruption can be a bit tricky.

One simplifying approach is to use a double buffer technique. Suppose you have an input thread, a compute thread, and an output thread. You'll have two pairs of buffers here, one pair for input and another pair for output. Your input thread will store input data in one of the input buffer pairs while your compute thread will operate on the input data that has previously been stored in the other input buffer. When the input thread finishes writing to an input buffer, it marks that newly-populated input buffer as ready for the compute thread and waits until the other input buffer is no longer in use by the compute thread. The compute thread needs to perform an analogous set actions when it finishes processing the data from an input buffer.
 
  • #3
I'm not sure what I/O jobs the Fortran progam is doing, but with read ahead and write behind caching in hard drives and operating systems, you should be getting a lot of concurrency even with a single threaded application.

Perhaps the input file(s) are highly fragmented which would slow things down due to all the random accessing. You might consider defragging that partition.

You didn't mention what type of device was involved in the I/O. If you're taking real time readings from an array of instrumented sensors, that could be I/O intensive and a candiate for multi-threading if the drivers for those insturments aren't already buffering data.

You didn't mention what operating system you were using. I've written some simple multi-threaded apps for Windows in C, but I haven't used Fortan in a long time.
 
Last edited:
  • #4
I'm not familiar with the specifics of Fortran in the regard. But programs that read data one byte/word/whatever one at a time, process it and then write an output byte/word/whatever one at a time, can often be sped up dramatically by reading and writing the data in blocks to/from a buffer (array) of several kB or more.
 
  • #5
xuphys said:
It takes a lot of time for my Fortran program to do I/O jobs. Is there any way to do I/O jobs while at the same time it is doing computing jobs? To do I/O jobs and computing jobs at the same time would save a lot of time.

You can do that in PL/I (in Windows and other systems).
 

1. How can I perform input/output (I/O) operations while also running computations in Fortran?

Fortran allows for simultaneous I/O and computing jobs by utilizing the OPEN and READ/WRITE statements. These statements can be used to open files for reading or writing, and then read or write data while the program is also performing calculations.

2. Can I specify the location of the I/O file in Fortran?

Yes, you can specify the location of the I/O file in Fortran by using the OPEN statement with the FILE option. This allows you to specify the path and file name for the file you want to open for I/O operations.

3. How can I ensure that my I/O operations are synchronized with my computing jobs in Fortran?

To ensure synchronization between I/O and computing jobs in Fortran, you can use the SYNC option in the OPEN statement. This will ensure that any I/O operations are completed before the next computing job is executed.

4. Can I perform multiple I/O operations at once in Fortran?

Yes, Fortran allows for multiple I/O operations to be performed simultaneously by using the MULTIPLE option in the OPEN statement. This allows for efficient handling of multiple I/O tasks within a single program.

5. How can I handle errors that may occur during I/O operations in Fortran?

In Fortran, you can use the ERR option in the OPEN statement to handle errors that may occur during I/O operations. This will allow you to specify the actions to take in case of an error, such as displaying an error message or exiting the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
4
Views
587
Back
Top