如何将多维数组传递给 pyopencl.algorithm.copy_if() -- PyOpenCL,
How to pass a Multi-dimensional array to pyopencl.algorithm.copy_if() -- PyOpenCL,
以下代码向我发出警告 "warning: incompatible integer to pointer conversion passing '__global int' to parameter of type '__global int *'; take the address with &",但没有产生预期的结果。
import pyopencl as cl
import pyopencl.array
import pyopencl.algorithm
import numpy as np
platform = cl.get_platforms()
my_devices = platform[0].get_devices(device_type=cl.device_type.ALL)
ctx = cl.Context(devices=my_devices)
queue = cl.CommandQueue(ctx)
aryary = np.array([[10, 11, 12, 13, 14, 15, 16, 17], [1, 2, 3, 4, 0, 0, 0, 0], [108, 0, 0, 0, 0, 0, 0, 0]], np.int32)
cl_aryary = cl.array.to_device(queue, aryary)
lenary = np.array([8, 4, 1], np.int32)
cl_lenary = cl.array.to_device(queue, lenary)
result = cl.algorithm.copy_if(
cl_aryary,
"sum_array(ary[i], len[i]) == 108",
extra_args=[('len', cl_lenary)],
preamble='''
int sum_array(__global int *a, int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + a[i];
}
return(sum);
}
''',
queue=queue
)
print(result)
编辑:供参考:
https://documen.tician.de/pyopencl/algorithm.html#module-pyopencl.algorithm
函数int sum_array(__global int *a, int num_elements)
的调用在"sum_array(ary[i], len[i]) == 108",
行,但应该有一个指针而不是函数中定义的数组元素。那将是:
"sum_array(&ary[i], len[i]) == 108",
以下代码向我发出警告 "warning: incompatible integer to pointer conversion passing '__global int' to parameter of type '__global int *'; take the address with &",但没有产生预期的结果。
import pyopencl as cl
import pyopencl.array
import pyopencl.algorithm
import numpy as np
platform = cl.get_platforms()
my_devices = platform[0].get_devices(device_type=cl.device_type.ALL)
ctx = cl.Context(devices=my_devices)
queue = cl.CommandQueue(ctx)
aryary = np.array([[10, 11, 12, 13, 14, 15, 16, 17], [1, 2, 3, 4, 0, 0, 0, 0], [108, 0, 0, 0, 0, 0, 0, 0]], np.int32)
cl_aryary = cl.array.to_device(queue, aryary)
lenary = np.array([8, 4, 1], np.int32)
cl_lenary = cl.array.to_device(queue, lenary)
result = cl.algorithm.copy_if(
cl_aryary,
"sum_array(ary[i], len[i]) == 108",
extra_args=[('len', cl_lenary)],
preamble='''
int sum_array(__global int *a, int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + a[i];
}
return(sum);
}
''',
queue=queue
)
print(result)
编辑:供参考:
https://documen.tician.de/pyopencl/algorithm.html#module-pyopencl.algorithm
函数int sum_array(__global int *a, int num_elements)
的调用在"sum_array(ary[i], len[i]) == 108",
行,但应该有一个指针而不是函数中定义的数组元素。那将是:
"sum_array(&ary[i], len[i]) == 108",