public void RepeatValueTypeCastTest() { var t = (object)DateTime.Now; var result = QueryExpr.Repeat(t, 10); Assert.AreEqual(Enumerable.Repeat(t, 10), result.Run()); }
public static void Main(string[] args) { var t = Tuple.Create(10, 10); var func = Extensions.Compile <Tuple <int, int>, IEnumerable <int> >( tup => from x in QueryExpr.Range(1, tup.Item1) from y in Enumerable.Range(1, tup.Item2) select x *y ); var xs = func(t).ToArray(); //var input = Enumerable.Range(1, 20000000).Select(x => (double)x).ToArray(); //var query = input.AsQueryExpr()./*Where(x => x % 2 == 0).Select(x => x * x)*/Sum().Compile(); //var parQuery = input.AsParallelQueryExpr()./*Where(x => x % 2 == 0).Select(x => x * x)*/Sum().Compile(); ////var partitioner = Partitioner.Create(0, 100); ////var tuples = partitioner.GetDynamicPartitions().ToArray(); //Measure(() => //{ // double sum = 0; // for (int i = 0; i < input.Length; i++) // { // var x = input[i]; // //if (x % 2 == 0) // sum += x * x; // } //}); //Measure(() => query()); //Measure(() => parQuery()); }
public int PythagoreanTriplesLinqOpt() { return((from a in QueryExpr.Range(1, max + 1) from b in Enumerable.Range(a, max + 1 - a) from c in Enumerable.Range(b, max + 1 - b) where a * a + b * b == c * c select true).Count().Run()); }
public void Zip() { Prop.ForAll <List <int> >(ms => { var xs = QueryExpr.Zip(ms, ms, (a, b) => a * b).Run(); var ys = Enumerable.Zip(ms, ms, (a, b) => a * b); return(Enumerable.SequenceEqual(xs, ys)); }).QuickCheckThrowOnFailure(); }
private int UserDefinedAnonymousTypeTest(bool enable) { var anon = new { A = 1, B = "42" }; try { var t = QueryExpr.Range(1, 42).Select(_ => anon.A).Sum().Run(enable); return(t); } catch (Exception ex) { throw ex.InnerException; } }
/// <summary> /// Creates a query that projects each element of a sequence to an GpuArray, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <typeparam name="TCol">The type of the intermediate elements collected by collectionSelector.</typeparam> /// <typeparam name="TResult">The type of the elements of the sequence returned by selector.</typeparam> /// <param name="query">A query whose values to project.</param> /// <param name="collectionSelector">A transform function to apply to each element of the input sequence.</param> /// <param name="resultSelector">A transform function to apply to each element of the intermediate sequence.</param> /// <returns>A query whose elements are the result of invoking the one-to-many transform function on each element of the input sequence and the result selector function on each element therein.</returns> public static IGpuQueryExpr <IGpuArray <TResult> > SelectMany <TSource, TCol, TResult>(this IGpuQueryExpr <IGpuArray <TSource> > query, Expression <Func <TSource, IGpuArray <TCol> > > collectionSelector, Expression <Func <TSource, TCol, TResult> > resultSelector) { var paramExpr = collectionSelector.Parameters.Single(); var bodyExpr = collectionSelector.Body; if (bodyExpr.NodeType == ExpressionType.MemberAccess) { var nested = Tuple.Create(paramExpr, QueryExpr.NewSource((MemberExpression)bodyExpr, typeof(TCol), QueryExprType.Gpu)); return(new GpuQueryExpr <IGpuArray <TResult> >(QueryExpr.NewNestedQueryTransform(nested, resultSelector, query.Expr))); } else { throw new InvalidOperationException("Not supported " + bodyExpr.ToString()); } }
public void PreCompileFunc() { Prop.ForAll <int>(i => { if (i < 1) { return(true); } var t = Extensions.Compile <int, IEnumerable <string> >(m => QueryExpr.Range(1, m).Select(n => n.ToString())); var x = t(i); var y = Enumerable.Range(1, i).Select(n => n.ToString()); return(Enumerable.SequenceEqual(x, y)); }).QuickCheckThrowOnFailure(); }
public void PreCompileAction() { Prop.ForAll <int>(i => { if (i < 1) { return(true); } var xs = new List <int>(); Expression <Func <int, IQueryExpr> > lam = m => QueryExpr.Range(1, m).ForEach(n => xs.Add(n)); Action <int> t = Extensions.Compile <int>(lam); t(i); var ys = new List <int>(); Enumerable.Range(1, i).ToList().ForEach(n => ys.Add(n)); return(Enumerable.SequenceEqual(xs, ys)); }).QuickCheckThrowOnFailure(); }
public void SelectManyComprehensionNestedTestTypeEraser() { Prop.ForAll <int>(max => { if (max < 0) { return(true); } var x = (from a in QueryExpr.Range(1, max + 1) from b in Enumerable.Range(a, max + 1 - a) where a * a + b * b == b select Tuple.Create(a, b)).ToArray().Run(); var y = (from a in Enumerable.Range(1, max + 1) from b in Enumerable.Range(a, max + 1 - a) where a * a + b * b == b select Tuple.Create(a, b)).ToArray(); return(Enumerable.SequenceEqual(x, y)); }).QuickCheckThrowOnFailure(); }
public void SelectManyComprehensionNestedTest() { Spec.ForAny <int>(max => { if (max < 0) { return(true); } var x = (from a in QueryExpr.Range(1, max) from b in Enumerable.Range(1, max) from c in Enumerable.Range(1, max) where a + b == c select Tuple.Create(a, b, c)).ToArray().Run(); var y = (from a in Enumerable.Range(1, max) from b in Enumerable.Range(1, max) from c in Enumerable.Range(1, max) where a + b == c select Tuple.Create(a, b, c)).ToArray(); return(Enumerable.SequenceEqual(x, y)); }).QuickCheckThrowOnFailure(); }
/// <summary> /// Creates a new query that returns the number of elements in a gpu array. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <param name="query">A query whose elements will be count.</param> /// <returns>A query that returns the number of elements in the gpu array.</returns> public static IGpuQueryExpr <int> Count <TSource>(this IGpuQueryExpr <IGpuArray <TSource> > query) { return(new GpuQueryExpr <int>(QueryExpr.NewCount(query.Expr))); }
/// <summary> /// Creates a new query that computes the sum of a sequence of double values. /// </summary> /// <param name="query">A query whose sequence of int values to calculate the sum of.</param> /// <returns>A query that returns the sum of the values in the gpu array.</returns> public static IGpuQueryExpr <double> Sum(this IGpuQueryExpr <IGpuArray <double> > query) { return(new GpuQueryExpr <double>(QueryExpr.NewSum(query.Expr))); }
/// <summary> /// Creates a new query that computes the sum of a sequence of float values. /// </summary> /// <param name="query">A query whose sequence of int values to calculate the sum of.</param> /// <returns>A query that returns the sum of the values in the gpu array.</returns> public static IGpuQueryExpr <float> Sum(this IGpuQueryExpr <IGpuArray <float> > query) { return(new GpuQueryExpr <float>(QueryExpr.NewSum(query.Expr))); }
/// <summary> /// Creates a new query that filters a sequence of values based on a predicate. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <param name="query">An query whose values to filter.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <returns>A query that contains elements from the input query that satisfy the condition.</returns> public static IGpuQueryExpr <IGpuArray <TSource> > Where <TSource>(this IGpuQueryExpr <IGpuArray <TSource> > query, Expression <Func <TSource, bool> > predicate) { return(new GpuQueryExpr <IGpuArray <TSource> >(QueryExpr.NewFilter(predicate, query.Expr))); }
public Projection(QueryExpr source, QueryExpr selector) : base(QueryNodeType.Projection, source, selector) { }
/// <summary> /// Enables a gpu query. /// </summary> /// <typeparam name="TSource">The type of the elements in the source array.</typeparam> /// <param name="source">An array to convert to an IGpuQueryExpr.</param> /// <returns>A query that returns the elements of the source array.</returns> public static IGpuQueryExpr <IGpuArray <TSource> > AsGpuQueryExpr <TSource>(this IGpuArray <TSource> source) { return(new GpuQueryExpr <IGpuArray <TSource> >(QueryExpr.NewSource(Expression.Constant(source), typeof(TSource), QueryExprType.Gpu))); }
public void NestedRepeatInvalidArgTest() { Assert.Catch(typeof(ArgumentOutOfRangeException), () => QueryExpr.Range(1, 10).SelectMany(_ => Enumerable.Repeat(0, -1)).Run()); }
public Filter(QueryExpr source, QueryExpr predicate) : base(QueryNodeType.Filter, source, predicate) { }
public void RepeatInvalidArgTest() { Assert.Catch(typeof(ArgumentOutOfRangeException), () => QueryExpr.Repeat(0, -1).Run()); }
public void RepeatWhereTest() { var result = QueryExpr.Repeat(42, 10).Where(f => f < 5); Assert.AreEqual(Enumerable.Repeat(42, 10).Where(f => f < 5), result.Run()); }
public void RangeWhereTest() { var result = QueryExpr.Range(1, 10).Where(f => f < 5); Assert.AreEqual(Enumerable.Range(1, 10).Where(f => f < 5), result.Run()); }
/// <summary> /// A query that returns an array from an gpu array. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <param name="query">The query to create an array from.</param> /// <returns>A query that contains elements from the gpu array in an array form.</returns> public static IGpuQueryExpr <TSource[]> ToArray <TSource>(this IGpuQueryExpr <IGpuArray <TSource> > query) { return(new GpuQueryExpr <TSource[]>(QueryExpr.NewToArray(query.Expr))); }
public void RangeTest() { var result = QueryExpr.Range(1, 10); Assert.AreEqual(Enumerable.Range(1, 10), result.Run()); }
/// <summary> /// Creates a query that applies a specified function to the corresponding elements of two gpu arrays, producing a sequence of the results. /// </summary> /// <param name="first">The first gpu array to merge.</param> /// <param name="second">The first gpu array to merge.</param> /// <param name="resultSelector">A function that specifies how to merge the elements from the two gpu arrays.</param> /// <returns>A query that contains merged elements of two gpu arrays.</returns> public static IGpuQueryExpr <IGpuArray <TResult> > Zip <TFirst, TSecond, TResult>(IGpuArray <TFirst> first, IGpuArray <TSecond> second, Expression <Func <TFirst, TSecond, TResult> > resultSelector) { return(new GpuQueryExpr <IGpuArray <TResult> >(QueryExpr.NewZipWith(Expression.Constant(first), Expression.Constant(second), resultSelector))); }
public void RepeatTest() { var result = QueryExpr.Repeat(42, 10); Assert.AreEqual(Enumerable.Repeat(42, 10), result.Run()); }
/// <summary> /// Creates a new query that projects each element of a sequence into a new form. /// </summary> /// <typeparam name="TSource">The type of the elements of the query.</typeparam> /// <typeparam name="TResult">The type of the value returned by selector.</typeparam> /// <param name="query">A query whose values to invoke a transform function on.</param> /// <param name="selector">A transform function to apply to each element.</param> /// <returns>A query whose elements will be the result of invoking the transform function on each element of source.</returns> public static IGpuQueryExpr <IGpuArray <TResult> > Select <TSource, TResult>(this IGpuQueryExpr <IGpuArray <TSource> > query, Expression <Func <TSource, TResult> > selector) { return(new GpuQueryExpr <IGpuArray <TResult> >(QueryExpr.NewTransform(selector, query.Expr))); }
public Top(QueryExpr source, Constant count) : base(QueryNodeType.Top, source, count) { }
public UnaryQueryExpr(QueryNodeType nodeType, QueryExpr source, params QueryExpr[] arguments) : base(nodeType, new[] { source }.Concat(arguments).ToArray()) { Source = source; }
/// <summary> /// Initializes a new instance of the <see cref="GpuQueryExpr{T}"/> class. /// </summary> /// <param name="query">The expression.</param> public GpuQueryExpr(QueryExpr query) { _expr = query; }