private void DoWorkDeferred(NArray a, Normal normalDistribution, bool threaded = false)
        {
            // A version where assignment happens, but we defer execution.
            var result = NArray.CreateLike(a);

            result.Assign(a);

            var result2 = NArray.CreateLike(a);

            //var options = new DeferredExecution.VectorExecutionOptions() { MultipleThreads = threaded };

            for (int j = 0; j < 100; ++j)
            {
                NArray.Evaluate(() =>
                {
                    return(NMath.Log(NMath.Exp(result)));
                }, new List <NArray>(), Aggregator.ElementwiseAdd, new List <NArray> {
                    result2
                });

                //using (NArray.DeferredExecution(options))
                //{
                //    var temp = NMath.Exp(result);
                //    result.Assign(NMath.Log(temp));
                //}
            }
        }
Exemple #2
0
        public void CUDA()
        {
            var location = StorageLocation.Host;
            var normal   = new Normal(new RandomNumberStream(location), 0, 1);
            var input    = NArray.CreateRandom(1000, normal);
            var result   = NArray.CreateLike(input);

            NArray.Evaluate(() =>
            {
                return(NMath.Exp(input * 0.2 + 6));
            });
        }
        private void DoWorkInPlace(NArray a, Normal normalDistribution)
        {
            // This will do compute-limited work that we would expect to task parallelize well
            var result = NArray.CreateLike(a);

            result.Assign(a);
            for (int j = 0; j < 100; ++j)
            {
                IntelMathKernelLibrary.Exp(GetArray(result), 0, GetArray(result), 0, result.Length);
                IntelMathKernelLibrary.Log(GetArray(result), 0, GetArray(result), 0, result.Length);
            }
        }
        private void DoWork(NArray a, Normal normalDistribution)
        {
            // This will do compute-limited work that we would expect to task parallelize well
            var result = NArray.CreateLike(a);

            result.Assign(a);
            for (int j = 0; j < 100; ++j)
            {
                //result.FillNormal(random);

                var temp = NMath.Exp(result);
                result.Assign(NMath.Log(temp));
            }
        }
