Open & read command in fortran

  • Comp Sci
  • Thread starter Clint_Johnson
  • Start date
  • Tags
    Fortran
In summary, the problem in the given code is that the data from the files is not being read correctly, leading to a compiler error. A loop needs to be used to read in each element of the arrays from the files. The correct code should include loops for reading and writing the data from the arrays in the files.
  • #1
Clint_Johnson
9
0
I have two files I need to read arrays out of using the "open" command and I keep getting a compiler error saying "(col. 1) remark: LOOP WAS VECTORIZED." I assume this is because I am reading the data out of the files incorrectly but I don't really understand what "loop was vectorized" means. The data in the files is formatted as
IxJ
number
number
number
...

where I and J are the dimensions the array and number represents numbers in the array

Here's what I have although I am pretty sure my error lies in my reading of the array

Code:
PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNC
IMPLICIT NONE
CHARACTER(LEN=30) :: INPUT_FILE_1, INPUT_FILE_2
REAL, DIMENSION(10000,10000) :: DATA_ARRAY_1, DATA_ARRAY_2, DATA_ARRAY_3

PRINT *, "PLEASE ENTER THE COMPLETE NAME OF THE TWO FILES CONTAINING THE MATRICES YOU WISH TO MULTIPLY"
READ *, INPUT_FILE_1, INPUT_FILE_2

OPEN(1, FILE=INPUT_FILE_1, ACTION="READ", FORM='FORMATTED')
READ(1) DATA_ARRAY_1
CLOSE(1)

OPEN(2, FILE=INPUT_FILE_1, ACTION="READ", FORM='FORMATTED')
READ(2) DATA_ARRAY_2
CLOSE(2)

DATA_ARRAY_3 = DATA_ARRAY_1*DATA_ARRAY_2

OPEN(3, FILE="RESULTING_MATRIX.dat", ACTION="WRITE", FORM='FORMATTED')
WRITE(3) DATA_ARRAY_3
CLOSE(3)

END PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNC

Thanks in advance for the help!




Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
The problem is that you are not reading the data from your files correctly. You need to use a loop to read in each element of the array from the file. For example:PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNCIMPLICIT NONECHARACTER(LEN=30) :: INPUT_FILE_1, INPUT_FILE_2REAL, DIMENSION(10000,10000) :: DATA_ARRAY_1, DATA_ARRAY_2, DATA_ARRAY_3PRINT *, "PLEASE ENTER THE COMPLETE NAME OF THE TWO FILES CONTAINING THE MATRICES YOU WISH TO MULTIPLY"READ *, INPUT_FILE_1, INPUT_FILE_2OPEN(1, FILE=INPUT_FILE_1, ACTION="READ", FORM='FORMATTED')DO i = 1, 10000 DO j = 1, 10000 READ(1,*) DATA_ARRAY_1(i,j) END DOEND DOCLOSE(1)OPEN(2, FILE=INPUT_FILE_1, ACTION="READ", FORM='FORMATTED')DO k = 1, 10000 DO l = 1, 10000 READ(2,*) DATA_ARRAY_2(k,l) END DOEND DOCLOSE(2)DATA_ARRAY_3 = DATA_ARRAY_1*DATA_ARRAY_2OPEN(3, FILE="RESULTING_MATRIX.dat", ACTION="WRITE", FORM='FORMATTED')DO m = 1, 10000 DO n = 1, 10000 WRITE(3,*) DATA_ARRAY_3(m,n) END DOEND DOCLOSE(3)END PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNC
 
  • #3


I understand your frustration with getting a compiler error and not fully understanding what it means. In this case, the error message "loop was vectorized" means that the compiler has automatically optimized your code by converting the loop to a vector operation. This is usually a good thing and can improve the performance of your code. However, it is possible that the way you are reading the arrays is not compatible with vectorization, which is why you are getting the error.

Looking at your code, it seems like you are trying to read the entire data file into a single array. However, the data in your files is formatted as a matrix, with the dimensions I and J specified at the top. This means you need to read the data into a 2D array, with I rows and J columns. Here is a possible solution for reading the data:

PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNC
IMPLICIT NONE
CHARACTER(LEN=30) :: INPUT_FILE_1, INPUT_FILE_2
REAL, DIMENSION(:,:), ALLOCATABLE :: DATA_ARRAY_1, DATA_ARRAY_2, DATA_ARRAY_3
INTEGER :: I, J

PRINT *, "PLEASE ENTER THE COMPLETE NAME OF THE TWO FILES CONTAINING THE MATRICES YOU WISH TO MULTIPLY"
READ *, INPUT_FILE_1, INPUT_FILE_2

OPEN(1, FILE=INPUT_FILE_1, ACTION="READ", FORM='FORMATTED')
READ(1,*) I, J ! Read the dimensions of the array
ALLOCATE(DATA_ARRAY_1(I,J)) ! Allocate the array with the correct dimensions
READ(1,*) DATA_ARRAY_1 ! Read the data into the array
CLOSE(1)

OPEN(2, FILE=INPUT_FILE_2, ACTION="READ", FORM='FORMATTED') ! Note: changed to INPUT_FILE_2
READ(2,*) I, J
ALLOCATE(DATA_ARRAY_2(I,J))
READ(2,*) DATA_ARRAY_2
CLOSE(2)

DATA_ARRAY_3 = DATA_ARRAY_1*DATA_ARRAY_2

OPEN(3, FILE="RESULTING_MATRIX.dat", ACTION="WRITE", FORM='FORMATTED')
WRITE(3,*) DATA_ARRAY_3
CLOSE(3)

END PROGRAM MAT_MUL_FORTRAN_INTRINSIC_FUNC

This code reads the dimensions of the array from the first line of the file, allocates the array with the
 

Related to Open & read command in fortran

1. What is the "Open & read" command in Fortran?

The "Open & read" command in Fortran is used to open and read data from a file. It allows the user to specify the file name, the type of access (read, write, or append), and the file format (sequential or direct).

2. How do I use the "Open & read" command in Fortran?

To use the "Open & read" command in Fortran, you need to first declare variables to store the file name, access mode, and file format. Then, you can use the OPEN statement to specify these variables and open the file. After that, you can use the READ statement to read data from the file into your program.

3. What are the different access modes available in the "Open & read" command?

The different access modes available in the "Open & read" command are READ, WRITE, and APPEND. READ mode allows you to read data from a file, WRITE mode allows you to write data to a file, and APPEND mode allows you to add data to the end of an existing file.

4. Can I specify the file format when using the "Open & read" command?

Yes, you can specify the file format when using the "Open & read" command. The two file formats available are sequential and direct. Sequential format reads data in the same order as it was written, while direct format allows you to access specific records in the file.

5. What happens if the "Open & read" command cannot open the specified file?

If the "Open & read" command cannot open the specified file, it will display an error message and the program will terminate. It is important to ensure that the file exists and the correct file name, access mode, and file format are specified in the OPEN statement.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top