예제 #1
0
파일: Utils.cs 프로젝트: rohitvuppala/BoSSS
        public static bool[] SetCoupling(MatrixShape shape)
        {
            bool[] coupling = new bool[2];
            switch (shape)
            {
            case MatrixShape.diagonal:
            case MatrixShape.full:
                coupling = new bool[] { true, true };
                break;

            case MatrixShape.diagonal_var:
            case MatrixShape.full_var:
                coupling = new bool[] { false, true };
                break;

            case MatrixShape.diagonal_spec:
            case MatrixShape.full_spec:
                coupling = new bool[] { true, false };
                break;

            case MatrixShape.diagonal_var_spec:
            case MatrixShape.full_var_spec:
                coupling = new bool[] { false, false };
                break;

            default:
                throw new NotSupportedException(String.Format("{0} is not supported by this test", shape));
            }
            return(coupling);
        }
예제 #2
0
        public double[,,,] forward(double[,,,] input)
        {
            //获取样本的总数
            int sampleCount = input.GetLength(0);
            //获取单个样本的深度
            int sampleSingleDepth = input.GetLength(1);
            //获取特征图的行数(高)
            int inputRow = input.GetLength(2);
            //获取特征图的列数(宽)
            int inputColumn = input.GetLength(3);

            //创建最大值的
            SingleMaxIndex = new int[sampleCount][][];
            int row    = (inputRow - PadRow) / Stride + 1;
            int column = (inputColumn - PadColumn) / Stride + 1;
            var result = new double[sampleCount, sampleSingleDepth, row, column];

            for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
            {
                //初始化特征图最大索引对象
                SingleMaxIndex[sampleIndex] = new int[sampleSingleDepth][];
                //记录特征图的行和列信息
                InputShape = new MatrixShape(inputRow, inputColumn);
                for (int depth = 0; depth < sampleSingleDepth; depth++)
                {
                    LMatrix pad = im2col(input.GetNextDimVal(sampleIndex, depth, inputRow, inputColumn), row, column, PadRow, PadColumn, Stride);
                    SingleMaxIndex[sampleIndex][depth] = pad.MaxIndex();
                    //LMatrix data = pad.Matrix.Select(m => m.Max()).ToArray();
                    LMatrix data = pad.Max(1);
                    result.SetDimVal(data.ReShape(row, column), sampleIndex, depth, row, column);
                }
            }
            return(result);
        }
예제 #3
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);
        }
예제 #4
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!"));
        }
예제 #5
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!"));
        }
예제 #6
0
        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!"));
        }
예제 #7
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);
        }
예제 #8
0
파일: Utils.cs 프로젝트: rohitvuppala/BoSSS
        public static void SetDefaultSplitSelection(this SubBlockSelector sbs, MatrixShape shape, bool upper, bool islocal = true)
        {
            switch (shape)
            {
            case MatrixShape.diagonal:
            case MatrixShape.full:
                sbs.DefaultCellSplit(upper, islocal);
                break;

            case MatrixShape.diagonal_var:
            case MatrixShape.full_var:
                sbs.DefaultSpeciesSplit(upper);
                if (!islocal)
                {
                    sbs.AllExternalCellsSelection();
                }
                break;

            case MatrixShape.diagonal_spec:
            case MatrixShape.full_spec:
                sbs.DefaultVarSplit(upper);
                if (!islocal)
                {
                    sbs.AllExternalCellsSelection();
                }
                break;

            case MatrixShape.diagonal_var_spec:
            case MatrixShape.full_var_spec:
                sbs.DefaultCellSplit(upper, islocal);
                break;

            default:
                throw new NotSupportedException(String.Format("{0} is not supported by this test", shape));
            }
        }
예제 #9
0
파일: Utils.cs 프로젝트: rohitvuppala/BoSSS
        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);
        }
예제 #10
0
파일: Utils.cs 프로젝트: rohitvuppala/BoSSS
 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));
 }
예제 #11
0
파일: Utils.cs 프로젝트: rohitvuppala/BoSSS
        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);
        }
예제 #12
0
        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?
        }
예제 #13
0
        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);
        }
예제 #14
0
        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);
        }
예제 #15
0
        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); //
            }
        }