How to store large rank-4 array to file?

  • Thread starter Thread starter gluons
  • Start date Start date
  • Tags Tags
    Array File
Click For Summary
SUMMARY

This discussion focuses on efficiently storing and recalling large rank-4 arrays in Fortran, specifically using unformatted direct access files. The user attempts to implement a code structure that writes matrix elements indexed by four numbers to a file named 'OVERLAPS.OUT' using the Fortran 'open' and 'write' statements. The code compiles but crashes at runtime, indicating a logical error rather than a syntactical one. Recommendations include utilizing a debugger and improving code readability through better variable naming and formatting.

PREREQUISITES
  • Understanding of Fortran programming, particularly file I/O operations.
  • Familiarity with debugging techniques in Fortran.
  • Knowledge of matrix data structures and indexing in programming.
  • Experience with code readability best practices, including variable naming and whitespace usage.
NEXT STEPS
  • Learn advanced Fortran file I/O techniques for handling large datasets.
  • Explore debugging tools specific to Fortran, such as GDB or Valgrind.
  • Research best practices for writing maintainable Fortran code, focusing on variable naming conventions.
  • Investigate alternative data storage formats for large arrays, such as HDF5 or NetCDF.
USEFUL FOR

This discussion is beneficial for Fortran developers, computational scientists, and anyone involved in numerical computing who needs to manage large datasets efficiently.

gluons
Messages
15
Reaction score
0
I am writing a code to calculate and to operations with a large set of matrix elements, indexed by four numbers.

I am also writing these subroutines within a much larger code structure, so I do not have total freedom to modify the objects I am using.

I am trying something like this, which compiles but crashes. I am not sure if it would do what I want though, even if it didn't crash.

What is the best way to write operations like this? How can I most efficiently store the arrays and recall them?


inquire(iolength=recl) overlap
open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
do ikloc=1,nkptnrloc
ik=mpi_grid_map(nkpt,dim_k,loc=ikloc)
write(10,rec=ik) overlap(:,:,:,ikloc)
enddo
close(10)

inquire(iolength=recl) overlap
open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
do ikloc=1,nkptnrloc
ik=mpi_grid_map(nkpt,dim_k,loc=ikloc)
write(10,rec=ik) overlap(:,:,:,ikloc)
enddo
close(10)
 
Technology news on Phys.org
gluons said:
I am writing a code to calculate and to operations with a large set of matrix elements, indexed by four numbers.

I am also writing these subroutines within a much larger code structure, so I do not have total freedom to modify the objects I am using.

I am trying something like this, which compiles but crashes. I am not sure if it would do what I want though, even if it didn't crash.

What is the best way to write operations like this? How can I most efficiently store the arrays and recall them?
Deleted extra copy of code.
gluons said:
inquire(iolength=recl) overlap
open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
do ikloc=1,nkptnrloc
ik=mpi_grid_map(nkpt,dim_k,loc=ikloc)
write(10,rec=ik) overlap(:,:,:,ikloc)
enddo
close(10)

Others on this site are more versatile with fortran, as the last time I wrote any code of this type was about 20 years ago.

In any case, I don't believe you have given us enough information to be able to help. The problem you describe may or may not be occurring in the lines of code you show, and might be a result of something in the code that you don't show.

From your description your program is syntactically correct (it compiles successfully) but a logical error is causing problems at run time. Simply saying that the program "crashed" gives us no clue at all about why the crash occurred. Any additional information that the program produces at that time would be helpful for us to determine the cause.

If you have a debugger to use, that could be used to narrow down the cause of the crash. If you are unfamiliar with using debuggers, the old-fashioned way to find problems is to liberally sprinkle WRITE statements throughout the code to display the values of variables when the code is running.

<rant>
I mean no disrespect, but your coding style is pretty much identical to the coding style used by countless Fortran programmers. Here are some of the hallmarks of bad Fortran code.

  1. Incomprehensible variable names - ikloc, nkptnrloc, ik.
    I have no idea what these are supposed to represent, which makes it impossible to understand what their roles might be or the values they might have. I can guess that ikloc represents the location of something - ik? - but that's only a guess. Possibly nkptnrloc represents the location of something, but I can't even guess what an nkptnr might be.
    At one point many years ago, Fortran programmers were constrained to using short variable names due to the lack of memory. Although those days are long gone, all too many Fortran programmers continue following this style.
  2. Nonuse of whitespace - The statement below has not a single space character in it.
    Code:
    open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
    In general, it seems to me that Fortran programmers are unfamiliar with the location and purpose of the space bar. Whitespace added to program code makes it easier for us humans to read and understand the code. The lack of whitespace and the gobbledegook variable names puts the code pretty firmly into the "write-only" category, IMO.
Other traits that I often see in others' Fortran code are no indentation or random indentation of program control structures, and lack of comments.

Although the compiler couldn't care less about the names of variables, use of whitespace, indentation, and comments, we poor humans have a more difficult time understanding what the code is trying to do when little or no thought is given to these attributes of good programming style.
</rant>
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
22K