JULIA set in CUDA

Hi

I was going through an example program on JULIA set in the book CUDA by example (Equation 4.1, page 47). The code contains a structure defined as below on page 49 of the book:

http://developer.download.nvidia.com/books/cuda-by-example/cuda-by-example-sample.pdf

struct cuComplex {
float r;
float i;

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



};

Can anyone please the working of the statement cuComplex(float a, float b) : r(a), i(b) {}

Your question is unclear.

That struct represents a complex number with two floats as members, and the constructor which takes in the float values a and b and sets the struct’s values r and i respectively.

This is basic C++ construction initialization list;

[url]http://www.cprogramming.com/tutorial/initialization-lists-c++.html[/url]

Google is your friend!

Thanks for the explanation. I got what I needed.

hi i have other kind of problem Im new in this when i compile show me this

i dont know why

julia_set.cu(60): error: identifier “CPUBitmap” is undefined

julia_set.cu(60): error: expected a “)”

this is the code

1 #include<stdio.h>
2 #include<stdlib.h>
3
4
5 #define DIM 1000
6
7 struct cuComplex {
8 float r;
9 float i;
10 cuComplex( float a, float b ) : r(a), i(b) {}
11 device float magnitude2( void ) {
12 return r * r + i * i;
13 }
14 device cuComplex operator*(const cuComplex& a) {
15 return cuComplex(ra.r - ia.i, ia.r + ra.i);
16 }
17 device cuComplex operator+(const cuComplex& a) {
18 return cuComplex(r+a.r, i+a.i);
19 }
20 };
21
22 device int julia( int x, int y ) {
23 const float scale = 1.5;
24 float jx = scale * (float)(DIM/2 - x)/(DIM/2);
25 float jy = scale * (float)(DIM/2 - y)/(DIM/2);
26
27 cuComplex c(-0.8, 0.156);
28 cuComplex a(jx, jy);
29
30 int i = 0;
31 for (i=0; i<200; i++) {
32 a = a * a + c;
33 if (a.magnitude2() > 1000)
34 return 0;
35 }
36
37 return 1;
38 }
39
40 global void kernel( unsigned char ptr ) {
41 // map from blockIdx to pixel position
42 int x = blockIdx.x;
43 int y = blockIdx.y;
44 int offset = x + y * gridDim.x;
45
46 // now calculate the value at that position
47 int juliaValue = julia( x, y );
48 ptr[offset
4 + 0] = 255 * juliaValue;
49 ptr[offset4 + 1] = 0;
50 ptr[offset
4 + 2] = 0;
51 ptr[offset*4 + 3] = 255;
52 }
53
54 // globals needed by the update routine
55 struct DataBlock {
56 unsigned char dev_bitmap;
57 };
58
59 int main( void ) {
60 DataBlock data;
61 CPUBitmap bitmap( DIM, DIM, &data );
62 unsigned char dev_bitmap;
63
64 cudaMalloc( (void
)&dev_bitmap, bitmap.image_size() );
65 data.dev_bitmap = dev_bitmap;
66
67 dim3 grid(DIM,DIM);
68 kernel<<<grid,1>>>( dev_bitmap );
69
70 cudaMemcpy( bitmap.get_ptr(), dev_bitmap, bitmap.image_size(),cudaMemcpyDeviceToHost );
71
72 cudaFree( dev_bitmap );
73
74 bitmap.display_and_exit();
75 }