matviewstruct.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2006-2010 Werner Scholz
00005 
00006     www:   http://www.magpar.net/
00007     email: magpar(at)magpar.net
00008 
00009     magpar is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     magpar is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with magpar; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 static char const Id[] = "$Id: matviewstruct.c 2962 2010-02-04 19:50:44Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "util.h"
00028 
00029 int matviewstruct(MPI_Comm comm,Mat mat)
00030 {
00031   MagparFunctionLogBegin;
00032 
00033   int rank,size;
00034   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00035   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00036 
00037   PetscInt m,n;
00038   ierr = MatGetSize(mat,&m,&n);CHKERRQ(ierr);
00039 
00040   ierr = PetscViewerSetFormat(PETSC_VIEWER_STDOUT_SELF,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
00041   ierr = MatView(mat,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
00042 
00043   if (m!=n) {
00044     PetscPrintf(PETSC_COMM_WORLD,"Matrix not square, cannot calculate bandwidth.\n");
00045     MagparFunctionLogReturn(0);
00046   }
00047 
00048 /*
00049   ierr = MatView(mat,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
00050 */
00051 /* very slow if running in parallel
00052   ierr = MatView(mat,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);
00053   MagparFunctionLogReturn(0);
00054 */
00055 #if 0
00056   /* does not work 1 */
00057   Mat matloc;
00058   IS  isrow,iscol;
00059   ierr = MatGetSubMatrix(mat,isrow,iscol,n,MAT_INITIAL_MATRIX,&matloc);CHKERRQ(ierr);
00060 #elif 0
00061   /* does not work 2 */
00062   PetscInt from,to;
00063   ierr = MatGetOwnershipRange(mat,&from,&to);CHKERRQ(ierr);
00064 
00065   IS  isrow,iscol;
00066   ierr = ISCreateStride(PETSC_COMM_WORLD,to-from,from,1,&isrow);CHKERRQ(ierr);
00067   ierr = ISCreateStride(PETSC_COMM_WORLD,n,1,1,&iscol);CHKERRQ(ierr);
00068 
00069   Mat matloc;
00070   MatConvert(mat,MATSEQAIJ,MAT_INITIAL_MATRIX,&matloc);
00071 
00072 /* this is the problem:
00073 [1]PETSC ERROR: MatCreate_SeqAIJ() line 2864 in src/mat/impls/aij/seq/aij.c
00074 [1]PETSC ERROR: Argument out of range!
00075 [1]PETSC ERROR: Comm must be of size 1!
00076 [1]PETSC ERROR: MatSetType() line 64 in src/mat/interface/matreg.c
00077 [1]PETSC ERROR: MatConvert() line 3141 in src/mat/interface/matrix.c
00078 */
00079 
00080   ierr = ISDestroy(isrow);CHKERRQ(ierr);
00081   ierr = ISDestroy(iscol);CHKERRQ(ierr);
00082 #elif 0
00083   /* does not work 3 */
00084   Mat matloc;
00085   ierr = MatGetLocalMat(mat,MAT_INITIAL_MATRIX,&matloc);CHKERRQ(ierr);
00086 /*
00087   ierr = MatView(matloc,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);
00088   ierr = MatDestroy(matloc);CHKERRQ(ierr);
00089 */
00090 #else
00091   PetscInt from,to;
00092   ierr = MatGetOwnershipRange(mat,&from,&to);CHKERRQ(ierr);
00093 
00094   IS  isrow,iscol;
00095   ierr = ISCreateStride(comm,to-from,from,1,&isrow);CHKERRQ(ierr);
00096   ierr = ISCreateStride(comm,n,1,1,&iscol);CHKERRQ(ierr);
00097 
00098   Mat *submatloc;
00099   ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&submatloc);CHKERRQ(ierr);
00100 
00101   ierr = ISDestroy(isrow);CHKERRQ(ierr);
00102   ierr = ISDestroy(iscol);CHKERRQ(ierr);
00103 
00104   Mat matloc;
00105   matloc=submatloc[0];
00106 #endif
00107 
00108   /* calculate bandwidth */
00109 
00110   PetscInt *ia,*ja;
00111   PetscTruth done;
00112   ierr = MatGetRowIJ(matloc,0,PETSC_TRUE,PETSC_FALSE,&from,&ia,&ja,&done);CHKERRQ(ierr);
00113 
00114   /* assume that we get useful data back (not true on 2nd,...,nth processor)
00115   assert(done==PETSC_TRUE);
00116 */
00117 
00118   /* assume square matrix, no I-nodes used */
00119   int cmin,cmax,bw,bwmax;
00120   bwmax=0;
00121   for (int i=0;i<m;i++) {
00122     cmin=m;
00123     cmax=0;
00124     for (int j=0;j<(ia[i+1]-ia[i]);j++) {
00125       cmin=PetscMin(cmin,ja[ia[i]+j]);
00126       cmax=PetscMax(cmax,ja[ia[i]+j]);
00127     }
00128     bw=cmax-cmin+1;
00129     if (bw > bwmax) {
00130       bwmax=bw;
00131       n=i;
00132     }
00133   }
00134   PetscPrintf(comm,"max. bandwidth: %i in row %i (of %i)\n",bwmax,n,m);
00135 
00136   ierr = MatRestoreRowIJ(matloc,0,PETSC_TRUE,PETSC_FALSE,&from,&ia,&ja,&done);CHKERRQ(ierr);
00137 
00138 #ifdef PETSC_HAVE_X11
00139   if (!rank) ierr = MatView(matloc,PETSC_VIEWER_DRAW_SELF);CHKERRQ(ierr);
00140 #endif
00141 
00142   ierr = MatDestroyMatrices(1,&submatloc);CHKERRQ(ierr);
00143 
00144   MagparFunctionLogReturn(0);
00145 }
00146 

magpar - Parallel Finite Element Micromagnetics Package
Copyright (C) 2002-2010 Werner Scholz