作者Neisseria (Neisseria)
看板C_and_CPP
标题[讨论] 结合 Lua 和平行运算程式
时间Fri Feb 3 14:59:42 2017
小弟最近在练习用 Lua 写一些 toy library
写到後来就想到和 C 结合看看
节录我的 C 程式:
typedef struct DoubleVector {
size_t size;
double* vec;
} DoubleVector;
// Some code...
DoubleVector* ds_double_vector_add(DoubleVector* v1, DoubleVector* v2) {
size_t len1 = v1->size;
size_t len2 = v2->size;
if (len1 != len2) {
return NULL;
}
DoubleVector* v = ds_double_vector_new(len1);
#pragma omp parallel for
for (int i = 0; i < len1; i++) {
ds_double_vector_set(v, i,
ds_double_vector_get(v1, i) + ds_double_vector_get(v2, i));
}
return v;
}
void ds_double_vector_set(DoubleVector* v, size_t index, double data) {
if (index > v->size - 1) {
fprintf(stderr, "Index out of range\n");
return;
}
v->vec[index] = data;
}
double ds_double_vector_get(DoubleVector* v, size_t index) {
if (index > v->size - 1) {
fprintf(v, "Index out of range\n");
return 0.0;
}
return v->vec[index];
}
// Some code...
当然,这样的程式不太实用,只是练平行程式用的,就别太计较了 Orz
当我的 C 程式用 OpenMP 时,可以顺利编译
但从 Lua 呼叫时,程式会 seg fault
[Update]
後来跟据 LuaJIT 作者在某个 forum 的回答,加入以下两修正方式
1. 编译 LuaJIT 时,加入以下叙述:
make TARGET_FLAGS=-pthread
2. 在呼叫该函式的 Lua 程式尾部加上
os.exit(0)
这个解法不太优雅,总不可能要使用函式库的人
都用这样的条件编译 LuaJIT 和加上这个结尾
但至少目前可以顺利跑完程式
我的想法是改用别的平行程式函式库看看
如果顺利,再来版上分享
查了一下在 Lua 下写平行程式,似乎不太方便
如果要写多执行绪程式,大概都是开多个 Lua 虚拟机,然後再互传 message
[Update]
节录 Lua users wiki 有关於 multithreading programming 的内容:
ANSI C, the standard to which Lua complies, has no mechanism for managing
multiple threads of execution. Threads and the synchronization objects used to
control them are provided by the underlying operating system. You'll need to
use these in order to implement threading in Lua. You will not need to modify
the Lua distribution to do this.
...
Each thread in C which interacts with Lua will need its own Lua state. Each of
these states has its own runtime stack. When a new C thread is started, you can
create its Lua state in one of two ways. One way is to call lua_open. This
creates a new state which is independent of the states in other threads. In
this case, you'll need to initialize the Lua state (for example, loading
libraries) as if it was the state of a new program. This approach eliminates
the need for mutex locks (discussed below), but will keep the threads from
sharing global data.
The other approach is to call lua_newthread. This creates a child state which
has its own stack and which has access to global data.
...
简单地说,因 ANSI C 不提供 multithread 的机制,所以 Lua 也没有实作相关机制
如果需要 multithread program 就要从其他方面着手
而 SciLua 也有 vector 和 matrix 运算的函式库
查了一下原始码,内部呼叫 OpenBLAS
虽然不确定 SciLua 有没有用多执行绪在跑
但 OpenBLAS 本身的确有多执行绪
可能是我程式本身的问题
因为这个程式使用同一块 memory block 也没特别做保护
最近才开始练平行程式,有更好的写法欢迎指教
但我也不确定 Lua 是否适合撰写这样的程式
所以上来和各位大大讨论
先在这里谢过各位大大
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.115.66.16
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1486105185.A.EEE.html
1F:推 littleshan: lua state 并不是 thread safe, 他对多绪环境一无所知 02/03 19:05
的确如此
※ 编辑: Neisseria (59.115.66.16), 02/03/2017 20:52:17
2F:推 LPH66: lua 是有个 coroutine, 不过那跟平行是完全两码子事 02/03 21:31
的确如此,号称 cooperative multithreading 其实就是轮流执行
※ 编辑: Neisseria (59.115.66.16), 02/04/2017 10:33:31