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() ); }
public void GpuArrayIndexer() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int>(n => { if (n < 0) { n = 0; } var nums = Enumerable.Range(0, n).ToArray(); using (var _nums = context.CreateGpuArray(nums)) { using (var __nums = context.CreateGpuArray(nums)) { int length = __nums.Length; var xs = context.Run((from num in _nums.AsGpuQueryExpr() let y = __nums[num % length] select num + y).ToArray()); var ys = (from num in nums let y = nums[num % length] select num + y).ToArray(); return(xs.SequenceEqual(ys)); } } }).QuickCheckThrowOnFailure(); } }
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); } } } } }
public void SelectManyWithLet() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var query = (from x in _xs.AsGpuQueryExpr() from _x in _xs let test = x * _x select test + 1).ToArray(); var gpuResult = context.Run(query); var cpuResult = (from x in xs from _x in xs let test = x * _x select test + 1).ToArray(); return(gpuResult.SequenceEqual(cpuResult)); } }).QuickCheckThrowOnFailure(); } }
public void InnerEnumerablePipeline() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var query = (from x in _xs.AsGpuQueryExpr() let y = x let test = EnumerableEx.Generate(1, i => i < y, i => i + 1, i => i) .Take(10) .Count() select test + 1).ToArray(); var gpuResult = context.Run(query); var cpuResult = (from x in xs let y = x let test = EnumerableEx.Generate(1, i => i < y, i => i + 1, i => i) .Take(10) .Count() select test + 1).ToArray();; return(gpuResult.SequenceEqual(cpuResult)); } }).QuickCheckThrowOnFailure(); } }
public void FunctionSplicing() { using (var context = new GpuContext()) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { Expression <Func <int, int> > g = x => x + 1; Expression <Func <int, int> > f = x => 2 * g.Invoke(x); Expression <Func <int, int> > h = x => g.Invoke(x) + f.Invoke(1); var query = (from x in _xs.AsGpuQueryExpr() let m = h.Invoke(x) let k = f.Invoke(x) let y = g.Invoke(x) select k).ToArray(); var gpuResult = context.Run(query); Func <int, int> _g = x => x + 1; Func <int, int> _f = x => 2 * _g.Invoke(x); var cpuResult = (from x in xs select _f.Invoke(x)).ToArray(); return(gpuResult.SequenceEqual(cpuResult)); } }).QuickCheckThrowOnFailure(); } }
public void FunctionSplicingVariadic() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { Expression <Func <int, int> > g = x => x + 1; Expression <Func <int, int, int> > f = (x, y) => y * g.Invoke(x); Expression <Func <int, int, int, int> > h = (x, y, z) => g.Invoke(x) + f.Invoke(y, z); Expression <Func <int, int, int, int, int> > i = (x, y, z, w) => g.Invoke(x) + f.Invoke(x, y) + h.Invoke(x, y, z) + w; var query = (from x in _xs.AsGpuQueryExpr() let n = i.Invoke(x, x, x, x) let m = h.Invoke(x, x, x) let l = f.Invoke(x, x) let k = g.Invoke(x) select k).ToArray(); var gpuResult = context.Run(query); Func <int, int> _g = x => x + 1; var cpuResult = (from x in xs select _g.Invoke(x)).ToArray(); return(gpuResult.SequenceEqual(cpuResult)); } }).QuickCheckThrowOnFailure(); } }
public void MathFunctionsSingle() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var gpuResult = context.Run((from n in _xs.AsGpuQueryExpr() let pi = SMath.PI let c = SMath.Cos(n) let s = SMath.Sin(n) let f = SMath.Floor(pi) let sq = SMath.Sqrt(n * n) let ex = SMath.Exp(pi) let p = SMath.Pow(pi, 2) let a = SMath.Abs(c) let l = SMath.Log(n) select f *pi *c *s *sq *ex *p *a *l).ToArray()); var openClResult = this.MathFunctionsSingleTest(xs); return(gpuResult.Zip(openClResult, (x, y) => (float.IsNaN(x) && float.IsNaN(y)) ? true : System.Math.Abs(x - y) < 0.001) .SequenceEqual(Enumerable.Range(1, xs.Length).Select(_ => true))); } }).QuickCheckThrowOnFailure(); } }
public void Structs() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(nums => { var nodes = nums.Select(num => new Node { x = num, y = num }).ToArray(); using (var _nodes = context.CreateGpuArray(nodes)) { var xs = context.Run((from node in _nodes.AsGpuQueryExpr() let x = node.x + 2 let y = node.y + 1 select new Node { x = x, y = y }).ToArray()); var ys = (from node in nodes let x = node.x + 2 let y = node.y + 1 select new Node { x = x, y = y }).ToArray(); return(xs.SequenceEqual(ys)); } }).QuickCheckThrowOnFailure(); } }
public void LinqLet() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(nums => { using (var _nums = context.CreateGpuArray(nums)) { var x = context.Run((from num in _nums.AsGpuQueryExpr() let a = num * 2 let c = a + 1 let b = a * 2 let e = b - 5 let d = c * c let m = 3 select a + b + c + d + e + m + num).Sum()); var y = (from num in nums let a = num * 2 let c = a + 1 let b = a * 2 let e = b - 5 let d = c * c let m = 3 select a + b + c + d + e + m + num).Sum(); return(x == y); } }).QuickCheckThrowOnFailure(); } }
static void Main(string[] args) { // Based on http://brahma.codeplex.com/SourceControl/latest#trunk/Source/Samples/OpenCL/FastFourierTransform/Program.cs int size = 8388608; // Input Data Random random = new Random(); var input = Enumerable.Range(1, size).Select(x => new Complex { A = (float)random.NextDouble(), B = 0.0f }).ToArray(); var output = Enumerable.Range(1, size).Select(x => new Complex { A = 0.0f, B = 0.0f }).ToArray(); var xs = Enumerable.Range(0, size - 1).ToArray(); using (var context = new GpuContext()) { using (var _xs = context.CreateGpuArray(xs)) { var _input = context.CreateGpuArray(input); var _output = context.CreateGpuArray(output); // Forward FFT int fftSize = 2; for (int i = 0; i < System.Math.Log(size, 2.0); i++) { var query = (from x in _xs.AsGpuQueryExpr() let b = (((int)FMath.Floor((float)x / fftSize)) * (fftSize / 2)) let offset = x % (fftSize / 2) let x0 = b + offset let x1 = x0 + size / 2 let val0 = _input[x0] let val1 = _input[x1] let angle = -2 * FMath.PI * (x / fftSize) let t = new Complex { A = FMath.Cos(angle), B = FMath.Sin(angle) } select new Complex { A = val0.A + t.A * val1.A - t.B * val1.B, B = val0.B + t.B * val1.A + t.A * val1.B }); fftSize *= 2; context.Fill(query, _output); Swap(ref _input, ref _output); } } } }
public void Fill() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { using (var _out = context.CreateGpuArray(Enumerable.Range(1, xs.Length).ToArray())) { context.Fill(_xs.AsGpuQueryExpr().Select(n => n * 2), _out); var y = xs.Select(n => n * 2).ToArray(); return(_out.ToArray().SequenceEqual(y)); } } }).QuickCheckThrowOnFailure(); } }
public void Select() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var x = context.Run(_xs.AsGpuQueryExpr().Select(n => n * 2).ToArray()); var y = xs.Select(n => n * 2).ToArray(); return(x.SequenceEqual(y)); } }).QuickCheckThrowOnFailure(); } }
public void Zip() { using (var context = new GpuContext()) { Spec.ForAny <int[]>(ms => { using (var _ms = context.CreateGpuArray(ms)) { var xs = context.Run(GpuQueryExpr.Zip(_ms, _ms, (a, b) => a * b).Select(x => x + 1).ToArray()); var ys = Enumerable.Zip(ms, ms, (a, b) => a * b).Select(x => x + 1).ToArray(); return(xs.SequenceEqual(ys)); } }).QuickCheckThrowOnFailure(); } }
public void ZipWithFilter() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(ms => { using (var _ms = context.CreateGpuArray(ms)) { var xs = context.Run(GpuQueryExpr.Zip(_ms, _ms, (a, b) => a * b).Where(x => x % 2 == 0).ToArray()); var ys = Enumerable.Zip(ms, ms, (a, b) => a * b).Where(x => x % 2 == 0).ToArray(); return(xs.SequenceEqual(ys)); } }).QuickCheckThrowOnFailure(); } }
public void ZipWithReduction() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(ms => { using (var _ms = context.CreateGpuArray(ms)) { // Dot Product var xs = context.Run(GpuQueryExpr.Zip(_ms, _ms, (a, b) => a * b).Sum()); var ys = Enumerable.Zip(ms, ms, (a, b) => a * b).Sum(); return(xs == ys); } }).QuickCheckThrowOnFailure(); } }
static void Main(string[] args) { int size = 1000; Random random = new Random(); var input = Enumerable.Range(1, size).Select(x => (float)random.NextDouble()).ToArray(); using (var context = new GpuContext()) { using (var xs = context.CreateGpuArray(input)) { // Dot Product var query = GpuQueryExpr.Zip(xs, xs, (a, b) => a * b).Sum(); var result = context.Run(query); Console.WriteLine("Result: {0}", result); } } }
public void Sum() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var x = context.Run((from n in _xs.AsGpuQueryExpr() select n + 1).Sum()); var y = (from n in xs select n + 1).Sum(); return(x == y); } }).QuickCheckThrowOnFailure(); } }
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(); }
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(); } }
public void Where() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var x = context.Run((from n in _xs.AsGpuQueryExpr() where n % 2 == 0 select n + 1).ToArray()); var y = (from n in xs where n % 2 == 0 select n + 1).ToArray(); return(x.SequenceEqual(y)); } }).QuickCheckThrowOnFailure(); } }
public void Count() { using (var context = new GpuContext()) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var x = context.Run((from n in _xs.AsGpuQueryExpr() where n % 2 == 0 select n + 1).Count()); var y = (from n in xs where n % 2 == 0 select n + 1).Count(); return(x == y); } }).QuickCheckThrowOnFailure(); } }
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); } } } } }
public void ConstantLifting() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(nums => { using (var _nums = context.CreateGpuArray(nums)) { int c = nums.Length; var xs = context.Run((from num in _nums.AsGpuQueryExpr() let y = num + c let k = y + c select c + y + k).ToArray()); var ys = (from num in nums let y = num + c let k = y + c select c + y + k).ToArray(); return(xs.SequenceEqual(ys)); } }).QuickCheckThrowOnFailure(); } }
public void TernaryIfElse() { using (var context = new GpuContext(platformWildCard)) { Spec.ForAny <int[]>(xs => { using (var _xs = context.CreateGpuArray(xs)) { var query = (from n in _xs.AsGpuQueryExpr() where n > 10 select(n % 2 == 0) ? 1 : 0).ToArray(); var gpuResult = context.Run(query); var cpuResult = (from n in xs where n > 10 select(n % 2 == 0) ? 1 : 0).ToArray(); return(gpuResult.SequenceEqual(cpuResult)); } }).QuickCheckThrowOnFailure(); } }
static void Main(string[] args) { // Based on https://github.com/gsvgit/Brahma.FSharp/tree/master/Source/BlackScholes const int optionCount = 1000000; const float riskFreeInterestRate = 0.02f; const float volatility = 0.30f; const float A1 = 0.31938153f; const float A2 = -0.356563782f; const float A3 = 1.781477937f; const float A4 = -1.821255978f; const float A5 = 1.330274429f; // Helper functions Expression <Func <float, float> > k = x => 1.0f / (1.0f + 0.2316419f * FMath.Abs(x)); Expression <Func <float, float> > cnd = x => 1.0f - 1.0f / FMath.Sqrt(2.0f * FMath.PI) * FMath.Exp(-(FMath.Abs(x)) * (FMath.Abs(x)) / 2.0f) * (A1 * k.Invoke(x) + A2 * k.Invoke(x) * k.Invoke(x) + A3 * FMath.Pow(k.Invoke(x), 3) + A4 * FMath.Pow(k.Invoke(x), 4) + A5 * FMath.Pow(k.Invoke(x), 5)); Expression <Func <float, float> > cumulativeNormalDistribution = x => (x < 0.0f) ? 1.0f - cnd.Invoke(x) : cnd.Invoke(x); Expression <Func <float, float, float, float> > d1 = (stockPrice, strikePrice, timeToExpirationYears) => FMath.Log(stockPrice / strikePrice) + (riskFreeInterestRate + volatility * volatility / 2.0f) * timeToExpirationYears / (volatility * FMath.Sqrt(timeToExpirationYears)); Expression <Func <float, float, float> > d2 = (_d1, timeToExpirationYears) => _d1 - volatility * FMath.Sqrt(timeToExpirationYears); Expression <Func <float, float, float, float, float, float> > blackScholesCallOption = (_d1, _d2, stockPrice, strikePrice, timeToExpirationYears) => stockPrice *cumulativeNormalDistribution.Invoke(_d1) - strikePrice * FMath.Exp(-(riskFreeInterestRate) * timeToExpirationYears) * cumulativeNormalDistribution.Invoke(_d2); Expression <Func <float, float, float, float, float, float> > blackScholesPutOption = (_d1, _d2, stockPrice, strikePrice, timeToExpirationYears) => strikePrice *FMath.Exp(-riskFreeInterestRate *timeToExpirationYears) * cumulativeNormalDistribution.Invoke(-_d2) - stockPrice * cumulativeNormalDistribution.Invoke(-_d1); // Init data var random = new System.Random(); var data = Enumerable.Range(1, optionCount).Select(_ => new InputData { Stock = random.Random(5.0f, 30.0f), Strike = random.Random(1.0f, 100.0f), Times = random.Random(0.25f, 10.0f) }).ToArray(); // Main code Stopwatch timer = new Stopwatch(); timer.Start(); OutPutData[] results; using (GpuContext context = new GpuContext()) { using (var _data = context.CreateGpuArray(data)) { var query = (from d in _data.AsGpuQueryExpr() let _d1 = d1.Invoke(d.Stock, d.Strike, d.Times) let _d2 = d2.Invoke(_d1, d.Times) select new OutPutData { Call = blackScholesCallOption.Invoke(_d1, _d2, d.Stock, d.Strike, d.Times), Put = blackScholesPutOption.Invoke(_d1, _d2, d.Stock, d.Strike, d.Times) }).ToArray(); results = context.Run(query); } } timer.Stop(); var elapsed = timer.ElapsedMilliseconds; Console.WriteLine("Black Scholes computed on Gpu in {0} milliseconds", elapsed); //String.Join<OutPutData>(", ", Enumerable.Range(1,120). // Select(i => results.ElementAtOrDefault(i)).ToArray())); timer.Restart(); var cpuResults = CpuBlackScholes(data); timer.Stop(); elapsed = timer.ElapsedMilliseconds; Console.WriteLine("Black Scholes computed on Cpu in {0} milliseconds", elapsed); var resultsAreEqual = cpuResults.Zip(results, (a, b) => Tuple.Create(a, b)).All(pair => pair.Item1.IsEqual(pair.Item2)); Console.WriteLine("Results calculated by cpu and gpu are {0}", resultsAreEqual ? "equal" : "not equal"); Console.WriteLine("Results = {0}", String.Join <Results>(", ", Enumerable.Range(1, 120). Select(i => new Results() { Gpu = results.ElementAtOrDefault(i), Cpu = cpuResults.ElementAtOrDefault(i) }).ToArray())); Console.ReadLine(); }
static void Main(string[] args) { // Based on https://github.com/gsvgit/Brahma.FSharp/tree/master/Source/BlackScholes const int optionCount = 1000000; const float riskFreeInterestRate = 0.02f; const float volatility = 0.30f; const float A1 = 0.31938153f; const float A2 = -0.356563782f; const float A3 = 1.781477937f; const float A4 = -1.821255978f; const float A5 = 1.330274429f; // Helper functions Expression <Func <float, float> > k = x => 1.0f / (1.0f + 0.2316419f * FMath.Abs(x)); Expression <Func <float, float> > cnd = x => 1.0f - 1.0f / FMath.Sqrt(2.0f * FMath.PI) * FMath.Exp(-(FMath.Abs(x)) * (FMath.Abs(x)) / 2.0f) * (A1 * k.Invoke(x) + A2 * k.Invoke(x) * k.Invoke(x) + A3 * FMath.Pow(k.Invoke(x), 3) + A4 * FMath.Pow(k.Invoke(x), 4) + A5 * FMath.Pow(k.Invoke(x), 4)); Expression <Func <float, float> > cumulativeNormalDistribution = x => (x < 0.0f) ? 1.0f - cnd.Invoke(x) : cnd.Invoke(x); Expression <Func <float, float, float, float> > d1 = (stockPrice, strikePrice, timeToExpirationYears) => FMath.Log(stockPrice / strikePrice) + (riskFreeInterestRate + volatility * volatility / 2.0f) * timeToExpirationYears / (volatility * FMath.Sqrt(timeToExpirationYears)); Expression <Func <float, float, float> > d2 = (_d1, timeToExpirationYears) => _d1 - volatility * FMath.Sqrt(timeToExpirationYears); Expression <Func <float, float, float, float, float, float> > blackScholesCallOption = (_d1, _d2, stockPrice, strikePrice, timeToExpirationYears) => stockPrice *cumulativeNormalDistribution.Invoke(_d1) - strikePrice * FMath.Exp(-(riskFreeInterestRate) * timeToExpirationYears) * cumulativeNormalDistribution.Invoke(_d2); Expression <Func <float, float, float, float, float, float> > blackScholesPutOption = (_d1, _d2, stockPrice, strikePrice, timeToExpirationYears) => strikePrice *FMath.Exp(-riskFreeInterestRate *timeToExpirationYears) * cumulativeNormalDistribution.Invoke(-_d2) - stockPrice * cumulativeNormalDistribution.Invoke(-_d1); // Init data var random = new System.Random(); var data = Enumerable.Range(1, optionCount).Select(_ => new InputData { Stock = random.Random(5.0f, 30.0f), Strike = random.Random(1.0f, 100.0f), Times = random.Random(0.25f, 10.0f) }).ToArray(); // Main code using (GpuContext context = new GpuContext()) { using (var _data = context.CreateGpuArray(data)) { var query = (from d in _data.AsGpuQueryExpr() let _d1 = d1.Invoke(d.Stock, d.Strike, d.Times) let _d2 = d2.Invoke(_d1, d.Times) select new OutPutData { Call = blackScholesCallOption.Invoke(_d1, _d2, d.Stock, d.Strike, d.Times), Put = blackScholesPutOption.Invoke(_d1, _d2, d.Stock, d.Strike, d.Times) }).ToArray(); var result = context.Run(query); } } }