/// <summary> /// Добавление последовательности /// </summary> /// <param name="sequenceData"></param> /// <returns>Индекс добавленной последовательности</returns> public async Task <int> AddSequence(double[] sequenceData) { // Проверка наличия последовательности // (для исключения дублирования) // --- добавить --- int numSequences = GetNumSequences(); if (numSequences == 0) { await _fileBinarySeqIndex.WriteDataAsync(0); await _fileBinarySeqIndex.WriteDataAsync(sequenceData.Length); } else { int numElementsInDataSeqFile = (int)_fileBinarySeqData.GetNumElementsInFile; await _fileBinarySeqIndex.WriteDataAsync(numElementsInDataSeqFile + sequenceData.Length); } int addedSeqIndex = numSequences; // Записываем данные последовательности await _fileBinarySeqData.WriteDataAsync(sequenceData); return(addedSeqIndex); }
/// <summary> /// Устанавливает значения правой стороны матрицы /// </summary> /// <param name="rightSideData"></param> /// <returns></returns> public async Task SetRightSideAsync(double[] rightSideData) { int numRows = await _sparseMatrixRepSeq.GetNumRowsAsync(); if (rightSideData.Length != numRows) { throw new Exception($"Ошибка! Несоответствие числа строк матрицы ({numRows}) и правых значений СЛАУ ({rightSideData.Length})"); } await _fileBinaryRightSide.WriteDataAsync(rightSideData); }
/// <summary> /// Решение СЛАУ с помощью CUDA /// </summary> /// <returns>Время решения СЛАУ, мс</returns> public async Task <long> SolveCuda() { CudaSolveSparse sp = new CudaSolveSparse(); //Создаем решатель из библиотеки ManagedCuda CudaSparseMatrixDescriptor matrixDescriptor = new CudaSparseMatrixDescriptor(); // Создается дескриптор матрицы double tolerance = 0.0001; //Точность расчета //// Информация о разреженной матрице в CSR формате var matrixCsr = await SparseMatrixRepSeq.ConvertMatrixToCsrFormat(); if (matrixCsr.NumCols != matrixCsr.NumRows) { throw new Exception($"Ошибка! Число строк ({matrixCsr.NumRows}) не соответствует числу столбцов ({matrixCsr.NumCols})."); } //int numRows = 3; //var nonZeroValues = new double[] { 1.2, 3.3, 4, 5, 6, 7 }; //var rowPointers = new int[] { 0, 2, 4, 6 }; //var columnIndices = new int[] { 0, 1, 1, 2, 2, 3 }; //var rightSide = new double[] { 1, 2, 3 }; //var x = new double[rowPointers.Length-1]; //sp.CsrlsvluHost(numRows, nonZeroValues.Length, matrixDescriptor, nonZeroValues, // rowPointers, columnIndices, rightSide, // tolerance, 0, x); //Решение СЛАУ методом LU факторизации double[] rightSide = await GetRightSideAsync(); double[] solutionVector = new double[matrixCsr.NumRows]; Console.Write($"{DateTime.Now.ToShortTimeString()}: Решение СЛАУ..."); stopWatch.Restart(); sp.CsrlsvluHost(matrixCsr.NumRows, matrixCsr.CsrValA.Length, matrixDescriptor, matrixCsr.CsrValA, matrixCsr.CsrRowPtrA, matrixCsr.CsrColIndA, rightSide, tolerance, 0, solutionVector); //Решение СЛАУ методом LU факторизации stopWatch.Stop(); Console.WriteLine("OK"); Console.Write($"{DateTime.Now.ToShortTimeString()}: Запись решения СЛАУ в файл..."); await _fileBinaryResult.WriteDataAsync(solutionVector); Console.WriteLine("OK"); return(stopWatch.ElapsedMilliseconds); }
/// <summary> /// Добавление строки в конец матрицы с добавлением данных начиная с указанного индекса столбца /// </summary> /// <param name="rowData">Массив данных</param> /// /// <param name="colIndexStart">Индекс столбца начала вставки</param> public async Task AddRowAsync(double[] rowData, int colIndexStart) { await IncreaseNumRowsAsync(1); int numCols = await GetNumColsAsync(); if (numCols < colIndexStart + rowData.Length) { await SetNumColsAsync(colIndexStart + rowData.Length); } long numElementsInRowIndexFile = _fileBinaryRowIndex.GetNumElementsInFile; int numAddedElements = 0; for (int i = colIndexStart; i < colIndexStart + rowData.Length; i++) { if (Math.Abs(rowData[i - colIndexStart]) > double.Epsilon) { await _fileBinaryData.WriteDataAsync(rowData[i - colIndexStart]); await _fileBinaryColIndex.WriteDataAsync(i); numAddedElements++; } } if (numElementsInRowIndexFile == 0) { await _fileBinaryRowIndex.WriteDataAsync(0); await _fileBinaryRowIndex.WriteDataAsync(numAddedElements); } else { int lastElement = await _fileBinaryRowIndex.ReadAsync((int)numElementsInRowIndexFile - 1); await _fileBinaryRowIndex.WriteDataAsync(lastElement + numAddedElements); } }