C_and_CPP 板


LINE

小弟最近在练习用 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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP