[BEGINNER] host memory and device memory question questions about host memory and device memory

hi, i m trying to wrap my head around the idea of host memory and device memory in CUDA.

this piece of code is copy from JuliaSet in the “CUDA by example” book

[codebox]

struct cuComplex {

float   r;

float   i;

cuComplex( float a, float b ) : r(a), i(B)  {}

__device__ float magnitude2( void ) {

    return r * r + i * i;

}

__device__ cuComplex operator*(const cuComplex& a) {

    return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);

}

__device__ cuComplex operator+(const cuComplex& a) {

    return cuComplex(r+a.r, i+a.i);

}

};

device int julia( int x, int y ) {

const float scale = .5;

float jx = scale * (float)(DIM/2 - x)/(DIM/2);

float jy = scale * (float)(DIM/2 - y)/(DIM/2);

cuComplex c(-0.8, 0.156);

cuComplex a(jx, jy);

int i = 0;

for (i=0; i<200; i++) {

    a = a * a + c;

    if (a.magnitude2() > 1000)

        return 0;

}

return 1;

}[/codebox]

the above code compiles and runs fine, but i m confuse at what the compiler is doing and what the memory looks like.

heres what i think the above code does, please correct me if i m wrong:

device function “Julia” returns 1 if some complex number “a” belongs in the julia set, and 0 otherwise. the device function also

calls device utility functions for complex numbers, defined in the cuComplex struct.

i understand, sort of, that the julia function resides in device memory, and all functions declare using the device syntax.

but i dont get how the julia function knows where “cuComplex” is, since it s a struct defined in host memory?

does the compiler store a pointer to the struct definition and pass to device? i dont see the code using cudaMalloc to allocate that pointer

in device memory in code.

any help or advice on where i should read in the programming guide would be great.

thanks in advance

hi, i m trying to wrap my head around the idea of host memory and device memory in CUDA.

this piece of code is copy from JuliaSet in the “CUDA by example” book

[codebox]

struct cuComplex {

float   r;

float   i;

cuComplex( float a, float b ) : r(a), i(B)  {}

__device__ float magnitude2( void ) {

    return r * r + i * i;

}

__device__ cuComplex operator*(const cuComplex& a) {

    return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);

}

__device__ cuComplex operator+(const cuComplex& a) {

    return cuComplex(r+a.r, i+a.i);

}

};

device int julia( int x, int y ) {

const float scale = .5;

float jx = scale * (float)(DIM/2 - x)/(DIM/2);

float jy = scale * (float)(DIM/2 - y)/(DIM/2);

cuComplex c(-0.8, 0.156);

cuComplex a(jx, jy);

int i = 0;

for (i=0; i<200; i++) {

    a = a * a + c;

    if (a.magnitude2() > 1000)

        return 0;

}

return 1;

}[/codebox]

the above code compiles and runs fine, but i m confuse at what the compiler is doing and what the memory looks like.

heres what i think the above code does, please correct me if i m wrong:

device function “Julia” returns 1 if some complex number “a” belongs in the julia set, and 0 otherwise. the device function also

calls device utility functions for complex numbers, defined in the cuComplex struct.

i understand, sort of, that the julia function resides in device memory, and all functions declare using the device syntax.

but i dont get how the julia function knows where “cuComplex” is, since it s a struct defined in host memory?

does the compiler store a pointer to the struct definition and pass to device? i dont see the code using cudaMalloc to allocate that pointer

in device memory in code.

any help or advice on where i should read in the programming guide would be great.

thanks in advance

[font=“Courier New”]struct cuComplex[/font] is a type, not a variable. Whether a particular variable of that type resides in host memory or device memory depends on the declaration of that variable.

[font=“Courier New”]struct cuComplex[/font] is a type, not a variable. Whether a particular variable of that type resides in host memory or device memory depends on the declaration of that variable.