使用R的包(例如LAPACK)从Fortran源构建共享对象 [英] Build shared object from Fortran source with package(e.g. LAPACK) for R

查看:225
本文介绍了使用R的包(例如LAPACK)从Fortran源构建共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从Fortran源创建R的共享对象。也许我的一些问题没有意义。但是,我已经搜索了很多关于我的问题,但仍然没有明确的答案。所以,我想知道在R中创建这些共享对象(.dll文件)的大图是。



根据一些教程和手册,我需要准备一个Fortran文件 mymodf.f ,其中仅包含子程序,类似于以下内容:

  c文件mymodf.f 
子例程initmod(odeparms)
external odeparms
double precision parms(3)
common / myparms / parms

call odeparms(3, parm)
return
end

子例程派生(neq,t,y,ydot,yout,ip)
....
return
end

子程序jac(neq,t,y,ml,mu,pd,nrowpd,yout,ip)
....
return
end
c文件结尾mymodf.f

为了创建共享对象,它需要以下代码:

  system(R CMD SHLIB mymodf.f)
/ pre>

我想使用LAPACK中的子例程来解决一些li近似方程或逆矩阵。我应该怎么做,以便我可以在LAPACK中调用子程序?我问这个问题是因为需要在R中安装软件包来调用一些额外的功能,或者需要在C中添加 #include< xxxx.h> 作为头文件xxxx.h中的额外功能同样的情况。



此外,我已经从 http://www.netlib.org/lapack 。我应该把包裹放在哪里?我需要安装这个.tgz文件吗?



这是编程,编译和运行Fortran程序的一般情况。我有一些Fortran代码,这不是一个完整的程序,我想把它作为.dll文件为R。



非常感谢帮助!



(FYI,我在一台Windows 7 PC上工作)

解决方案

谁没有从Fortran源为R生成共享对象的经验,搜索上面提到的问题的答案是耗时的,因为有几个教程提到这些。看起来有些经验的人看不到这个线程。



如果你想在LAPACK或BLAS中调用子程序,你需要创建一个名为的文件MakeVars 没有文件类型,其中包含:

  PKG_LIBS = $(LAPACK_LIBS)$(BLAS_LIBS)$(FLIBS) 

注意:您不需要手动安装库LAPACK和BLAS,R已经包含它们。所有您需要做的是通过在Fortran源文件的相同目录下创建 MakeVars 文件来告诉编译器如何链接它们。对于其他Fortran库,我不知道如何链接它们。希望有人可以提供指南。



然后,您需要有一个Fortran源代码,例如将其命名为 my.f90 ,它包含类似的内容以下:

 子程序LIN(a,b)
隐式无
整数,参数:: n = 2,nrhs = 1
integer,parameter :: lda = n,ldb = n
double precision :: a(lda,n),b(ldb,nrhs)
integer :: info

调用DPOSV('U',n,nrhs,a,lda,b,ldb,info)

结束子程序LIN

下一步是在Mac中的Windows / .so文件中将Fortran代码编译为.dll文件(这是所谓的创建共享对象R)。为了在Windows中编译代码,您需要安装Rtools和R。此外,您的R的路径应该添加到系统变量的路径中。 (步骤:右键单击计算机图标 - >属性 - >高级系统设置 - >环境变量 - >在系统变量中查找并选择路径 - >单击编辑,然后添加R的路径,例如 C:\Program Files\R\R-3.0.2\bin; )。现在,你已经准备好编译代码了。您需要以管理员身份运行R,将其更改为包含Fortran文件的目录,然后在R控制台中执行命令:

  system('R CMD SHLIB my.f90')

你会看到共享对象 my.dll 在目录下创建。



最后一步是将共享对象加载到R中并运行它。按照我的例子,

  dyn.load('my.dll')#加载共享对象

a< -diag(c(1,2))#prepare参数* a *和* b *用于Fortran子例程
b <-c(3,4)
.Fortran(LIN ,a = as.double(a),b = as.double(b))#call Fortran子例程



