2013-07-11 24 views
1

如何以正確的方式將字符串列表傳遞給opencl內核?如何使用pyopencl將字符串列表傳遞給opencl內核?

我試過這種方式使用緩衝區(見下面的代碼),但我失敗了。

的OpenCL(struct.cl):

typedef struct{ 
      uchar uc[40]; 
} my_struct9; 

inline void try_this7_now(__global const uchar * IN_DATA , 
          const uint IN_len_DATA , 
          __global uchar * OUT_DATA){ 
    for (unsigned int i=0; i<IN_len_DATA ; i++) OUT_DATA[i] = IN_DATA[i]; 
} 

__kernel void try_this7(__global const my_struct9 * pS_IN_DATA , 
         const uint IN_len , 
         __global my_struct9 * pS_OUT){ 

    uint idx = get_global_id(0); 
for (unsigned int i=0; i<idx; i++) try_this7_now(pS_IN_DATA[i].uc, IN_len, pS_OUT[i].uc); 
    } 

的Python(opencl_struct.py):

# -*- coding: utf-8 -*- 

import pyopencl as cl 
import pyopencl.array as cl_array 
import numpy 

ctx = cl.create_some_context() 
queue = cl.CommandQueue(ctx) 
# -------------------------------------------------------- 
LIMIT = 40 
mf = cl.mem_flags 

import ctypes,sys,struct 
""" 
typedef struct{ 
      uchar uc[40]; 
} my_struct9; 
""" 
INlist = [] 
INlist.append("That is VERY cool!") 
INlist.append("It is a list!") 
INlist.append("A big one!") 
#INlist.append("But it failes to output. :-(") # PLAY WITH THOSE 
INlist.append("WTF is THAT?") # PLAY WITH THOSE 
print "INlist : "+str(INlist) 
print "largest string "+str(max(len(INlist[iL]) for iL in range(len(INlist)))) 
strLIMIT=str(LIMIT) 
s7 = struct.Struct( (str(strLIMIT+'s') *len(INlist))) 
IN_host_buffer = ctypes.create_string_buffer(s7.size) 
s7.pack_into(IN_host_buffer, 0, *INlist) 
IN_dev_buffer = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=IN_host_buffer) 

OUT_host_buffer = ctypes.create_string_buffer(s7.size) 

OUT_dev_buffer = cl.Buffer(ctx, mf.WRITE_ONLY, len(OUT_host_buffer)) 
print "> len(OUT_host_buffer) "+str(len(OUT_host_buffer)) 

# ======================================================================================== 
f = open("struct.cl", 'r') 
fstr = "".join(f.readlines()) 
prg = cl.Program(ctx, fstr).build() 

#cl.enqueue_copy(queue, IN_dev_buffer, IN_host_buffer, is_blocking=True) # copy data to device 
cl.enqueue_write_buffer(queue, IN_dev_buffer, IN_host_buffer).wait() 

prg.try_this7(queue, (1,), None, IN_dev_buffer, numpy.uint32(LIMIT), OUT_dev_buffer) 
# ======================================================================================== 
cl.enqueue_copy(queue, OUT_host_buffer, OUT_dev_buffer).wait() 

SSS = s7.unpack_from(OUT_host_buffer,0) 

# unpack here OUT_host_buffer 
print "(GPU) output : "+str(SSS)+" " 

for s in range(len(SSS)): 
print ">>> (GPU) output : "+str(SSS[s]) 

我跑的程序第一次用 「但它failes輸出」 爲第4列表元素。然後我通過增加和減少列表中的元素來玩弄。最後,出現這樣的問題: 程序的輸出應該是(精簡版)

(GPU)輸出:這是非常酷!

(GPU)輸出:它是一個列表!

(GPU)輸出:很大!

(GPU)輸出:WTF是這樣嗎?

但它是:

蟒蛇opencl_struct.py

INLIST:[ '!這是非常酷', '!這是一個 名單', '大一號!','跆拳道是這樣嗎?']

最大字符串18

len(OUT_host_buffer)160(GPU)output:('This is VERY cool!\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00', '這是一個 列表!\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00', 'A large one!\ x00 \ x00 \ x00 \ x00 \ x00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00' , '但它輸出失敗。 : - (\ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00' )

(GPU)輸出:這是非常酷!

(GPU)輸出:它是一個列表!

(GPU)輸出:很大!

(GPU)輸出:但它無法輸出。:-(

正如你可以看到,第四列表元素differes。

所以,也許我的做法是錯誤的或存在pyopencl錯誤或其他地方。

我使用的是NVidia 9400 GPU。

蘭博

回答

1

你的代碼在我看來很複雜。有些部分對我來說不是很清楚。舉例來說,我不明白爲什麼您只創建一個工作項:而不是使用現有的並行

prg.try_this7(queue, (1,), None,...) 

迫使你遍歷你的字符串(內核)。無論如何,如果我很明白,你想發送一些字符串到GPU複製他們在另一個緩衝區,讓他們回到主機端並顯示它們。

如果它是這裏的情況是一個版本僅使用numpy的,當然pyopencl:

import numpy as np 
import pyopencl as cl 


ctx = cl.create_some_context() 
queue = cl.CommandQueue(ctx) 
#The kernel uses one workitem per char transfert 
prog_str = """kernel void foo(global char *in, global char *out, int size){ 
        int idx = get_global_id(0); 
        if (idx < size){ 
         out[idx] = in[idx]; 
        } 
      }""" 
prog = cl.Program(ctx, prog_str).build() 
#Note that the type of the array of strings is '|S40' for the length 
#of third element is 40, the shape is 3 and the nbytes is 120 (3 * 40) 
original_str = np.array(('this is an average string', 
         'and another one', 
         "let's push even more with a third string")) 
mf = cl.mem_flags 
in_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=original_str) 
out_buf = cl.Buffer(ctx, mf.WRITE_ONLY, size=str_size) 
copied_str = np.zeros_like(original_str) 
#here launch the kernel with str_size number of workitems in this case 120 
#this mean that some of the workitems won't process any meaningful char 
#(not all string have a lenght of 40) but it's no biggie 
prog.foo(queue, (str_size,), None, in_buf, out_buf, np.int32(str_size)) 
cl.enqueue_copy(queue, copied_str, out_buf).wait() 
print copied_str 

而且顯示的結果:

['this is an average string' 'and another one' 
"let's push even more with a third string"] 
相關問題