/// <summary> /// Soves a linear system using a direct solver. /// </summary> /// <param name="Matrix">the matrix of the linear problem.</param> /// <param name="X">Output, (hopefully) the solution.</param> /// <param name="B">Input, the right-hand-side.</param> /// <returns>Actual number of iterations.</returns> static public void Solve_Direct <V, W>(this IMutableMatrixEx Matrix, V X, W B) where V : IList <double> where W : IList <double> // { using (var slv = new ilPSP.LinSolvers.PARDISO.PARDISOSolver()) { slv.DefineMatrix(Matrix); var SolRes = slv.Solve(X, B.ToArray()); } }
/// <summary> /// Solution of the system /// <see cref="LaplaceMtx"/>*<see cref="T"/> + <see cref="LaplaceAffine"/> = <see cref="RHS"/> /// using a black-box solver /// </summary> private void ClassicSolve(out double mintime, out double maxtime, out bool Converged, out int NoOfIter) { mintime = double.MaxValue; maxtime = double.MinValue; Converged = false; NoOfIter = int.MaxValue; for (int i = 0; i < base.Control.NoOfSolverRuns; i++) { // create sparse solver // -------------------- ISparseSolver ipSolver; LinearSolverCode solvercodes = LinearSolverCode.classic_pardiso; switch (solvercodes) { case LinearSolverCode.classic_pardiso: ipSolver = new ilPSP.LinSolvers.PARDISO.PARDISOSolver() { CacheFactorization = true, UseDoublePrecision = true }; break; case LinearSolverCode.classic_mumps: ipSolver = new ilPSP.LinSolvers.MUMPS.MUMPSSolver(); break; case LinearSolverCode.classic_cg: ipSolver = new ilPSP.LinSolvers.monkey.CG() { MaxIterations = 1000000, Tolerance = 1.0e-10, DevType = ilPSP.LinSolvers.monkey.DeviceType.Cuda }; break; default: throw new ArgumentException(); } ipSolver.DefineMatrix(LaplaceMtx); // call solver // ----------- T.Clear(); Console.WriteLine("RUN " + i + ": solving system..."); var RHSvec = RHS.CoordinateVector.ToArray(); BLAS.daxpy(RHSvec.Length, -1.0, this.LaplaceAffine, 1, RHSvec, 1); T.Clear(); SolverResult solRes = ipSolver.Solve(T.CoordinateVector, RHSvec); mintime = Math.Min(solRes.RunTime.TotalSeconds, mintime); maxtime = Math.Max(solRes.RunTime.TotalSeconds, maxtime); Converged = solRes.Converged; NoOfIter = solRes.NoOfIterations; Console.WriteLine("Pardiso phase 11: " + ilPSP.LinSolvers.PARDISO.PARDISOSolver.Phase_11.Elapsed.TotalSeconds); Console.WriteLine("Pardiso phase 22: " + ilPSP.LinSolvers.PARDISO.PARDISOSolver.Phase_22.Elapsed.TotalSeconds); Console.WriteLine("Pardiso phase 33: " + ilPSP.LinSolvers.PARDISO.PARDISOSolver.Phase_33.Elapsed.TotalSeconds); ipSolver.Dispose(); } }
private void ClassicSolve(out double mintime, out double maxtime, out bool Converged, out int NoOfIter) { /* * double rnk, condNo, det; * bool isPosDef; * { * MultidimensionalArray output = MultidimensionalArray.Create(1,5); * var bmc = new BatchmodeConnector(_Flav:BatchmodeConnector.Flavor.Octave, MatlabExecuteable: "d:\\cygwin64\\bin\\bash.exe"); * //var bmc = new BatchmodeConnector(_Flav: BatchmodeConnector.Flavor.Matlab); * bmc.PutSparseMatrix(this.LaplaceMtx, "Mtx"); * bmc.Cmd("condNo = condest(Mtx);"); * bmc.Cmd("rnk = rank(Mtx);"); * //bmc.Cmd("rnk = 0;"); * bmc.Cmd("[V,r]=chol(-0.5*(Mtx+Mtx'));"); * //bmc.Cmd("deti = det(Mtx);"); * bmc.Cmd("output = [condNo,rnk,0.0,4.0,r];"); * bmc.GetMatrix(output, "output"); * bmc.Execute(false); * condNo = output[0, 0]; * rnk = output[0, 1]; * det = output[0, 2]; * Debug.Assert(output[0, 3] == 4.0); * isPosDef = output[0, 4] == 0; * } * Console.WriteLine("Matrix:"); * Console.WriteLine(" size: " + this.LaplaceMtx.NoOfRows); * Console.WriteLine(" rank: " + rnk); * Console.WriteLine(" det: " + det); * Console.WriteLine(" cond. No.: {0:0.####E-00}", condNo); * Console.WriteLine(" pos. def.: " + isPosDef); */ // create sparse solver // -------------------- ISparseSolver ipSolver; { ipSolver = new ilPSP.LinSolvers.PARDISO.PARDISOSolver(); //ipSolver = new ilPSP.LinSolvers.monkey.CG(); //((ilPSP.LinSolvers.monkey.CG)ipSolver).MaxIterations = 50000; //((ilPSP.LinSolvers.monkey.CG)ipSolver).Tolerance = 1.0e-10; ipSolver.DefineMatrix(LaplaceMtx); } // call solver // ----------- mintime = double.MaxValue; maxtime = double.MinValue; SolverResult solRes = default(SolverResult); int NoOfSolverRuns = base.Control.NoOfSolverRuns; for (int i = 0; i < NoOfSolverRuns; i++) { T.Clear(); Console.WriteLine("RUN " + i + ": solving system..."); var RHSvec = RHS.CoordinateVector.ToArray(); //RHSvec.SaveToTextFile("DG" + this.T.Basis.Degree + "_RHS.txt"); BLAS.daxpy(RHSvec.Length, -1.0, this.LaplaceAffine, 1, RHSvec, 1); T.Clear(); T.InitRandom(); solRes = ipSolver.Solve(T.CoordinateVector, RHSvec); mintime = Math.Min(solRes.RunTime.TotalSeconds, mintime); maxtime = Math.Max(solRes.RunTime.TotalSeconds, maxtime); System.GC.Collect(); //T.CoordinatesAsVector.SaveToTextFile("DG" + this.T.Basis.Degree + "_SOLUTION.txt"); } Converged = solRes.Converged; NoOfIter = solRes.NoOfIterations; ipSolver.Dispose(); }