2017-05-26 10 views
0

以下は、参照のための私の作業コードです:PyopenclでGPUメモリを解放し、同じバッファを別の配列に使用するには?

vector = numpy.array([1, 2, 4, 8], numpy.float32) #cl.array.vec.float4 
matrix = numpy.zeros((1, 4), cl.array.vec.float4) 
matrix[0, 0] = (1, 2, 4, 8) 
matrix[0, 1] = (16, 32, 64, 128) 
matrix[0, 2] = (3, 6, 9, 12) 
matrix[0, 3] = (5, 10, 15, 25) 
# vector[0] = (1, 2, 4, 8) 


platform=cl.get_platforms() #gets all platforms that exist on this machine 
device=platform[0].get_devices(device_type=cl.device_type.GPU) #gets all GPU's that exist on first platform from platform list 
context=cl.Context(devices=[device[0]]) #Creates context for all devices in the list of "device" from above. context.num_devices give number of devices in this context 
print("everything good so far") 
program=cl.Program(context,""" 
__kernel void matrix_dot_vector(__global const float4 * matrix,__global const float *vector,__global float *result) 
{ 
int gid = get_global_id(0); 

result[gid]=dot(matrix[gid],vector[0]); 
} 

""").build() 
queue=cl.CommandQueue(context) 
# queue=cl.CommandQueue(context,cl_device_id device) #Context specific to a device if we plan on using multiple GPUs for parallel processing 

mem_flags = cl.mem_flags 
matrix_buf = cl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=matrix) 
vector_buf = cl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=vector) 
matrix_dot_vector = numpy.zeros(4, numpy.float32) 
global_size_of_GPU= 0 
destination_buf = cl.Buffer(context, mem_flags.WRITE_ONLY, matrix_dot_vector.nbytes) 
# threads_size_buf = cl.Buffer(context, mem_flags.WRITE_ONLY, global_size_of_GPU.nbytes) 
program.matrix_dot_vector(queue, matrix_dot_vector.shape, None, matrix_buf, vector_buf, destination_buf) 

## Step #11. Move the kernel’s output data to host memory. 
cl.enqueue_copy(queue, matrix_dot_vector, destination_buf) 
# cl.enqueue_copy(queue, global_size_of_GPU, threads_size_buf) 
print(matrix_dot_vector) 
# print(global_size_of_GPU) 

# COPY SAME ARRAY FROM GPU AGAIN 
cl.enqueue_copy(queue, matrix_dot_vector, destination_buf) 
print(matrix_dot_vector) 
print('copied same array twice') 
  1. 私はGPUにmatrix_buf & destination_bufにメモリを解放するにはどうすればよいです。 1つは読み取り専用で、他は書き込みのみです。
  2. は、pyopenclで新しいバッファを作成することなく、同じmatrix_bufに異なる行列配列をロードできます。同じバッファ内に新しい データをロードすると、それよりはるかに速く、同じサイズを再作成すると、毎回バッファーが になります。
  3. 古いバッファーでロードする新しい配列 は、そのバッファー内にあった古い配列よりサイズが小さい場合は問題ありません。 新しい配列はバッファのサイズとまったく同じでなければなりませんか? 1.再

答えて

0
  1. matrix_buf.release()& destination_buf.release() - これはGPUにそれぞれのバッファに割り当てられたメモリを解放します。そのメモリを解放する方がいいのであれば、メモリエラーにならないようにする。 GPU機能が終了すると、すべてのGPUメモリが自動的にpyopenclによってクリアされます。 - {doqtor}
  2. cl.enqueue_copy(queue、matrix_buf、matrix_2) - 新しい行列bufを再作成せずにmatrix_bufに新しいmatrix_2配列をロードする。
  3. 既存のバッファを再利用してその一部を使用しても問題ありません。カーネル側では、アクセスしたい部分を制御します。 -
0
  • 私は、バッファの変数が範囲外であるか明示的release()を呼び出すことができたときに、バッファが解放されると信じています。この場合、バッファが読み込みか書き込みのみかは重要ではありません。
  • Re 2.ホスト側から変更可能な配列へのアクセスを返すpyopencl.enqueue_map_buffer()を試してください。 More here
  • Re 3.既存のバッファを再利用してその一部を使用しても構いません。カーネル側では、アクセスしたい部分を制御できます。
+0

{doqtorによって} uが一例で「リリース()」& 『pyopencl.enqueue_map_bufferを()』説明していただけます私はあなたが提供されたリンクが、その不可解を読んでみました –

+0

ここでの例を見てください:。。 [pyopencl.buffer.release](http://nullege.com/codes/search?cq=pyopencl.buffer.release)および[pyopencl.enqueue_map_buffer](http://nullege.com/codes/search?cq=pyopencl .enqueue_map_buffer) – doqtor

関連する問題