Create a makefile with allocatable memory in fortran

  • Context: Fortran 
  • Thread starter Thread starter VasanthG
  • Start date Start date
  • Tags Tags
    Fortran Memory
Click For Summary

Discussion Overview

The discussion revolves around creating a makefile for compiling Fortran subroutines that utilize dynamic memory allocation. Participants explore issues related to syntax errors in ALLOCATE statements and the organization of source files, including the use of include files versus modules.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • VasanthG describes an error encountered when trying to compile Fortran subroutines with dynamic memory allocation, specifically a syntax error in the ALLOCATE statement.
  • Some participants request to see the Fortran source code and the exact error messages to better understand the issue.
  • There is a suggestion that the subroutine may not have access to the variables being allocated, raising questions about the scope and declaration of these variables.
  • One participant proposes that VasanthG is mixing pre and post Fortran 90 approaches, suggesting a need to convert include files into modules and change file extensions to .f90.
  • VasanthG acknowledges the confusion and expresses gratitude for the assistance received, indicating a willingness to implement suggested changes.
  • A later reply confirms that after making the suggested changes, VasanthG was able to compile the files successfully.

Areas of Agreement / Disagreement

Participants generally agree that the mixing of Fortran 77 and Fortran 90 approaches is problematic, but there is no consensus on the best practices for structuring the code and makefile.

Contextual Notes

There are unresolved issues regarding the proper organization of source files, the use of modules versus include files, and the implications of file extensions on compilation. Specific variable declarations and their accessibility within subroutines remain unclear.

Who May Find This Useful

Individuals working with Fortran, particularly those dealing with dynamic memory allocation and makefile creation, may find the discussion relevant.

VasanthG
Messages
13
Reaction score
0
I have many subroutines which I want to compile individually using makefile. But the memory allocation is not specified in ".INC" file but as separate subroutine depending on the user input. The "gfortran" compiler is showing the following error:

-Syntax error in ALLOCATE statement.

This is not due to missing bracket or something on my carelessness in typing as the program runs fine with a single file.

Please help me create a makefile with dynamic memory allocation.

Thank you
VasanthG
 
Technology news on Phys.org
please post your code
 
The folder contains all subroutines. The following is my makefile:
===============================================

SRC = ./
EXE = ./
CFLAGS = -c

DEFINEOBJ={list of object files to be created}
uvlm: $(DEFINEOBJ) $(SRC) file.INC
gfortran -o $(EXE)uvlm $(DEFINEOBJ)
clean:
rm uvlm $(DEFINEOBJ)

uvlm.o: $(SRC)uvlm.f $(SRC)file.INC
gfortran $(CFLAGS) $(SRC)uvlm.f

//then list of object files compiled

===========================================

file.INC contains list of variables with allocatable memory. Separate subroutine is written to allocate memory.

When I compile I get -Syntax error in ALLOCATE statement-.
Hope the problem is clear.

Thanks for your time.
Vasanth.G
 
Nope, the problem is not clear...

I want to see the fortran source where the ALLOCATE statement is
I want to see copy/paste of the terminal when you compile and the error message
 
c ====================
c Allocation of memory
c ====================

subroutine memory_alloc

allocate (x(MM,4),y(MM,4),z(MM,4))
allocate (x_c(MM,4),y_c(MM,4),z_c(MM,4))
allocate (x_v(MM,1),y_v(MM,1),z_v(MM,1),norm_x(MM,3))
allocate (xc_ics(MM,4),yc_ics(MM,4),zc_ics(MM,4))
allocate (xv_ics(MM,4),yv_ics(MM,4),zv_ics(MM,4))
allocate (w_x(MM,4),w_y(MM,4),w_z(MM,4))
allocate (wx_v(MM,1),wy_v(MN,1),wz_v(MM,1))
allocate (xc_i(MM,4),yc_i(MM,4),zc_i(MM,4))
allocate (u_bcs(MM,3),v_bcs(MM,3),w_bcs(MM,3))
allocate (u_ics(MM,3),v_ics(MM,3),w_ics(MM,3),result_vel(MM,3))
allocate (wx_ics(MM,4),wy_ics(MM,4),wz_ics(MM,4),sum_wake(MM,3))

