Create a makefile with allocatable memory in fortran

In summary: This is not supported and will not work.\In summary, Vasanth is asking for help with creating a makefile that will dynamically allocate memory. The makefile he has created does not work because the ALLOCATE statement in it has a syntax error.
  • #1
VasanthG
13
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
  • #2
please post your code
 
  • #3
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
 
  • #4
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
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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

  • simple_make.txt
    2.1 KB · Views: 484
  • vortex.txt
    3.4 KB · Views: 635
  • memory_alloc.txt
    2.2 KB · Views: 480
  • #9
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 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
 

Related to Create a makefile with allocatable memory in fortran

What is a makefile?

A makefile is a special type of script or file used in software development to automate the compilation and linking of source code into executable files. It contains a set of instructions, called rules, that specify how to build and organize the project's source code.

How do I create a makefile in Fortran?

To create a makefile in Fortran, you will need to create a text file with the name "Makefile" or "makefile" and save it in the same directory as your Fortran source files. This file should contain the necessary rules and commands to compile and link your code into an executable file.

What is allocatable memory in Fortran?

Allocatable memory in Fortran is a type of dynamic memory allocation that allows the programmer to allocate and deallocate memory during program execution. This is useful for creating flexible data structures and optimizing memory usage.

How do I allocate memory in Fortran?

To allocate memory in Fortran, you will need to use the ALLOCATE statement. This statement takes in the name of the variable to be allocated and the size of the memory block, and then reserves the necessary memory for that variable.

How do I deallocate memory in Fortran?

To deallocate memory in Fortran, you will need to use the DEALLOCATE statement. This statement takes in the name of the variable or array that was previously allocated and frees up the memory used by it. It is important to deallocate memory when it is no longer needed to prevent memory leaks and optimize memory usage.

Similar threads

  • Programming and Computer Science
Replies
4
Views
694
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
11
Views
14K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
8
Views
7K
Back
Top