Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion error in drawing the Julia set #20

Open
mindbound opened this issue Feb 20, 2015 · 5 comments
Open

Conversion error in drawing the Julia set #20

mindbound opened this issue Feb 20, 2015 · 5 comments

Comments

@mindbound
Copy link

I'm trying to use the following code to draw the Julia set (using Julia 0.3.5):

julia.jl

using CUDArt
using PyPlot

w = 2048 * 2
h = 2048 * 2
q = [complex64(r, i) for i = 1 : -(2.0 / w) : -1, r = -1.5 : (3.0 / h) : 1.5]

julia(q :: Array{Complex64}, maxiter :: Integer) = begin
    res = CUDArt.devices(dev -> CUDArt.capability(dev)[1] >= 2, nmax = 1) do devlist
        CUDArt.device(devlist[1])

        prg = CUDArt.CuModule("julia.ptx") do md
            juliafn = CUDArt.CuFunction(md, "julia")
            out = CUDArt.CudaArray(Uint16, size(q))
            CUDArt.launch(juliafn, length(q), 1, (q, out, uint16(maxiter)))
            CUDArt.to_host(out)
        end
    end

    res
end

j = julia(q, 200)
imshow(j, extent = [-1.5, 1.5, -1.0, 1.0])

julia.cu (compiled to julia.ptx via nvcc -ptx julia.cu)

extern "C"
{
    __global__ void julia(float2* q, unsigned short* out, const unsigned short maxiter)
    {
        int idx = blockIdx.x * blockDim.x + threadIdx.x;

        float nreal = 0.0f;
        float real = q[idx].x;
        float imag = q[idx].y;

        out[idx] = 0;

        for (int i = 0; i < maxiter; i++)
        {
            if (real * real + imag * imag > 4.0f)
            {
                out[idx] = i;
            }

            nreal = real * real - imag * imag + (-0.5f);
            imag = 2 * real * imag + 0.75f;
            real = nreal;
        }
    }
}

When trying to run the code, it exits with the error 'rawpointer' has no method matching rawpointer(::Array{Complex{Float32},2}). Is this a lacking convert for the type, or is there something wrong with my code? Sorry for not posting this on StackOverflow or /r/julia but I expect people here might be better informed about the peculiarities of CUDArt itself.

@moon6pence
Copy link
Contributor

I didn't test the code yet, but you have to copy Array to CudaArray to use in GPU.

Use CudaArray like this before calling the kernel:

        d_q = CudaArray(q)
        CUDArt.launch(juliafn, length(d_q), 1, (d_q, out, uint16(maxiter)))

@mindbound
Copy link
Author

That fails with:

_ERROR: Invalid value
in checkdrv at /home/???/.julia/v0.3/CUDArt/src/module.jl:6
in launch at /home/???/.julia/v0.3/CUDArt/src/execute.jl:16
in anonymous at /home/???/julia.jl:16
in CuModule at /home/???/.julia/v0.3/CUDArt/src/module.jl:39
in anonymous at /home/???/julia.jl:12
in devices at /home/???/.julia/v0.3/CUDArt/src/device.jl:57
in devices at /home/???/.julia/v0.3/CUDArt/src/device.jl:49
in julia at /home/???/julia.jl:9
in include at /usr/bin/../lib/julia/sys.so
in include_from_node1 at loading.jl:128
in process_options at /usr/bin/../lib/julia/sys.so
in start at /usr/bin/../lib/julia/sys.so
while loading /home/???/julia.jl, in expression starting on line 24

@timholy
Copy link
Contributor

timholy commented Feb 22, 2015

@moon6pence is right that you needed to move the memory to the GPU first. His suggestion seemed to have overcome the first problem, but now there seems to be something else going on. As a first thing to check, does Pkg.test("CUDArt") pass for you?

@mindbound
Copy link
Author

Yes, the tests do pass.

@moon6pence
Copy link
Contributor

In launching cuda kernel,

CUDArt.launch(juliafn, length(d_q), 1, (d_q, out, uint16(maxiter)))

You have to specify proper block size rather than 1. ex) 256, 512, 1024, ...
It may be work with block size 1 but definitely run slow.

CUDArt.launch(juliafn, div(length(d_q), 512), 512, (d_q, out, uint16(maxiter)))

However, I couldn't get the julia image.
Keep checking julia.cu generates proper julia set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants