PyOpenCL:如何创建本地内存缓冲区? [英] PyOpenCL: how to create a local memory buffer?
问题描述
这里可能是非常简单的问题,但我已经搜索了几个小时却没有任何内容.
Probably extremely simple question here, but I've been searching for it for hours with nothing to show for.
我有这段代码,我想要一个 256 位(8 uint32)bitstring_gpu 作为设备中的本地内存指针:
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
这稍后用于内核调用:
prg.get_active_hard_locations_64bit(queue, (HARD_LOCATIONS,), None, memory_addresses_gpu.data, bitstring_gpu, distances_gpu.data, hash_table_gpu.data ).wait()
... 并且 bitstring 只是一个随机的 256 位 bitstring(通过 sha256 生成,然后转换为一个 numpy 数组.
... 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
如何让 bitstring_buf 进入快速的本地内存?
How can I make bitstring_buf go to fast, local memory?
相关但不同的问题;这里正在内核中创建缓冲区...
Related, but different question; here the buffer is being created inside the kernel...
如何在 pyopencl 中创建可变大小的 __local 内存?
推荐答案
我可能误解了这个问题,但是如果您希望将数组放置在本地内存中 - 您可以完全在内核中进行.您的 OpenCL 代码(而不是 Python 代码)负责分配和复制到本地内存.
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.
例如,此 OpenCL 代码(在内核中)将创建一个包含 8 个本地 uint 的数组,并将一个 uint 从全局内存复制到新的本地数组中.
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.
希望有帮助!
这篇关于PyOpenCL:如何创建本地内存缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!