2013-07-22 4 views
1

hi Я написал этот код в pyopencl, чтобы получить разреженный случайный вектор, но проблема в том, что я не могу написать какое-либо значение в индексированном массиве, в чем проблема? выход всегда равен нулю!писать в индексированный массив в 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 

ответ

0

Кажется, что 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. Создать массив float32, который имеет те же размеры, что и ваш indices массив, позволяет называть его tmp_random.
  2. Генерация случайных чисел и хранить их в tmp_random
  3. Написать ядро, которое принимает indices, tmp_random и x в качестве аргументов. Каждый поток считывает один индекс из indices и соответствующее случайное число от tmp_random и сохраняет его в нужном месте в x.