Пример #1
0
        public static void Main(string[] args)
        {
            (new GpuQueryTests()).CompiledKernel();
            return;

            {
                int[] xs = new int[] { -1, -1 };


                using (var context = new GpuContext())
                {
                    Expression <Func <int, IGpuArray <int>, IGpuQueryExpr <int> > > queryExpr = (n, _xs) =>
                                                                                                _xs.AsGpuQueryExpr().Select(x => x * n).Sum();


                    using (var _xs = context.CreateGpuArray(xs))
                    {
                        using (var kernel = context.Compile(queryExpr))
                        {
                            var result = context.Run(kernel, 42, _xs);
                        }
                    }
                }
            }
        }
Пример #2
0
        public ScalarLinqRenderer(Action <int, int, int> dp, Func <bool> abortFunc)
            : base(dp, abortFunc)
        {
            Pair[] pairs = Enumerable.Range(0, 312).SelectMany(y => Enumerable.Range(0, 534).Select(x => new Pair {
                X = x, Y = y
            })).ToArray();
            int[] output = Enumerable.Range(0, pairs.Length).ToArray();
            this.context = new GpuContext("*");
            this._pairs  = context.CreateGpuArray(pairs);
            this._output = context.CreateGpuArray(output);

            parFunc = ParallelExtensions.Compile <float, float, float, int[]>(
                (_ymin, _xmin, _step) =>
                (from pair in pairs.AsParallelQueryExpr()
                 let _y = _ymin + _step * pair.Y
                          let _x = _xmin + _step * pair.X
                                   let c = new MyComplex(_x, _y)
                                           let iters = EnumerableEx.Generate(c, x => x.SquareLength < limit, x => x * x + c, x => x)
                                                       .Take(max_iters)
                                                       .Count()
                                                       select iters).ToArray()
                );

            seqFunc = Extensions.Compile <float, float, float, int[]>(
                (_ymin, _xmin, _step) =>
                (from pair in pairs.AsQueryExpr()
                 let _y = _ymin + _step * pair.Y
                          let _x = _xmin + _step * pair.X
                                   let c = new MyComplex(_x, _y)
                                           let iters = EnumerableEx.Generate(c, x => x.SquareLength < limit, x => x * x + c, x => x)
                                                       .Take(max_iters)
                                                       .Count()
                                                       select iters).ToArray()
                );
            gpuKernel =
                context.Compile <float, float, float, IGpuArray <int> >(
                    (_ymin, _xmin, _step) =>
                    from pair in _pairs.AsGpuQueryExpr()
                    let _y                                           = _ymin + _step * pair.Y
                                              let _x                 = _xmin + _step * pair.X
                                                               let c = new Complex {
                Real = _x, Img = _y
            }
                    let iters = EnumerableEx.Generate(c, x => squareLength.Invoke(x) < limit,
                                                      x => add.Invoke(mult.Invoke(x, x), c), x => x)
                                .Take(max_iters)
                                .Count()
                                select iters);
        }
Пример #3
0
 public void CompiledKernel()
 {
     using (var context = new GpuContext(platformWildCard))
     {
         Expression <Func <int, IGpuArray <int>, IGpuQueryExpr <int> > > queryExpr = (n, _xs) =>
                                                                                     _xs.AsGpuQueryExpr().Select(x => x * n).Sum();
         var kernel = context.Compile(queryExpr);
         int _n     = 42;
         Spec.ForAny <int[]>(xs =>
         {
             using (var _xs = context.CreateGpuArray(xs))
             {
                 var gpuResult = context.Run(kernel, _n, _xs);
                 var cpuResult = xs.Select(x => x * _n).Sum();
                 return(gpuResult == cpuResult);
             }
         }).QuickCheckThrowOnFailure();
     }
 }
Пример #4
0
        static void Main(string[] args)
        {
            int       size   = 1000000;
            Random    random = new Random();
            var       input  = Enumerable.Range(1, size).Select(x => (float)random.NextDouble()).ToArray();
            Stopwatch timer  = new Stopwatch();

            float[] gpuResult;
            using (var context = new GpuContext())
            {
                using (var xs = context.CreateGpuArray(input))
                {
                    // Dot Product
                    var kernel = context.Compile <IGpuArray <float>, float>(_xs => GpuQueryExpr.Zip(_xs, _xs, (a, b) => a * b).Sum());
                    timer.Start();
                    gpuResult = Enumerable.Range(1, 100).Select(i => context.Run(kernel, xs)).ToArray();
                    timer.Stop();
                    var elapsed = timer.ElapsedTicks;
                    Console.WriteLine("GPU Computation w/o compilation took {0} ticks", elapsed / 100.0);
                    Console.WriteLine("Result: {0}", gpuResult[0]);
                }

                //var elapsed = timer.ElapsedMilliseconds;
                //Console.WriteLine("GPU Computation took {0} milliseconds", elapsed);
            }

            timer.Restart();
            var res = Enumerable.Range(1, 100).Select(i => input.Zip(input, (a, b) => a * b).Sum()).ToArray();

            timer.Stop();
            var elapsed2 = timer.ElapsedTicks;

            Console.WriteLine("CPU Computation took {0} ticks", elapsed2 / 100.0);
            if (res[0] == gpuResult[0])
            {
                Console.WriteLine("Cpu and Gpu computations are identical");
            }
            else
            {
                Console.WriteLine("Cpu and Gpu computations are different");
            }
            Console.ReadLine();
        }
Пример #5
0
        public static void Main(string[] args)
        {
            {
                var xs = Enumerable.Range(1, 1000000).Select(x => (long)x).ToArray();


                using (var context = new GpuContext("Intel*"))
                {
                    Expression <Func <int, IGpuArray <long>, IGpuQueryExpr <long> > > queryExpr = (n, _xs) =>
                                                                                                  _xs.AsGpuQueryExpr().Select(x => x * n).Sum();


                    using (var _xs = context.CreateGpuArray(xs))
                    {
                        using (var kernel = context.Compile(queryExpr))
                        {
                            var result = context.Run(kernel, 2, _xs);
                        }
                    }
                }
            }
        }