Actual source code: slepc-interface.c

  1: /*
  2:    Modification of the *temp* implementation of the BLOPEX multivector in order
  3:    to wrap created PETSc vectors as multivectors.

  5:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  7:    Copyright (c) 2002-2009, Universidad Politecnica de Valencia, Spain

  9:    This file is part of SLEPc.
 10:       
 11:    SLEPc is free software: you can redistribute it and/or modify it under  the
 12:    terms of version 3 of the GNU Lesser General Public License as published by
 13:    the Free Software Foundation.

 15:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 16:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 17:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 18:    more details.

 20:    You  should have received a copy of the GNU Lesser General  Public  License
 21:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 22:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 23: */

 25: #include "petscsys.h"
 26: #include "petscvec.h"
 27: #include "petscmat.h"
 28: #include <assert.h>
 29: #include <stdlib.h>
 30: #include "petscblaslapack.h"
 31: #include "interpreter.h"
 32: #include "temp_multivector.h"
 33: #include "../src/contrib/blopex/petsc-interface/petsc-interface.h"
 34: #include "slepc-interface.h"

 36: static void* mv_TempMultiVectorCreateFromPETScVector( void* ii_, int n, void* sample )
 37: {
 38:   int i;
 39:   Vec *vecs = (Vec*)sample;

 41:   mv_TempMultiVector* x;
 42:   mv_InterfaceInterpreter* ii = (mv_InterfaceInterpreter*)ii_;

 44:   x = (mv_TempMultiVector*) malloc(sizeof(mv_TempMultiVector));
 45:   assert( x != NULL );
 46: 
 47:   x->interpreter = ii;
 48:   x->numVectors = n;
 49: 
 50:   x->vector = (void**) calloc( n, sizeof(void*) );
 51:   assert( x->vector != NULL );

 53:   x->ownsVectors = 1;
 54:   x->mask = NULL;
 55:   x->ownsMask = 0;

 57:   for ( i = 0; i < n; i++ ) {
 58:     x->vector[i] = (void*)vecs[i];
 59:   }

 61:   return x;
 62: }

 64: static void mv_TempMultiPETSCVectorDestroy( void* x_ )
 65: {
 66:   mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

 68:   if ( x == NULL )
 69:     return;

 71:   if ( x->ownsVectors && x->vector != NULL ) {
 72:        free(x->vector);
 73:   }
 74:   if ( x->mask && x->ownsMask )
 75:     free(x->mask);
 76:   free(x);
 77: }


 80: /*
 81:     Create an InterfaceInterpreter using the PETSc implementation
 82:     but with a modified CreateMultiVector that doesn't create any
 83:     new vector.
 84: */
 85: int SLEPCSetupInterpreter( mv_InterfaceInterpreter *i )
 86: {

 88:   i->CreateVector = PETSC_MimicVector;
 89:   i->DestroyVector = PETSC_DestroyVector;
 90:   i->InnerProd = PETSC_InnerProd;
 91:   i->CopyVector = PETSC_CopyVector;
 92:   i->ClearVector = PETSC_ClearVector;
 93:   i->SetRandomValues = PETSC_SetRandomValues;
 94:   i->ScaleVector = PETSC_ScaleVector;
 95:   i->Axpy = PETSC_Axpy;

 97:   /* Multivector part */

 99:   i->CreateMultiVector = mv_TempMultiVectorCreateFromPETScVector;
100:   i->CopyCreateMultiVector = mv_TempMultiVectorCreateCopy;
101:   i->DestroyMultiVector = mv_TempMultiVectorDestroy;

103:   i->Width = mv_TempMultiVectorWidth;
104:   i->Height = mv_TempMultiVectorHeight;
105:   i->SetMask = mv_TempMultiVectorSetMask;
106:   i->CopyMultiVector = mv_TempMultiVectorCopy;
107:   i->ClearMultiVector = mv_TempMultiVectorClear;
108:   i->SetRandomVectors = mv_TempMultiVectorSetRandom;
109:   i->MultiInnerProd = mv_TempMultiVectorByMultiVector;
110:   i->MultiInnerProdDiag = mv_TempMultiVectorByMultiVectorDiag;
111:   i->MultiVecMat = mv_TempMultiVectorByMatrix;
112:   i->MultiVecMatDiag = mv_TempMultiVectorByDiagonal;
113:   i->MultiAxpy = mv_TempMultiVectorAxpy;
114:   i->MultiXapy = mv_TempMultiVectorXapy;
115:   i->Eval = mv_TempMultiVectorEval;

117:   return 0;
118: }

120: /*
121:     Change the multivector destructor in order to destroy the multivector
122:     structure without destroy the PETSc vectors.
123: */
124: void SLEPCSetupInterpreterForDignifiedDeath( mv_InterfaceInterpreter *i )
125: {
126:   i->DestroyMultiVector = mv_TempMultiPETSCVectorDestroy;
127: }