Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdio.h>
- #include <math.h>
- #include "graphics.h"
- #include "tryf.h"
- #define ImageHeight 500
- #define ImageWidth 500
- __global__ void addKernel(int *a)
- {
- double MinRe = -2.0;
- double MaxRe = 0.5;
- double MinIm = -1.2;
- double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
- double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
- double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
- int MaxIterations = 12;
- bool isInside=true;
- unsigned int go_upto_X=ImageWidth;
- int x=0,n,count=1;
- int col=threadIdx.y;
- int row=threadIdx.x;
- //cuPrintf("Hi");
- unsigned int y=col*23+row;
- if(y>=500) goto end;//Each row will have a separate thread.
- // for(;count<6;y++)
- // {
- double c_im = MaxIm - y*Im_factor;
- for(x=0; x<go_upto_X; ++x)
- {
- double c_re = MinRe + x*Re_factor;
- double Z_re = c_re, Z_im = c_im;isInside = true;
- for(n=0; n<MaxIterations; ++n)
- {
- double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
- if(Z_re2 + Z_im2 > 4)
- {
- isInside = false;
- break;
- }
- Z_im = 2*Z_re*Z_im + c_im;
- Z_re = Z_re2 - Z_im2 + c_re;
- }
- *(a+y*ImageWidth*3+x*3+0)=x;
- *(a+y*ImageWidth*3+x*3+1)=y;
- if(!isInside) *(a+y*ImageWidth*3+x*3+2)=n;
- else{ *(a+y*ImageWidth*3+x*3+2)=MaxIterations-1;}
- }//count++;
- // }
- end:
- }
- int main()
- {
- int flag=0,count=1;
- initwindow(ImageWidth,ImageHeight,"MandelBrot Set");
- int *points=(int *)malloc (ImageHeight*ImageWidth*3*sizeof(int));
- /*for(int i=0;i<ImageHeight;i++)
- {
- for(int j=0;j<ImageWidth;j++)
- {
- *(points+i*ImageWidth*3+j*3+0)=j;
- *(points+i*ImageWidth*3+j*3+1)=i;
- *(points+i*ImageWidth*3+j*3+2)=5;
- }
- }*/
- int *dev_points=0;
- cudaMalloc((void **) &dev_points , ImageHeight*ImageWidth*3*sizeof (int) ) ;
- cudaMemcpy(dev_points, points, ImageHeight*ImageWidth*3 * sizeof(int), cudaMemcpyHostToDevice);
- // Launch a kernel on the GPU with one thread for each element.
- dim3 dimGrid(1,1);
- dim3 dimBlock(23,23);
- addKernel<<<dimGrid, dimBlock>>>(dev_points);
- // addKernel<<<1,500>>>(dev_points);
- cudaMemcpy( points, dev_points, ImageHeight*ImageWidth*3, cudaMemcpyDeviceToHost );
- int x,y,color;
- for(int i=0;i<ImageHeight;i++)
- {
- for(int j=0;j<ImageWidth;j++)
- {
- x=*(points+i*ImageWidth*3+j*3+0);
- y=*(points+i*ImageWidth*3+j*3+1);
- color=*(points+i*ImageWidth*3+j*3+2);
- // if(y<0) {flag=1;count++;break;}
- 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);
- switch(color)
- {
- case 0:putpixel(x,y,WHITE);break;
- case 1:putpixel(x,y,YELLOW);break;
- case 2:putpixel(x,y,BLUE);break;
- case 3:putpixel(x,y,CYAN);break;
- case 4:putpixel(x,y,GREEN);break;
- case 5:putpixel(x,y,RED);break;
- case 6:putpixel(x,y,MAGENTA);break;
- case 7:putpixel(x,y,LIGHTBLUE);break;
- case 8:putpixel(x,y,LIGHTGREEN);break;
- case 9:putpixel(x,y,LIGHTCYAN);break;
- case 10:putpixel(x,y,LIGHTRED);break;
- case 11:putpixel(x,y,LIGHTMAGENTA);break;
- default:putpixel(x,y,BLACK);break;
- }
- }//if(flag && count>5)break;
- }
- // Check for any errors launching the kernel
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement