public void TransposeCreatesTransposedArrayForTwoDimensionArray() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray <double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { array[i, j] = (long)i * size1 + j; } } var transposed = array.Transpose(); Assert.AreEqual(array.NumElements, transposed.NumElements); Assert.AreEqual(size0, transposed.Size1); Assert.AreEqual(size1, transposed.Size0); for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { Assert.AreEqual(array[i, j], transposed[j, i]); } } } }
public NMFactorization GetRandomFactorization(int featuresCount) { if (featuresCount <= 0) { throw new ArgumentOutOfRangeException("featuresCount", "Maximum features number must be positive integer."); } var rc = sparseMatrixReader.RowsCount; var cc = sparseMatrixReader.ColumnsCount; var w = new MemoryMappedArray <double>(rc, featuresCount); var h = new MemoryMappedArray <double>(featuresCount, cc); try { // Initialize the weight and feature matrices with random values FillRandom(w); FillRandom(h); } catch (Exception) { w.Dispose(); h.Dispose(); throw; } return(new NMFactorization(w, h)); }
public void TwoDimensionIndexedPropertyWorksCorrectly() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray <double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { array[i, j] = (long)i * size1 + j; } } for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { double v = (double)i * size1 + j; Assert.AreEqual(v, array[i, j]); } } } }
public void OneDimensionConstructorDoesNotThrowException() { const int length = 10000; using (var array = new MemoryMappedArray<double>(length)) { } }
public void TransposeCreatesTransposedArrayForOneDimensionArray() { const int length = 10000; using (var array = new MemoryMappedArray <double>(length)) { for (int i = 0; i < length; i++) { array[i] = i; } var transposed = array.Transpose(); Assert.AreEqual(length, transposed.NumElements); Assert.AreEqual(1, transposed.Size1); Assert.AreEqual(length, transposed.Size0); for (int i = 1; i < array.Size0; i++) { for (int j = 1; i < array.Size1; j++) { Assert.AreEqual(array[i, j], transposed[j, i]); } } } }
public void OneDimensionConstructorDoesNotThrowException() { const int length = 10000; using (var array = new MemoryMappedArray <double>(length)) { } }
public void TwoDimensionConstructorDoesNotThrowException() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray<double>(size0, size1)) { } }
public void TwoDimensionConstructorDoesNotThrowException() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray <double>(size0, size1)) { } }
public void OneDimensionArrayReturnsCorrectSizesAndCount() { const int length = 10000; using (var array = new MemoryMappedArray<double>(length)) { Assert.AreEqual(length, array.NumElements); Assert.AreEqual(1, array.Size0); Assert.AreEqual(length, array.Size1); } }
public void OneDimensionArrayReturnsCorrectSizesAndCount() { const int length = 10000; using (var array = new MemoryMappedArray <double>(length)) { Assert.AreEqual(length, array.NumElements); Assert.AreEqual(1, array.Size0); Assert.AreEqual(length, array.Size1); } }
public void TwoDimensionArrayReturnsCorrectSizesAndCount() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray<double>(size0, size1)) { Assert.AreEqual(size0 * size1, array.NumElements); Assert.AreEqual(size0, array.Size0); Assert.AreEqual(size1, array.Size1); } }
public void OneDimensionConstructorCreateArrayInitializedWithDefaultValue() { const int length = 10000; using (var array = new MemoryMappedArray<double>(length)) { for (int i = 0; i < length; i++) { Assert.AreEqual(0, array[i]); } } }
public void OneDimensionConstructorCreateArrayInitializedWithDefaultValue() { const int length = 10000; using (var array = new MemoryMappedArray <double>(length)) { for (int i = 0; i < length; i++) { Assert.AreEqual(0, array[i]); } } }
public void TwoDimensionArrayReturnsCorrectSizesAndCount() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray <double>(size0, size1)) { Assert.AreEqual(size0 * size1, array.NumElements); Assert.AreEqual(size0, array.Size0); Assert.AreEqual(size1, array.Size1); } }
public NMFactorization(MemoryMappedArray<double> w, MemoryMappedArray<double> h) { if (w == null) { throw new ArgumentNullException("w"); } if (h == null) { throw new ArgumentNullException("h"); } this.w = w; this.h = h; }
public NMFactorization(MemoryMappedArray <double> w, MemoryMappedArray <double> h) { if (w == null) { throw new ArgumentNullException("w"); } if (h == null) { throw new ArgumentNullException("h"); } this.w = w; this.h = h; }
/// <summary> /// Instanciate a new memory mapped array for inter-process access. /// </summary> /// <param name="view">The memory mapped file view.</param> /// <param name="length">The count of entries of the array.</param> /// <param name="bytesPerEntry">The (maximal) count of bytes per entry.</param> /// <param name="cooperative"> /// If true, it accepts other length and bytesPerEntry if the array already exists. /// If falls it throws an exception if the array already exists but with other length or bytesPerEntry. /// </param> /// <param name="offset">An optional initial positive offset before the array starts.</param> public MemoryMappedQueue(MemoryMappedFileView view, int length, int bytesPerEntry, bool cooperative, int offset) { this.headerOffset = offset; this.view = view; this.array = new MemoryMappedArray(view,length,bytesPerEntry,cooperative,offset+16); this.length = array.Length; if(StatusOnline != QueueStatus.Initialized) { InOnline = 0; OutOnline = 0; CountOnline = 0; StatusOnline = QueueStatus.Initialized; } }
public void TwoDimensionConstructorCreateArrayInitializedWithDefaultValue() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray <double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { Assert.AreEqual(0, array[i, j]); } } } }
public void OneDimensionIndexedPropertyWorksCorrectly() { const int length = 10000; using (var array = new MemoryMappedArray<double>(length)) { for (int i = 0; i < length; i++) { array[i] = i; } for (int i = 0; i < length; i++) { Assert.AreEqual(i, array[i]); } } }
public void CreateNewCreatesTwoDimensionalMatrixAndOpenCorrectlyReadIt() { const int size0 = 500; const int size1 = 20; var tempFileName = Path.GetTempFileName(); try { File.Delete(tempFileName); long count; using (var array = MemoryMappedArray <double> .CreateNew(tempFileName, size0, size1)) { count = array.NumElements; for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { array[i, j] = (long)i * size1 + j; } } } using (var array = MemoryMappedArray <double> .Open(tempFileName)) { Assert.AreEqual(size0, array.Size0); Assert.AreEqual(size1, array.Size1); Assert.AreEqual(count, array.NumElements); for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { double v = (double)i * size1 + j; Assert.AreEqual(v, array[i, j]); } } } } finally { File.Delete(tempFileName); } }
public void OneDimensionIndexedPropertyWorksCorrectly() { const int length = 10000; using (var array = new MemoryMappedArray <double>(length)) { for (int i = 0; i < length; i++) { array[i] = i; } for (int i = 0; i < length; i++) { Assert.AreEqual(i, array[i]); } } }
public void CreateNewCreatesOneDimensionalMatrixAndOpenCorrectlyReadIt() { const int length = 100; var tempFileName = Path.GetTempFileName(); try { File.Delete(tempFileName); int size0; int size1; long count; using (var array = MemoryMappedArray <double> .CreateNew(tempFileName, length)) { size0 = array.Size0; size1 = array.Size1; count = array.NumElements; for (int i = 0; i < length; i++) { array[i] = i; } } using (var array = MemoryMappedArray <double> .Open(tempFileName)) { Assert.AreEqual(size0, array.Size0); Assert.AreEqual(size1, array.Size1); Assert.AreEqual(count, array.NumElements); for (int i = 0; i < length; i++) { Assert.AreEqual(i, array[i]); } } } finally { File.Delete(tempFileName); } }
public void TwoDimensionIndexedPropertyWorksCorrectly() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray<double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { array[i, j] = (long)i * size1 + j; } } for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { double v = (double)i * size1 + j; Assert.AreEqual(v, array[i, j]); } } } }
public void TransposeCreatesTransposedArrayForTwoDimensionArray() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray<double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { array[i, j] = (long)i * size1 + j; } } var transposed = array.Transpose(); Assert.AreEqual(array.NumElements, transposed.NumElements); Assert.AreEqual(size0, transposed.Size1); Assert.AreEqual(size1, transposed.Size0); for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { Assert.AreEqual(array[i, j], transposed[j, i]); } } } }
public void TransposeCreatesTransposedArrayForOneDimensionArray() { const int length = 10000; using (var array = new MemoryMappedArray<double>(length)) { for (int i = 0; i < length; i++) { array[i] = i; } var transposed = array.Transpose(); Assert.AreEqual(length, transposed.NumElements); Assert.AreEqual(1, transposed.Size1); Assert.AreEqual(length, transposed.Size0); for (int i = 1; i < array.Size0; i++) { for (int j = 1; i < array.Size1; j++) { Assert.AreEqual(array[i,j], transposed[j,i]); } } } }
public void UpdateFactorization(IArray <double> w, IArray <double> h) { var rc = sparseMatrixReader.RowsCount; if (rc != w.Size0) { throw new ArgumentOutOfRangeException("w", "Weight matrix has invalid rows count."); } var cc = sparseMatrixReader.ColumnsCount; if (cc != h.Size1) { throw new ArgumentOutOfRangeException("h", "Feature matrix has invalid columns count."); } var featuresCount = w.Size1; if (featuresCount != h.Size0) { throw new ArgumentException("Weights and features matrix has different number of features."); } // Update feature matrix using (var hn = new MemoryMappedArray <double>(featuresCount, cc)) using (var wTw = new MemoryMappedArray <double>(featuresCount, featuresCount)) using (var hd = new MemoryMappedArray <double>(featuresCount, cc)) { // w.T var wT = w.Transpose(); // hn = w.T * A foreach (var rowIdxSparseVector in GetNumberedRows(sparseMatrixReader.ReadRows())) { var localRowIdxSparseVector = rowIdxSparseVector; // multiply column (aRow) from w.T by row (aRow) from A (sparse vector) Parallel.For(0, featuresCount, i => { var multiplicationFactor = wT[i, localRowIdxSparseVector.Key]; foreach (var pair in localRowIdxSparseVector.Value) { hn[i, pair.Key] += multiplicationFactor * pair.Value; } }); } // wTw = w.T * w - is symmetric array Parallel.For(0, featuresCount, i => { // optimization: compute right upper half array only for (int j = i; j < featuresCount; j++) { // compute wTw[i,j] as dot product of row i in wT by column j in w var v = GetMultiplicationElement(wT, w, i, j); wTw[i, j] = v; // optimization: copy result to wTw[j,i] (left bottom) if (i != j) { wTw[j, i] = v; } } }); // hd = (w.T * w) * h Parallel.For(0, featuresCount, i => { for (int j = 0; j < cc; j++) { hd[i, j] = GetMultiplicationElement(wTw, h, i, j); } }); // update h = h .* hn ./ hd Parallel.For(0, featuresCount, i => { for (int j = 0; j < cc; j++) { var nom = hn[i, j]; var den = hd[i, j]; if (nom != den) { h[i, j] = h[i, j] * nom / den; } } }); } // Update weights matrix using (var wn = new MemoryMappedArray <double>(rc, featuresCount)) using (var hhT = new MemoryMappedArray <double>(featuresCount, featuresCount)) using (var wd = new MemoryMappedArray <double>(rc, featuresCount)) { // h.T var hT = h.Transpose(); // wn = A * h.T foreach (var rowIdxSparseVector in GetNumberedRows(sparseMatrixReader.ReadRows())) { var localRowIdxSparseVector = rowIdxSparseVector; Parallel.For(0, featuresCount, hTColumn => { wn[localRowIdxSparseVector.Key, hTColumn] = localRowIdxSparseVector.Value.Sum(pair => pair.Value * hT[pair.Key, hTColumn]); }); } // hhT = h * h.T - symmetric array Parallel.For(0, featuresCount, i => { // optimization: compute right upper half array only for (int j = i; j < featuresCount; j++) { // compute hhT[i,j] as dot product of row i in h by column j in hT var v = GetMultiplicationElement(h, hT, i, j); hhT[i, j] = v; // optimization: copy result to hhT[j,i] (left bottom) if (i != j) { hhT[j, i] = v; } } }); // wd = w * (h * h.T) Parallel.For(0, rc, i => { for (int j = 0; j < featuresCount; j++) { wd[i, j] = GetMultiplicationElement(w, hhT, i, j); } }); // update w = w .* wn ./ wd Parallel.For(0, rc, i => { for (int j = 0; j < featuresCount; j++) { var nom = wn[i, j]; var den = wd[i, j]; if (nom != den) { w[i, j] = w[i, j] * nom / den; } } }); } }
public void TwoDimensionConstructorCreateArrayInitializedWithDefaultValue() { const int size0 = 500; const int size1 = 20; using (var array = new MemoryMappedArray<double>(size0, size1)) { for (int i = 0; i < size0; i++) { for (int j = 0; j < size1; j++) { Assert.AreEqual(0, array[i, j]); } } } }