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/m.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燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP