FEI Version of the Day
fei_ProcEqns.cpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #include <fei_macros.hpp>
00045 #include <fei_defs.h>
00046 
00047 #include <fei_TemplateUtils.hpp>
00048 #include <fei_ProcEqns.hpp>
00049 
00050 //==============================================================================
00051 ProcEqns::ProcEqns()
00052  : procs_(),
00053    eqnsPerProc_(),
00054    procEqnNumbers_(),
00055    procEqnLengths_()
00056 {
00057 }
00058 
00059 //==============================================================================
00060 ProcEqns::~ProcEqns() {
00061    deleteMemory();
00062 }
00063 
00064 //==============================================================================
00065 ProcEqns* ProcEqns::deepCopy()
00066 {
00067 //
00068 //This function returns a DEEP copy (including all data) of 'this' object.
00069 //
00070    ProcEqns* dest = new ProcEqns;
00071 
00072    int len = procs_.size();
00073 
00074    dest->procs_.resize(len);
00075    dest->eqnsPerProc_.resize(len);
00076 
00077    dest->procEqnNumbers_.resize(len);
00078    dest->procEqnLengths_.resize(len);
00079 
00080    for(int i=0; i<len; i++) {
00081       dest->procs_[i] = procs_[i];
00082       dest->eqnsPerProc_[i] = eqnsPerProc_[i];
00083 
00084       dest->procEqnNumbers_[i] = new std::vector<int>(*(procEqnNumbers_[i]));
00085       dest->procEqnLengths_[i] = new std::vector<int>(*(procEqnLengths_[i]));
00086    }
00087 
00088    return(dest);
00089 }
00090 
00091 //==============================================================================
00092 void ProcEqns::deleteMemory() {
00093   for(unsigned i=0; i<procEqnNumbers_.size(); i++) {
00094     delete procEqnNumbers_[i];
00095     delete procEqnLengths_[i];
00096   }
00097 }
00098 
00099 //==============================================================================
00100 void ProcEqns::addEqn(int eqnNumber, int proc) {
00101 
00102    internalAddEqn(eqnNumber, 0, proc);
00103 }
00104 
00105 //==============================================================================
00106 void ProcEqns::addEqn(int eqnNumber, int eqnLength, int proc) {
00107    internalAddEqn(eqnNumber, eqnLength, proc);
00108 }
00109 
00110 //==============================================================================
00111 void ProcEqns::internalAddEqn(int eqnNumber, int eqnLength, int proc) {
00112 //
00113 //This function adds proc to the recvProcs_ list if it isn't already
00114 //present, and adds eqnNumber to the correct row of the procEqnNumbers_
00115 //table if eqnNumber isn't already in there.
00116 //
00117 
00118   //is proc already in our list of procs?
00119   std::vector<int>::iterator
00120     p_iter = std::lower_bound(procs_.begin(),procs_.end(), proc);
00121 
00122   unsigned offset = p_iter - procs_.begin();
00123 
00124   if (p_iter == procs_.end() || proc != *p_iter) {
00125     //proc was NOT already present, so
00126     //we need to insert it, and insert new rows in the tables
00127     //procEqnNumbers_ and procEqnLengths.
00128 
00129     procs_.insert(p_iter, proc);
00130 
00131     procEqnNumbers_.insert(procEqnNumbers_.begin()+offset,new std::vector<int>(1));
00132     (*(procEqnNumbers_[offset]))[0] = eqnNumber;
00133 
00134     procEqnLengths_.insert(procEqnLengths_.begin()+offset, new std::vector<int>(1));
00135     (*(procEqnLengths_[offset]))[0] = eqnLength;
00136 
00137     eqnsPerProc_.insert(eqnsPerProc_.begin()+offset, 1);
00138   }
00139   else {
00140     //proc was already in the procs_ list.
00141     //is eqnNumber already in our list of eqns for proc?
00142     //if not, add it.
00143 
00144     std::vector<int>& procEqnNums = *(procEqnNumbers_[offset]);
00145     std::vector<int>::iterator pe_iter =
00146       std::lower_bound(procEqnNums.begin(),procEqnNums.end(), eqnNumber);
00147 
00148     unsigned offset2 = pe_iter - procEqnNums.begin();
00149 
00150     if (pe_iter == procEqnNums.end() || eqnNumber != *pe_iter) {
00151       procEqnNumbers_[offset]->insert(procEqnNumbers_[offset]->begin()+offset2,eqnNumber);
00152       procEqnLengths_[offset]->insert(procEqnLengths_[offset]->begin()+offset2,eqnLength);
00153       eqnsPerProc_[offset] = procEqnNumbers_[offset]->size();
00154     }
00155     else {
00156       (*(procEqnLengths_[offset]))[offset2] = eqnLength;
00157     }
00158   }
00159 }
00160 
00161 //==============================================================================
00162 void ProcEqns::setProcEqnLengths(int* eqnNumbers, int* eqnLengths, int len)
00163 {
00164    if (len == 0) return;
00165 
00166    int numProcs = procs_.size();
00167 
00168    for(int i=0; i<numProcs; i++) {
00169       for(int j=0; j<eqnsPerProc_[i]; j++) {
00170          int eqn_j = (*(procEqnNumbers_[i]))[j];
00171          int ins = -1;
00172    int index = fei::binarySearch( eqn_j,
00173               eqnNumbers, len, ins);
00174 
00175          if (index < 0) {
00176             fei::console_out() << "fei: ProcEqns::setProcEqnLengths: ERROR, "
00177                  << eqn_j << " not found." << FEI_ENDL;
00178             std::abort();
00179          }
00180 
00181          (*(procEqnLengths_[i]))[j] = eqnLengths[index];
00182       }
00183    }
00184 }
00185 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends