/*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SLEPc - Scalable Library for Eigenvalue Problem Computations
Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
This file is part of SLEPc.
SLEPc is free software: you can redistribute it and/or modify it under the
terms of version 3 of the GNU Lesser General Public License as published by
the Free Software Foundation.
SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
static char help[] = "Tests MatDenseReduce.\n\n";
#include <slepcmatdense.h>
#undef __FUNCT__
#define __FUNCT__ "main"
int main( int argc, char **argv )
{
MatDense A,B; /* matrices */
PetscScalar *v;
PetscInt n=45,m,i,j;
PetscBool flag,verbose = PETSC_FALSE;
PetscErrorCode ierr;
SlepcInitialize(&argc,&argv,(char*)0,help);
ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);CHKERRQ(ierr);
if(!flag) m=n;
ierr = PetscOptionsGetBool(PETSC_NULL,"-verbose",&verbose,&flag);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"\nDense matrices of size %Dx%D\n\n",n,m);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create matrices
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = MatDenseCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatDenseSetMaxSizes(A,m,n);CHKERRQ(ierr);
ierr = MatDenseSetSizes(A,0,0,m,n);CHKERRQ(ierr);
ierr = MatDenseSetFromOptions(A);CHKERRQ(ierr);
ierr = MatDenseSetUpPreallocation(A);CHKERRQ(ierr);
ierr = MatDenseDuplicate(A,MATDENSE_DO_NOT_COPY_VALUES,&B);CHKERRQ(ierr);
ierr = MatDenseSetFromOptions(B);CHKERRQ(ierr);
ierr = MatDenseSetUpPreallocation(B);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Initialize matrices
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = MatDenseGetArray(A,&v);CHKERRQ(ierr);
for (j=0; j<n; j++) {
for (i=0; i<m; i++) {
v[m*j+i] = 1.0;
}
}
ierr = MatDenseRestoreArray(A,&v);CHKERRQ(ierr);
if (verbose) {
ierr = MatDenseView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
ierr = MatDenseGetArray(B,&v);CHKERRQ(ierr);
for (j=0; j<n; j++) {
for (i=0; i<m; i++) {
v[m*j+i] = i % 2 ? 1.0 : 2.0;
}
}
ierr = MatDenseRestoreArray(B,&v);CHKERRQ(ierr);
if (verbose) {
ierr = MatDenseView(B,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Test operations
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = MatDenseReduce(A,MPIU_SUM);CHKERRQ(ierr);
ierr = MatDenseReduce(B,MPIU_SUM);CHKERRQ(ierr);
ierr = MatDenseAXPY(B,1.0,A);CHKERRQ(ierr);
if (verbose) {
ierr = MatDenseView(B,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
ierr = MatDenseDestroy(&A);CHKERRQ(ierr);
ierr = MatDenseDestroy(&B);CHKERRQ(ierr);
ierr = SlepcFinalize();CHKERRQ(ierr);
return 0;
}