Ich versuche, numpy Array zum Kernel als Array von opencl Vektor übergeben. (numpy Array von np.int32 -> int3 *) Aber das Ergebnis scheint verwildert. Es wird sehr geschätzt werden, wenn jemand erklären, warum es passiert ist.Übergeben Array von Vektor in pyOpencl
Vielen Dank im Voraus.
Der Quellcode:
import pyopencl as cl
import numpy as np
platforms = cl.get_platforms()
ctx = cl.Context(dev_type=cl.device_type.GPU, properties=[(cl.context_properties.PLATFORM, platforms[0])])
queue = cl.CommandQueue(ctx)
rowcnt = 3
ipt = np.linspace(1, rowcnt * 3, num=rowcnt * 3, dtype=np.int32)
rst = np.ones((rowcnt * 3), dtype=np.int32)
mf = cl.mem_flags
iptbuf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=ipt)
rstbuf = cl.Buffer(ctx, mf.WRITE_ONLY, rst.nbytes)
src = '''
__kernel void test(__global int3* ipt, __global int* rst) {
int idx = get_global_id(0);
rst[idx * 3] = ipt[idx].x;
rst[idx * 3 + 1] = ipt[idx].y;
rst[idx * 3 + 2] = ipt[idx].z;
}
'''
prg = cl.Program(ctx, src).build()
prg.test(queue, (rowcnt,), None, iptbuf, rstbuf)
cl.enqueue_copy(queue, rst, rstbuf)
print ipt
print rst
Erwartete Ausgabe:
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
Ergebnisausgabe:
[1 2 3 4 5 6 7 8 9]
[1 2 3 5 6 7 9 0 0]