从Fortran将数组作为函数参数传递给lua

库马尔

我正在寻找Fortran示例(也是接口函数)以将数组作为参数传递给lua函数。我能够使用fortlua项目启动。但是提供的示例一次传递了一个元素。感谢任何帮助。

--Lua code 

local q1
local q2
function getoutput( qout1, qout2)
-- qout1 and qout2 are arrays with some dimension
  q1 = qout1
  q2 = qout2
end

-在我使用的fortran中

config_function('getoutput', args, 2, cstatus)

但是设置args是我在寻求帮助的地方。以下代码完成了标量参数变量而不是数组的工作。

!> Evaluate a function in the config file and get its result.
FUNCTION config_function(name,args,nargs,status)
    REAL :: config_function
    CHARACTER(LEN=*) :: name
    REAL, DIMENSION(nargs) :: args
    REAL(KIND=c_double) :: anarg
    INTEGER :: nargs
    INTEGER :: status
    INTEGER :: iargs
    INTEGER(c_int) :: stackstart

    stackstart = lua_gettop(mluastate)

    config_function = 0
    status = 0


    CALL lua_getglobal(mluastate,TRIM(name)//C_NULL_CHAR)
    IF ( lua_type(mluastate,-1) .eq. LUA_TFUNCTION ) THEN
        DO iargs = 1,nargs
          anarg = args(iargs)
          CALL lua_pushnumber(mluastate,anarg)
        ENDDO
        IF (lua_pcall(mluastate,nargs,1,0) .eq. 0) THEN
          if (lua_isnumber(mluastate,-1) .ne. 0) THEN
            config_function = lua_tonumber(mluastate,-1)
            CALL lua_settop(mluastate,-2)
          ELSE
            ! Nothing to pop here
            status=-3
          ENDIF
        ELSE
          CALL lua_settop(mluastate,-2)
          status=-2
        ENDIF
    ELSE
        CALL lua_settop(mluastate,-2)
        status=-1
    ENDIF
    IF (stackstart .ne. lua_gettop(mluastate)) THEN
       WRITE(*,*) 'The stack is a different size coming out of config_function'
    ENDIF
END FUNCTION config_function
哈拉尔德克

为了进一步说明我的观点,这是一个小程序,在Aotus的帮助下实现了数组参数

program aot_vecarg_test
  use flu_binding, only: flu_State, flu_settop

  use aotus_module, only: open_config_file, close_config
  use aot_fun_module, only: aot_fun_type, aot_fun_do, &
    &                       aot_fun_put, aot_fun_open, &
    &                       aot_fun_close
  use aot_references_module, only: aot_reference_for, aot_reference_to_top
  use aot_table_module, only: aot_table_open, aot_table_close, &
    &                         aot_table_from_1Darray

  implicit none

  type(flu_State) :: conf
  type(aot_fun_type) :: luafun
  integer :: iError
  character(len=80) :: ErrString
  real :: args(2)
  integer :: argref
  integer :: arghandle

  args(1) = 1.0
  args(2) = 2.0

  call create_script('aot_vecarg_test_config.lua')
  write(*,*)
  write(*,*) 'Running aot_vecarg_test...'
  write(*,*) ' * open_config_file (aot_vecarg_test_config.lua)'
  call open_config_file(L = conf, filename = 'aot_vecarg_test_config.lua', &
    &                   ErrCode = iError, ErrString = ErrString)
  if (iError /= 0) then
    write(*,*) ' : unexpected FATAL Error occured !!!'
    write(*,*) ' : Could not open the config file aot_ref_test_config.lua:'
    write(*,*) trim(ErrString)
    STOP
  end if
  write(*,*) '  : success.'

  ! Create a table with data
  call aot_table_from_1Darray( L       = conf,      &
    &                          thandle = arghandle, &
    &                          val     = args       )
  ! Create a reference to this table
  call flu_setTop(L = conf, n = arghandle)
  argref = aot_reference_for(L = conf)

  ! Start the processing of the function
  call aot_fun_open(L = conf, fun = luafun, key = 'print_array')
  ! Put the previously defined table onto the stack by using the reference
  call aot_reference_to_top(L = conf, ref = argref)
  ! Put the top of the stack to the argument list of the Lua function
  call aot_fun_put(L = conf, fun = luafun)
  ! Execute the Lua function
  call aot_fun_do(L = conf, fun = luafun, nresults = 0)
  call aot_fun_close(L = conf, fun = luafun)

  write(*,*) ' * close_conf'
  call close_config(conf)
  write(*,*) '  : success.'
  write(*,*) '... Done with aot_vecarg_test.'
  write(*,*) 'PASSED'

contains

  subroutine create_script(filename)
    character(len=*) :: filename

    open(file=trim(filename), unit=22, action='write', status='replace')
    write(22,*) '-- test script for vectorial argument'
    write(22,*) 'function print_array(x)'
    write(22,*) '  for i, num in ipairs(x) do'
    write(22,*) '    print("Lua:"..num)'
    write(22,*) '  end'
    write(22,*) 'end'
    close(22)
  end subroutine create_script

end program aot_vecarg_test

这利用了一些帮助例程aot_table_from_1Darray,该例程为实数数组创建Lua表。看一下它的代码,看看如何将数据放入表中。

然后,我们创建对该表的引用,以方便日后查找它,并将其作为参数传递给Lua函数。该示例创建了相应的Lua脚本本身,该脚本定义了一个简单函数,该函数期望将单个表作为输入并打印每个表条目。运行此命令将产生以下输出:

 Running aot_vecarg_test...
  * open_config_file (aot_vecarg_test_config.lua)
   : success.
Lua:1.0
Lua:2.0
  * close_conf
   : success.
 ... Done with aot_vecarg_test.
 PASSED

其中以Lua开头的两行是由Lua函数print_array编写的。

还有其他可能的解决方案,但是我希望这至少可以使人们对如何实现这一想法有所了解。我们还可以考虑扩展aot_fun_put接口以照顾数组本身。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数组作为参数传递给函数

来自分类Dev

将表作为参数传递给Lua中的函数

来自分类Dev

将数组数组作为参数传递给函数

来自分类Dev

将数组作为参数传递给pick中的函数

来自分类Dev

将数组范围作为参数传递给函数?

来自分类Dev

JavaScript将数组作为单独的参数传递给函数

来自分类Dev

将数组作为单个参数传递给php函数

来自分类Dev

将参数作为数组传递给PowerShell函数

来自分类Dev

将数组从结构作为参数传递给函数

来自分类Dev

将数组作为参数传递给C函数

来自分类Dev

将数组作为参数传递给pick中的函数

来自分类Dev

将指针传递给char数组作为函数的参数-C

来自分类Dev

将php数组作为链接参数传递给ajax函数

来自分类Dev

将numpy数组作为参数传递给theano函数

来自分类Dev

将结构数组作为参数传递给函数

来自分类Dev

将多个数组作为参数传递给函数

来自分类Dev

Fortran将数组传递给函数

来自分类Dev

将函数作为参数传递给函数

来自分类Dev

将函数作为参数传递给函数

来自分类Dev

将矩阵作为参数传递给函数

来自分类Dev

将函数作为参数传递给操作

来自分类Dev

将函数作为参数传递给方法

来自分类Dev

将函数作为参数传递给Action

来自分类Dev

将向量作为参数传递给函数

来自分类Dev

将数组作为参数传递给printf

来自分类Dev

将2d数组作为1d参数fortran传递给子例程

来自分类Dev

将数组作为函数参数传递给setTimeout的行为与传递变量不同

来自分类Dev

将2D数组作为参数传递给采用1D数组的函数

来自分类Dev

Bash脚本:将数组作为参数传递给函数并打印该数组