public void CheckExponentialPerformance() { IntelMathKernelLibrary.SetSequential(); var location = StorageLocation.Host; using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111)) { var normalDistribution = new Normal(randomStream, 0, 1); var a = new NArray(location, 5000); var b = new NArray(location, 5000); a.FillRandom(normalDistribution); var aArray = GetArray(a); var bArray = GetArray(b); var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000; ++i) { IntelMathKernelLibrary.Exp(aArray, 0, bArray, 0, 5000); } var baseline = watch.ElapsedMilliseconds; Console.WriteLine("5 millions in place Exps"); Console.WriteLine(baseline); } }
public void TestRandomNumberGeneration() { var location = StorageLocation.Host; // We create the stream. // This identifies the stream and stores the state or pointer to the state (in the case // of IntelMKL, CUDA, etc). using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111)) { // We then create a distribution based on the stream. // This stores only specific parameters of the distribution (i.e. mean and standard deviation). // The purpose of the design is to push scaling and offsetting of random numbers to the Provider, // which can do this efficiently whilst generating. var normal = new Normal(randomStream, 0, 1); var a = new NArray(location, 1000); var b = new NArray(location, 1000); // When we call FillRandom, we need to use a Provider that is appropriate to the stream. a.FillRandom(normal); b.FillRandom(normal); var v = a * b; } }
private NArray CalculateSyntheticReturns(NArray correlationMatrix, int returnsCount) { var location = StorageLocation.Host; var cholesky = NMath.CholeskyDecomposition(correlationMatrix); var variates = new NArray(location, returnsCount, correlationMatrix.RowCount); using (var stream = new RandomNumberStream(location)) { var normal = new Normal(stream, 0, 1); variates.FillRandom(normal); } return(variates * cholesky.Transpose()); }
public void CreateInputs() { var location = StorageLocation.Host; using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111)) { var normalDistribution = new Normal(randomStream, 0, 1); _a = new NArray(location, 5000); _a.FillRandom(normalDistribution); _a_array = GetArray(_a); _b = new NArray(location, 5000); _b.FillRandom(normalDistribution); _b_array = GetArray(_b); _c = new NArray(location, 5000); _c.FillRandom(normalDistribution); _c_array = GetArray(_c); } }
private NArray CalculateSyntheticReturns(NArray correlationMatrix, int returnsCount) { var location = StorageLocation.Host; var cholesky = NMath.CholeskyDecomposition(correlationMatrix); var variates = new NArray(location, returnsCount, correlationMatrix.RowCount); using (var stream = new RandomNumberStream(location)) { var normal = new Normal(stream, 0, 1); variates.FillRandom(normal); } return variates * cholesky.Transpose(); }
public void TranscendentalFunctionTest() { IntelMathKernelLibrary.SetSequential(); var location = StorageLocation.Host; using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111)) { var normalDistribution = new Normal(randomStream, 0, 1); var a = new NArray(location, 5000); a.FillRandom(normalDistribution); var watch = new Stopwatch(); watch.Start(); var options = new ParallelOptions() { MaxDegreeOfParallelism = 1 }; Parallel.For(0, 100, options, (i) => { DoWork(a, normalDistribution); }); var baseline = watch.ElapsedMilliseconds; Console.WriteLine("Base-line"); Console.WriteLine(baseline); watch.Restart(); Parallel.For(0, 100, (i) => { DoWork(a, normalDistribution); }); var baselineThreaded = watch.ElapsedMilliseconds; Console.WriteLine("Base-line threaded"); Console.WriteLine(baselineThreaded); watch.Restart(); Parallel.For(0, 100, options, (i) => { DoWorkInPlace(a, normalDistribution); }); var oneThread = watch.ElapsedMilliseconds; Console.WriteLine("In place"); Console.WriteLine(oneThread); watch.Restart(); Parallel.For(0, 100, (i) => { DoWorkInPlace(a, normalDistribution); }); var multipleThreads = watch.ElapsedMilliseconds; Console.WriteLine("In place threaded"); Console.WriteLine(multipleThreads); Console.WriteLine(oneThread / (double)multipleThreads); watch.Restart(); Parallel.For(0, 100, options, (i) => { DoWorkDeferred(a, normalDistribution); }); var deferred = watch.ElapsedMilliseconds; Console.WriteLine("Deferred"); Console.WriteLine(deferred); watch.Restart(); Parallel.For(0, 100, (i) => { DoWorkDeferred(a, normalDistribution); }); var deferredMultipleThreads = watch.ElapsedMilliseconds; Console.WriteLine("Deferred threaded"); Console.WriteLine(deferredMultipleThreads); watch.Restart(); } }
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); } }