1

I am a beginner learning how to exploit GPU for parallel computation using python and cupy. I would like to implement my code to simulate some problems in physics and require to use complex number, but don't know how to manage it. Although there are examples in Cupy's official document, it only mentions about include complex.cuh library and how to declare a complex variable. I can't find any example about how to assign a complex number correctly, as well ass how to call the function in the complex.cuh library to do calculation.

I am stuck in line 11 of this code. I want to make a complex number value equal x[tIdx]+j*y[t_Idx], j is the imaginary number. I tried several ways and no one works, so I left this one here.

import cupy as cp
import time

add_kernel = cp.RawKernel(r'''
#include <cupy/complex.cuh>
extern "C" __global__
void test(double* x, double* y, complex<float>* z){
    int tId_x = blockDim.x*blockIdx.x + threadIdx.x;
    int tId_y = blockDim.y*blockIdx.y + threadIdx.y;
    
    complex<float>* value = complex(x[tId_x],y[tId_y]);

    z[tId_x*blockDim.y*gridDim.y+tId_y] = value;
}''',"test")

x = cp.random.rand(1,8,4096,dtype = cp.float32)
y = cp.random.rand(1,8,4096,dtype = cp.float32)
z = cp.zeros((4096,4096), dtype = cp.complex64)
t1 = time.time()
add_kernel((128,128),(32,32),(x,y,z))
print(time.time()-t1)

What is the proper way to assign a complex number in the RawKernel?
Thank you for answering this question!

OhnTzu
  • 23
  • 5
  • It should be `complex value{...}`, the `*` makes it a pointer which is a reason for the assignment not working.You could also directly assign to `z[tId_x*blockDim.y*gridDim.y+tId_y]`, the generated assembly is probably exactly the same. – paleonix Dec 02 '22 at 12:56

1 Answers1

1

@plaeonix, thank you very much for your hint. I find out the answer.

This line: complex<float>* value = complex(x[tId_x],y[tId_y]) should be replaced to: complex<float> value = complex<float>(x[tId_x],y[tId_y])

Then the assignment of a complex number works.

OhnTzu
  • 23
  • 5