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

Click For Summary

Discussion Overview

The discussion revolves around optimizing I/O operations in Fortran programs, specifically exploring ways to perform I/O jobs concurrently with computational tasks. Participants consider various methods, including threading and buffering techniques, to enhance performance and reduce execution time.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using threads to separate I/O-intensive and CPU-intensive tasks, mentioning Fortran wrappers for POSIX threads and OpenMP as potential solutions.
  • Concerns are raised about thread safety in Fortran, particularly regarding common blocks and the risk of data corruption between threads.
  • A double buffer technique is proposed as a method to manage data safely between input and compute threads, ensuring that each thread operates on distinct buffers.
  • Another participant questions the nature of the I/O jobs and suggests that operating system-level caching might already provide concurrency benefits, even in single-threaded applications.
  • Fragmentation of input files is mentioned as a potential issue that could slow down I/O operations, with a recommendation to defragment the relevant partition.
  • There is a suggestion that reading and writing data in blocks rather than one byte/word at a time could significantly improve performance.
  • A participant notes that similar concurrent I/O capabilities exist in PL/I, indicating that this is not unique to Fortran.

Areas of Agreement / Disagreement

Participants express a variety of viewpoints on the best approach to handle I/O and computation concurrently, with no clear consensus on a single method or solution. The discussion remains open with multiple competing ideas and techniques being explored.

Contextual Notes

Participants do not specify the exact nature of the I/O jobs or the operating system in use, which may affect the applicability of the proposed solutions. Additionally, the discussion does not resolve the potential limitations of threading in Fortran or the specifics of data handling in multi-threaded environments.

Who May Find This Useful

Readers interested in optimizing Fortran applications, particularly those dealing with I/O operations and computational tasks, may find the insights and proposed techniques relevant.

xuphys
Messages
7
Reaction score
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
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.
 
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:
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.
 
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).
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K