Fortran OpenMP parallelization and writing in multiple files.

Click For Summary
SUMMARY

The discussion centers on the challenges of writing data to multiple files in Fortran using OpenMP for parallelization. The user encounters an error stating 'File already opened in another unit' when attempting to open files in different threads. The solution involves ensuring that each thread writes to a unique file name, as the current implementation does not differentiate file names across threads. The use of the OMP_get_thread_num() function is correct, but it must be paired with unique file naming to avoid conflicts.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with OpenMP parallelization techniques
  • Knowledge of file handling in Fortran
  • Experience with multi-threading concepts
NEXT STEPS
  • Research unique file naming strategies in Fortran
  • Learn about OpenMP thread management and data sharing
  • Explore error handling techniques in Fortran file I/O
  • Investigate performance optimization in parallel file writing
USEFUL FOR

This discussion is beneficial for Fortran developers, computational scientists, and anyone involved in parallel programming and file I/O operations using OpenMP.

eleteroboltz
Messages
7
Reaction score
0
Hello.

I'm attempting to write some data in different files (each thread write in each file), but I'm getting an error saying: 'File already opened in another unit'. I'm using the function OMP_get_thread_num() from OpenMP library in order to open individual files in individual threads.

Code:
!$OMP PARALLEL DEFAULT(PRIVATE) SHARED(PeVet,num,eta,xi,p) FIRSTPRIVATE(Temp,Told)
!$OMP DO
DO q=1,5
    Pe = PeVet(q)

    LocalThread = 22 +  OMP_get_thread_num()
    WRITE(FileName,'(a,i4.4,a,i4.4,a,i2.1,a)') 'Velocity-Imax',Imax,'Jmax',Jmax,'Kn',p,'.dat'
    OPEN(UNIT=LocalThread,FILE=FileName)

    DO j=1,jmax
        ud(j)= (3.d0/2.d0)*(1.d0+8*Kn*bV-eta(j)**2)/(1.d0+12*Kn*bV)
        Write(LocalThread,*) eta(j), ud(j)
    END DO
    CLOSE(LocalThread)
END DO
!$OMP END DO
!$OMP END PARALLEL

I don't know what I'm doing wrong...
Please, help me guys.
Thank you in advance
 
Last edited:
Technology news on Phys.org
It looks like the error is meaningful, isn't it?

I mean, you may be making sure that the unit number is different in every thread, but it does not look like you are making sure that the file name become different...in other words, it looks like you open the file the first time around from the first thread that gets there...and THEN, you are attempting to open the same file from another thread with a different unit number...follow?

are you trying to write to different files or the same? or what?
 
Gsal, you are totally right.

Thank you
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K