首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入pyopencl中的索引数组

写入pyopencl中的索引数组
EN

Stack Overflow用户
提问于 2013-07-22 09:12:11
回答 1查看 130关注 0票数 1

嗨,我用pyopencl写了这段代码来获取稀疏随机向量,但问题是我不能将任何值写入索引数组,这是什么问题?输出始终为零!!

以下是我的代码

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-04-08 18:40:49

看起来x[indices]返回的是副本而不是视图。

代码语言:javascript
复制
>>> 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)

您可以尝试执行以下操作:

随机数创建一个和你的随机数组具有相同维数的随机数组,我们称它为tmp_random.

  • Generate随机数组,并将它们存储在tmp_random

  • Write中,这个内核以indicesindicestmp_randomx为参数。每个线程从indices中读取一个索引,从tmp_random中读取相应的随机数,并将其存储在x.

中的正确位置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17778597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档