Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "graphics.h"
  6. #include "tryf.h"
  7. #define ImageHeight 500
  8. #define ImageWidth 500
  9.  
  10.  
  11. __global__ void addKernel(int *a)
  12. {
  13.  
  14. double MinRe = -2.0;
  15. double MaxRe = 0.5;
  16. double MinIm = -1.2;
  17. double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
  18. double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
  19. double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
  20. int MaxIterations = 12;
  21. bool isInside=true;
  22. unsigned int go_upto_X=ImageWidth;
  23.  
  24.  
  25. int x=0,n,count=1;
  26. int col=threadIdx.y;
  27. int row=threadIdx.x;
  28. //cuPrintf("Hi");
  29.  
  30. unsigned int y=col*23+row;
  31. if(y>=500) goto end;//Each row will have a separate thread.
  32. // for(;count<6;y++)
  33. // {
  34. double c_im = MaxIm - y*Im_factor;
  35. for(x=0; x<go_upto_X; ++x)
  36. {
  37. double c_re = MinRe + x*Re_factor;
  38.  
  39. double Z_re = c_re, Z_im = c_im;isInside = true;
  40.  
  41. for(n=0; n<MaxIterations; ++n)
  42. {
  43. double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
  44. if(Z_re2 + Z_im2 > 4)
  45. {
  46. isInside = false;
  47. break;
  48. }
  49. Z_im = 2*Z_re*Z_im + c_im;
  50. Z_re = Z_re2 - Z_im2 + c_re;
  51. }
  52. *(a+y*ImageWidth*3+x*3+0)=x;
  53. *(a+y*ImageWidth*3+x*3+1)=y;
  54. if(!isInside) *(a+y*ImageWidth*3+x*3+2)=n;
  55. else{ *(a+y*ImageWidth*3+x*3+2)=MaxIterations-1;}
  56.  
  57. }//count++;
  58. // }
  59. end:
  60.  
  61. }
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67. int flag=0,count=1;
  68. initwindow(ImageWidth,ImageHeight,"MandelBrot Set");
  69. int *points=(int *)malloc (ImageHeight*ImageWidth*3*sizeof(int));
  70. /*for(int i=0;i<ImageHeight;i++)
  71. {
  72. for(int j=0;j<ImageWidth;j++)
  73. {
  74. *(points+i*ImageWidth*3+j*3+0)=j;
  75. *(points+i*ImageWidth*3+j*3+1)=i;
  76. *(points+i*ImageWidth*3+j*3+2)=5;
  77. }
  78. }*/
  79.  
  80.  
  81.  
  82. int *dev_points=0;
  83. cudaMalloc((void **) &dev_points , ImageHeight*ImageWidth*3*sizeof (int) ) ;
  84.  
  85. cudaMemcpy(dev_points, points, ImageHeight*ImageWidth*3 * sizeof(int), cudaMemcpyHostToDevice);
  86.  
  87. // Launch a kernel on the GPU with one thread for each element.
  88. dim3 dimGrid(1,1);
  89. dim3 dimBlock(23,23);
  90. addKernel<<<dimGrid, dimBlock>>>(dev_points);
  91. // addKernel<<<1,500>>>(dev_points);
  92.  
  93. cudaMemcpy( points, dev_points, ImageHeight*ImageWidth*3, cudaMemcpyDeviceToHost );
  94. int x,y,color;
  95. for(int i=0;i<ImageHeight;i++)
  96. {
  97. for(int j=0;j<ImageWidth;j++)
  98. {
  99. x=*(points+i*ImageWidth*3+j*3+0);
  100. y=*(points+i*ImageWidth*3+j*3+1);
  101. color=*(points+i*ImageWidth*3+j*3+2);
  102. // if(y<0) {flag=1;count++;break;}
  103. printf("%d %d %d %d %d %d %d %dn",*(points+i*ImageWidth*3+j*3+0),*(points+i*ImageWidth*3+j*3+1),*(points+i*ImageWidth*3+j*3+2),i*ImageWidth*3+j*3+0,i,j,ImageHeight,ImageWidth);
  104. switch(color)
  105. {
  106. case 0:putpixel(x,y,WHITE);break;
  107. case 1:putpixel(x,y,YELLOW);break;
  108. case 2:putpixel(x,y,BLUE);break;
  109. case 3:putpixel(x,y,CYAN);break;
  110. case 4:putpixel(x,y,GREEN);break;
  111. case 5:putpixel(x,y,RED);break;
  112. case 6:putpixel(x,y,MAGENTA);break;
  113. case 7:putpixel(x,y,LIGHTBLUE);break;
  114. case 8:putpixel(x,y,LIGHTGREEN);break;
  115. case 9:putpixel(x,y,LIGHTCYAN);break;
  116. case 10:putpixel(x,y,LIGHTRED);break;
  117. case 11:putpixel(x,y,LIGHTMAGENTA);break;
  118. default:putpixel(x,y,BLACK);break;
  119. }
  120.  
  121. }//if(flag && count>5)break;
  122. }
  123. // Check for any errors launching the kernel
  124. getch();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement