看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) vc2017 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) OpenCL 問題(Question): 我在kernal中傳入3個大小不同的一維陣列 我如何知道get_global_id(0) 回傳的index是屬於誰的? 餵入的資料(Input): 三個大小不同的一維陣列,a_Cost為輸出 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) __kernel void CostDataMat_kernel(__global const int a_RangeUpScale, __global const int a_ImgWidth, __global const int a_ImgHeight, __global const char *a_Left, __global const char *a_Right, __global int *a_Cost) { // Get the index of the current element to be processed int index = get_global_id(0); int ImgSize = a_ImgWidth*a_ImgHeight; int layer = index/ImgSize; int layer_mod = index%ImgSize; int i = layer_mod/a_ImgWidth; int j = layer_mod%a_ImgWidth; int Y_Abs = 0; int Cb_Abs = 0; int Cr_Abs = 0; // Do the operation Y_Abs = abs(a_Left[layer_mod*3] - a_Right[layer_mod*3]); Cr_Abs = abs(a_Left[layer_mod*3+1] - a_Right[layer_mod*3+1]); Cb_Abs = abs(a_Left[layer_mod*3+2] - a_Right[layer_mod*3+2]); if(j >= a_RangeUpScale) { a_Cost[index] = Y_Abs + ((Cr_Abs + Cb_Abs)>>1); } else { if(layer < j+1) { a_Cost[index] = Y_Abs + ((Cr_Abs + Cb_Abs)>>1); } else { a_Cost[index] = -1; } } } 補充說明(Supplement): 另外想請教各位大大,如何知道kernal有哪些函數可以用 ex: abs 等等的 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.230.27 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1503390765.A.64C.html ※ 編輯: hardman1110 (114.34.230.27), 08/22/2017 16:50:46
LPH66: index 是 kernel 的編號, 不是陣列的編號 08/22 17:04
LPH66: kernel 做事的概念是「幾號 kernel 去拿陣列的哪幾格做事」 08/22 17:06
LPH66: 只是一般來說會因為方便讓 kernel 以自己編號去取元素 08/22 17:06
LPH66: 當然其他狀況也有可能是會成一個 kernel 抓很多格做事 08/22 17:07
LPH66: 這時去取哪幾格時就要自己去算哪個 kernel 該取哪幾格 08/22 17:07
LPH66: 另外你最後的問題, 去找 OpenCL 標準文件裡面寫得很清楚 08/22 17:09
LPH66: 或者我印象中有看過一個是把各種函數整理成的 cheat sheet 08/22 17:09
LPH66: (包含 host 端和 kernel 端的函數都有) 08/22 17:09
hardman1110: 我傳進去的陣列型態大小都不一樣,我該如何知道切了 08/22 17:11
hardman1110: 多少kernal(work item) 然後每個work item包含每個 08/22 17:12
hardman1110: 陣列的哪幾格? 08/22 17:13
LPH66: 這是你在決定的事; 你要決定每個 work item 的工作量是多少 08/22 17:25
LPH66: 每個人要怎麼拿到他所要的工作量做事 08/22 17:25
hardman1110: 查到clEnqueueNDRangeKernel可控制要切多少workitem 08/22 17:26
LPH66: 有的時候甚至可以寫說「幾號以後不用做那邊的事」之類的 08/22 17:26
hardman1110: 但每個workitem對應到陣列哪一段就不清楚原理了 08/22 17:26
LPH66: 然後再 enqueue 你要的數量的 workitem 去做事 08/22 17:26
LPH66: 例如如果我要一個 workitem 做一格, 那就是照編號取元素 08/22 17:27
LPH66: 如果我要一個 workitem 做兩格, 那可以編號乘二再取兩格 08/22 17:27
LPH66: 重點是你要搞懂你是怎麼分配工作下去的 08/22 17:28
LPH66: 一個簡單的比方是你請了一堆工讀生分擔工作 08/22 17:29
LPH66: 那你總要告訴工讀生「你的工作範圍在哪裡」 08/22 17:29
LPH66: 而做出一個簡單易懂的分配方式就是你在問的"對應" 08/22 17:29
LPH66: 讓每個工讀生能只從自己分到的編號直接領到工作做 08/22 17:30
hardman1110: 這樣說起來我要index以哪個陣列為主就enqueue那個陣 08/22 17:45
hardman1110: 列的size嚕? 08/22 17:45