return
end subroutine memory_alloc
==================================================
MM defined in init.f.
==================================================
This is my .INC file
-----------------------------
implicit none
real*8, allocatable, dimension(:,:,:,:)::wi_wke_ics_t
real*8, allocatable, dimension(:,:)::x,y,z,w_x,w_y,w_z
real*8, allocatable, dimension(:,:)::x_c,y_c,z_c,wx_v,wy_v,wz_v
real*8, allocatable, dimension(:,:)::x_v,y_v,z_v
real*8, allocatable, dimension(:,:)::norm_x
real*8, allocatable, dimension(:,:)::xc_ics,yc_ics,zc_ics
real*8, allocatable, dimension(:,:)::xv_ics,yv_ics,zv_ics
real*8, allocatable, dimension(:,:)::temp1,temp2,temp3,temp4,ic
==================================================

THIS ARE SOME OF THE VARIABLES. memory.f CONTAINS INSTRUCTION TO ALLOCATE MEMORY.
file.INC CONTAINS THE LIST OF VARIABLES.
 
gfortran -c ./memory_alloc.f
./memory_alloc.f:9.18:

allocate (x(MM,4),y(MM,4),z(MM,4))

Error: Syntax error in ALLOCATE statement at (1)

=============================
SAME ERROR CONTINUES ALL THE WAY TILL END.
 
I guess you insist in not posting everything or attaching all files...dude, you are killing me, here, by spoon feeding the sources!...the thing is that it is very important to see the entire contents of every file and where each is being called from.

For example, I have no idea if "subroutine memory_alloc" has accesss to all the variables is allocating...is subroutine memory_alloc contained within a module or within another procedure where such variables have been declared? If not, well, that's your problem...you are trying to allocate arrays that you never declared as far as memory_alloc is concerned.

...or who know what else! I have no access to all the sources or a chance to compile them myself.
 
Hi Mr.gsal,

I am sorry the post is unclear. I was thinking of not making the post very long with unnecessary information. I have declared the same variables for which the subroutine "memory.f" allocates the memory. I am not sure if my question was valid. Sorry to have bothered you so much. I will figure out and post the makefile. I really thank you for all your replies.

Thanks
Vasanth.G
 

Attachments

Vasanth:

I presume that you changed the extensions of the attached files from .f/.INC to .txt only before uploading them, correct? And that in actuality they do have to corresponding .f and .INC extensions as they appear in the make file.

By the way, just to make sure, you are aware of this much, right? that Linux is case sensitive and that "INC" is not the same as "inc".

I see that you have a bunch of files to compile...and I think I see what you are trying to do.

What you are trying to do seems to be a mix of two different approaches, so, it is not going to work...you are mixing pre and post Fortran 90 approaches.

The traditional (pre Fortran 90) approach uses include files and common blocks; basically, you type all your variable declarations in one file (as in vortex.INC) but you also need to include a common block in the same file. You then include this *.INC file into each other *.f source file that needs to have access to those variables; you do this via the 'include vortex.INC' fortran statement...something like what you are trying to do with the "use vortex" statement in memory_alloc.txt ...but this is where you are mixing stuff...

...vortex.INC, as it is, is just an include file because is not a proper (complete) fortran procedure or module and it is not compilable by itself...it is a bunch of lines meant to be included, i.e., as if typed inside other procedures...this is what the 'include' fortran statement does.

...but let's forget about such approach

The new approach is to use modules.

So, you need to turn your program into a proper Fortran 90 program.

To start, I recommend changing the extension of all your files to f90. I think your using of extensions *.f is telling gfortran to compile using f77 syntax and Fortran 77 does not have the allocatable statement...that is why you are getting a syntax error.

Then, you need to replace all those 'c' at the beginning of comment lines with '!'

Then, you need to fix all continuation lines by eliminating the mark from column 6 and adding an '&' at the end of the previous line, instead.

Then, you need to turn your vortex file into a proper module.

Then, in every other source of yours, you do "use vortex"...as you have attempted.



That is it, for now...let's see what kind of shape you end up after these changes.
 
  • Like
Likes   Reactions: 1 person
  • #10
Mr.gsal,

Thanks for your help. I did exactly as you said-mixing FORTRAN77 with F90. Now I am able to compile the files after I changed the .inc file to a module and "use vortex" option-thanks. I need to link object files and create an executable file. I guess the files are compiled even without renaming and still the compiler accepts "$" sign for line continuation.

I am pretty novice in these programming as you could have figured out. Thanks again for your help!
Vasanth.G
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
Replies
11
Views
16K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 34 ·
2
Replies
34
Views
5K