I had made a very minimal ray tracer in Julia, and was in the process of implementing a faster version that uses CUDA. The full code is too extensive to share, but here is the part that I think is most relevant to the question:
world = World(RGB(1, 1, 1), 5e-6, shapes, lights, 0.2, 4)
camera = Camera((0, -5000, -5000), 1000, (0, 0, 0), 1920, 1080)
canvas = CUDA.fill(world.background, camera.height, camera.width)
function fillpixel!(arr::CuArray)
height = size(arr)[1]
for j in 1:length(arr)
ind = (j % height, ceil(j / height))
ray = [([ind[2], ind[1]] - [camera.width / 2, camera.height / 2])..., camera.depth]
(ray[2], ray[3]) = (cos(camera.rotation[1] atan(ray[3], ray[2])), sin(camera.rotation[1] atan(ray[3], ray[2]))) .* sqrt(ray[2]^2 ray[3]^2)
(ray[1], ray[3]) = (cos(camera.rotation[2] atan(ray[3], ray[1])), sin(camera.rotation[2] atan(ray[3], ray[1]))) .* sqrt(ray[1]^2 ray[3]^2)
(ray[1], ray[2]) = (cos(camera.rotation[3] atan(ray[2], ray[1])), sin(camera.rotation[3] atan(ray[2], ray[1]))) .* sqrt(ray[2]^2 ray[1]^2)
v = (Inf, nothing, nothing)
for object in world.objects
t = traceray(ray, camera.position, object, mindistance=camera.depth)
t !== nothing && t[1] < v[1] && (v = (t[1], t[2], object))
end
v[1] != Inf && (arr[j] = computecolor(v[3].material, ray, v[1], v[2], world, camera.position . v[1] * ray, v[3]))
return nothing
end
end
@cuda fillpixel!(canvas)
but when I run the program, it gives me the following error:
CUDA.jl: ERROR: LoadError: MethodError: no method matching typeof(fillpixel!)(::CuDeviceMatrix{RGB{Float32}, 1})
and I am unable to find out what causes this error and what exactly I'm doing wrong.
Thanks.
CodePudding user response:
Two comments: fillpixel!(arr::CuArray)
limits your function to only the type CuArray
. CUDA.jl translates the host side representation CuArray
to the device side representation `CuDeviceArray. So if you loosen your type restrictions you won't run into this issue.
Secondly you don't want to iterate over the array inside the kernel you launched. You either want to use a function like map
or map!
to implement the data-parallelism or use the CUDA index primitives.