【问题标题】:How to pass a Multi-dimensional array to pyopencl.algorithm.copy_if() -- PyOpenCL,如何将多维数组传递给 pyopencl.algorithm.copy_if() -- PyOpenCL,
【发布时间】:2019-05-03 12:38:00
【问题描述】:

以下代码给了我警告“警告:不兼容的整数到指针的转换将'__global int'传递给'__global int *'类型的参数;使用&获取地址”并且不会产生所需的结果。

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

【问题讨论】:

  • 函数sum_array在哪里调用?我认为,有一个参数a 没有正确地作为指针或数组放入。不应该是:"sum_array(&amp;ary[i], len[i]) == 108", 吗?
  • 它在 "sum_array(ary[i], len[i]) == 108" 行中被调用,提交数组的每个成员都被评估为 "ary"。我在想 ary[i] 会拉出组成数组,但不确定发生了什么。多维数组正在以某种方式变平?

标签: python c opencl pyopencl


【解决方案1】:

函数int sum_array(__global int *a, int num_elements) 的调用在"sum_array(ary[i], len[i]) == 108", 行中,但应该有一个指针,而不是函数中定义的数组元素。那将是: "sum_array(&amp;ary[i], len[i]) == 108",

【讨论】:

  • 这似乎是正确的。现在解决我的代码中的另一个变量类型不匹配错误;一旦我让它真正工作,我会确认。
  • 是的,我确定这是我的 C 代码。我不是 C 程序员。错误::817:47:警告:不同符号整数的比较:'unsigned long'和'const psc_index_type'(又名'const int')if (psc_K * psc_LID_0 + psc_k
  • 我找不到任何 `psc_index_type` 或 psc_K、psc_LID_0、aso,当您混合有符号和无符号类型时,C 会发出此警告,尤其是使用比较函数 linke &lt; 混合使用很危险它或转换这些不同的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多