0

I am working on interfacing some modern Fortran code with R and get the following error:

forrtl: severe (151): allocatable array is already allocated

Even though the error message is the same, I don't think the origin of this issue is not directly related with this entry here


The problem/background:

I have compiled my Fortran code myself as shared object, using ifort and the relevant flags

I can call the shared object using .Fortran once in R. Then I have to do dyn.unload("HX.so") in order to then reload it (dyn.load("HX.so") and rerun it.

Just running and rerunning an R script that finishes the R session in between works fine.

During single runs and after loading and unloading in R, the content that is passed back to me in my R session or printed during running the R script is the expected output.

Here is what else is shown after the error message:

forrtl: severe (151): allocatable array is already allocated
Image              PC                Routine            Line        Source
libifcoremt.so.5   00007FF6276E7432  for_alloc_allocat     Unknown  Unknown
HX.so              00007FF628464F3F  init_                 Unknown  Unknown
HX.so              00007FF62845F2D1  hx_                   Unknown  Unknown
libR.so            00007FF637316E76  Unknown               Unknown  Unknown
libR.so            00007FF63734EC8B  Rf_eval               Unknown  Unknown
libR.so            00007FF63735213E  Unknown               Unknown  Unknown
libR.so            00007FF63734EA93  Rf_eval               Unknown  Unknown
libR.so            00007FF637351060  Unknown               Unknown  Unknown
libR.so            00007FF63734EA93  Rf_eval               Unknown  Unknown
libR.so            00007FF6373500C7  Rf_applyClosure       Unknown  Unknown
libR.so            00007FF63734E86F  Rf_eval               Unknown  Unknown
libR.so            00007FF63735213E  Unknown               Unknown  Unknown
libR.so            00007FF63734EA93  Rf_eval               Unknown  Unknown
libR.so            00007FF637376102  Rf_ReplIteration      Unknown  Unknown
libR.so            00007FF637376451  Unknown               Unknown  Unknown
libR.so            00007FF63737650F  run_Rmainloop         Unknown  Unknown
R                  000000000040080B  main                  Unknown  Unknown
libc-2.17.so       00007FF633E77C05  __libc_start_main     Unknown  Unknown
R                  000000000040083B  Unknown               Unknown  Unknown

What I have tried so far:

As the error message suggests, I have identified all ALLOCATED ALLOCATABLES and looked whether they are all DEALLOCATED as well. I did find a few that were not, but that has not solved the issue.

I checked that the Fortran code is running until the end using a simple WRITE (*,*) 'END' statement before END SUBROUTINE.


The aim is to be able to call the Fortran code many times in R, to do sensitivity analysis- so, always having to unload and load it would slow things down substantially.

If you think I need to provide more information, let me know.


edit: compiling with -g -traceback does not catch anything at run or compile time.

compiling with -g -traceback -check catches the following error at runtime:

forrtl: severe (151): allocatable array is already allocated
Image              PC                Routine            Line            Source
libifcoremt.so.5   00007F52C1432432  for_alloc_allocat     Unknown  Unknown
HX.so              00007F52C21D890F  init_                      27  INIT.f90
HX.so              00007F52C21B685B  hx_                       278 HX.f90
libR.so            00007F52D1095E76  Unknown               Unknown  Unknown
libR.so            00007F52D10CDC8B  Rf_eval               Unknown  Unknown
..see above.

line 278 in the "main" subroutine (HX) calls subroutine INIT, which then in line 27 shows an ALLOCATABLE. Doing grep I have not found it being allocated anywhere else. NOTE: but it also was not deallocated anywhere..

all flags used:

F90 = ifort
FFLAGS = -O3 -fpic -r8 -g -check -traceback  # double-precision now run in Fortran , -fpic for creating shared object file
LDFLAGS = -lnetcdff -lnetcdf -shared   #-shared, for creating a shared object file

My R-code:

dyn.load("HX.so")

 years<-c(2007,2008,2009)
 n_years<-as.integer(length(years))
 y<-array(0,dim=c(14,6,n_years))
 run_model <- function(pa=params) {
 out<-.Fortran('HX',pa,n_years,array(as.numeric(0),dim=c(14,6,n_years)))
 return(out)
 }
out<-run_model(c(30,2))
out

NOTE- solution: It was actually a DEALLOCATE issue.. Not all allocated arrays had been deallocated by the end of the run..and I simply had not found them all ( even though that was one of therouble shooting attempts I had made in advance of posting this..) Is my guess correct that maybe the compiler didn't pick up on the issue because unlike in the previous post on stackoverflow, the (de-)allocation was not technically in a loop ( until I turned the Fortran executable into a shared object)? I would find this surprising, but the only info I found is that deallocation is just "good programming practice" ?

4

0 回答 0