Fortran OpenMP parallelization and writing in multiple files.

In summary, the user is attempting to write data into different files using threads and the OpenMP library. However, they are getting an error stating that the file is already opened in another unit. The user is using the function OMP_get_thread_num() to open individual files in individual threads but it seems like they are not ensuring that the file names are different. This could be causing the error. The user is unsure of what they are doing wrong and is seeking assistance.
  • #1
eleteroboltz
7
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
  • #2
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?
 
  • #3
Gsal, you are totally right.

Thank you
 

1. What is Fortran OpenMP parallelization and how does it work?

Fortran OpenMP (Open Multi-Processing) is a programming model that allows for parallelization of code in Fortran. It works by dividing the code into multiple threads, which can then be executed simultaneously on different cores of a processor, thereby improving the performance of the code.

2. How is parallelization achieved in Fortran OpenMP?

Parallelization in Fortran OpenMP is achieved by using compiler directives, which are special instructions that tell the compiler how to divide the code into threads. These directives are added to the code and control aspects such as the number of threads, data sharing, and synchronization.

3. Can any Fortran code be parallelized using OpenMP?

No, not all Fortran code can be parallelized using OpenMP. Only certain parts of the code that can be divided into independent tasks can be parallelized. Also, the code must not have any data dependencies or race conditions, as these can lead to incorrect results.

4. Why is writing to multiple files important in Fortran OpenMP?

Writing to multiple files in Fortran OpenMP is important because it allows for better performance and scalability of the code. When multiple threads are writing to the same file, it can lead to conflicts and slow down the execution. By writing to multiple files, each thread can write to a separate file, reducing the chances of conflicts and improving performance.

5. What are some best practices for writing code with Fortran OpenMP parallelization and multiple files?

Some best practices for writing code with Fortran OpenMP parallelization and multiple files include using appropriate compiler directives, minimizing data dependencies, avoiding race conditions, and using efficient file I/O techniques such as buffered writing. It is also important to test and debug the code thoroughly before using it for large-scale simulations.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top