dense3d_mpi.cpp

Go to the documentation of this file.
00001 /* Kernel Independent Fast Multipole Method
00002    Copyright (C) 2004 Lexing Ying, New York University
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2, or (at your option)
00007 any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00012 for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; see the file COPYING.  If not, write to the Free
00016 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017 02111-1307, USA.  */
00018 
00024 #include "dense3d_mpi.hpp"
00025 #include "common/vecmatop.hpp"
00026 
00027 
00028 /* Constructor for Dense3d_MPI - takes prefix string as argument*/
00029 Dense3d_MPI::Dense3d_MPI(const string& p): 
00030   KnlMat3d_MPI(p), _srcAllPos(NULL), _srcAllNor(NULL)
00031 {
00032 }
00033 /* Destructor for Dense3d_MPI */
00034 Dense3d_MPI::~Dense3d_MPI()
00035 {
00036   if(_srcAllPos!=NULL) {         VecDestroy(_srcAllPos);  }
00037   if(_srcAllNor!=NULL) {         VecDestroy(_srcAllNor);  }
00038 }
00039 
00040 // ----------------------------------------------------------------------
00041 /* Dense3d_MPI setup function scatters source positions and normals
00042  * as necessary */
00043 #undef __FUNCT__
00044 #define __FUNCT__ "Dense3d_MPI::setup"
00045 int Dense3d_MPI::setup()
00046 {
00047   //begin
00048   pA(_srcPos!=NULL && _srcNor!=NULL && _trgPos!=NULL);
00049   //--------------------------------------------------------------------------
00050   /* Create a Scatter context - copies all source position values to each processor
00051         * See http://www-unix.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-current/docs/manualpages/Vec/VecScatterCreateToAll.html */
00052   {
00053          VecScatter ctx;
00054          pC( VecScatterCreateToAll(_srcPos, &ctx, &_srcAllPos) );
00055          pC( VecScatterBegin(_srcPos, _srcAllPos, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00056          pC( VecScatterEnd(  _srcPos, _srcAllPos, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00057          pC( VecScatterDestroy(ctx) );
00058   }
00059   /* Create a Scatter context - copies all source normal values to each processor */
00060   {
00061          VecScatter ctx;
00062          pC( VecScatterCreateToAll(_srcNor, &ctx, &_srcAllNor) );
00063          pC( VecScatterBegin(_srcNor, _srcAllNor, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00064          pC( VecScatterEnd(  _srcNor, _srcAllNor, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00065          pC( VecScatterDestroy(ctx) );
00066   }
00067   return(0);
00068 }
00069 
00070 // ----------------------------------------------------------------------
00071 /* Dense3d_MPI evaluate function does a direct multiplication for a direct solution */
00072 #undef __FUNCT__
00073 #define __FUNCT__ "Dense3d_MPI::evaluate"
00074 int Dense3d_MPI::evaluate(Vec srcDen, Vec trgVal) 
00075 {
00076   //begin
00077   // CHECK
00078   pA(_srcPos!=NULL && _srcNor!=NULL && _trgPos!=NULL && srcDen!=NULL && trgVal!=NULL);
00079   //-----------------------------------
00080   int dim  = this->dim();
00081   int srcDOF = this->srcDOF();
00082   int trgDOF = this->trgDOF();
00083   /* Get global number of source positions */
00084   int srcGlbNum = procGlbNum(_srcPos);
00085   /* Get local number of target positions */
00086   int trgLclNum = procLclNum(_trgPos);
00087   
00088   Vec srcAllPos = _srcAllPos;
00089   Vec srcAllNor = _srcAllNor;
00090   Vec srcAllDen;
00091   /* Create scatter context to scatter source densities to all processors */
00092   {
00093          VecScatter ctx;
00094          pC( VecScatterCreateToAll(srcDen, &ctx, &srcAllDen) );
00095          pC( VecScatterBegin(srcDen, srcAllDen, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00096          pC( VecScatterEnd(  srcDen, srcAllDen, INSERT_VALUES, SCATTER_FORWARD, ctx) );
00097          pC( VecScatterDestroy(ctx) );
00098   }
00099   
00100   Vec trgLclPos = _trgPos;
00101   Vec trgLclVal =  trgVal;
00102 
00103   /* Create matrices for source positions, normals, densities.  See common/nummat.hpp for
00104         * more information on matrices */
00105   double* srcAllPosArr; pC( VecGetArray(srcAllPos, &srcAllPosArr) );
00106   DblNumMat srcAllPosMat(dim, srcGlbNum, false, srcAllPosArr);
00107   double* srcAllNorArr; pC( VecGetArray(srcAllNor, &srcAllNorArr) );
00108   DblNumMat srcAllNorMat(dim, srcGlbNum, false, srcAllNorArr);
00109   double* srcAllDenArr; pC( VecGetArray(srcAllDen, &srcAllDenArr) );
00110   DblNumVec srcAllDenVec(srcDOF*srcGlbNum, false, srcAllDenArr);
00111 
00112   /* Create matrices for target positions and values */
00113   double* trgLclPosArr; pC( VecGetArray(trgLclPos, &trgLclPosArr) );
00114   DblNumMat trgLclPosMat(dim, trgLclNum, false, trgLclPosArr);
00115   double* trgLclValArr; pC( VecGetArray(trgLclVal, &trgLclValArr) );
00116   DblNumVec trgLclValVec(trgDOF*trgLclNum, false, trgLclValArr);
00117 
00118   /* Create an evaluation context and evaluate based on kernel type */
00119   DblNumMat inter(trgDOF, srcGlbNum*srcDOF);
00120   /* Do multiplication one line of the matrices at a time */
00121   for(int i=0; i<trgLclNum; i++) {
00122          DblNumMat onePosMat(dim, 1, false, trgLclPosMat.clmdata(i));
00123          DblNumVec oneValVec(trgDOF, false, trgLclValVec.data()+trgDOF*i);
00124          /* Create kernel multiplier context based on kernel type */
00125          pC( _knl.buildKnlIntCtx(srcAllPosMat, srcAllNorMat, onePosMat, inter) );
00126          /* Computes 1.0*inter*srcAllDenVec + 0.0*oneValVec = oneValVec */
00127          pC( dgemv(1.0, inter, srcAllDenVec, 0.0, oneValVec) );
00128   }
00129   
00130   pC( VecRestoreArray(srcAllPos, &srcAllPosArr) );
00131   pC( VecRestoreArray(srcAllNor, &srcAllNorArr) );
00132   pC( VecRestoreArray(srcAllDen, &srcAllDenArr) );
00133   
00134   pC( VecRestoreArray(trgLclPos, &trgLclPosArr) );
00135   pC( VecRestoreArray(trgLclVal, &trgLclValArr) );
00136   
00137   pC( VecDestroy(srcAllDen) );
00138   
00139   return(0);
00140 }
00141 
00142 

Generated on Sun Dec 4 21:12:40 2005 for fmm3d_mpi by  doxygen 1.4.5