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-2013, Universitat Politecnica de Valencia, Spain

  9:    This file is part of SLEPc.

 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 <petscvec.h>
 26: #include <stdlib.h>
 27: #include <blopex_interpreter.h>
 28: #include <blopex_temp_multivector.h>
 29: #include "slepc-interface.h"

 31: static void* mv_TempMultiVectorCreateFromPETScVector(void* ii_,BlopexInt n,void* sample)
 32: {
 33:   int i;
 34:   Vec *vecs = (Vec*)sample;

 36:   mv_TempMultiVector* x;
 37:   mv_InterfaceInterpreter* ii = (mv_InterfaceInterpreter*)ii_;

 39:   x = (mv_TempMultiVector*)malloc(sizeof(mv_TempMultiVector));
 40:   if (!x) SETERRABORT(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Allocation for x failed");

 42:   x->interpreter = ii;
 43:   x->numVectors = n;

 45:   x->vector = (void**)calloc(n,sizeof(void*));
 46:   if (!x->vector) SETERRABORT(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Allocation for x->vector failed");

 48:   x->ownsVectors = 1;
 49:   x->mask = NULL;
 50:   x->ownsMask = 0;

 52:   for (i=0;i<n;i++) {
 53:     x->vector[i] = (void*)vecs[i];
 54:   }
 55:   return x;
 56: }

 58: static void mv_TempMultiPETSCVectorDestroy(void* x_)
 59: {
 60:   mv_TempMultiVector* x = (mv_TempMultiVector*)x_;

 62:   if (!x) return;

 64:   if (x->ownsVectors && x->vector) free(x->vector);
 65:   if (x->mask && x->ownsMask) free(x->mask);
 66:   free(x);
 67: }

 69: /*
 70:     Create an InterfaceInterpreter using the PETSc implementation
 71:     but overloading CreateMultiVector that doesn't create any
 72:     new vector.
 73: */
 74: int SLEPCSetupInterpreter(mv_InterfaceInterpreter *i)
 75: {
 76:   PETSCSetupInterpreter(i);
 77:   i->CreateMultiVector = mv_TempMultiVectorCreateFromPETScVector;

 79:   return 0;
 80: }

 82: /*
 83:     Change the multivector destructor in order to destroy the multivector
 84:     structure without destroy the PETSc vectors.
 85: */
 86: void SLEPCSetupInterpreterForDignifiedDeath(mv_InterfaceInterpreter *i)
 87: {
 88:   i->DestroyMultiVector = mv_TempMultiPETSCVectorDestroy;
 89: }