Hi Andreas,
thank you for your rapid answer.
On 10/16/2012 01:30 AM, Andreas Kloeckner wrote:
Vasco <vasco+python(a)tenner.nl> writes:
Hi,
I want to copy the contents of a float to the imaginary part of a
complex on the gpu. Something like:
a = gpuarray.zeros(1,dtype=float)
c = gpuarray.zeros(1,dtype=complex)
c.gpudata.imag = float.gpudata
This would be a non-contiguous copy. (I.e. it would assign every second
float.) CUDA's built-in copies can't do that. The easiest way is to
use an elementwise kernel, from which you can use your set_real and
set_imag functions.
Now i tried the following:
gpu_a = gpuarray.to_gpu(np.array([[2.+1.j],[3.+2.j]]))
gpu_a = gpuarray.to_gpu(np.array([[3.+4.j],[5.+6.j]]))
from pycuda.elementwise import ElementwiseKernel
copy_imag = ElementwiseKernel(
"pycuda::complex<float> *z, pycuda::complex<float> *x",
"z[i].set_imag(x[i].imag())",
"copy_imag",
preamble="#include <pycuda-complex.hpp>",)
copy_imag(gpu_a,gpu_b)
which reponded: CompileError
.....
stderr:
kernel.cu(19): error: class "pycuda::complex<float>" has no member
"set_imag"
While I added this function in the pycuda-complex.hpp file which is use
by the compiler at line 91
Closely related (in mathematical sense) is the following statement:
>> z = abs(x)*exp(1.j*angle(y))
NotImplementedError: multi-d slicing is not yet implemented
I want to apply this at a 2d array.
I tried:
gpu_a = gpuarray.to_gpu(np.array([[1.+2.j,3.+4.j],[5.+6.j,7.+8.j]]))
gpu_b = gpuarray.to_gpu(np.array([[-1.-2.j,-3.-4.j],[-5.-6.j,-7.-8.j]]))
set_intensity = ElementwiseKernel(
# """Sets z: intensity from x and phase from y"""
"pycuda::complex<float> *z, pycuda::complex<float>
*x,pycuda::complex<float> *y",
"pycuda::complex<float> j(0,1);z[i] = abs(x[i]) *
exp(j*tan(y[i].real()/y[i].imag()))",
"set_intensity",
preamble="#include <pycuda-complex.hpp>",)
set_intensity(gpu_b,gpu_a,gpu_a)
print gpu_b
array([[ 5.29980882e-315 +5.30498948e-315j,
5.30757980e-315 +5.31017013e-315j],
[ -5.00000000e+000 -6.00000000e+000j,
-7.00000000e+000 -8.00000000e+000j]])
It only alters the first row, with very strange results.
Groet,
Vasco
Ps. Is there a way to get syntaxhighlighting better in an email? Or is
it preferable to ask questions like this on stackoverflow.com?