Exemplo n.º 1
0
        private static ComplexD arcsin(ComplexD c)
        {
            ComplexD x1  = new ComplexD(-c.y, c.x);
            ComplexD one = new ComplexD(1, 0);
            ComplexD x2  = sqrt(ComplexD.Subtract(one, ComplexD.Multiply(c, c)));

            return(ComplexD.Multiply(new ComplexD(0, -1), log(ComplexD.Add(x1, x2))));
        }
Exemplo n.º 2
0
        public static void complexAdd(GThread thread, ComplexD[,] a, ComplexD[,] b, ComplexD[,] c)
        {
            int x = thread.blockIdx.x;
            int y = 0;

            while (y < YSIZE)
            {
                c[x, y] = ComplexD.Add(a[x, y], b[x, y]);
                y++;
            }
        }
Exemplo n.º 3
0
        private static void setPixel(GThread thread, int W, int H, int rpnLength, byte[] bmp, byte[] colorMap, ComplexD[] stackMem,
                                     byte[] rpnFormula, ComplexD[] rpnKoef, double rotation,
                                     double scale, ComplexD ctr, ComplexD init, double infty, int steps)
        {
            int pixelId = thread.blockIdx.x * thread.blockDim.x + thread.threadIdx.x;

            int x = pixelId % (2 * W);
            int y = pixelId / (2 * W);

            if (y >= 2 * H)
            {
                return;
            }

            ComplexD c1 = new ComplexD(Cudafy.GMath.Cos((float)rotation), Cudafy.GMath.Sin((float)rotation));
            ComplexD c2 = new ComplexD((x - W) * (scale / W), (y - H) * (scale / W));
            ComplexD c  = ComplexD.Add(ComplexD.Multiply(c1, c2), ctr);

            int clr = getSequenceDivergence(c, rpnLength, rpnFormula, rpnKoef, init, infty, steps, stackMem, Function.MAX_STACK_SIZE * pixelId);

            bmp[3 * pixelId]     = colorMap[3 * clr];
            bmp[3 * pixelId + 1] = colorMap[3 * clr + 1];
            bmp[3 * pixelId + 2] = colorMap[3 * clr + 2];
        }
Exemplo n.º 4
0
        private static int Mandelbrot(ComplexD c)
        {
            //DEBUG = MANDELBROT
            double r = Cudafy.GMath.Sqrt((float)((c.x - 0.25F) * (c.x - 0.25F) + c.y * c.y));
            double t = Cudafy.GMath.Atan2((float)c.y, (float)c.x);

            if (r <= 0.5 * (1 - Cudafy.GMath.Cos((float)t)))
            {
                return(0);
            }


            ComplexD z = new ComplexD(0, 0);

            for (int i = 0; i < 200; i++)
            {
                z = ComplexD.Add(ComplexD.Multiply(z, z), c);
                if (z.x * z.x + z.y * z.y > 4)
                {
                    return(1);
                }
            }
            return(0);
        }
Exemplo n.º 5
0
        public static void Execute()
        {
            CudafyModule km = CudafyTranslator.Cudafy();

            GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target);

            gpu.LoadModule(km);

            // 2D
            Console.WriteLine("kernel");
            ComplexD[,] host_A = new ComplexD[XSIZE, YSIZE];
            ComplexD[,] host_B = new ComplexD[XSIZE, YSIZE];
            ComplexD[,] host_C = new ComplexD[XSIZE, YSIZE];
            int i = 0;

            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE; y++)
                {
                    host_A[x, y] = new ComplexD(i, i);
                    host_B[x, y] = new ComplexD(2, 0);
                    i++;
                }
            }
            ComplexD[,] dev_A = gpu.CopyToDevice(host_A);
            ComplexD[,] dev_B = gpu.CopyToDevice(host_B);
            ComplexD[,] dev_C = gpu.Allocate <ComplexD>(XSIZE, YSIZE);

            Console.WriteLine("complexAdd");
            gpu.Launch(XSIZE, 1, "complexAdd", dev_A, dev_B, dev_C);
            gpu.CopyFromDevice(dev_C, host_C);
            i = 0;
            bool pass = true;

            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE && pass; y++)
                {
                    ComplexD expected = ComplexD.Add(host_A[x, y], host_B[x, y]);
                    pass = host_C[x, y].x == expected.x && host_C[x, y].y == expected.y;
                }
            }
            Console.WriteLine(pass ? "Pass" : "Fail");

            Console.WriteLine("complexSub");
            gpu.Launch(XSIZE, 1, "complexSub", dev_A, dev_B, dev_C);
            gpu.CopyFromDevice(dev_C, host_C);
            i    = 0;
            pass = true;
            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE && pass; y++)
                {
                    ComplexD expected = ComplexD.Subtract(host_A[x, y], host_B[x, y]);
                    pass = host_C[x, y].x == expected.x && host_C[x, y].y == expected.y;
                }
            }
            Console.WriteLine(pass ? "Pass" : "Fail");

            Console.WriteLine("complexMpy");
            gpu.Launch(XSIZE, 1, "complexMpy", dev_A, dev_B, dev_C);
            gpu.CopyFromDevice(dev_C, host_C);
            i    = 0;
            pass = true;
            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE && pass; y++)
                {
                    ComplexD expected = ComplexD.Multiply(host_A[x, y], host_B[x, y]);
                    //Console.WriteLine("{0} {1} : {2} {3}", host_C[x, y].R, host_C[x, y].I, expected.R, expected.I);
                    pass = Verify(host_C[x, y], expected, 1e-14F);
                    i++;
                }
            }
            Console.WriteLine(pass ? "Pass" : "Fail");

            Console.WriteLine("complexDiv");
            gpu.Launch(XSIZE, 1, "complexDiv", dev_A, dev_B, dev_C);
            gpu.CopyFromDevice(dev_C, host_C);
            i    = 0;
            pass = true;
            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE && pass; y++)
                {
                    ComplexD expected = ComplexD.Divide(host_A[x, y], host_B[x, y]);
                    //Console.WriteLine("{0} {1} : {2} {3}", host_C[x, y].R, host_C[x, y].I, expected.R, expected.I);
                    if (i > 0)
                    {
                        pass = Verify(host_C[x, y], expected, 1e-13F);
                    }
                    i++;
                }
            }
            Console.WriteLine(pass ? "Pass" : "Fail");

            Console.WriteLine("complexAbs");
            gpu.Launch(XSIZE, 1, "complexAbs", dev_A, dev_C);
            gpu.CopyFromDevice(dev_C, host_C);
            i    = 0;
            pass = true;
            for (int x = 0; x < XSIZE; x++)
            {
                for (int y = 0; y < YSIZE && pass; y++)
                {
                    double expected = ComplexD.Abs(host_A[x, y]);
                    pass = Verify(host_C[x, y].x, expected, 1e-2F);
                    //Console.WriteLine("{0} {1} : {2}", host_C[x, y].x, host_C[x, y].y, expected);
                    i++;
                }
            }
            Console.WriteLine(pass ? "Pass" : "Fail");

            gpu.FreeAll();
        }
