Пример #1
0
        //GPUArray<int> h = new GPUArray<int>(100000000);
        public Game(GameWindow window) : base(window)
        {
            new Shader2D("SIFT.shaders.plain_color.glsl").Run(Window.Width, Window.Height, Window.Canvas);
            //object a = new int();
            //Console.WriteLine(a.GetType());
            ////return;
            int         n      = 100000000;
            int         loop_n = 1; // 60 * 32;
            GPUIntArray s_gpu  = new GPUIntArray(n);
            List <int>  s_cpu  = new List <int>(n); for (int i = 0; i < n; i++)

            {
                s_cpu.Add(0);
            }

            Print(s_gpu.IsValue(7122));
            s_gpu.Value(7122);
            Print(s_gpu.IsValue(7122));

            GL.Finish();
            //Print("GPU time:", a=Timing(() =>
            //{
            //    for (int i = 0; i < n * power * 11; i++)
            //    {
            //        s_gpu[Rand.Next(n)] = Rand.Next();
            //    }
            //    GL.Finish();
            //}));
            TimeSpan a, b;

            Print("GPU time:", a = Timing(() =>
            {
                for (int i = 0; i < loop_n; i++)
                {
                    //Print("i =", i);
                    s_gpu.Random();
                    //s_gpu.Data(Shuffled(Range(n)).ToArray());
                    s_gpu.Sort();
                    //s_gpu.Value(0);
                    //if (!s_gpu.IsSorted())
                    //{
                    //    Print(s_gpu);
                    //}
                    Assert(s_gpu.IsSorted());
                }
                GL.Finish();
            }));
            Print("CPU time:", b = Timing(() =>
            {
                for (int i = 0; i < loop_n; i++)
                {
                    //Print("i =", i);
                    s_cpu.Random();
                    s_cpu.Sort();
                    Assert(s_cpu.IsSorted());
                }
            }));
            Print("speedup =", b.TotalMilliseconds / a.TotalMilliseconds);
            Print("finish");
        }
Пример #2
0
                public void Sort()
                {
                    int n = value.Length;

                    if (n <= 1)
                    {
                        return;
                    }
                    int max_level = (__builtin_popcount(n) == 1 ? 31 : 32) - __builtin_clz(n);

                    left.Value(0); rigt.Value(n - 1);
                    //Print(value);
                    for (int level = 1; level <= max_level; level++)
                    {
                        //OpenTK.Graphics.OpenGL.GL.Finish();
                        //Print(Timing(() =>
                        //{
                        new Shader("SIFT.shaders.merge_sort.glsl").QueueForRunInSequence(n,
                                                                                         ("level", level),
                                                                                         value, left, rigt, output);
                        //    OpenTK.Graphics.OpenGL.GL.Finish();
                        //}));
                        value.Swap(output);
                        //Print(value);
                        //Print(debug);
                    }
                }
Пример #3
0
                public void Sort()
                {
                    int n = value.Length;

                    if (n <= 1)
                    {
                        return;
                    }
                    int num_levels = (__builtin_popcount(n) == 1 ? 31 : 32) - __builtin_clz(n);

                    left.Value(0); rigt.Value(n - 1);
                    for (int start_level = 1; start_level <= num_levels; start_level++)
                    {
                        for (int level = start_level; level >= 1; level--)
                        {
                            new Shader("SIFT.shaders.bitonic_merge.glsl").QueueForRunInSequence(n,
                                                                                                ("start_level", start_level),
                                                                                                ("level", level),
                                                                                                ("reverse", level == start_level ? 1 : 0),
                                                                                                value, left, rigt, output);
                            value.Swap(output);
                            //Print(value);
                        }
                    }
                }
Пример #4
0
                public void Sort()
                {
                    int n = value.Length;

                    if (n <= 1)
                    {
                        return;
                    }
                    int max_level = (__builtin_popcount(n) == 1 ? 31 : 32) - __builtin_clz(n);

                    left.Value(0); rigt.Value(n - 1);
                    //Print(value);
                    int stride = 16;// Math.Max(1, n / Shader.default_group_size_x / gpu_execute_unit_count);

                    //Print("stride =", stride);
                    for (int level = 1; level <= max_level; level++)
                    {
                        //OpenTK.Graphics.OpenGL.GL.Finish();
                        //Print(Timing(() =>
                        //{
                        new Shader("SIFT.shaders.merge_batch_sort.glsl").QueueForRunInSequence((n + stride - 1) / stride,
                                                                                               ("level", level),
                                                                                               ("stride", stride),
                                                                                               value, left, rigt, output);
                        //    OpenTK.Graphics.OpenGL.GL.Finish();
                        //}));
                        value.Swap(output);
                        //Print(value);
                        //Print(debug);
                    }
                }
Пример #5
0
                public void Sort()
                {
                    int n = value.Length;

                    if (n <= 1)
                    {
                        return;
                    }
                    int max_level = (__builtin_popcount(n) == 1 ? 31 : 32) - __builtin_clz(n);

                    left.Value(0); rigt.Value(n - 1);
                    //Print(value);
                    for (int level = 1; level <= max_level; level++)
                    {
                        int total_execute_cnt = 0;
                        for (int stride_level = max_level; stride_level >= 0; stride_level--)
                        {
                            // (1 << sl) - 1, 3(1 << sl) - 1, 5(1 << sl) - 1, 7(1 << sl) - 1, ...
                            int offset = (1 << stride_level) - 1;
                            //int stride = 1 << stride_level << 1;
                            // offset + ? * stride <= n - 1
                            // ? * stride <= n - 1 - offset
                            int execute_cnt = ((n - 1 - offset) >> stride_level >> 1) + 1;
                            total_execute_cnt += execute_cnt;

                            //OpenTK.Graphics.OpenGL.GL.Finish();
                            //Print(Timing(() =>
                            //{
                            new Shader("SIFT.shaders.merge_sort_write_a.glsl").QueueForRunInSequence(execute_cnt,
                                                                                                     ("level", level),
                                                                                                     ("stride_level", stride_level),
                                                                                                     value, left, rigt, a);
                            //    OpenTK.Graphics.OpenGL.GL.Finish();
                            //}));
                            //Print(a);
                        }
                        //Print();
                        Assert(total_execute_cnt == n);
                        new Shader("SIFT.shaders.merge_sort_read_a_write_o.glsl").QueueForRunInSequence(n,
                                                                                                        ("level", level),
                                                                                                        value, left, rigt, a, output);
                        value.Swap(output);
                        //Print(value);
                        //Print(debug);
                    }
                }