/// <summary> /// defines the problem matrix /// </summary> public void Init(MultigridOperator op) { // init & check // ============ this.m_MgOperator = op; //OpMatrix = op.OperatorMatrix; OpMatrix = new ilPSP.LinSolvers.monkey.CPU.RefMatrix(op.OperatorMatrix.ToMsrMatrix()); var MgMap = op.Mapping; if (!OpMatrix.RowPartitioning.EqualsPartition(MgMap.Partitioning)) { throw new ArgumentException("Row partitioning mismatch."); } if (!OpMatrix.ColPartition.EqualsPartition(MgMap.Partitioning)) { throw new ArgumentException("Column partitioning mismatch."); } // init solver chain // ================= if (SolverChain == null || SolverChain.Length <= 0) { throw new NotSupportedException("Illegal configuration."); } for (int i = 0; i < SolverChain.Length; i++) { SolverChain[i].Init(op); } }
ilPSP.LinSolvers.monkey.CPU.RefMatrix m_Matrix; // im Moment schneller, ca 5X //BlockMsrMatrix m_Matrix; public void Init(MultigridOperator op) { using (new FuncTrace()) { var M = op.OperatorMatrix; var MgMap = op.Mapping; this.m_MgOp = op; if (!M.RowPartitioning.EqualsPartition(MgMap.Partitioning)) { throw new ArgumentException("Row partitioning mismatch."); } if (!M.ColPartition.EqualsPartition(MgMap.Partitioning)) { throw new ArgumentException("Column partitioning mismatch."); } //this.m_Matrix = M; this.m_Matrix = new ilPSP.LinSolvers.monkey.CPU.RefMatrix(M.ToMsrMatrix()); /* * int n = m_Matrix.RowPartitioning.LocalLength; * if(n > 50000) { * var _Matrix = new ilPSP.LinSolvers.monkey.CPU.RefMatrix(M.ToMsrMatrix()); * * double[] xTest = new double[n]; * double[] bTest = new double[n]; * Random r = new Random(123); * for(int i = 0; i < n; i++) { * xTest[i] = r.NextDouble(); * bTest[i] = r.NextDouble(); * } * * * double[] b1 = bTest.CloneAs(); * double[] x1 = bTest.CloneAs(); * Stopwatch monkey = new Stopwatch(); * monkey.Start(); * for (int i = 0; i < 100; i++) * _Matrix.SpMV(1.0, x1, -0.1, b1); * monkey.Stop(); * * double[] b2 = bTest.CloneAs(); * double[] x2 = bTest.CloneAs(); * Stopwatch block = new Stopwatch(); * block.Start(); * for (int i = 0; i < 100; i++) * m_Matrix.SpMV(1.0, x1, -0.1, b1); * block.Stop(); * * Console.WriteLine("SPMV monkey: " + monkey.Elapsed.TotalSeconds); * Console.WriteLine("SPMV block MSR: " + block.Elapsed.TotalSeconds); * } */ if (Precond != null) { Precond.Init(op); } } }
/// <summary> /// Tests if a matrix (associated with some operator) depends only on values in this sub-grid, /// or not. /// </summary> /// <param name="_Mtx">some operator matrix</param> /// <param name="RowMap">row-/co-domain mapping for matrix <paramref name="_Mtx"/></param> /// <param name="ColMap">column/domain mapping for matrix <paramref name="_Mtx"/></param> /// <param name="NoOfTests"></param> /// <returns> /// 0.0 if there is no dependency of the matrix <paramref name="_Mtx"/> /// on values outside of this subgrid. /// </returns> public double TestMatrixDependency(MsrMatrix _Mtx, UnsetteledCoordinateMapping RowMap, UnsetteledCoordinateMapping ColMap, int NoOfTests = 10) { Random RND = new Random(); if (!_Mtx.RowPartitioning.Equals(RowMap)) { throw new ArgumentException(); } if (!_Mtx.ColPartition.Equals(ColMap)) { throw new ArgumentException(); } var Mtx = new ilPSP.LinSolvers.monkey.CPU.RefMatrix(_Mtx); int[] IR = RowMap.BasisS.Count.ForLoop(i => i); int[] IC = ColMap.BasisS.Count.ForLoop(i => i); var SgrdRow = RowMap.GetSubvectorIndices(this, true, IR); var SgrdCol = SgrdRow; // ColMap.GetSubvectorIndices(this, true, true, IC); double[] X0 = new double[ColMap.LocalLength]; double[] Y0 = new double[RowMap.LocalLength]; for (int i = 0; i < X0.Length; i++) { X0[i] = RND.NextDouble(); } Mtx.SpMV(1.0, X0, 0.0, Y0); int i0Row = RowMap.i0; int i0Col = ColMap.i0; int iECol = ColMap.iE; double err = 0; for (int iTest = 0; iTest < NoOfTests; iTest++) { double[] X1 = new double[ColMap.LocalLength]; double[] Y1 = new double[RowMap.LocalLength]; for (int i = 0; i < X0.Length; i++) { X1[i] = RND.NextDouble(); } foreach (int i in SgrdCol) { if (i >= i0Col && i < iECol) { X1[i - i0Col] = X0[i - i0Col]; } } Mtx.SpMV(1.0, X1, 0.0, Y1); foreach (int k in SgrdRow) { double Y1k = Y1[k - i0Row]; double Y0k = Y0[k - i0Row]; //Debug.Assert((Y0k - Y1k).Pow2() < 1.0e-8); err += (Y0k - Y1k).Pow2(); } } return(err.MPISum()); }