Esempio n. 1
0
        public static int CeilingLog2(uint v)
        {
            int r = Bithacks.FloorLog2(v);

            if (r < 0)
            {
                return(r);
            }
            if (v != (uint)Bithacks.Power2((uint)r))
            {
                return(r + 1);
            }
            else
            {
                return(r);
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
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();
            }
        }