PyOpenCL: how to create a local memory buffer?

蓝咒 提交于 2021-02-05 07:36:59

问题


Probably extremely simple question here, but I've been searching for it for hours with nothing to show for.

I have this piece of code, I'd like to have a 256-bit (8 uint32) bitstring_gpu as a localmemory pointer in the device:

def Get_Bitstring_GPU_Buffer(ctx, bitstring):
    bitstring_gpu = cl.Buffer(ctx, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=bitstring)
return bitstring_gpu

This is later used on a kernel call:

prg.get_active_hard_locations_64bit(queue, (HARD_LOCATIONS,), None, memory_addresses_gpu.data, bitstring_gpu, distances_gpu.data, hash_table_gpu.data ).wait()

... and bitstring is just a random 256-bit bitstring (generated through sha256, then transformed into a numpy array.

def Get_Random_Bitstring():    
    bitstring = address_space_through_sha256_SDM.get_bitstring(str(random.randrange(2**32-1)))
return bitstring

How can I make bitstring_buf go to fast, local memory?

Related, but different question; here the buffer is being created inside the kernel...

How to create variable sized __local memory in pyopencl?


回答1:


I may have misunderstood the question, but if you want your array placed in local memory - you do that entirely inside the kernel. Your OpenCL code, not your Python code, is responsible for allocating and copying to local memory.

For example, this OpenCL code - inside a kernel - will create an array of 8 local uints and and copy one uint from global memory into the new local array.

__local uint my_local_array[8];

my_local_array[0] = my_global_array[0];

When you want your results back from local memory, you copy from local to global, then copy from global back to the host.

Hope that helps!



来源:https://stackoverflow.com/questions/30271634/pyopencl-how-to-create-a-local-memory-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!