OSDN Git Service

modified: utilsrc/src/Admin/Makefile
[eos/others.git] / utiltools / X86MAC64 / cuda / nvvm / libnvvm-samples / cuda-c-linking / math-funcs.cu
1 /*
2  * Copyright 1993-2012 NVIDIA Corporation.  All rights reserved.
3  *
4  * Please refer to the NVIDIA end user license agreement (EULA) associated
5  * with this source code for terms and conditions that govern your use of
6  * this software. Any use, reproduction, disclosure, or distribution of
7  * this software and related documentation outside the terms of the EULA
8  * is strictly prohibited.
9  *
10  */
11
12 // Simple implementation of Mandelbrot set from Wikipedia
13 // http://en.wikipedia.org/wiki/Mandelbrot_set
14
15 // Note that this kernel is meant to be a simple, straight-forward
16 // implementation, and so may not represent optimized GPU code.
17 extern "C"
18 __device__
19 void mandelbrot(float* Data) {
20
21   // Which pixel am I?
22   unsigned DataX = blockIdx.x * blockDim.x + threadIdx.x;
23   unsigned DataY = blockIdx.y * blockDim.y + threadIdx.y;
24   unsigned Width = gridDim.x * blockDim.x;
25   unsigned Height = gridDim.y * blockDim.y;
26
27   float R, G, B, A;
28
29   // Scale coordinates to (-2.5, 1) and (-1, 1)
30
31   float NormX = (float)DataX / (float)Width;
32   NormX *= 3.5f;
33   NormX -= 2.5f;
34
35   float NormY = (float)DataY / (float)Height;
36   NormY *= 2.0f;
37   NormY -= 1.0f;
38
39   float X0 = NormX;
40   float Y0 = NormY;
41
42   float X = 0.0f;
43   float Y = 0.0f;
44
45   unsigned Iter = 0;
46   unsigned MaxIter = 1000;
47
48   // Iterate
49   while(X*X + Y*Y < 4.0f && Iter < MaxIter) {
50     float XTemp = X*X - Y*Y + X0;
51     Y = 2.0f*X*Y + Y0;
52
53     X = XTemp;
54
55     Iter++;
56   }
57
58   unsigned ColorG = Iter % 50;
59   unsigned ColorB = Iter % 25;
60
61   R = 0.0f;
62   G = (float)ColorG / 50.0f;
63   B = (float)ColorB / 25.0f;
64   A = 1.0f;
65
66   Data[DataY*Width*4+DataX*4+0] = R;
67   Data[DataY*Width*4+DataX*4+1] = G;
68   Data[DataY*Width*4+DataX*4+2] = B;
69   Data[DataY*Width*4+DataX*4+3] = A;
70 }