Add c-language function in the ROMS compiling process

Discussion on computers, ROMS installation and compiling

Moderators: arango, robertson

Post Reply
Message
Author
s.celestino
Posts: 14
Joined: Wed Feb 01, 2017 2:40 pm
Location: University of Naples - Federico II

Add c-language function in the ROMS compiling process

#1 Post by s.celestino » Mon Feb 05, 2018 9:21 am

Hello,

I want to add a C function (to compute the resident set size memory used by ROMS) in the compiling process. I need to compile the object C file together the object fortran files.

This part in the makefile (line 185)?

Code: Select all

#--------------------------------------------------------------------------
#  Set Pattern rules.
#--------------------------------------------------------------------------

%.o: %.F

%.o: %.f90
    cd $(SCRATCH_DIR); $(FC) -c $(FFLAGS) $(notdir $<)

%.f90: %.F
    $(CPP) $(CPPFLAGS) $(MY_CPP_FLAGS) $< > $*.f90
    $(CLEAN) $*.f90

User avatar
kate
Posts: 3702
Joined: Wed Jul 02, 2003 5:29 pm
Location: IMS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#2 Post by kate » Mon Feb 05, 2018 4:38 pm

I needed to add a C function for the waves-in-a-circle problem. I believe the chunk of makefile you show does nothing at all. It's all in the functions. I added these to https://github.com/kshedstrom/roms/blob ... n/makefile:

Code: Select all

  c_sources  :=

Code: Select all

# $(call source-to-object, source-file-list)
c-source-to-object = $(call source-dir-to-binary-dir,       \
                     $(subst .c,.o,$(filter %.c,$1))        \
                     $(subst .cc,.o,$(filter %.cc,$1)))

# $(call make-c-library, library-name, source-file-list)
define make-c-library
   libraries += $(SCRATCH_DIR)/$1
   c_sources += $2

   $(SCRATCH_DIR)/$1: $(call source-dir-to-binary-dir,    \
                      $(subst .c,.o,$(filter %.c,$2))     \
                      $(subst .cc,.o,$(filter %.cc,$2)))
        $(AR) $(ARFLAGS) $$@ $$^
        $(RANLIB) $$@
endef

# $(call one-c-compile-rule, binary-file, source-file)
define one-c-compile-rule
  $1: $2
        cd $$(SCRATCH_DIR); $$(CXX) -c $$(CXXFLAGS) $$<

endef
Then in your compilers file you also need:

Code: Select all

               CC := gcc
              CXX := g++
           CFLAGS :=
         CXXFLAGS :=

Code: Select all

ifdef USE_DEBUG
           FFLAGS += -g -fbounds-check
           CFLAGS += -g
         CXXFLAGS += -g
else
           FFLAGS += -O3 -ffast-math
           CFLAGS += -O3
         CXXFLAGS += -O3
endif
Finally, you need to invoke the compiling of your C code. I forget how we did that before. It was for the Hong Kong workshop, files which are no longer being served by ARSC with the demise of ARSC. I can poke around later for them.

User avatar
kate
Posts: 3702
Joined: Wed Jul 02, 2003 5:29 pm
Location: IMS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#3 Post by kate » Tue Feb 06, 2018 1:13 am

Hah, I didn't do so much work back in 2011. I just needed one new C routine, so I did this in Master/Module.mk:

Code: Select all

diff --git a/Master/Module.mk b/Master/Module.mk
index 4ff5e1b..a9c943d 100644
--- a/Master/Module.mk
+++ b/Master/Module.mk
@@ -18,8 +18,8 @@ ifdef LD_WINDOWS
 $(BIN):        $(libraries) $(local_objs)
        $(LD) $(FFLAGS) $(local_objs) -o $@ $(libraries) $(LIBS_WIN32) $(LDFLAGS)
 else
-$(BIN):        $(libraries) $(local_objs)
-       $(LD) $(FFLAGS) $(LDFLAGS) $(local_objs) -o $@ $(libraries) $(LIBS)
+$(BIN):        $(libraries) $(local_objs) $(SCRATCH_DIR)/bessi.o
+       $(LD) $(FFLAGS) $(LDFLAGS) $(local_objs) $(SCRATCH_DIR)/bessi.o -o $@ $(libraries) $(LIBS)
 endif
 
 $(eval $(compile-rules))
In other words, I added $(SCRATCH_DIR)/bessi.o to the dependencies and to the link list. Then in the makefile, I added these lines below the "all:" target, directions for creating $(SCRATCH_DIR)/bessi.o:

Code: Select all

$(SCRATCH_DIR)/bessi.o: $(MY_ANALYTICAL_DIR)/bessi.c
[TAB]gcc -c $< -o $@
Note that you *must* have a tab before "gcc".

s.celestino
Posts: 14
Joined: Wed Feb 01, 2017 2:40 pm
Location: University of Naples - Federico II

Re: Add c-language function in the ROMS compiling process

#4 Post by s.celestino » Wed Feb 07, 2018 8:23 am

Wow thanks a lot! I will try it now :D

s.celestino
Posts: 14
Joined: Wed Feb 01, 2017 2:40 pm
Location: University of Naples - Federico II

Re: Add c-language function in the ROMS compiling process

#5 Post by s.celestino » Wed Feb 07, 2018 2:28 pm

Thanks kate,

I updated all the code you cited.

and the makefile:

Code: Select all

#--------------------------------------------------------------------------
#  Build target directories.
#--------------------------------------------------------------------------

.PHONY: all

all: $(SCRATCH_DIR) $(SCRATCH_DIR)/MakeDepend $(BIN) rm_macros

$(SCRATCH_DIR)/memuse.o: $(MY_ANALYTICAL_DIR)/memuse.c
    gcc -c $< -o $@
but after the compiling process I get this:

Code: Select all

ar: creating /home/ocean/WC13/I4DVAR/Build/libUTIL.a
master.f90:122:25:

    print*, 'Mem Used:: ', memuse(), 'KB'
                         1
Error: Function ‘memuse’ at (1) has no IMPLICIT type
make: *** [/home/ldamore/ocean/WC13/I4DVAR/Build/master.o] Error 1
make: *** Waiting for unfinished jobs....
memuse() is defined in the memuse.c. So I did not compile the C object file together with the other files in fortran. Where am I wrong?

User avatar
kate
Posts: 3702
Joined: Wed Jul 02, 2003 5:29 pm
Location: IMS/UAF, USA

Re: Add c-language function in the ROMS compiling process

#6 Post by kate » Wed Feb 07, 2018 4:27 pm

Actually, that's not what it's complaining about. It's complaining that you didn't declare the type of memuse in the Fortran code. Does memuse return an integer or a real? You have to tell the compiler since we have turned on IMPLICIT NONE (we don't want it making assumptions for us). At the top of your code, just add something like:

Code: Select all

real(r8) :: memuse
or

Code: Select all

integer :: memuse

s.celestino
Posts: 14
Joined: Wed Feb 01, 2017 2:40 pm
Location: University of Naples - Federico II

Re: Add c-language function in the ROMS compiling process

#7 Post by s.celestino » Wed Feb 07, 2018 9:29 pm

kate wrote:Actually, that's not what it's complaining about. It's complaining that you didn't declare the type of memuse in the Fortran code. Does memuse return an integer or a real? You have to tell the compiler since we have turned on IMPLICIT NONE (we don't want it making assumptions for us). At the top of your code, just add something like:

Code: Select all

real(r8) :: memuse
or

Code: Select all

integer :: memuse
Oh yes, you are right. I'm a noob on Fortran :D

Thanks.

Post Reply