Exemplo n.º 6
0
        private static ComplexD eval(int rpnLength, byte[] rpnFormula, ComplexD[] rpnKoef, ComplexD c, ComplexD z, ComplexD[] stack, int stackOffset)
        {
            //return ComplexD.Add( ComplexD.Multiply(ComplexD.Multiply(z,z),z),c);


            int sPtr = stackOffset - 1;
            int vPtr = 0;

            for (int i = 0; i < rpnLength; i++)
            {
                byte v = rpnFormula[i];
                if (v == 0)
                {
                    stack[++sPtr] = rpnKoef[vPtr++];
                }
                else if (v <= 5)
                {
                    if (v == 1)
                    {
                        stack[sPtr - 1] = ComplexD.Add(stack[sPtr - 1], stack[sPtr]);
                    }
                    else if (v == 2)
                    {
                        stack[sPtr - 1] = ComplexD.Subtract(stack[sPtr - 1], stack[sPtr]);
                    }
                    else if (v == 3)
                    {
                        stack[sPtr - 1] = ComplexD.Multiply(stack[sPtr - 1], stack[sPtr]);
                    }
                    else if (v == 4)
                    {
                        stack[sPtr - 1] = ComplexD.Divide(stack[sPtr - 1], stack[sPtr]);
                    }
                    else if (v == 5)
                    {
                        stack[sPtr - 1] = pow(stack[sPtr - 1], stack[sPtr]);
                    }

                    sPtr--;
                }
                else if (v <= 10)
                {
                    if (v == 6)
                    {
                        stack[sPtr] = log(stack[sPtr]);
                    }
                    else if (v == 7)
                    {
                        stack[sPtr] = exp(stack[sPtr]);
                    }
                    else if (v == 8)
                    {
                        stack[sPtr] = sin(stack[sPtr]);
                    }
                    else if (v == 9)
                    {
                        stack[sPtr] = cos(stack[sPtr]);
                    }
                    else if (v == 10)
                    {
                        stack[sPtr] = tg(stack[sPtr]);
                    }
                }
                else if (v <= 19)
                {
                    if (v == 11)
                    {
                        stack[sPtr] = ctg(stack[sPtr]);
                    }
                    else if (v == 12)
                    {
                        stack[sPtr] = arcsin(stack[sPtr]);
                    }
                    else if (v == 13)
                    {
                        stack[sPtr] = arccos(stack[sPtr]);
                    }
                    else if (v == 14)
                    {
                        stack[sPtr] = arctg(stack[sPtr]);
                    }
                    else if (v == 15)
                    {
                        stack[sPtr] = arcctg(stack[sPtr]);
                    }
                    else if (v == 16)
                    {
                        stack[sPtr] = sh(stack[sPtr]);
                    }
                    else if (v == 17)
                    {
                        stack[sPtr] = ch(stack[sPtr]);
                    }
                    else if (v == 18)
                    {
                        stack[sPtr] = th(stack[sPtr]);
                    }
                    else if (v == 19)
                    {
                        stack[sPtr] = cth(stack[sPtr]);
                    }
                }
                else if (v <= 25)
                {
                    if (v == 20)
                    {
                        stack[sPtr] = new ComplexD(abs(stack[sPtr]), 0);
                    }
                    if (v == 21)
                    {
                        stack[sPtr] = new ComplexD(stack[sPtr].x, 0);
                    }
                    if (v == 22)
                    {
                        stack[sPtr] = new ComplexD(stack[sPtr].y, 0);
                    }
                    if (v == 23)
                    {
                        stack[sPtr] = new ComplexD(arg(stack[sPtr]), 0);
                    }
                    if (v == 24)
                    {
                        stack[sPtr] = sqrt(stack[sPtr]);
                    }
                    if (v == 25)
                    {
                        stack[sPtr] = new ComplexD(-stack[sPtr].x, -stack[sPtr].y);
                    }
                }
                else if (v == 64)
                {
                    stack[++sPtr] = c;
                }
                else if (v == 65)
                {
                    stack[++sPtr] = z;
                }
            }

            return(stack[stackOffset]);
        }
Exemplo n.º 7
0
        private static ComplexD ch(ComplexD c)
        {
            ComplexD t = ComplexD.Add(exp(c), exp(new ComplexD(-c.x, -c.y)));

            return(new ComplexD(t.x / 2, t.y / 2));
        }