/// <summary>Updates the current curve interpolator. /// </summary> /// <param name="gridPointCount">The number of grid points, i.e. the number of relevant elements of <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArguments">The arguments of the grid points, thus labels of the curve in its <see cref="System.Double" /> representation in ascending order.</param> /// <param name="gridPointValues">The values of the grid points corresponding to <paramref name="gridPointArguments" />.</param> /// <param name="state">The state of the grid points, i.e. <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" />, with respect to the previous function call.</param> /// <param name="gridPointArgumentStartIndex">The null-based start index of <paramref name="gridPointArguments" /> to take into account.</param> /// <param name="gridPointValueStartIndex">The null-based start index of <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArgumentIncrement">The increment for <paramref name="gridPointArguments" />.</param> /// <param name="gridPointValueIncrement">The increment for <paramref name="gridPointValues" />.</param> /// <remarks> /// This method should be called if grid points have been changed, added, removed etc. and before evaluating the grid point curve at a specified point. /// <para>If no problem occurred, the flag <see cref="IOperable.IsOperable" /> will be set to <c>true</c>.</para> /// </remarks> public void Update(int gridPointCount, IList <double> gridPointArguments, IList <double> gridPointValues, GridPointCurve.State state, int gridPointArgumentStartIndex = 0, int gridPointValueStartIndex = 0, int gridPointArgumentIncrement = 1, int gridPointValueIncrement = 1) { if (gridPointCount <= 0) { m_GridPointCount = 0; // current instance is not operable } else { m_GridPointCount = gridPointCount; if (state.HasFlag(GridPointCurve.State.GridPointArgumentChanged)) { if (ArrayMemory.Reallocate(ref m_GridPointArguments, gridPointCount, Math.Max(10, gridPointCount / 5)) == true) { m_ReadOnlyGridPointArguments = new ReadOnlyCollection <double>(m_GridPointArguments); } gridPointArguments.CopyTo(m_GridPointArguments, gridPointCount, gridPointArgumentStartIndex, sourceIncrement: gridPointArgumentIncrement); } if (state.HasFlag(GridPointCurve.State.GridPointValueChanged)) { if (ArrayMemory.Reallocate(ref m_GridPointValues, gridPointCount, Math.Max(10, gridPointCount / 5)) == true) { m_ReadOnlyGridPointValues = new ReadOnlyCollection <double>(m_GridPointValues); } gridPointValues.CopyTo(m_GridPointValues, gridPointCount, gridPointValueStartIndex, sourceIncrement: gridPointValueIncrement); } } m_IntegralCache.EarmarkUpdateRequest(); }
public baseobj_stub(string path, ClientContext c) : base(path, c) { rr_cb2 = new CallbackClient <Func <double, double, CancellationToken, Task> >("cb2"); rr_p1 = new PipeClient <double[]>("p1", this); rr_w1 = new WireClient <double[]>("w1", this); rr_m1 = new ArrayMemoryClient <double>("m1", this, MemberDefinition_Direction.both); }
public void SliceDisposeBenchmark() { // 6.3 MOPS var count = 1_000_000; var rounds = 10; var arrSize = 1000; var arr = Enumerable.Range(0, arrSize).ToArray(); var mem = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true); var vs = VectorStorage.Create(mem, 0, mem.Length); Assert.AreEqual(arr.Length, vs.Length); for (int r = 0; r < rounds; r++) { using (Benchmark.Run("Slice/Dispose", count)) { for (int i = 0; i < count; i++) { var vs1 = vs.Slice(0, vs.Vec.Length, 1, externallyOwned: true); vs1.Dispose(); } } } Benchmark.Dump(); vs.Dispose(); Assert.IsTrue(vs.IsDisposed); }
/// <summary>Updates the current instance, i.e. stores the grid points and returns references to the coefficients of the splines. /// </summary> /// <param name="gridPointCount">The number of grid points, i.e. the number of relevant elements of <paramref name="gridPointArguments"/> and <paramref name="gridPointValues"/> to take into account.</param> /// <param name="gridPointArguments">The arguments of the grid points, thus labels of the curve in its <see cref="System.Double"/> representation.</param> /// <param name="gridPointValues">The values of the grid points corresponding to <paramref name="gridPointArguments"/>.</param> /// <param name="state">The state of the grid points, i.e. <paramref name="gridPointArguments"/> and <paramref name="gridPointValues"/>, with respect to the previous function call.</param> /// <param name="coefficientsB">A reference to the coefficients 'b' with respect to f(t) = a[j] + b[j]*(t[j] - t) + c[j] * (t[j] - t)^2 + d[j] * (t[j] - t)^3. The caller of this method has to set valid coefficients (output).</param> /// <param name="coefficientsC">A reference to the coefficients 'c' with respect to f(t) = a[j] + b[j]*(t[j] - t) + c[j] * (t[j] - t)^2 + d[j] * (t[j] - t)^3. The caller of this method has to set valid coefficients (output).</param> /// <param name="coefficientsD">A reference to the coefficients 'd' with respect to f(t) = a[j] + b[j]*(t[j] - t) + c[j] * (t[j] - t)^2 + d[j] * (t[j] - t)^3. The caller of this method has to set valid coefficients (output).</param> /// <param name="gridPointArgumentStartIndex">The null-based start index of <paramref name="gridPointArguments" /> to take into account.</param> /// <param name="gridPointValueStartIndex">The null-based start index of <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArgumentIncrement">The increment for <paramref name="gridPointArguments" />.</param> /// <param name="gridPointValueIncrement">The increment for <paramref name="gridPointValues" />.</param> public void Update(int gridPointCount, IList <double> gridPointArguments, IList <double> gridPointValues, GridPointCurve.State state, out double[] coefficientsB, out double[] coefficientsC, out double[] coefficientsD, int gridPointArgumentStartIndex = 0, int gridPointValueStartIndex = 0, int gridPointArgumentIncrement = 1, int gridPointValueIncrement = 1) { m_GridPointCount = gridPointCount; if (state.HasFlag(GridPointCurve.State.GridPointArgumentChanged)) { if (ArrayMemory.Reallocate(ref m_GridPointArguments, gridPointCount, Math.Max(10, gridPointCount / 5)) == true) { m_ReadOnlyGridPointArguments = new ReadOnlyCollection <double>(m_GridPointArguments); } gridPointArguments.CopyTo(m_GridPointArguments, gridPointCount, gridPointArgumentStartIndex, sourceIncrement: gridPointArgumentIncrement); } if (state.HasFlag(GridPointCurve.State.GridPointValueChanged)) { if (ArrayMemory.Reallocate(ref m_GridPointValues, gridPointCount, Math.Max(10, gridPointCount / 5)) == true) { m_ReadOnlyGridPointValues = new ReadOnlyCollection <double>(m_GridPointValues); } gridPointValues.CopyTo(m_GridPointValues, gridPointCount, gridPointValueStartIndex, sourceIncrement: gridPointValueIncrement); } /* allocate memory for spline coefficients if necessary and add a small buffer to avoid reallocation of memory: */ ArrayMemory.Reallocate(ref m_CoefficientsB, gridPointCount - 1, Math.Max(10, gridPointCount / 5)); coefficientsB = m_CoefficientsB; ArrayMemory.Reallocate(ref m_CoefficientsC, gridPointCount - 1, Math.Max(10, gridPointCount / 5)); coefficientsC = m_CoefficientsC; ArrayMemory.Reallocate(ref m_CoefficientsD, gridPointCount - 1, Math.Max(10, gridPointCount / 5)); coefficientsD = m_CoefficientsD; IntegralCacheUpdateRequested = true; }
public void CouldCreateVsAndReadElements() { var count = 1000; var arr = Enumerable.Range(0, count).ToArray(); var r = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true); var vs = VectorStorage.Create(r, 0, r.Length); Assert.AreEqual(arr.Length, vs.Length); long sum = 0L; for (int i = 0; i < arr.Length; i++) { var vi = vs.DangerousGet <int>(i); if (vi != i) { Assert.Fail("vi != i"); } sum += vs.DangerousGet <int>(i); } Console.WriteLine(sum); vs.Dispose(); Assert.IsTrue(vs.IsDisposed); }
public void RentReturnPinUnpinBenchmark() { #if !DEBUG var count = 10_000_000; #else var count = 1_000; #endif using (Benchmark.Run("FullCyclePinUnpin", count)) { for (int i = 0; i < count; i++) { var memory = ArrayMemory <byte> .Create(32 * 1024); if (memory.Array.Length != 32 * 1024) { Assert.Fail("Length"); } (memory.Pin(0)).Dispose(); if (memory.IsRetained) { Assert.Fail(); } } } }
public void WorksWithNonBlittables() { var arr = new string[] { "a" }; var r = ArrayMemory <string> .Create(arr, 0, arr.Length, externallyOwned : true, pin : false); var h0 = r.Pin(); Assert.IsFalse(r.IsPinned); var h1 = r.Pin(); h1.Dispose(); Assert.AreEqual(1, r.ReferenceCount); var vec = r.Vec; Assert.AreEqual(1, vec.Length); Assert.AreEqual("a", vec[0]); var rm = r.Retain(); Assert.AreEqual(2, r.ReferenceCount); Assert.AreEqual("a", rm.Span[0]); rm.Dispose(); Assert.AreEqual(1, r.ReferenceCount); h0.Dispose(); }
public void CannotDoubleDispose() { var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true); ((IDisposable)memory).Dispose(); Assert.Throws <ObjectDisposedException>(() => { ((IDisposable)memory).Dispose(); }); }
public void TestReadWriteOutOfBounds() { var memory = new ArrayMemory(5); Assert.Throws <IndexOutOfRangeException>(() => memory.write(6, true)); Assert.Throws <IndexOutOfRangeException>(() => memory.read(6)); }
public baseobj_stub(WrappedServiceStub innerstub) : base(innerstub) { rr_cb2 = new CallbackClient <Action <double, double> >("cb2"); rr_p1 = new Pipe <double[]>(innerstub.GetPipe("p1")); rr_w1 = new Wire <double[]>(innerstub.GetWire("w1")); rr_m1 = new ArrayMemoryClient <double>(innerstub.GetArrayMemory("m1")); }
public void RentReturnBenchmarkRetainablePoolOverCapacity() { var count = 1_000_000; var capacity = 25; var batch = capacity * 2; var pool = new RetainableMemoryPool <byte>((p, l) => { var am = ArrayMemory <byte> .Create(l, pin: true); // Attach pool am._pool = p; return(am); }, 16, 1024 * 1024, capacity, 0); var list = new List <RetainableMemory <byte> >(batch); using (Benchmark.Run("FullCycle", count * batch)) { for (int i = 0; i < count; i++) { for (int j = 0; j < batch; j++) { list.Add(pool.RentMemory(32 * 1024)); } foreach (var arrayMemory in list) { pool.Return(arrayMemory); } list.Clear(); } } pool.Dispose(); }
public void CouldCreateVsAndReadElements() { var count = 1000; var arr = Enumerable.Range(0, count).ToArray(); var r = ArrayMemory <int> .Create(arr); var vs = VecStorage.Create(r, 0, r.Length); Assert.AreEqual(arr.Length, vs.Vec.Length); long sum = 0L; for (int i = 0; i < arr.Length; i++) { var vi = vs.Vec.DangerousGetUnaligned <int>(i); if (vi != i) { Assert.Fail("vi != i"); } sum += vs.Vec.DangerousGetUnaligned <int>(i); } Console.WriteLine(sum); vs.Dispose(); }
public void TestReadWrite() { var memory = new ArrayMemory(1); memory.write(0, true); Assert.True(memory.read(0)); }
/// <summary>Updates the current curve interpolator. /// </summary> /// <param name="gridPointCount">The number of grid points, i.e. the number of relevant elements of <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArguments">The arguments of the grid points, thus labels of the curve in its <see cref="System.Double" /> representation in ascending order.</param> /// <param name="gridPointValues">The values of the grid points corresponding to <paramref name="gridPointArguments" />.</param> /// <param name="state">The state of the grid points, i.e. <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" />, with respect to the previous function call.</param> /// <param name="gridPointArgumentStartIndex">The null-based start index of <paramref name="gridPointArguments" /> to take into account.</param> /// <param name="gridPointValueStartIndex">The null-based start index of <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArgumentIncrement">The increment for <paramref name="gridPointArguments" />.</param> /// <param name="gridPointValueIncrement">The increment for <paramref name="gridPointValues" />.</param> /// <remarks> /// This method should be called if grid points have been changed, added, removed etc. and before evaluating the grid point curve at a specified point. /// <para>If no problem occurred, the flag <see cref="IOperable.IsOperable" /> will be set to <c>true</c>.</para> /// </remarks> public void Update(int gridPointCount, IList <double> gridPointArguments, IList <double> gridPointValues, GridPointCurve.State state, int gridPointArgumentStartIndex = 0, int gridPointValueStartIndex = 0, int gridPointArgumentIncrement = 1, int gridPointValueIncrement = 1) { m_BoundaryCondition.Update(gridPointCount, gridPointArguments, gridPointValues, state, gridPointArgumentStartIndex, gridPointValueStartIndex, gridPointArgumentIncrement, gridPointValueIncrement); double[] coefficientsB, coefficientsC, coefficientsD; m_SplineEvaluator.Update(gridPointCount, gridPointArguments, gridPointValues, state, out coefficientsB, out coefficientsC, out coefficientsD, gridPointArgumentStartIndex, gridPointValueStartIndex, gridPointArgumentIncrement, gridPointValueIncrement); int n = gridPointCount; int nMinusOne = n - 1; double deltaT; // LU decomposition is necessary if and only if the labels have been changed, too: if (state.HasFlag(GridPointCurve.State.GridPointArgumentChanged) == true) { ArrayMemory.Reallocate(ref m_DiagonalElements, n, Math.Max(10, n / 5)); ArrayMemory.Reallocate(ref m_SubDiagonalElements, nMinusOne, Math.Max(10, n / 5)); ArrayMemory.Reallocate(ref m_SuperDiagonalElements, nMinusOne, Math.Max(10, n / 5)); ArrayMemory.Reallocate(ref m_SecondSuperDiagonalElements, nMinusOne - 1, Math.Max(10, n / 5)); ArrayMemory.Reallocate(ref m_PivotIndices, n, Math.Max(10, n / 5)); deltaT = m_SplineEvaluator.GridPointArguments[1] - m_SplineEvaluator.GridPointArguments[0]; for (int j = 1; j < nMinusOne; j++) { double nextDeltaT = m_SplineEvaluator.GridPointArguments[j + 1] - m_SplineEvaluator.GridPointArguments[j]; m_SubDiagonalElements[j - 1] = deltaT; m_DiagonalElements[j] = 2.0 * (deltaT + nextDeltaT); m_SuperDiagonalElements[j] = nextDeltaT; deltaT = nextDeltaT; } m_BoundaryCondition.GetRemainingMatrixElements(out m_DiagonalElements[0], out m_SuperDiagonalElements[0], out m_SubDiagonalElements[n - 2], out m_DiagonalElements[nMinusOne]); LAPACK.LinearEquations.MatrixFactorization.dgttrf(n, m_SubDiagonalElements, m_DiagonalElements, m_SuperDiagonalElements, m_SecondSuperDiagonalElements, m_PivotIndices); } /* Compute the right hand side of Ax=b, where A is the tri-diagonal matrix already calculated and * b depends on the second derivatives and the boundary condition only. We store the value of b in *coefficientsC*: */ deltaT = m_SplineEvaluator.GridPointArguments[1] - m_SplineEvaluator.GridPointArguments[0]; for (int j = 1; j < nMinusOne; j++) { double nextDeltaT = m_SplineEvaluator.GridPointArguments[j + 1] - m_SplineEvaluator.GridPointArguments[j]; coefficientsC[j] = 3.0 * ((m_SplineEvaluator.GridPointValues[j + 1] - m_SplineEvaluator.GridPointValues[j]) / nextDeltaT - (m_SplineEvaluator.GridPointValues[j] - m_SplineEvaluator.GridPointValues[j - 1]) / deltaT); deltaT = nextDeltaT; } m_BoundaryCondition.GetRemainingRightHandSideElements(out coefficientsC[0], out coefficientsC[nMinusOne]); /* Calculate the solution x of Ax=b. The solution is the coefficient 'c' and 'dttrs' stores the result in 'coefficientsC': */ LAPACK.LinearEquations.Solver.dgttrs(n, m_SubDiagonalElements, m_DiagonalElements, m_SuperDiagonalElements, m_SecondSuperDiagonalElements, m_PivotIndices, coefficientsC, 1); /* Calculate the coefficients b_j and d_j: */ for (int k = 0; k < nMinusOne; k++) { deltaT = m_SplineEvaluator.GridPointArguments[k + 1] - m_SplineEvaluator.GridPointArguments[k]; coefficientsD[k] = (coefficientsC[k + 1] - coefficientsC[k]) / (3.0 * deltaT); coefficientsB[k] = (m_SplineEvaluator.GridPointValues[k + 1] - m_SplineEvaluator.GridPointValues[k]) / deltaT - deltaT * (2 * coefficientsC[k] + coefficientsC[k + 1]) / 3.0; } }
public void CannotDisposeRetained() { var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true); var rm = memory.Retain(); Assert.Throws <InvalidOperationException>(() => { ((IDisposable)memory).Dispose(); }); rm.Dispose(); }
public void CannotDoubleDispose() { var memory = ArrayMemory <byte> .Create(32 * 1024); Assert.IsFalse(memory.IsPoolable); Assert.IsFalse(memory.IsPooled); Assert.IsFalse(memory.IsDisposed); memory.Dispose(); Assert.IsTrue(memory.IsDisposed); Assert.Throws <ObjectDisposedException>(() => { memory.Dispose(); }); }
internal Series(TKey[] keys, TValue[] values) { if (keys == null) { throw new ArgumentNullException(nameof(keys)); } if (values == null) { throw new ArgumentNullException(nameof(values)); } if (keys.Length != values.Length) { throw new ArgumentException("Different keys and values length"); } var ks = KeySorting.Strong; if (keys.Length > 1) { var cmp = KeyComparer <TKey> .Default; for (int i = 1; i < keys.Length; i++) { var c = cmp.Compare(keys[i], keys[i - 1]); if (c == 0) { ks = KeySorting.Weak; } else if (c < 0) { ks = KeySorting.NotSorted; break; } } } Flags = new Flags((byte)((byte)Mutability.ReadOnly | (byte)ks)); if (keys.Length == 0 && values.Length == 0) { Debug.Assert(Data == DataBlock.Empty); return; } var keyMemory = ArrayMemory <TKey> .Create(keys); var keyVs = VecStorage.Create(keyMemory, 0, keyMemory.Length); var valMemory = ArrayMemory <TValue> .Create(values); var valVs = VecStorage.Create(valMemory, 0, valMemory.Length); var block = DataBlock.SeriesCreate(rowIndex: keyVs, values: valVs, rowLength: keys.Length); Data = block; }
public void CannotDisposeRetained() { var memory = ArrayMemory <byte> .Create(32 * 1024); Assert.IsFalse(memory.IsPoolable); Assert.IsFalse(memory.IsPooled); Assert.IsFalse(memory.IsDisposed); var rm = memory.Retain(); Assert.Throws <InvalidOperationException>(() => { memory.Dispose(); }); rm.Dispose(); Assert.IsTrue(memory.IsDisposed); }
private static BaseContainer <int> CreateIntBaseContainer(int capacity, int length) { var bc = new BaseContainer <int>(); var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray()); var vs = RetainedVec.Create(rm, 0, rm.Length); var block = DataBlock.Create(rowIndex: vs, rowLength: length); bc.Data = block; return(bc); }
private static BaseContainer <int> CreateIntBaseContainer(int capacity, int length) { var bc = new BaseContainer <int>(); var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray(), externallyOwned : true); var vs = VectorStorage.Create(rm, 0, rm.Length, 1); var block = DataBlock.Create(rowIndex: vs, rowLength: length); bc.DataBlock = block; return(bc); }
/// <summary>Updates the current curve fitting object. /// </summary> /// <param name="gridPointCount">The number of grid points, i.e. the number of relevant elements of <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArguments">The arguments of the grid points, thus labels of the curve in its <see cref="System.Double" /> representation in ascending order.</param> /// <param name="gridPointValues">The values of the grid points corresponding to <paramref name="gridPointArguments" />.</param> /// <param name="gridPointArgumentHint">Describes the structure of the grid point arguments.</param> /// <param name="gridPointValueHint">Describes the structure of the grid point values.</param> /// <param name="state">The state of the grid points, i.e. <paramref name="gridPointArguments" /> and <paramref name="gridPointValues" />, with respect to the previous function call.</param> /// <param name="gridPointArgumentStartIndex">The null-based start index of <paramref name="gridPointArguments" /> to take into account.</param> /// <param name="gridPointValueStartIndex">The null-based start index of <paramref name="gridPointValues" /> to take into account.</param> /// <param name="gridPointArgumentIncrement">The increment for <paramref name="gridPointArguments" />.</param> /// <param name="gridPointValueIncrement">The increment for <paramref name="gridPointValues" />.</param> /// <remarks> /// This method should be called if grid points have been changed, added, removed etc. and before evaluating the grid point curve at a specified point. /// <para>If no problem occurred, the flag <see cref="IOperable.IsOperable" /> will be set to <c>true</c>.</para> /// <para>This method should always store all required data for later use, i.e. creates deep copies of the arguments.</para> /// </remarks> public void Update(int gridPointCount, IList <double> gridPointArguments, IList <double> gridPointValues, MklGridPointCurve.xHintValue gridPointArgumentHint, MklGridPointCurve.yHintValue gridPointValueHint, GridPointCurve.State state = GridPointCurve.State.GridPointChanged, int gridPointArgumentStartIndex = 0, int gridPointValueStartIndex = 0, int gridPointArgumentIncrement = 1, int gridPointValueIncrement = 1) { /* lets store a copy of the original grid point values: */ if (state.HasFlag(GridPointCurve.State.GridPointValueChanged)) { if (ArrayMemory.Reallocate(ref m_OriginalGridPointValues, gridPointCount, Math.Max(10, gridPointCount / 5)) == true) { m_OriginalGridPointValuesReadOnlyCollection = new ReadOnlyCollection <double>(m_OriginalGridPointValues); } gridPointValues.CopyTo(m_OriginalGridPointValues, gridPointCount, gridPointValueStartIndex, sourceIncrement: gridPointValueIncrement); } m_DataFitting.Update(gridPointCount, gridPointArguments, gridPointValues, gridPointArgumentHint, gridPointValueHint, (n, x) => { VectorUnit.Basics.Log(n, x); }, state, gridPointArgumentStartIndex, gridPointValueStartIndex, gridPointArgumentIncrement, gridPointValueIncrement); }
public void RentReturnBenchmark() { var count = 100_000_000; using (Benchmark.Run("FullCycle", count)) { for (int i = 0; i < count; i++) { var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true); ((IDisposable)memory).Dispose(); } } }
public OpenTitan_OneTimeProgrammableMemoryController(Machine machine) : base(machine) { memoryLock = new Object(); transitionCountLock = new Object(); DefineRegisters(); Reset(); aValues = new ushort[ABValuesWordsCount]; bValues = new ushort[ABValuesWordsCount]; cValues = new ushort[CDValuesWordsCount]; dValues = new ushort[CDValuesWordsCount]; InitPositionConsumedToLifeCycleMapping(); underlyingMemory = new ArrayMemory(0x1000); }
public void RentReturnBenchmark() { #if !DEBUG var count = 100_000_000; #else var count = 1_000; #endif using (Benchmark.Run("FullCycle", count)) { for (int i = 0; i < count; i++) { var memory = ArrayMemory <byte> .Create(32, pin : false); ((IDisposable)memory).Dispose(); } } }
public void CreateDisposeBenchmark() { #if !DEBUG var count = 100_000_000; #else var count = 1_000; #endif using (Benchmark.Run("FullCycle", count)) { for (int i = 0; i < count; i++) { var memory = ArrayMemory <byte> .Create(32); (memory).Dispose(); } } }
public void OffsetsAreOk() { var array = new byte[100]; using var rm = new RetainedMemory <byte>(ArrayMemory <byte> .Create(array, 50, 50, true), 25, 25, true); // 85 - 95 using var rmc = rm.Clone(10, 10); GC.Collect(2, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); GC.Collect(2, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); rmc.GetSpan()[5] = 1; Assert.AreEqual(1, array[90]); }
public void VectorStorageReadBench() { var count = 1_000_000; var rounds = 10; var mult = 500; var arr = Enumerable.Range(0, count).ToArray(); var mem = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true); var stride = 2; var vs = VectorStorage.Create(mem, (stride - 1), mem.Length - (stride - 1), stride); Assert.AreEqual(arr.Length / stride, vs.Length); int sum = 0; for (int r = 0; r < rounds; r++) { using (Benchmark.Run("VS Read", vs.Length * mult)) { var vector = vs.GetVector <int>(); for (int _ = 0; _ < mult; _++) { for (int i = 0; i < vs.Length; i++) { var vi = vector[i]; if (vi != i * stride + (stride - 1)) { Assert.Fail("vi != i * 2"); } unchecked { sum += vi; } } } } } Benchmark.Dump(); Console.WriteLine(sum); }
public void CouldTryFindBlockAtSingleChunkBench() { var count = 50_000_000; var rounds = 20; // for this test capacity is irrelevant - interpolation search hits exact position on first try var capacity = count / 100; var bc = new BaseContainer <int>(); var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray()); var vs = VecStorage.Create(rm, 0, rm.Length); var block = DataBlock.Create(rowIndex: vs, rowLength: vs.Vec.Length); bc.Data = block; for (int r = 0; r < rounds; r++) { using (Benchmark.Run("TryFindChunkAt", count)) { var m = count / capacity; for (int _ = 0; _ < m; _++) { for (int i = 1; i < capacity; i++) { var searchIndexRef = i; var found = bc.TryFindBlockAt(ref searchIndexRef, Lookup.LE, out var c, out var ci); if (!found || !ReferenceEquals(block, c) || i != ci || i != searchIndexRef ) { Assert.Fail(); } } } } } Benchmark.Dump(); bc.Dispose(); }
/// <summary>Updates the internal cache. /// </summary> private void Update() { ArrayMemory.Reallocate(ref m_Cache, GridPointCount, pufferSize: 5); var cumIntegralValue = m_Cache[0] = 0.0; var lowerBound = GridPointArguments[0]; for (int k = 1; k < GridPointCount; k++) { var upperBound = GridPointArguments[k]; var upperGridPointValue = GridPointValues[k]; cumIntegralValue += CurveDataFitting.GetIntegral(lowerBound, upperBound, k - 1); m_Cache[k] = cumIntegralValue; /* prepare for next loop: */ lowerBound = upperBound; } UpdateRequested = false; }
public void VectorStorageReadBench() { var count = 1_000_000; var rounds = 10; var mult = 500; var arr = Enumerable.Range(0, count).ToArray(); var mem = ArrayMemory <int> .Create(arr); var vs = VecStorage.Create(mem, 0, mem.Length); Assert.AreEqual(arr.Length, vs.Vec.Length); int sum = 0; for (int r = 0; r < rounds; r++) { using (Benchmark.Run("VS Read", vs.Vec.Length * mult)) { for (int _ = 0; _ < mult; _++) { for (int i = 0; i < vs.Vec.Length; i++) { var vi = vs.Vec.DangerousGetUnaligned <int>(i); //if (vi != i) //{ // Assert.Fail("vi != i"); //} unchecked { sum += vi; } } } } } Benchmark.Dump(); Console.WriteLine(sum); }