当前位置: 首页 > 面试题库 >

通过共享库连接Python和Torch7(Lua)

彭涵衍
2023-03-14
问题内容

我正在尝试在python和lua之间传递数据(数组),并且我想使用Torch7框架在lua中处理数据。我认为这可以通过C来最好地完成,因为python和lua与C进行了接口。另外一些优点是不需要这种方式进行数据复制(仅传递指针)并且速度很快。

我实现了两个程序,一个程序将lua嵌入到c中,另一个程序将python将数据传递给c。它们都编译为可执行二进制文件时都可以工作。但是,当将c to
lua程序改为共享库时,此操作将无效。

详细信息:我正在使用64位ubuntu 14.04和12.04。我正在使用luajit 2.0.2和/ usr / local /中安装的lua
5.1,依赖库位于/ usr / local / lib中,标头位于/ usr / local / include中,我正在使用python 2.7

C到lua程序的代码是:

张量

require 'torch'

function hi_tensor(t)
   print(‘Hi from lua')
   torch.setdefaulttensortype('torch.FloatTensor')
   print(t)
return t*2
end

cluaf.h

void multiply (float* array, int m, int n, float *result, int m1, int n1);

cluaf.c

#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luaT.h"
#include "TH/TH.h"

void multiply (float* array, int m, int n, float *result, int m1, int n1)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs( L );

    // loading the lua file
    if (luaL_loadfile(L, "tensor.lua") || lua_pcall(L, 0, 0, 0))
    {
        printf("error: %s \n", lua_tostring(L, -1));
    }

    // convert the c array to Torch7 specific structure representing a tensor
    THFloatStorage* storage =  THFloatStorage_newWithData(array, m*n);
    THFloatTensor* tensor = THFloatTensor_newWithStorage2d(storage, 0, m, n, n, 1);
    luaT_newmetatable(L, "torch.FloatTensor", NULL, NULL, NULL, NULL);

    // load the lua function hi_tensor
    lua_getglobal(L, "hi_tensor");
    if(!lua_isfunction(L,-1))
    {
        lua_pop(L,1);
    }

    //this pushes data to the stack to be used as a parameter
    //to the hi_tensor function call
    luaT_pushudata(L, (void *)tensor, "torch.FloatTensor");

    // call the lua function hi_tensor
    if (lua_pcall(L, 1, 1, 0) != 0)
    {
        printf("error running function `hi_tensor': %s \n", lua_tostring(L, -1));
    }

    // get results returned from the lua function hi_tensor
    THFloatTensor* z = luaT_toudata(L, -1, "torch.FloatTensor");
    lua_pop(L, 1);
    THFloatStorage *storage_res =  z->storage;
    result = storage_res->data;

    return ;
}

然后测试我做:

luajit -b tensor.lua tensor.o

gcc -w -c -Wall -Wl,-E -fpic cluaf.c -lluajit -lluaT -lTH -lm -ldl -L /usr/local/lib

gcc -shared cluaf.o tensor.o -L/usr/local/lib -lluajit -lluaT -lTH -lm -ldl -Wl,-E -o libcluaf.so

gcc -L. -Wall -o test main.c -lcluaf

./test

输出:

Hi from lua
 1.0000  0.2000
 0.2000  5.3000
[torch.FloatTensor of dimension 2x2]

c result 2.000000 
c result 0.400000 
c result 0.400000 
c result 10.60000

到目前为止,一切都很好。但是,当我尝试在python中使用共享库时,它会中断。

test.py

from ctypes import byref, cdll, c_int
import ctypes
import numpy as np
import cython

