python - PyOpenCL Multidimensional Array -


i have code multidimensional array addition using pyopencl. problem result wrong first dimension. have been consulting link.

from __future__ import absolute_import, print_function import numpy np import pyopencl cl  n = 4 a_np = np.random.rand(n,n).astype(np.float32) b_np = np.random.rand(n,n).astype(np.float32)  ctx = cl.create_some_context() queue = cl.commandqueue(ctx)  mf = cl.mem_flags a_g = cl.buffer(ctx, mf.read_only | mf.copy_host_ptr, hostbuf=a_np) b_g = cl.buffer(ctx, mf.read_only | mf.copy_host_ptr, hostbuf=b_np)  prg = cl.program(ctx, """     __kernel void sum(         __global const float *a_g, __global const float *b_g, __global float *res_g) {             int = get_global_id(1);             int j = get_global_id(0);             res_g[i,j] = a_g[i,j] + b_g[i,j];     } """).build()  res_g = cl.buffer(ctx, mf.write_only, a_np.nbytes) prg.sum(queue, a_np.shape, none, a_g, b_g, res_g)  res_np = np.empty_like(a_np) cl.enqueue_copy(queue, res_np, res_g)  # check on cpu numpy: print(res_np - (a_np + b_np)) print(np.linalg.norm(res_np - (a_np + b_np))) print (res_np) print (a_np + b_np) 

matrix 1:

[[ 0.2990678   0.76585543  0.71866363  0.30202991]  [ 0.20604192  0.01989171  0.02402978  0.82826865]  [ 0.75456071  0.62410605  0.4374246   0.85372066]  [ 0.37000021  0.5734672   0.4250721   0.2456535 ]] 

matrix 2:

[[ 0.83109927  0.53289926  0.24182947  0.39531609]  [ 0.53014964  0.62028325  0.2397541   0.03364789]  [ 0.83543158  0.1162187   0.21168791  0.22438531]  [ 0.2178313   0.76118374  0.23737679  0.41660839]] 

expected result:

[[ 1.13016701  1.29875469  0.96049309  0.69734597]  [ 0.73619157  0.64017498  0.26378387  0.86191654]  [ 1.58999228  0.74032474  0.64911252  1.07810593]  [ 0.5878315   1.33465099  0.66244888  0.6622619 ]] 

script result:

[[ 1.13016701  1.29875469  0.96049309  0.69734597]  [ 0.          0.          0.          0.        ]  [ 0.          0.          0.          0.        ]  [ 0.          0.          0.          0.        ]] 

the problem lies here:

res_g[i,j] = a_g[i,j] + b_g[i,j];

this not how access elements of multidimensional array in opencl. opencl subset of c , wikipedia:

in c , c++ programming languages, comma operator (represented token ,) binary operator evaluates first operand , discards result, , evaluates second operand , returns value (and type).

so evaluated to: res_g[j] = a_g[j] + b_g[j];

therefore correctly should this:

res[i + size * j] = ...

consult link provided again, there.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -