Пример #1
0
        public void RepeatValueTypeCastTest()
        {
            var t      = (object)DateTime.Now;
            var result = QueryExpr.Repeat(t, 10);

            Assert.AreEqual(Enumerable.Repeat(t, 10), result.Run());
        }
Пример #2
0
        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());
        }
Пример #3
0
 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());
 }
Пример #4
0
        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();
        }
Пример #5
0
        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;
            }
        }
Пример #6
0
        /// <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());
            }
        }
Пример #7
0
        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();
        }
Пример #8
0
        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();
        }
Пример #9
0
        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();
        }
Пример #10
0
        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();
        }
Пример #11
0
 /// <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)));
 }
Пример #12
0
 /// <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)));
 }
Пример #13
0
 /// <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)));
 }
Пример #14
0
 /// <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)));
 }
Пример #15
0
 public Projection(QueryExpr source, QueryExpr selector)
     : base(QueryNodeType.Projection, source, selector)
 {
 }
Пример #16
0
 /// <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)));
 }
Пример #17
0
 public void NestedRepeatInvalidArgTest()
 {
     Assert.Catch(typeof(ArgumentOutOfRangeException), () => QueryExpr.Range(1, 10).SelectMany(_ => Enumerable.Repeat(0, -1)).Run());
 }
Пример #18
0
 public Filter(QueryExpr source, QueryExpr predicate)
     : base(QueryNodeType.Filter, source, predicate)
 {
 }
Пример #19
0
 public void RepeatInvalidArgTest()
 {
     Assert.Catch(typeof(ArgumentOutOfRangeException), () => QueryExpr.Repeat(0, -1).Run());
 }
Пример #20
0
        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());
        }
Пример #21
0
        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());
        }
Пример #22
0
 /// <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)));
 }
Пример #23
0
        public void RangeTest()
        {
            var result = QueryExpr.Range(1, 10);

            Assert.AreEqual(Enumerable.Range(1, 10), result.Run());
        }
Пример #24
0
 /// <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)));
 }
Пример #25
0
        public void RepeatTest()
        {
            var result = QueryExpr.Repeat(42, 10);

            Assert.AreEqual(Enumerable.Repeat(42, 10), result.Run());
        }
Пример #26
0
 /// <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)));
 }
Пример #27
0
 public Top(QueryExpr source, Constant count)
     : base(QueryNodeType.Top, source, count)
 {
 }
Пример #28
0
 public UnaryQueryExpr(QueryNodeType nodeType, QueryExpr source, params QueryExpr[] arguments)
     : base(nodeType, new[] { source }.Concat(arguments).ToArray())
 {
     Source = source;
 }
Пример #29
0
 /// <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;
 }