注意:您需要从R卸载共享对象,以便当R仍在运行时可以删除或编译它:

  dyn.unload('my.dll')


I am new to create shared objects for R from Fortran sources. Perhaps some of my questions do not make sense. However, I have searched a lot about my questions but still get no clear answer. So, I would like to figure out what the big picture to create these shared objects(.dll files)in R is.

According some tutorials and manuals, I need to prepare a Fortran file mymodf.f, which contains only subroutines, similarly as below:

c file mymodf.f
   subroutine initmod(odeparms)
    external odeparms
    double precision parms(3)
    common /myparms/parms

     call odeparms(3, parms)
    return
   end

   subroutine derivs (neq, t, y, ydot, yout, ip)
    ....
    return
   end

   subroutine jac (neq, t, y, ml, mu, pd, nrowpd, yout, ip)
    ....
    return
   end
c end of file mymodf.f

In order to create the shared object, it requires the following code:

system("R CMD SHLIB mymodf.f")

I want to use the subroutines in LAPACK to solve some linear equations or inverse matrices. What should I do so that I can call the subroutines in LAPACK? I ask this question because one needs to install packages in R to call some extra functions or one needs to add #include <xxxx.h> as header in C to call some extra functions in xxxx.h. Just the same situation.

Also, I have downloaded lapack-3.5.0.tgz from http://www.netlib.org/lapack. Where should I put package to? Do I need to install this .tgz file?

This is not a normal case to code, compile and run a Fortran program. I have some Fortran codes which is not a complete program and I want to complie it as .dll file for R.

Thanks very much for helping!

(FYI, I am working on a Windows 7 PC)

解决方案

For a guy who has no experience about generating shared object from Fortran source for R, it is time-consuming to search the answer of the questions I asked above because there are really few tutorials which mention about these. It seems those who have experience about this do not see this thread.

If you want to call subroutines in LAPACK or BLAS, you need to create a file called Makevars with no file type which contains:

PKG_LIBS=$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

Remark: you do not need to install libraries LAPACK and BLAS manually, R has already included them. All you need to do is to tell the compiler how to link them by creating the Makevars file under same directory of your Fortran source file. For other Fortran libraries, I don't know how to link them. Hope someone can provide a guide.

Then, you need to have a Fortran source, say name it my.f90 and it contains something like below:

  subroutine LIN(a,b)
     implicit none
     integer, parameter :: n=2, nrhs=1
     integer, parameter :: lda=n, ldb=n
     double precision :: a(lda,n), b(ldb,nrhs)
     integer :: info

     call DPOSV('U',n,nrhs,a,lda,b,ldb,info)

  end subroutine LIN

The next step is to compile the Fortran code as .dll file in Windows/.so file in Mac (this is so-called creating shared object for R). In order to compile the code in Windows, you need to have Rtools and R installed. Also, the path of your R should be added into "Path" of system variables. (Steps: Right click Computer icon->Properties->Advanced system settings->Environment Variables->Find and select "Path" in System variables->click "Edit" and add the path of your R, for example, C:\Program Files\R\R-3.0.2\bin;). Now, you are ready to compile the code. You need to open your R as run as administrator, change to the directory containing the Fortran file and then execute a command in R console:

system('R CMD SHLIB my.f90')

You will see that the shared object my.dll is created under the directory.

The final step is to load the shared object into R and run it. Following my example,

dyn.load('my.dll') #load the shared object

a<-diag(c(1,2)) #prepare arguments *a* and *b* for the Fortran subroutine
b<-c(3,4)
.Fortran("LIN",a=as.double(a),b=as.double(b)) #call the Fortran subroutine

Remark: you need to unload the shared object from R so that you can delete or compile over it when R is still running:

dyn.unload('my.dll')

这篇关于使用R的包(例如LAPACK)从Fortran源构建共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