public static void MapConsistencyTest( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder ) { //if no selection is chosen mapping should be the same as of origin //assume: indexing is right 'cause of other tests Utils.TestInit((int)UseXdg, DGOrder); Console.WriteLine("MapConsistencyTest({0},{1})", UseXdg, DGOrder); //Arrange MultigridOperator MGOp = Utils.CreateTestMGOperator(UseXdg, DGOrder); var sbs = new SubBlockSelector(MGOp.Mapping); var mask = new BlockMask(sbs); var stw = new Stopwatch(); stw.Restart(); //Act --- Create Mapping from mask stw.Start(); var submatrix = mask.GetSubBlockMatrix(MGOp.OperatorMatrix); stw.Stop(); var rowpart = submatrix._RowPartitioning; var colpart = submatrix._ColPartitioning; //Assert --- Equal Partition of mask and origin Assert.AreEqual(rowpart, colpart); Assert.IsTrue(rowpart.IsLocallyEqual(MGOp.Mapping)); }
public static void GetExternalRowsTest( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(4)] int Res) { //Matlabaufruf --> gesamte Matrix nach Matlab schreiben //Teilmatritzen gemäß Globalid extrahieren //Mit ExternalRows vergleichen //Die große Frage: funktioniert der batchmode connector parallel? Beim rausschreiben beachten Utils.TestInit((int)UseXdg, DGOrder); Console.WriteLine("GetExternalRowsTest({0},{1})", UseXdg, DGOrder); //Arrange --- setup mgo and mask MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MatrixShape.laplace, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; //Delete this plz ... //M.SaveToTextFileSparse("M"); //int[] A = Utils.GimmeAllBlocksWithSpec(map, 9); //int[] B = Utils.GimmeAllBlocksWithSpec(map, 18); //if (map.MpiRank == 0) { // A.SaveToTextFileDebug("ACells"); // B.SaveToTextFileDebug("BCells"); //} var selector = new SubBlockSelector(map); var dummy = new BlockMsrMatrix(map); // we are only interested in getting indices, so a dummy is sufficient var mask = new BlockMask(selector, dummy); //Arrange --- get stuff to put into matlab int[] GlobalIdx_ext = Utils.GetAllExtCellIdc(map); double[] GlobIdx = GlobalIdx_ext.Length.ForLoop(i => (double)GlobalIdx_ext[i] + 1.0); //Arrange --- get external rows by mask BlockMsrMatrix extrows = BlockMask.GetAllExternalRows(mgo.Mapping, mgo.OperatorMatrix); //Assert --- idc and rows of extrows have to be the same Assert.IsTrue(GlobIdx.Length == extrows._RowPartitioning.LocalLength); //Arrange --- get external rows by matlab var infNorm = MultidimensionalArray.Create(1, 1); using (BatchmodeConnector matlab = new BatchmodeConnector()) { //note: BatchmodeCon maybe working on proc0 but savetotxt file, etc. (I/O) is full mpi parallel //so concider this as full mpi-parallel matlab.PutSparseMatrix(M, "M"); matlab.PutSparseMatrix(extrows, "M_test"); matlab.PutVector(GlobIdx, "Idx"); matlab.Cmd(String.Format("M_ext = M(Idx, :);")); matlab.Cmd("n=norm(M_test-M_ext,inf)"); matlab.GetMatrix(infNorm, "n"); matlab.Execute(); } //Assert --- test if we actually got the right Matrix corresponding to Index Assert.IsTrue(infNorm[0, 0] == 0.0); }
public static void SubSelection( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.full_var_spec, MatrixShape.full_spec, MatrixShape.full_var, MatrixShape.full)] MatrixShape MShape, [Values(4)] int Res) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape, Res); Console.WriteLine("SubSelection({0},{1},{2},{3})", UseXdg, DGOrder, MShape, Res); //Arrange --- create test matrix, MG mapping MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; //Arrange --- get mask int[] cells = Utils.GetCellsOfOverlappingTestBlock(map); Array.Sort(cells); var sbs = new SubBlockSelector(map); sbs.CellSelector(cells, false); BlockMsrMatrix M_ext = BlockMask.GetAllExternalRows(map, M); var mask = new BlockMask(sbs, M_ext); //Arrange --- get GlobalIdxList int[] idc = Utils.GetIdcOfSubBlock(map, cells); bool[] coup = Utils.SetCoupling(MShape); var M_sub = mask.GetSubBlockMatrix(M, false, coup[0], coup[1]); var infNorm = MultidimensionalArray.Create(4, 1); int rank = map.MpiRank; using (BatchmodeConnector matlab = new BatchmodeConnector()) { double[] GlobIdx = idc.Count().ForLoop(i => (double)idc[i] + 1.0); Assert.IsTrue(GlobIdx.Length == M_sub.NoOfRows); matlab.PutSparseMatrix(M, "M"); // note: M_sub lives on Comm_Self, therefore we have to distinguish between procs ... matlab.PutSparseMatrixRankExclusive(M_sub, "M_sub"); matlab.PutVectorRankExclusive(GlobIdx, "Idx"); matlab.Cmd("M_0 = full(M(Idx_0, Idx_0));"); matlab.Cmd("M_1 = full(M(Idx_1, Idx_1));"); matlab.Cmd("M_2 = full(M(Idx_2, Idx_2));"); matlab.Cmd("M_3 = full(M(Idx_3, Idx_3));"); matlab.Cmd("n=[0; 0; 0; 0];"); matlab.Cmd("n(1,1)=norm(M_0-M_sub_0,inf);"); matlab.Cmd("n(2,1)=norm(M_1-M_sub_1,inf);"); matlab.Cmd("n(3,1)=norm(M_2-M_sub_2,inf);"); matlab.Cmd("n(4,1)=norm(M_3-M_sub_3,inf);"); matlab.GetMatrix(infNorm, "n"); matlab.Execute(); } Assert.IsTrue(infNorm[rank, 0] == 0.0); }
public static void SplitVectorOperations( XDGusage UseXdg, int DGOrder, MatrixShape MShape ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("SplitVectorOperations({0},{1},{2})", UseXdg, DGOrder, MShape); //matrix Erzeugung wie in ExtractDiagonalCellBlocks... //Auf der HierarchieEbene, auf der Kopplung ausgesetzt wird kann Auswahl vorgenommen werden //bei var: 0 / 1, bei DG: <=1 / >1, bei spec: A / B, bei Cells: odd / even //accumulierte Teilergebnisse sind dann == fullM*fullX var mop = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape); var map = mop.Mapping; double[] Vec = Utils.GetRandomVector(mop.Mapping.LocalLength); //Arrange --- setup masking SubBlockSelector sbsA = new SubBlockSelector(map); sbsA.SetDefaultSplitSelection(MShape, true); BlockMask maskA = new BlockMask(sbsA, null); SubBlockSelector sbsB = new SubBlockSelector(map); sbsB.SetDefaultSplitSelection(MShape, false); BlockMask maskB = new BlockMask(sbsB, null); double[] VecAB = new double[Vec.Length]; //Arrange --- some time measurement Stopwatch stw = new Stopwatch(); stw.Reset(); //Act --- stw.Start(); var VecA = maskA.GetSubVec(Vec); var VecB = maskB.GetSubVec(Vec); maskA.AccSubVec(VecA, VecAB); maskB.AccSubVec(VecB, VecAB); stw.Stop(); Debug.Assert(Vec.L2Norm() != 0); double fac = ((MShape == MatrixShape.full_var || MShape == MatrixShape.diagonal_var) && UseXdg == XDGusage.none) ? -2.0 : -1.0; VecAB.AccV(fac, Vec); //Assert --- are extracted blocks and Assert.IsTrue(VecAB.L2Norm() == 0.0, String.Format("L2Norm neq 0!")); }
public static void VectorSplitOperation( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.full_var_spec, MatrixShape.full_spec, MatrixShape.full)] MatrixShape MShape, [Values(4)] int Res) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape, Res); Console.WriteLine("VectorSplitOperation({0},{1},{2},{3})", UseXdg, DGOrder, MShape, Res); //Arrange --- create test matrix, MG mapping MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; BlockMsrMatrix M_ext = BlockMask.GetAllExternalRows(map, M); double[] Vec = Utils.GetRandomVector(M_ext.RowPartitioning.LocalLength); //Arrange --- setup masking SubBlockSelector sbsA = new SubBlockSelector(map); sbsA.SetDefaultSplitSelection(MShape, true, false); BlockMask maskA = new BlockMask(sbsA, M_ext); SubBlockSelector sbsB = new SubBlockSelector(map); sbsB.SetDefaultSplitSelection(MShape, false, false); BlockMask maskB = new BlockMask(sbsB, M_ext); double[] VecAB = new double[Vec.Length]; //Arrange --- some time measurement Stopwatch stw = new Stopwatch(); stw.Reset(); //Act --- stw.Start(); var VecA = maskA.GetSubVec(Vec, new double[0]); var VecB = maskB.GetSubVec(Vec, new double[0]); maskA.AccSubVec(VecA, VecAB, new double[0]); maskB.AccSubVec(VecB, VecAB, new double[0]); stw.Stop(); Debug.Assert(Vec.L2Norm() != 0); double fac = ((MShape == MatrixShape.full_var || MShape == MatrixShape.diagonal_var) && UseXdg == XDGusage.none) ? -2.0 : -1.0; VecAB.AccV(fac, Vec); //Assert --- are extracted blocks and Assert.IsTrue(VecAB.L2Norm() == 0.0, String.Format("L2Norm neq 0!")); }
public static MultigridOperator CreateTestMGOperator(out double[] Vec, XDGusage UseXdg = XDGusage.none, int DGOrder = 2, MatrixShape MShape = MatrixShape.full, int Resolution = 4) { MultigridOperator retMGOp; using (var solver = new SubBlockTestSolver2Var() { m_UseXdg = UseXdg, m_DGorder = DGOrder, m_Mshape = MShape, m_Res = Resolution }) { solver.Init(null); solver.RunSolverMode(); retMGOp = solver.MGOp; Vec = solver.someVec; } return(retMGOp); }
public static void CellBlockVectorOperations( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.diagonal, MatrixShape.diagonal_var, MatrixShape.diagonal_spec, MatrixShape.diagonal_var_spec)] MatrixShape MShape ) { //matrix Erzeugung wie in ExtractDiagonalCellBlocks... //Auf der HierarchieEbene, auf der Kopplung ausgesetzt wird kann Auswahl vorgenommen werden //bei var: 0 / 1, bei DG: <=1 / >1, bei spec: A / B, bei Cells: odd / even //accumulierte Teilergebnisse sind dann == fullM*fullX Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("CellBlockVectorOperations({0},{1},{2})", UseXdg, DGOrder, MShape); var mop = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape); var map = mop.Mapping; double[] Vec = Utils.GetRandomVector(mop.Mapping.LocalLength); //Arrange --- setup masking SubBlockSelector SBS = new SubBlockSelector(map); BlockMask mask = new BlockMask(SBS, null); //Arrange --- some time measurement Stopwatch stw = new Stopwatch(); stw.Reset(); //Assert --- all diagonal blocks are extracted //Assert.IsTrue(blocks.Length == map.LocalNoOfBlocks); double[] Vec_col = new double[map.LocalLength]; for (int i = 0; i < map.LocalNoOfBlocks; i++) { stw.Start(); double[] Vec_i = mask.GetSubVecOfCell(Vec, i); mask.AccSubVecOfCell(Vec_i, i, Vec_col); stw.Stop(); } Vec_col.AccV(-1.0, Vec); //Assert --- are extracted blocks and Assert.IsTrue(Vec_col.L2Norm() == 0.0, String.Format("L2Norm neq 0!")); }
public static void SubMatrixExtractionWithCoupling( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.diagonal, MatrixShape.diagonal_var, MatrixShape.full_spec, MatrixShape.full_var_spec)] MatrixShape MShape ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("ExtractSubMatrixAndIgnoreCoupling({0},{1},{2})", UseXdg, DGOrder, MShape); //Arrange --- get multigridoperator MultigridOperator MGOp = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape); BlockMsrMatrix M = MGOp.OperatorMatrix; MultigridMapping map = MGOp.Mapping; //Arrange --- setup masking SubBlockSelector SBS = new SubBlockSelector(map); BlockMask mask = new BlockMask(SBS, null); bool[] coup = Utils.SetCoupling(MShape); //Arrange --- some time measurement Stopwatch stw = new Stopwatch(); stw.Reset(); //Act --- establish submatrix stw.Start(); //var Ones = M.CloneAs(); //Ones.Clear(); //Ones.SetAll(1); //var extractOnes = mask.GetSubBlockMatrix(Ones, false, coup[0], coup[1]); var Mext = mask.GetSubBlockMatrix(M, false, coup[0], coup[1]); stw.Stop(); var Mquad = M.ConvertToQuadraticBMsr(mask.GlobalIList_Internal.ToArray(), true); Mext.Acc(-1.0, Mquad); //Assert --- Mext conains only diagonal blocks of M Assert.IsTrue(Mext.InfNorm() == 0); }
public static void ExternalIndexTest( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(4)] int Res ) { Utils.TestInit((int)UseXdg, DGOrder); Console.WriteLine("ExternalIndexTest({0},{1})", UseXdg, DGOrder); //Arrange --- Get global index by mapping MultigridOperator MGOp = Utils.CreateTestMGOperator(UseXdg, DGOrder, MatrixShape.laplace, Res); var map = MGOp.Mapping; int[] GlobalIdxMap_ext = Utils.GetAllExtCellIdc(map); //Arrange --- Prepare stuff for mask var selector = new SubBlockSelector(map); var dummy = new BlockMsrMatrix(map); // we are only interested in getting indices, so a dummy is sufficient var stw = new Stopwatch(); stw.Reset(); //Act --- do the masking to get index lists stw.Start(); var mask = new BlockMask(selector, dummy); stw.Stop(); int[] GlobalIdxMask_ext = mask.GlobalIList_External.ToArray(); //Assert --- Idx lists are of same length Assert.IsTrue(GlobalIdxMap_ext.Length == GlobalIdxMask_ext.Length); //Assert --- Compare map and mask indices for (int iLoc = 0; iLoc < GlobalIdxMask_ext.Length; iLoc++) { Assert.IsTrue(GlobalIdxMask_ext[iLoc] == GlobalIdxMap_ext[iLoc]); } }
public static void LocalIndexTest( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder) { Utils.TestInit((int)UseXdg, DGOrder); Console.WriteLine("LocalLIndexTest({0},{1})", UseXdg, DGOrder); //Arrange --- Get global index by mapping MultigridOperator MGOp = Utils.CreateTestMGOperator(UseXdg, DGOrder); var map = MGOp.Mapping; int[] fields = map.NoOfVariables.ForLoop(i => i); int[] GlobalIdxMap_loc = map.GetSubvectorIndices(fields); //Arrange --- Prepare stuff for mask var selector = new SubBlockSelector(map); var stw = new Stopwatch(); stw.Reset(); //Act --- do the masking to get index lists stw.Start(); var mask = new BlockMask(selector, null); stw.Stop(); int[] GlobalIdxMask_loc = mask.GlobalIList_Internal.ToArray(); //Assert --- Idx lists are of same length Assert.IsTrue(GlobalIdxMap_loc.Length == GlobalIdxMask_loc.Length); //Assert --- Compare map and mask indices for (int iLoc = 0; iLoc < GlobalIdxMask_loc.Length; iLoc++) { Assert.True(GlobalIdxMap_loc[iLoc] == GlobalIdxMask_loc[iLoc]); } }
public static void VectorCellwiseOperation( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.diagonal_var_spec, MatrixShape.diagonal_spec, MatrixShape.diagonal_var, MatrixShape.diagonal)] MatrixShape MShape, [Values(4)] int Res ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("SubMatrixIgnoreCoupling({0},{1},{2})", UseXdg, DGOrder, MShape); //Arrange --- create test matrix, MG mapping MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; //Arrange --- masking and subblock extraction of external cells var sbs = new SubBlockSelector(map); sbs.AllExternalCellsSelection(); var M_ext = BlockMask.GetAllExternalRows(map, M); var mask = new BlockMask(sbs, M_ext); var eblocks = mask.GetDiagonalBlocks(M, false, false); //Dictionary<int, int[]> Didc = Utils.GetDictOfAllExtCellIdc(map); //Arrange --- generate rnd vector and distribute it double[] vec = new double[map.LocalLength]; vec = Utils.GetRandomVector(map.LocalLength); var vec_ex = new MPIexchange <double[]>(map, vec); vec_ex.TransceiveStartImReturn(); vec_ex.TransceiveFinish(0.0); Debug.Assert(vec_ex.Vector_Ext.L2Norm() != 0); //Arrange --- stopwatch var stw = new Stopwatch(); stw.Reset(); //Arrange --- get extended (loc+external cells) vector double[] Vec_ext = new double[vec.Length + vec_ex.Vector_Ext.Length]; mask.AccSubVec(vec_ex.Vector_Ext, Vec_ext); bool test = eblocks.Length.MPIEquals(); Debug.Assert(test); //Act --- calculate blockwise result: M_i*vec_i=Res_i double[] Res_ext = new double[Vec_ext.Length]; stw.Start(); for (int i = 0; i < eblocks.Length; i++) { //int iBlock = i + map.AggGrid.iLogicalCells.NoOfLocalUpdatedCells; double[] vec_i = mask.GetSubVecOfCell(Vec_ext, i); double[] Res_i = new double[vec_i.Length]; eblocks[i].MatVecMul(1.0, vec_i, 0.0, Res_i); mask.AccSubVecOfCell(Res_i, i, Res_ext); if (map.MpiRank == 0) { eblocks[i].ConvertToMsr().SaveToTextFileSparseDebug(String.Format("block_{0}_{1}", i, map.MpiRank)); vec_i.SaveToTextFileDebug(String.Format("vec_{0}_{1}", i, map.MpiRank)); Res_i.SaveToTextFileDebug(String.Format("Res_{0}_{1}", i, map.MpiRank)); } } stw.Stop(); //Act --- project Res_i onto Res_g and Res_g=M_ext*vec_ext-Res_g double[] Res_g = mask.GetSubVec(Res_ext); var qM_ext = M_ext.ConvertToQuadraticBMsr(mask.GlobalIList_External.ToArray(), false); qM_ext.SpMV(1.0, vec_ex.Vector_Ext, -1.0, Res_g); if (map.MpiRank == 0) { vec_ex.Vector_Ext.SaveToTextFileDebug("vec_g"); Res_g.SaveToTextFileDebug("Res_g"); M_ext.SaveToTextFileSparseDebug("M_ext"); qM_ext.SaveToTextFileSparseDebug("qM_ext"); } //Assert --- |Res_g| should be at least near to zero Assert.IsTrue(Res_g.L2Norm() == 0.0); }
public static void MultiplyTest( [Values(XDGusage.none, XDGusage.mixed1, XDGusage.mixed2, XDGusage.all)] XDGusage UseXdg, [Values(1, 3)] int DGOrder, [Values(false, true)] bool compressL1, [Values(false, true)] bool compressL2) { unsafe { int[] Params = new int[8], ParamsGlob = new int[8]; fixed(int *pParams = Params, pParamsGlob = ParamsGlob) { pParams[0] = (int)UseXdg; pParams[1] = DGOrder; pParams[2] = compressL1 ? 1 : 0; pParams[3] = compressL2 ? 1 : 0; pParams[4] = -pParams[0]; pParams[5] = -pParams[1]; pParams[6] = -pParams[2]; pParams[7] = -pParams[3]; csMPI.Raw.Allreduce((IntPtr)pParams, (IntPtr)pParamsGlob, 8, csMPI.Raw._DATATYPE.INT, csMPI.Raw._OP.MIN, csMPI.Raw._COMM.WORLD); } int[] ParamsMin = ParamsGlob.GetSubVector(0, 4); int[] ParamsMax = ParamsGlob.GetSubVector(4, 4); for (int i = 0; i < 4; i++) { if (Params[i] != ParamsMin[i]) { throw new ApplicationException(); } if (Params[i] != -ParamsMax[i]) { throw new ApplicationException(); } } Console.WriteLine("MultiplyTest({0},{1},{2},{3})", UseXdg, DGOrder, compressL1, compressL2); } using (var solver = new Matrix_MPItestMain() { m_UseXdg = UseXdg, m_DGorder = DGOrder }) { // create the test data // ==================== solver.Init(null); solver.RunSolverMode(); Stopwatch stw = new Stopwatch(); stw.Reset(); stw.Start(); BlockMsrMatrix M = solver.OperatorMatrix; int[] Ilist1 = solver.ProblemMapping.GetSubvectorIndices(false, 0); int[] Ilist2 = solver.ProblemMapping.GetSubvectorIndices(false, 1); foreach (int i in Ilist1) { Assert.IsTrue(solver.ProblemMapping.IsInLocalRange(i)); } foreach (int i in Ilist2) { Assert.IsTrue(solver.ProblemMapping.IsInLocalRange(i)); } var Blk1 = solver.ProblemMapping.GetSubBlocking(Ilist1, csMPI.Raw._COMM.WORLD, compressL1 ? -1 : 0); var Blk2 = solver.ProblemMapping.GetSubBlocking(Ilist2, csMPI.Raw._COMM.WORLD, compressL2 ? -1 : 0); int[] Tlist1 = compressL1 ? default(int[]) : Blk1.GetOccupiedIndicesList(); int[] Tlist2 = compressL2 ? default(int[]) : Blk2.GetOccupiedIndicesList(); if (Tlist1 != null) { Assert.AreEqual(Tlist1.Length, Ilist1.Length); foreach (int i in Tlist1) { Assert.IsTrue(Blk1.IsInLocalRange(i)); } } if (Tlist2 != null) { Assert.AreEqual(Tlist2.Length, Ilist2.Length); foreach (int i in Tlist2) { Assert.IsTrue(Blk2.IsInLocalRange(i)); } } BlockMsrMatrix M11 = new BlockMsrMatrix(Blk1, Blk1); BlockMsrMatrix M12 = new BlockMsrMatrix(Blk1, Blk2); BlockMsrMatrix M21 = new BlockMsrMatrix(Blk2, Blk1); BlockMsrMatrix M22 = new BlockMsrMatrix(Blk2, Blk2); M.AccSubMatrixTo(1.0, M11, Ilist1, Tlist1, Ilist1, Tlist1); M.AccSubMatrixTo(1.0, M12, Ilist1, Tlist1, Ilist2, Tlist2); M.AccSubMatrixTo(1.0, M21, Ilist2, Tlist2, Ilist1, Tlist1); M.AccSubMatrixTo(1.0, M22, Ilist2, Tlist2, Ilist2, Tlist2); /* * MultidimensionalArray CheckRes2 = MultidimensionalArray.Create(1, 4); * using (var MatlabRef = new BatchmodeConnector()) { * * MatlabRef.PutVector(Ilist1.Select(i => (double)i + 1.0).ToArray(), "Ilist1"); * MatlabRef.PutVector(Ilist2.Select(i => (double)i + 1.0).ToArray(), "Ilist2"); * MatlabRef.PutVector(Tlist1 == null ? Ilist1.Length.ForLoop(i => (double)i + 1.0 + Blk1.i0) : Tlist1.Select(i => (double)i + 1.0).ToArray(), "Tlist1"); * MatlabRef.PutVector(Tlist2 == null ? Ilist2.Length.ForLoop(i => (double)i + 1.0 + Blk2.i0) : Tlist2.Select(i => (double)i + 1.0).ToArray(), "Tlist2"); * * MatlabRef.PutSparseMatrix(solver.AltOperatorMatrix, "M"); * * * MatlabRef.Cmd("L1 = {0};", Blk1.TotalLength); * MatlabRef.Cmd("L2 = {0};", Blk2.TotalLength); * //MatlabRef.Cmd("refM11 = sparse(L1, L1);"); * //MatlabRef.Cmd("refM12 = sparse(L1, L2);"); * MatlabRef.Cmd("refM21 = sparse(L2, L1);"); * //MatlabRef.Cmd("refM22 = sparse(L2, L2);"); * * //MatlabRef.Cmd("refM11(Tlist1, Tlist1) = M(Ilist1, Ilist1);"); * //MatlabRef.Cmd("refM12(Tlist1, Tlist2) = M(Ilist1, Ilist2);"); * MatlabRef.Cmd("refM21(Tlist2, Tlist1) = M(Ilist2, Ilist1);"); * //MatlabRef.Cmd("refM22(Tlist2, Tlist2) = M(Ilist2, Ilist2);"); * * //MatlabRef.Cmd("err11 = norm(refM11 - M11, inf);"); * //MatlabRef.Cmd("err12 = norm(refM12 - M12, inf);"); * //MatlabRef.Cmd("err21 = norm(refM21 - M21, inf);"); * //MatlabRef.Cmd("err22 = norm(refM22 - M22, inf);"); * * MatlabRef.Cmd("CheckRes = [refM21(1339, 1321), 0.0, 1.567, 0 ];"); * MatlabRef.GetMatrix(CheckRes2, "CheckRes"); * * MatlabRef.Execute(); * } */ // test multipliation (later verified by matlab) BlockMsrMatrix M11xM12 = new BlockMsrMatrix(M11._RowPartitioning, M12._ColPartitioning); M11xM12.Acc(1.0, M12); BlockMsrMatrix.Multiply(M11xM12, M11, M12); BlockMsrMatrix M22xM21 = new BlockMsrMatrix(M22._RowPartitioning, M21._ColPartitioning); BlockMsrMatrix.Multiply(M22xM21, M22, M21); double ProdNorm = M22xM21.InfNorm(); stw.Stop(); //M.SaveToTextFileSparse(@"C:\tmp\M.txt"); //M11.SaveToTextFileSparse(@"C:\tmp\M11.txt"); //M12.SaveToTextFileSparse(@"C:\tmp\M12.txt"); //M21.SaveToTextFileSparse(@"C:\tmp\M21.txt"); //M22.SaveToTextFileSparse(@"C:\tmp\M22.txt"); //M22xM21.SaveToTextFileSparse(@"C:\tmp\M22xM21.txt"); using (var MatlabRef = new BatchmodeConnector()) { MultidimensionalArray CheckRes = MultidimensionalArray.Create(1, 4); MatlabRef.PutSparseMatrix(M11, "M11"); MatlabRef.PutSparseMatrix(M12, "M12"); MatlabRef.PutSparseMatrix(M21, "M21"); MatlabRef.PutSparseMatrix(M22, "M22"); MatlabRef.PutSparseMatrix(M11xM12, "M11xM12"); MatlabRef.PutSparseMatrix(M22xM21, "M22xM21"); MatlabRef.Cmd("refM11xM12 = M12 + M11*M12;"); MatlabRef.Cmd("refM22xM21 = M22*M21;"); MatlabRef.Cmd("err1112 = norm(refM11xM12 - M11xM12, inf);"); MatlabRef.Cmd("err2221 = norm(refM22xM21 - M22xM21, inf);"); MatlabRef.Cmd("CheckRes = [err1112, err2221, 0, 0];"); MatlabRef.GetMatrix(CheckRes, "CheckRes"); MatlabRef.Execute(); Console.WriteLine("Matlab check M11*M12: " + CheckRes[0, 0]); Console.WriteLine("Matlab check M22*M21: " + CheckRes[0, 1]); Assert.IsTrue(CheckRes[0, 0] == 0.0); Assert.IsTrue(CheckRes[0, 1] < 1.0e-10 * ProdNorm); //Assert.IsTrue(CheckRes[0, 2] == 0.0); //Assert.IsTrue(CheckRes[0, 3] == 0.0); } Console.WriteLine("Time spend in matrix operations: " + stw.Elapsed.TotalSeconds + " sec."); TotTime_MatrixOp += stw.Elapsed; } }
public static MultigridOperator CreateTestMGOperator(XDGusage UseXdg = XDGusage.none, int DGOrder = 2, MatrixShape MShape = MatrixShape.full, int Resolution = 4) { return(CreateTestMGOperator(out double[] Vec, UseXdg, DGOrder, MShape, Resolution)); }
public static MultigridOperator CreateTestMGOperator(out AggregationGridData[] MGSeq, XDGusage UseXdg = XDGusage.all, int DGOrder = 2, MatrixShape MShape = MatrixShape.laplace, int Resolution = 4) { MultigridOperator retMGOp; using (var solver = new SubBlockTestSolver2Var() { m_UseXdg = UseXdg, m_DGorder = DGOrder, m_Mshape = MShape, m_Res = Resolution }) { solver.Init(null); solver.RunSolverMode(); retMGOp = solver.MGOp; MGSeq = solver.MgSeq; } return(retMGOp); }
public static void SubBlockExtractionWithCoupling( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.diagonal, MatrixShape.diagonal_var, MatrixShape.diagonal_spec, MatrixShape.diagonal_var_spec)] MatrixShape MShape ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("ExtractDiagonalBlocks({0},{1},{2})", UseXdg, DGOrder, MShape); //Arrange --- get multigridoperator MultigridOperator MGOp = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape); BlockMsrMatrix M = MGOp.OperatorMatrix; MultigridMapping map = MGOp.Mapping; //Arrange --- setup masking SubBlockSelector SBS = new SubBlockSelector(map); BlockMask mask = new BlockMask(SBS, null); bool[] coupling = Utils.SetCoupling(MShape); //Arrange --- some time measurement Stopwatch stw = new Stopwatch(); stw.Reset(); //Arrange --- setup auxiliary matrix //this will show us if more is extracted, than it should ... var Mprep = new BlockMsrMatrix(map); Mprep.Acc(1.0, M); //Act --- diagonal subblock extraction stw.Start(); var blocks = mask.GetDiagonalBlocks(Mprep, coupling[0], coupling[1]); stw.Stop(); //Assert --- all diagonal blocks are extracted Assert.IsTrue(blocks.Length == map.LocalNoOfBlocks); for (int i = 0; i < map.LocalNoOfBlocks; i++) { //Arrange --- get ith diagonal block of M: M_i int iBlock = i + map.AggGrid.CellPartitioning.i0; int L = map.GetBlockLen(iBlock); int i0 = map.GetBlockI0(iBlock); var Mblock = MultidimensionalArray.Create(L, L); M.ReadBlock(i0, i0, Mblock); //Act --- M_i-Mones_i Mblock.Acc(-1.0, blocks[i]); //Assert --- are extracted blocks and Assert.IsTrue(Mblock.InfNorm() == 0.0, String.Format("infNorm of block {0} neq 0!", i)); } //BlockMsrMatrix all1; //all1.SetAll(1); //Generate broken diagonal matrix, die zur Maske passt: M //M+all1=M_prep //Wende Extraction auf M_prep an, Man sollte nun M bekommen //Test: M_prep-extract(M_prep)=all1 //Test-crit: Result.SumEntries=DOF^2 oder Result.Max()==Result.Min()==1 //oder (besser) //Test: M-extract(M_prep)=zeros //Test-crit: Result.InfNorm()==0 //Der Test kann für ExtractSubMatrix mit ignore coupling wiederholt werden //eventuell: Testmatrix finden mit brauchbaren Nebendiagonalen für einen Fall //Was wird getestet: funktioniert ignorecoupling richtig? }
public static void FastSubMatrixExtraction( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.laplace)] MatrixShape MShape, [Values(4)] int Res ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("FastSubMatrixExtraction({0},{1},{2})", UseXdg, DGOrder, MShape); //Arrange --- MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; var sbs = new SubBlockSelector(map); int[] extcells = sbs.AllExternalCellsSelection(); var M_ext = BlockMask.GetAllExternalRows(map, M); var mask = new BlockMask(sbs, M_ext); //Arrange --- get index list of all external cells int[] idc = Utils.GetAllExtCellIdc(map); double[] GlobIdx = idc.Count().ForLoop(i => (double)idc[i] + 1.0); //Arrange --- stopwatch var stw = new Stopwatch(); stw.Reset(); //Act --- Extract SubMatrix stw.Start(); BlockMsrMatrix subM = mask.GetSubBlockMatrix(M); stw.Stop(); //Arrange --- Extract Blocks in Matlab and substract var infNorm = MultidimensionalArray.Create(4, 1); int rank = map.MpiRank; using (BatchmodeConnector matlab = new BatchmodeConnector()) { matlab.PutSparseMatrix(M, "M"); // note: M_sub lives on Comm_Self, therefore we have to distinguish between procs ... matlab.PutSparseMatrixRankExclusive(subM, "M_sub"); matlab.PutVectorRankExclusive(GlobIdx, "Idx"); matlab.Cmd("M_0 = M(Idx_0, Idx_0);"); matlab.Cmd("M_1 = M(Idx_1, Idx_1);"); matlab.Cmd("M_2 = M(Idx_2, Idx_2);"); matlab.Cmd("M_3 = M(Idx_3, Idx_3);"); matlab.Cmd("n=[0; 0; 0; 0];"); matlab.Cmd("n(1,1)=norm(M_0-M_sub_0,inf);"); matlab.Cmd("n(2,1)=norm(M_1-M_sub_1,inf);"); matlab.Cmd("n(3,1)=norm(M_2-M_sub_2,inf);"); matlab.Cmd("n(4,1)=norm(M_3-M_sub_3,inf);"); matlab.GetMatrix(infNorm, "n"); matlab.Execute(); } //Assert --- mask blocks and extracted blocks are the same Assert.IsTrue(infNorm[rank, 0] == 0.0); }
public static void SubMatrixTest( [Values(XDGusage.none, XDGusage.mixed1, XDGusage.mixed2, XDGusage.all)] XDGusage UseXdg, [Values(1, 3)] int DGOrder, [Values(false, true)] bool compressL1, [Values(false, true)] bool compressL2) { unsafe { int[] Params = new int[8], ParamsGlob = new int[8]; fixed(int *pParams = Params, pParamsGlob = ParamsGlob) { pParams[0] = (int)UseXdg; pParams[1] = DGOrder; pParams[2] = compressL1 ? 1 : 0; pParams[3] = compressL2 ? 1 : 0; pParams[4] = -pParams[0]; pParams[5] = -pParams[1]; pParams[6] = -pParams[2]; pParams[7] = -pParams[3]; csMPI.Raw.Allreduce((IntPtr)pParams, (IntPtr)pParamsGlob, 8, csMPI.Raw._DATATYPE.INT, csMPI.Raw._OP.MIN, csMPI.Raw._COMM.WORLD); } int[] ParamsMin = ParamsGlob.GetSubVector(0, 4); int[] ParamsMax = ParamsGlob.GetSubVector(4, 4); for (int i = 0; i < 4; i++) { if (Params[i] != ParamsMin[i]) { throw new ApplicationException(); } if (Params[i] != -ParamsMax[i]) { throw new ApplicationException(); } } Console.WriteLine("SubMatrixTest({0},{1},{2},{3})", UseXdg, DGOrder, compressL1, compressL2); } using (var solver = new Matrix_MPItestMain() { m_UseXdg = UseXdg, m_DGorder = DGOrder }) { // create the test data // ==================== BoSSS.Solution.Application.CommandLineOptions opts = null; //opts = new BoSSS.Solution.Application.CommandLineOptions(); solver.Init(null, opts); solver.RunSolverMode(); Stopwatch stw = new Stopwatch(); stw.Reset(); stw.Start(); BlockMsrMatrix M = solver.OperatorMatrix; int[] Ilist1 = solver.ProblemMapping.GetSubvectorIndices(false, 0); int[] Ilist2 = solver.ProblemMapping.GetSubvectorIndices(false, 1); foreach (int i in Ilist1) { Assert.IsTrue(solver.ProblemMapping.IsInLocalRange(i)); } foreach (int i in Ilist2) { Assert.IsTrue(solver.ProblemMapping.IsInLocalRange(i)); } var Blk1 = solver.ProblemMapping.GetSubBlocking(Ilist1, csMPI.Raw._COMM.WORLD, compressL1 ? -1 : 0); var Blk2 = solver.ProblemMapping.GetSubBlocking(Ilist2, csMPI.Raw._COMM.WORLD, compressL2 ? -1 : 0); int[] Tlist1 = compressL1 ? default(int[]) : Blk1.GetOccupiedIndicesList(); int[] Tlist2 = compressL2 ? default(int[]) : Blk2.GetOccupiedIndicesList(); if (Tlist1 != null) { Assert.AreEqual(Tlist1.Length, Ilist1.Length); foreach (int i in Tlist1) { Assert.IsTrue(Blk1.IsInLocalRange(i)); } } if (Tlist2 != null) { Assert.AreEqual(Tlist2.Length, Ilist2.Length); foreach (int i in Tlist2) { Assert.IsTrue(Blk2.IsInLocalRange(i)); } } BlockMsrMatrix M11 = new BlockMsrMatrix(Blk1, Blk1); BlockMsrMatrix M12 = new BlockMsrMatrix(Blk1, Blk2); BlockMsrMatrix M21 = new BlockMsrMatrix(Blk2, Blk1); BlockMsrMatrix M22 = new BlockMsrMatrix(Blk2, Blk2); M.AccSubMatrixTo(1.0, M11, Ilist1, Tlist1, Ilist1, Tlist1); M.AccSubMatrixTo(1.0, M12, Ilist1, Tlist1, Ilist2, Tlist2); M.AccSubMatrixTo(1.0, M21, Ilist2, Tlist2, Ilist1, Tlist1); M.AccSubMatrixTo(1.0, M22, Ilist2, Tlist2, Ilist2, Tlist2); BlockMsrMatrix restored_M = new BlockMsrMatrix(M._RowPartitioning, M._ColPartitioning); int[] Idx1 = compressL1 ? Blk1.LocalLength.ForLoop(i => i + Blk1.i0) : Tlist1; int[] Idx2 = compressL2 ? Blk2.LocalLength.ForLoop(i => i + Blk2.i0) : Tlist2; M11.AccSubMatrixTo(1.0, restored_M, Idx1, Ilist1, Idx1, Ilist1); M12.AccSubMatrixTo(1.0, restored_M, Idx1, Ilist1, Idx2, Ilist2); M21.AccSubMatrixTo(1.0, restored_M, Idx2, Ilist2, Idx1, Ilist1); M22.AccSubMatrixTo(1.0, restored_M, Idx2, Ilist2, Idx2, Ilist2); // test transpose-operator var M_TT = M.Transpose().Transpose(); var M11_TT = M11.Transpose().Transpose(); var M12_TT = M12.Transpose().Transpose(); var M21_TT = M21.Transpose().Transpose(); var M22_TT = M22.Transpose().Transpose(); M_TT.Acc(-1.0, M); M11_TT.Acc(-1.0, M11); M12_TT.Acc(-1.0, M12); M21_TT.Acc(-1.0, M21); M22_TT.Acc(-1.0, M22); double M_TT_norm = M_TT.InfNorm(); double M11_TT_norm = M11_TT.InfNorm(); double M12_TT_norm = M12_TT.InfNorm(); double M21_TT_norm = M21_TT.InfNorm(); double M22_TT_norm = M22_TT.InfNorm(); Assert.IsTrue(M_TT_norm == 0.0, "Transpose^2 is not identity."); Assert.IsTrue(M11_TT_norm == 0.0, "Transpose^2 is not identity."); Assert.IsTrue(M12_TT_norm == 0.0, "Transpose^2 is not identity."); Assert.IsTrue(M21_TT_norm == 0.0, "Transpose^2 is not identity."); Assert.IsTrue(M22_TT_norm == 0.0, "Transpose^2 is not identity."); //M.SaveToTextFileSparse(@"C:\tmp\M.txt"); //M11.SaveToTextFileSparse(@"C:\tmp\M11.txt"); //M12.SaveToTextFileSparse(@"C:\tmp\M12.txt"); //M21.SaveToTextFileSparse(@"C:\tmp\M21.txt"); //M22.SaveToTextFileSparse(@"C:\tmp\M22.txt"); //restored_M.SaveToTextFileSparse(@"C:\tmp\Mr.txt"); stw.Stop(); using (var MatlabRef = new BatchmodeConnector()) { MatlabRef.PutVector(Ilist1.Select(i => (double)i + 1.0).ToArray(), "Ilist1"); MatlabRef.PutVector(Ilist2.Select(i => (double)i + 1.0).ToArray(), "Ilist2"); MatlabRef.PutVector(Tlist1 == null ? Ilist1.Length.ForLoop(i => (double)i + 1.0 + Blk1.i0) : Tlist1.Select(i => (double)i + 1.0).ToArray(), "Tlist1"); MatlabRef.PutVector(Tlist2 == null ? Ilist2.Length.ForLoop(i => (double)i + 1.0 + Blk2.i0) : Tlist2.Select(i => (double)i + 1.0).ToArray(), "Tlist2"); MultidimensionalArray CheckRes = MultidimensionalArray.Create(1, 4); MatlabRef.PutSparseMatrix(M, "M"); MatlabRef.PutSparseMatrix(M11, "M11"); MatlabRef.PutSparseMatrix(M12, "M12"); MatlabRef.PutSparseMatrix(M21, "M21"); MatlabRef.PutSparseMatrix(M22, "M22"); MatlabRef.Cmd("L1 = {0};", Blk1.TotalLength); MatlabRef.Cmd("L2 = {0};", Blk2.TotalLength); MatlabRef.Cmd("refM11 = sparse(L1, L1);"); MatlabRef.Cmd("refM12 = sparse(L1, L2);"); MatlabRef.Cmd("refM21 = sparse(L2, L1);"); MatlabRef.Cmd("refM22 = sparse(L2, L2);"); MatlabRef.Cmd("refM11(Tlist1, Tlist1) = M(Ilist1, Ilist1);"); MatlabRef.Cmd("refM12(Tlist1, Tlist2) = M(Ilist1, Ilist2);"); MatlabRef.Cmd("refM21(Tlist2, Tlist1) = M(Ilist2, Ilist1);"); MatlabRef.Cmd("refM22(Tlist2, Tlist2) = M(Ilist2, Ilist2);"); MatlabRef.Cmd("err11 = norm(refM11 - M11, inf);"); MatlabRef.Cmd("err12 = norm(refM12 - M12, inf);"); MatlabRef.Cmd("err21 = norm(refM21 - M21, inf);"); MatlabRef.Cmd("err22 = norm(refM22 - M22, inf);"); MatlabRef.Cmd("CheckRes = [err11, err12, err21, err22];"); MatlabRef.GetMatrix(CheckRes, "CheckRes"); MatlabRef.Execute(); Console.WriteLine("Matlab check 11: " + CheckRes[0, 0]); Console.WriteLine("Matlab check 12: " + CheckRes[0, 1]); Console.WriteLine("Matlab check 21: " + CheckRes[0, 2]); Console.WriteLine("Matlab check 22: " + CheckRes[0, 3]); Assert.IsTrue(CheckRes[0, 0] == 0.0); Assert.IsTrue(CheckRes[0, 1] == 0.0); Assert.IsTrue(CheckRes[0, 2] == 0.0); Assert.IsTrue(CheckRes[0, 3] == 0.0); } stw.Start(); restored_M.Acc(-1.0, M); double err = restored_M.InfNorm(); Console.WriteLine("Submatrix operations error: " + err); Assert.IsTrue(err == 0.0); restored_M.Clear(); restored_M.Acc(1.0, M); IMutuableMatrixEx_Extensions.Acc(restored_M, -1.0, M); double err2 = restored_M.InfNorm(); Console.WriteLine("Submatrix operations error: " + err2); Assert.IsTrue(err2 == 0.0); stw.Stop(); Console.WriteLine("Time spend in matrix operations: " + stw.Elapsed.TotalSeconds + " sec."); TotTime_MatrixOp += stw.Elapsed; } }
public static void SpMVTest( [Values(XDGusage.none, XDGusage.mixed1, XDGusage.mixed2, XDGusage.all)] XDGusage UseXdg, [Values(1, 3)] int DGOrder, [Values(false, true)] bool compressL1, [Values(false, true)] bool compressL2) { unsafe { int[] Params = new int[8], ParamsGlob = new int[8]; fixed(int *pParams = Params, pParamsGlob = ParamsGlob) { pParams[0] = (int)UseXdg; pParams[1] = DGOrder; pParams[2] = compressL1 ? 1 : 0; pParams[3] = compressL2 ? 1 : 0; pParams[4] = -pParams[0]; pParams[5] = -pParams[1]; pParams[6] = -pParams[2]; pParams[7] = -pParams[3]; csMPI.Raw.Allreduce((IntPtr)pParams, (IntPtr)pParamsGlob, 8, csMPI.Raw._DATATYPE.INT, csMPI.Raw._OP.MIN, csMPI.Raw._COMM.WORLD); } int[] ParamsMin = ParamsGlob.GetSubVector(0, 4); int[] ParamsMax = ParamsGlob.GetSubVector(4, 4); for (int i = 0; i < 4; i++) { if (Params[i] != ParamsMin[i]) { throw new ApplicationException(); } if (Params[i] != -ParamsMax[i]) { throw new ApplicationException(); } } Console.WriteLine("SpMVTest({0},{1},{2},{3})", UseXdg, DGOrder, compressL1, compressL2); } using (var solver = new Matrix_MPItestMain() { m_UseXdg = UseXdg, m_DGorder = DGOrder }) { // create the test data // ==================== BoSSS.Solution.Application.CommandLineOptions opts = null; //opts = new BoSSS.Solution.Application.CommandLineOptions(); solver.Init(null, opts); solver.RunSolverMode(); Stopwatch stw = new Stopwatch(); stw.Reset(); BlockMsrMatrix M = solver.OperatorMatrix; double[] B = new double[M.RowPartitioning.LocalLength]; double[] X = new double[M.ColPartition.LocalLength]; Random R = new Random(); for (int i = 0; i < X.Length; i++) { X[i] = R.NextDouble(); } for (int i = 0; i < B.Length; i++) { B[i] = R.NextDouble(); } double[] Bb4 = B.CloneAs(); double RefNorm = B.L2NormPow2().MPISum().Sqrt() * 1e-10; stw.Start(); M.SpMV(1.6, X, 0.5, B); stw.Stop(); //M.SaveToTextFileSparse(@"C:\tmp\M.txt"); //M11.SaveToTextFileSparse(@"C:\tmp\M11.txt"); //M12.SaveToTextFileSparse(@"C:\tmp\M12.txt"); //M21.SaveToTextFileSparse(@"C:\tmp\M21.txt"); //M22.SaveToTextFileSparse(@"C:\tmp\M22.txt"); //M22xM21.SaveToTextFileSparse(@"C:\tmp\M22xM21.txt"); using (var MatlabRef = new BatchmodeConnector()) { MultidimensionalArray CheckRes = MultidimensionalArray.Create(1, 1); MatlabRef.PutSparseMatrix(M, "M"); MatlabRef.PutVector(Bb4, "Bref"); MatlabRef.PutVector(B, "B"); MatlabRef.PutVector(X, "X"); MatlabRef.Cmd("Bref = Bref*0.5 + M*X*1.6;"); MatlabRef.Cmd("errB = norm(B - Bref, 2);"); MatlabRef.Cmd("CheckRes = [errB];"); MatlabRef.GetMatrix(CheckRes, "CheckRes"); MatlabRef.Execute(); Console.WriteLine("Matlab check SpMV: " + CheckRes[0, 0]); Assert.LessOrEqual(CheckRes[0, 0], RefNorm, "Error in SpMV"); } Console.WriteLine("Time spend in matrix operations: " + stw.Elapsed.TotalSeconds + " sec."); TotTime_MatrixOp += stw.Elapsed; } }
public static void SubBlockExtraction( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.diagonal_var_spec, MatrixShape.diagonal_spec, MatrixShape.diagonal_var, MatrixShape.diagonal)] MatrixShape MShape, [Values(4)] int Res ) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape); Console.WriteLine("SubMatrixIgnoreCoupling({0},{1},{2})", UseXdg, DGOrder, MShape); //Arrange --- create test matrix and MG mapping MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; //Arrange --- masking of all external cells var sbs = new SubBlockSelector(map); sbs.AllExternalCellsSelection(); var M_ext = BlockMask.GetAllExternalRows(map, M); var mask = new BlockMask(sbs, M_ext); //bool[] coup = Utils.SetCoupling(MShape); //Arrange --- get index dictonary of all external cell indices Dictionary <int, int[]> Didc = Utils.GetDictOfAllExtCellIdc(map); //Arrange --- stopwatch var stw = new Stopwatch(); stw.Reset(); //Act --- Extract subblocks stw.Start(); //var eblocks = mask.GetSubBlocks(M,coup[0],coup[1],coup[2]); var eblocks = mask.GetDiagonalBlocks(M, false, false); stw.Stop(); //Assert --- same number of blocks? Assert.IsTrue(eblocks.Length == M_ext._RowPartitioning.LocalNoOfBlocks); bool test = eblocks.Length.MPIEquals(); Debug.Assert(test); for (int iBlock = 0; iBlock < eblocks.Length; iBlock++) { var infNorm = MultidimensionalArray.Create(4, 1); int rank = map.MpiRank; int ExtBlockIdx = iBlock + map.AggGrid.iLogicalCells.NoOfLocalUpdatedCells; Didc.TryGetValue(ExtBlockIdx, out int[] idc); using (BatchmodeConnector matlab = new BatchmodeConnector()) { double[] GlobIdx = idc.Count().ForLoop(i => (double)idc[i] + 1.0); Assert.IsTrue(GlobIdx.Length == eblocks[iBlock].Lengths[0]); MsrMatrix M_sub = eblocks[iBlock].ConvertToMsr(); matlab.PutSparseMatrix(M, "M"); // note: M_sub lives on Comm_Self, therefore we have to distinguish between procs ... matlab.PutSparseMatrixRankExclusive(M_sub, "M_sub"); matlab.PutVectorRankExclusive(GlobIdx, "Idx"); matlab.Cmd("M_0 = full(M(Idx_0, Idx_0));"); matlab.Cmd("M_1 = full(M(Idx_1, Idx_1));"); matlab.Cmd("M_2 = full(M(Idx_2, Idx_2));"); matlab.Cmd("M_3 = full(M(Idx_3, Idx_3));"); matlab.Cmd("n=[0; 0; 0; 0];"); matlab.Cmd("n(1,1)=norm(M_0-M_sub_0,inf);"); matlab.Cmd("n(2,1)=norm(M_1-M_sub_1,inf);"); matlab.Cmd("n(3,1)=norm(M_2-M_sub_2,inf);"); matlab.Cmd("n(4,1)=norm(M_3-M_sub_3,inf);"); matlab.GetMatrix(infNorm, "n"); matlab.Execute(); } Assert.IsTrue(infNorm[rank, 0] == 0.0); // } }