【问题标题】:write into indexed array in pyopencl在 pyopencl 中写入索引数组
【发布时间】:2013-07-22 01:12:11
【问题描述】:

嗨,我在 pyopencl 中编写了这段代码以获得稀疏随机向量,但问题是我无法将任何值写入索引数组有什么问题?输出总是零!!

这是我的代码

import pyopencl as cl
import numpy as np
from pyopencl import array 
from pyopencl import clrandom

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx,
        properties=cl.command_queue_properties.PROFILING_ENABLE)

x=array.zeros(queue, 512, dtype=np.float32 )

indices = clrandom.rand(queue, 17 , dtype=np.int32 ,luxury=2, a=1 , b=512)

clrandom.RanluxGenerator(queue,luxury=0).fill_normal(x[indices], mu=0, sigma=1)

print x

【问题讨论】:

    标签: python-2.7 opencl pyopencl


    【解决方案1】:

    x[indices] 似乎返回的是副本而不是视图。

    >>> import pyopencl as cl
    >>> import numpy as np
    >>> from pyopencl import array 
    >>> from pyopencl import clrandom
    >>> 
    >>> ctx = cl.create_some_context()
    >>> queue = cl.CommandQueue(ctx,
    ...         properties=cl.command_queue_properties.PROFILING_ENABLE)
    >>> 
    >>> x=array.zeros(queue, 512, dtype=np.float32 )
    >>> 
    >>> indices = clrandom.rand(queue, 17 , dtype=np.int32 ,luxury=2, a=1 , b=512)
    >>> x_cp = x[indices]
    >>> x_cp
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
            0.,  0.,  0.,  0.], dtype=float32)
    >>> clrandom.RanluxGenerator(queue,luxury=0).fill_normal(x_cp, mu=0, sigma=1)
    >>> x_cp
    array([ 1.16633689,  0.65845662,  1.97530341, -0.53728914,  1.38982224,
            1.47071588, -0.73042828,  1.29367638,  1.2390343 ,  2.89497447,
           -0.75589401,  0.04600764, -4.28992653,  0.50232059,  0.4881362 ,
            0.01112503, -0.46737072], dtype=float32)
    >>> x[indices]
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
            0.,  0.,  0.,  0.], dtype=float32)
    

    您可以尝试以下方法:

    1. 创建一个与 indices 数组具有相同维度的 float32 数组,我们称之为 tmp_random
    2. 生成随机数并将它们存储在tmp_random
    3. 编写一个以indicestmp_randomx 作为参数的内核。每个线程从indices 中读取一个索引,并从tmp_random 中读取相应的随机数,并将其存储在x 的正确位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多