- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试在 python 和 lua 之间传递数据(数组),我想使用 Torch7 框架在 lua 中操作数据。我认为这最好通过 C 来完成,因为 python 和 lua 与 C 接口(interface)。另外一些优点是这种方式不需要数据复制(仅传递指针)并且速度很快。
我实现了两个程序,一个是 lua 嵌入到 c 中,另一个是 python 将数据传递给 c。它们在编译为可执行二进制文件时都可以工作。然而,当 c 到 lua 程序改为共享库时,事情就不起作用了。
详情:我使用的是 64 位 ubuntu 14.04 和 12.04。我正在使用 luajit 2.0.2,lua 5.1 安装在/usr/local/依赖库在/usr/local/lib 中, header 在/usr/local/include 中我正在使用 python 2.7
c转lua程序的代码是:
张量.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 中使用共享库时,它会中断。
测试.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,-Erequire() 尝试从导出的符号(在 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
链接器标志从解释器导出它们来完成的。此标志仅适用于可执行文件 中的符号,不适用于共享库。对于共享库,存在类似的东西:dlopen
函数的 RTLD_GLOBAL
标志。默认情况下,编译器命令行上列出的所有共享库都使用 RTLD_LOCAL
加载,但幸运的是,Linux 会重用已经打开的库句柄。所以你可以:
使用 RTLD_GLOBAL
预加载 Lua(JIT) 库 before 它会自动加载(加载 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')
# ...
或者之后使用 dlopen
的 RTLD_NOLOAD
标志更改 Lua(JIT) 库句柄的标志。虽然这个标志不在 POSIX 中,您可能必须使用 C 来这样做。参见例如here .
关于python - 通过共享库连接 Python 和 Torch7(Lua),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24712972/
只是想知道是否有可能找出谁从 Windows 共享中读取了文件(最好使用 .NET,但 win32 native 可以)? 我想做的是创建类似 awstats 的东西对于 Windows 共享,这样我
是否可以列出 Intent.ACTION_SEND ?我的意思是我需要知道是否有人通过 action_send 在 Facebook 上分享或在 Twitter 上发推文。 最佳答案 也许你想要一个更
我正在使用 Google Apps 应用程序。实际上,我想在不使用密码的情况下访问另一个 ID。我使用了 OAuth,它运行良好。但我无法分享特定人的日历。我尝试了以下代码。 GoogleOAuthP
我怎样才能只创建模拟器...可能吗?我知道,设备需要分发证书。 最佳答案 您只需将应用程序目录从 iPhone 模拟器复制到另一个实例/操作系统版本,它就应该可以工作。 因此,如果您想分发 3.1.3
我想使用多阶段构建来避免每次构建应用程序时都下载我的 Java 项目所需的所有 Maven 依赖项。 我正在考虑在第一阶段解决 Maven 依赖项,然后在第二阶段构建应用程序,这将需要访问在前一阶段下
我正在寻找保护用户下载内容的初步想法。用户下载充满有趣资源的 zip 文件,这些资源被提取到本地文件系统中以供应用程序使用。我的目标是防止用户通过互联网将下载的资源共享给其他用户(假设他们获得了对文件
我想知道在具有移动和桌面版本的网站上共享身份验证、 session 管理等的最佳方法是什么。我们正在运行 Tomcat,并且更愿意将移动站点和桌面站点的应用程序保持在不同的节点上。 我看过类似的帖子,
我发现了这个单例的实现。我怎样才能创建指向它的指针或共享指针?` 为什么这不起作用?自动测试 = Singleton::Instance(); class Singleton { public: st
我有一个 heroku 项目,我想与其他人分享。作为the instructions describe ,我使用 virtualenv 来管理环境和依赖项。有没有办法在新机器上从 requiremen
Maven 将所有 jar 存储在本地存储库 ~/.m2/repository/ 下。用户多时占用空间大。 那么,是否可以由多个用户共享这个本地存储库,或许在不同的目录结构下? 最佳答案 简单的回答
为什么共享 worker 在重新加载页面时死了?应该是复活了我该如何解决这个问题? 重装前 重新加载后(在example.com上按F5) parent worker var port = new S
我正在开发多个小型应用程序,这些应用程序将共享通用和共享模块和 Assets 。 关于如何创建项目结构的部分在这里回答:https://stackoverflow.com/a/61254557/135
我在 RHEL 上安装了 jenkins (localhost:8080),我能够成功地构建代码 现在,我想设置主/从代理。 我的笔记本电脑将充当“Master Jenkins”,而我同事的笔记本电脑
我有这种方法可以根据我使用的 EXTRA_STREAM 共享文本文件或图片。我有这两个我可以选择 i.putExtra(Intent.EXTRA_STREAM, uri); i.putExtra(In
我正在使用 R 中的一个数据分析项目,我正在使用 R 中的敏感私有(private)数据进行一些逻辑和多级建模。我爱上了 。预订 包,我已经创建了一本关于我们的工作流程和分析管道的相当广泛的书。问题是
我正在构建的应用程序需要在 UITabBarController 框架内为多个 View (及其 subview )显示共享的自定义 UIToolbar。自定义工具栏的内容在所有 View 中都是相同
我有多个应用程序,我想共享相同的 eslint 配置: - project_root/ - app1/ - node_modules/ - eslint.rc
我有多个 Electron 应用程序。一个是主应用程序,其他几个功能应用程序。主应用程序上的按钮很少,这将导致功能应用程序打开。这里的问题是每个应用程序都有一个主进程,该进程导致要利用更多的CPU。是
我正在开发一个 Node.js 后端,它通过 websocket 与一些桌面客户端进行通信,而服务器端的通信是从 Web 前端发起的。一切正常,因为我将 SockJS Connection 实例存储在
我对托管多个网站的服务器上的多个用户帐户使用私有(private) SSH key 和无密码条目。 我为每个用户帐户使用相同的私钥。 (因为我很懒?或者那是“正确”的方式)。 我现在想授权该国不同地区
我是一名优秀的程序员,十分优秀!