Exemple #5
0
        public void BlackScholesPerformance()
        {
            double k          = 90;
            var    volatility = 0.2;
            var    t          = 5;

            var s            = new List <NArray>();
            int batchCount   = 1000;
            int vectorLength = 5000;

            using (var stream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A))
            {
                var normal = new Normal(stream, 0, 1);
                for (int i = 0; i < batchCount; ++i)
                {
                    s.Add(100 * NMath.Exp(NArray.CreateRandom(vectorLength, normal) * 0.2));
                }
            }

            var result = NArray.CreateLike(s.First());

            double elapsedTime = TestHelpers.TimeitSeconds(() =>
            {
                for (int i = 0; i < batchCount; ++i)
                {
                    NArray.Evaluate(() =>
                    {
                        return(Finance.BlackScholes(CallPut.Call, s[i], k, volatility, t));
                    }, new List <NArray>(), Aggregator.ElementwiseAdd, new List <NArray> {
                        result
                    });
                }
            });

            Console.WriteLine(string.Format("Time per option price (single core): {0} ns", elapsedTime * 1e9 / (batchCount * vectorLength)));
            Console.WriteLine(string.Format("Valuations per second (single core): {0:F0} million", batchCount * vectorLength / (elapsedTime * 1e6)));
        }
        /// <summary>
        /// Test of efficiency for a large number of simple vector operations.
        /// In particular, tests enhanced recycling of locals.
        /// </summary>
        public void SimpleTest()
        {
            // create 500 random vectors
            var vectorLength = 5000;
            var vectors      = new NArray[500];

            using (var randomStream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                for (int i = 0; i < vectors.Length; ++i)
                {
                    vectors[i] = NArray.CreateRandom(vectorLength, normalDistribution);
                }
            }
            var watch  = new System.Diagnostics.Stopwatch(); watch.Start();
            var result = NArray.CreateLike(vectors.First());

            for (int i = 0; i < vectors.Length; ++i)
            {
                for (int j = 0; j < vectors.Length; ++j)
                {
                    result.Add(vectors[i] * vectors[j] * 123);
                    //result = result + vectors[i] * vectors[j] * 123;
                }
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

            var arrays = vectors.Select(v => (v.Storage as ManagedStorage <double>).Array).ToArray();

            watch.Restart();
            var arrayResult = new double[vectors.First().Length];

            for (int i = 0; i < vectors.Length; ++i)
            {
                for (int j = 0; j < vectors.Length; ++j)
                {
                    for (int k = 0; k < arrayResult.Length; ++k)
                    {
                        arrayResult[k] += arrays[i][k] * arrays[j][k] * 123;
                    }
                }
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

            watch.Restart();
            result = NArray.CreateLike(vectors.First());
            for (int i = 0; i < vectors.Length; ++i)
            {
                var batch = NArray.Evaluate(() =>
                {
                    NArray res = NArray.CreateScalar(0);
                    for (int j = 0; j < vectors.Length; ++j)
                    {
                        res = res + vectors[i] * vectors[j] * 123;
                    }
                    return(res);
                });
                result += batch;
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
        }
        public void VectorFundamentalsTest()
        {
            var location = StorageLocation.Host;

            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                var a      = new NArray(StorageLocation.Host, 5000);
                var b      = new NArray(StorageLocation.Host, 5000);
                var c      = new NArray(StorageLocation.Host, 5000);
                var d      = new NArray(StorageLocation.Host, 5000);
                var result = NArray.CreateLike(a);
                a.FillRandom(normalDistribution);
                b.FillRandom(normalDistribution);
                c.FillRandom(normalDistribution);
                d.FillRandom(normalDistribution);
                var aArray      = GetArray(a);
                var bArray      = GetArray(b);
                var cArray      = GetArray(c);
                var dArray      = GetArray(d);
                var resultArray = GetArray(result);

                Console.WriteLine(); Console.WriteLine("In place vector Exp MKL");
                TestHelpers.Timeit(() =>
                {
                    IntelMathKernelLibrary.Exp(aArray, 0, resultArray, 0, result.Length);
                }, 20, 50);

                Console.WriteLine(); Console.WriteLine("In place vector Exp C sharp");
                TestHelpers.Timeit(() =>
                {
                    for (int i = 0; i < 5000; ++i)
                    {
                        resultArray[i] = Math.Exp(aArray[i]);
                    }
                }, 20, 50);

                Console.WriteLine(); Console.WriteLine("In place vector aX + Y MKL");
                TestHelpers.Timeit(() =>
                {
                    IntelMathKernelLibrary.ConstantAddMultiply(aArray, 0, 3, 0, resultArray, 0, 5000);
                    //IntelMathKernelLibrary.Multiply(bArray, 0, resultArray, 0, resultArray, 0, result.Length);
                    IntelMathKernelLibrary.Add(bArray, 0, resultArray, 0, resultArray, 0, result.Length);
                    //IntelMathKernelLibrary.Exp(resultArray, 0, resultArray, 0, result.Length);
                    // 3 reads and 2 writes
                }, 20, 50);

                Console.WriteLine(); Console.WriteLine("In place vector aX + Y C sharp");
                TestHelpers.Timeit(() =>
                {
                    for (int i = 0; i < 5000; ++i)
                    {
                        resultArray[i] = 3 * aArray[i] + bArray[i]; // 2 reads and a write
                        //resultArray[i] = Math.Exp(3 * aArray[i] + bArray[i]); // 2 reads and a write
                    }
                }, 20, 50);

                Console.WriteLine(); Console.WriteLine("Immediate mode; creating storage");
                TestHelpers.Timeit(() =>
                {
                    //var result2 = NMath.Exp(3 * a + b);
                    var result2 = 3 * a + b;
                }, 20, 50);

                var result3 = NArray.CreateLike(a);

                Console.WriteLine(); Console.WriteLine("Deferred mode; storage passed in");
                TestHelpers.Timeit(() =>
                {
                    NArray.Evaluate(() =>
                    {
                        //return NMath.Exp(3 * a + b);
                        return(3 * a + b);
                    }
                                    , new List <NArray>(), Aggregator.ElementwiseAdd, new List <NArray> {
                        result3
                    });
                }, 20, 50);

                return;

                Console.WriteLine(); Console.WriteLine("Deferred mode; storage passed in");
                TestHelpers.Timeit(() =>
                {
                    NArray.Evaluate(() =>
                    {
                        return(3 * a + b);
                    }
                                    , new List <NArray>(), Aggregator.ElementwiseAdd, new List <NArray> {
                        result
                    });
                }, 20, 50);
            }
        }
Exemple #8
0
        public void TestBlackScholes()
        {
            var location = StorageLocation.Host;

            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                double deltaT = 1;
                double r      = 0.1;
                double vol    = 0.3;

                var options = new ParallelOptions();
                options.MaxDegreeOfParallelism = 1;

                var    variates     = NArray.CreateRandom(5000, normalDistribution);
                NArray optionPrices = NArray.CreateLike(variates);
                var    watch        = new Stopwatch(); watch.Start();

                Parallel.For(0, 1000, options, (i) =>
                {
                    optionPrices      = NArray.CreateLike(variates);
                    var logStockPrice = Math.Log(100)
                                        + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                    var stockPrices = NMath.Exp(logStockPrice);

                    optionPrices.Assign(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                });
                Console.WriteLine("Baseline sequential");
                Console.WriteLine(watch.ElapsedMilliseconds); watch.Restart();

                Parallel.For(0, 1000, (i) =>
                {
                    optionPrices      = NArray.CreateLike(variates);
                    var logStockPrice = Math.Log(100)
                                        + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                    var stockPrices = NMath.Exp(logStockPrice);

                    optionPrices.Assign(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                });
                Console.WriteLine("Baseline threaded");
                Console.WriteLine(watch.ElapsedMilliseconds);

                NArray optionPrices2 = NArray.CreateLike(variates);

                watch.Restart();
                var vectorOptions = new DeferredExecution.VectorExecutionOptions()
                {
                    MultipleThreads = true
                };
                Parallel.For(0, 1000, options, (i) =>
                {
                    optionPrices2 = NArray.CreateLike(variates);
                    NArray.Evaluate(optionPrices2, () =>
                    {
                        var logStockPrice = Math.Log(100)
                                            + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                        var stockPrices = NMath.Exp(logStockPrice);

                        return(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                    });
                });
                Console.WriteLine("Deferred sequential");
                Console.WriteLine(watch.ElapsedMilliseconds);
                Console.WriteLine(CheckitString((optionPrices.Storage as ManagedStorage <double>).Array, (optionPrices2.Storage as ManagedStorage <double>).Array));
                watch.Restart();

                Parallel.For(0, 1000, (i) =>
                {
                    //optionPrices2 = NArray.CreateLike(variates);
                    optionPrices2 = NArray.Evaluate(() =>
                    {
                        var logStockPrice = Math.Log(100)
                                            + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                        var stockPrices = NMath.Exp(logStockPrice);

                        return(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                    });
                });
                Console.WriteLine("Deferred threaded");
                Console.WriteLine(watch.ElapsedMilliseconds); watch.Restart();
                Console.WriteLine(CheckitString((optionPrices.Storage as ManagedStorage <double>).Array, (optionPrices2.Storage as ManagedStorage <double>).Array));
            }
        }
Exemple #9
0
        public void SimpleSpeedTest()
        {
            var factory = new NArrayFactory(StorageLocation.Host);

            IntelMathKernelLibrary.SetAccuracyMode(VMLAccuracy.LowAccuracy);

            int length = 1024 * 5;

            var a  = factory.CreateFromEnumerable(Enumerable.Range(0, length).Select(i => (double)i / length));
            var a2 = (a.Storage as VectorAccelerator.NArrayStorage.ManagedStorage <double>).Array;
            var b  = factory.CreateFromEnumerable(Enumerable.Range(3, length).Select(i => (double)i / length));
            var b2 = (b.Storage as VectorAccelerator.NArrayStorage.ManagedStorage <double>).Array;

            double[] result  = null;
            NArray   resultN = null;

            Console.WriteLine("Allocation");
            Timeit(
                () =>
            {
                result = new double[length];
            });
            Console.WriteLine(Environment.NewLine);

            result = new double[length];
            Console.WriteLine("Managed optimal");
            Timeit(
                () =>
            {
                for (int i = 0; i < result.Length; ++i)
                //Parallel.For(0, result.Length, (i) =>
                {
                    result[i] = 3 * Math.Log(Math.Exp(a2[i]) * Math.Exp(b2[i]));
                }
                //);
            });
            Console.WriteLine(Environment.NewLine);

            var vectorOptions = new VectorAccelerator.DeferredExecution.VectorExecutionOptions();

            vectorOptions.MultipleThreads = true;
            resultN = NArray.CreateLike(a);
            Console.WriteLine("Deferred threaded");
            Timeit(
                () =>
            {
                NArray.Evaluate(vectorOptions, () =>
                {
                    return(3 * NMath.Log(NMath.Exp(a) * NMath.Exp(b)));
                });
            });
            Console.WriteLine(CheckitString(result, (resultN.Storage as ManagedStorage <double>).Array));
            Console.WriteLine(Environment.NewLine);

            resultN = NArray.CreateLike(a);
            vectorOptions.MultipleThreads = false;
            Console.WriteLine("Deferred not threaded");
            Timeit(
                () =>
            {
                NArray.Evaluate(vectorOptions, () =>
                {
                    return(3 * NMath.Log(NMath.Exp(a) * NMath.Exp(b)));
                });
            });
            Console.WriteLine(CheckitString(result, (resultN.Storage as ManagedStorage <double>).Array));
            Console.WriteLine(Environment.NewLine);

            Console.WriteLine("Immediate");
            Timeit(
                () =>
            {
                resultN.Assign(3 * NMath.Log(NMath.Exp(a) * NMath.Exp(b)));
            });
            Console.WriteLine(CheckitString(result, (resultN.Storage as ManagedStorage <double>).Array));
            Console.WriteLine(Environment.NewLine);

            Console.WriteLine("Hit any key");
            Console.ReadKey();
        }