l = cdll.LoadLibrary(‘absolute_path_to_so/libcluaf.so')

a = np.arange(4, dtype=np.float64).reshape((2,2))
b = np.arange(4, dtype=np.float64).reshape((2,2))

l.multiply.argtypes = [ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int,     ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int]
a_list = []
b_list = []

for i in range(a.shape[0]):
    for j in range(a.shape[1]):
            a_list.append(a[i][j])

for i in range(b.shape[0]):
     for j in range(b.shape[1]):
        b_list.append(b[i][j])

arr_a = (ctypes.c_float * len(a_list))()
arr_b = (ctypes.c_float * len(b_list))()

l.multiply(arr_a, ctypes.c_int(2), ctypes.c_int(2), arr_b, ctypes.c_int(2), ctypes.c_int(2))

我跑:

python test.py

输出为:

error: error loading module 'libpaths' from file '/usr/local/lib/lua/5.1/libpaths.so':
    /usr/local/lib/lua/5.1/libpaths.so: undefined symbol: lua_gettop

我在网络上的任何地方都搜索了此错误,但他们要么建议(1)包括-Wl,-E以导出符号,要么建议(2)添加对链接所做的依赖关系。(1)我有-
Wl,-E,但似乎什么也没做。(2)我已经包含了依赖项(-L / usr / local / lib -lluajit -lluaT -lTH -lm
-ldl)

python测试不是在导入共享库时失败,而是在lua内部的’require torch’被调用时失败。在这种情况下,这与我发现的其他情况也有所不同。

luajit.so定义了符号lua_gettop(通过nm /usr/local/lib/luajit.so可以看到)lua.h定义了LUA_API
int(lua_gettop)(lua_State * L);

我想在将c编译为二进制文件时可以用,因为它可以在lua.h中找到所有符号,但是使用共享库却不会从luajit.so中选择lua_gettop(我不知道为什么)。

www.luajit.org/running.html说:“在大多数基于ELF的系统(例如Linux)上,在链接应用程序时,您需要显式导出全局符号,例如:-Wl,-E
require()尝试加载嵌入式程序。来自导出符号(在Windows上为* .exe或lua51.dll)和package.cpath中共享库的字节码数据。

package.cpath和package.path为:

./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so

./?.lua;/usr/local/share/luajit-2.0.2/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua

这是nm libcluaf.so返回的内容:

00000000002020a0 B __bss_start
00000000002020a0 b completed.6972
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000a50 t deregister_tm_clones
0000000000000ac0 t __do_global_dtors_aux
0000000000201dd8 t __do_global_dtors_aux_fini_array_entry
0000000000202098 d __dso_handle
0000000000201de8 d _DYNAMIC
00000000002020a0 D _edata
00000000002020a8 B _end
0000000000000d28 T _fini
0000000000000b00 t frame_dummy
0000000000201dd0 t __frame_dummy_init_array_entry
0000000000000ed0 r __FRAME_END__
0000000000202000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000000918 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201de0 d __JCR_END__
0000000000201de0 d __JCR_LIST__
                 w _Jv_RegisterClasses
                 U lua_getfield
0000000000000d99 R luaJIT_BC_tensor
                 U luaL_loadfile
                 U luaL_newstate
                 U luaL_openlibs
                 U lua_pcall
                 U lua_settop
                 U luaT_newmetatable
                 U lua_tolstring
                 U luaT_pushudata
                 U luaT_toudata
                 U lua_type
0000000000000b35 T multiply
                 U printf@@GLIBC_2.2.5
0000000000000a80 t register_tm_clones
                 U THFloatStorage_newWithData
                 U THFloatTensor_newWithStorage2d
00000000002020a0 d __TMC_END__

提前致谢


问题答案:

在Linux上,Lua模块不直接链接到Lua库,而是期望找到已经加载的Lua
API函数。通常,这是通过使用-Wl,-E链接器标志从解释器中导出它们来完成的。该标志仅适用于 执行文件中的
符号,不适用于共享库。对于共享库,存在类似的东西:函数的RTLD_GLOBAL标志dlopen。默认情况下,编译器命令行上列出的所有共享库都使用RTLD_LOCAL代替加载,但是幸运的是Linux重用了已经打开的库句柄。因此,您可以:

在自动加载Lua(JIT)库RTLD_GLOBAL 之前,先 对其进行预加载(在加载时发生libcluaf.so):

from ctypes import byref, cdll, c_int
import ctypes

lualib = ctypes.CDLL("libluajit-5.1.so", mode=ctypes.RTLD_GLOBAL)
l = cdll.LoadLibrary('absolute_path_to_so/libcluaf.so')
# ...

或稍后使用RTLD_NOLOAD标志更改Lua(JIT)库句柄的标志dlopen。但是,此标志不在POSIX中,您可能必须使用C来这样做。参见例如这里。



 类似资料:
  • 用Python来编写网站,必须要能够通过python操作数据库,所谓操作数据库,就是通过python实现对数据的连接,以及对记录、字段的各种操作。上一讲提到的那种操作方式,是看官直接通过交互模式来操作数据库。 安装python-MySQLdb 要想通过python来操作数据库,还需要在已经安装了mysql的基础上安装一个称之为mysqldb的库,它是一个接口程序,python通过它对mysql数据

  • 笔记操作系统是ubuntu 14* 我在MYSQL服务器上的连接让我头疼不已。我尝试通过我的公共ip连接到我的MYSQL服务器内部。但我无法连接。当我连接到本地主机和外部连接时,我能够连接。 我注释了绑定地址,这应该将地址绑定到0.0.0.0。我还试图将绑定广告设置为0.0.0.0。这也行不通。 我的主机文件设置如下: 当我将端口80上的telnet连接到我的公共ip时,没有问题。 我得到: 但当

  • 我试图将我编译的简单共享库(libhello.so)链接到我的本机C++代码中。库libhello.so文件是: 1。Get13.h 2.get13.cpp Android文件是: 1。myapplication/jni/android.mk 问题是,在gradle构建过程中,我在文件native-lib.cpp中得到了这个错误: 我使用android NDK包中包含的make_standalon

  • 我正在使用谷歌驱动器开发一个应用程序。我希望用户能够通过链接共享文件,将权限设置为和,如Google Developers文档中所述。 但是,我不知道分享什么链接。当我在Google Drive browser界面中共享文件时,我会看到的格式: 这种格式的链接不是文件资源对象的一部分,也不是从设置权限的http调用返回的。我希望有人能给我解释一下如何通过REST API获得这个链接?

  • 我正在开发一个应用程序,它必须通过whatsapp共享mp3文件。 我现在的代码如下: 例如,我可以通过Gmail成功共享,但通过whatsapp无法实现。这是我的代码的问题还是whatsapp不允许你共享mp3文件? 提前感谢!

  • 问题内容: 背景: 我们每天晚上都有一份詹金斯工作()构建可交付成果。我们还有另一项工作(),第二天通过专有协议将可交付成果推送到生产机器。这是因为某些生产机器仅在一天中的某些时段可用(这也使我们有机会修复任何最后的构建中断)。 需要访问作业构建的可交付成果(因此需要访问同一工作空间)。我们有多个节点和并发的内部版本(因此是不可预测的工作空间),并且由于资源有限,所以最好不要将作业绑定到固定的节点