Exemplo n.º 1
0
        static int Max(int[] data)
        {
            int n      = data.Length;
            int result = 0;

            {
                for (int level = 1; level <= Bithacks.Log2(n); level++)
                {
                    int step = Bithacks.Power2(level);
                    Campy.Parallel.For(n / step, idx =>
                    {
                        var i   = step * idx;
                        data[i] = data[i] < data[i + step / 2] ? data[i + step / 2] : data[i];
                    });
                }

                result = data[0];
            }
            return(result);
        }
Exemplo n.º 2
0
        static void Sum2()
        {
            int   n          = Bithacks.Power2(10);
            float result_gpu = 0;
            float result_cpu = 0;

            {
                List <float> data = Enumerable.Range(0, n).Select(i => ((float)i) / 10).ToList();
                for (int level = 1; level <= Bithacks.Log2(n); level++)
                {
                    int step = Bithacks.Power2(level);
                    for (int idx = 0; idx < n / step; idx++)
                    {
                        var i = step * idx;
                        data[i] = data[i] + data[i + step / 2];
                    }
                }
                result_cpu = data[0];
            }
            {
                List <float> data = Enumerable.Range(0, n).Select(i => ((float)i) / 10).ToList();
                for (int level = 1; level <= Bithacks.Log2(n); level++)
                {
                    int step = Bithacks.Power2(level);
                    Campy.Parallel.For(n / step, idx =>
                    {
                        var i   = step * idx;
                        data[i] = data[i] + data[i + step / 2];
                    });
                }
                result_gpu = data[0];
            }
            if (result_gpu != result_cpu)
            {
                throw new Exception();
            }
        }