parallel/pico/FieldExchanger.cpp

00001 // -*- C++ -*-
00002 //
00003 //  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00004 //
00005 //                                   Fehmi Cirak
00006 //                        California Institute of Technology
00007 //                           (C) 2004 All Rights Reserved
00008 //
00009 //  <LicenseText>
00010 //
00011 //  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00012 //
00013 // EDITED BY MATT GALLOWAY FOR USE WITH DAPTA
00014 
00015 #include "DAPTA.hpp"
00016 
00017 #include <cassert>
00018 
00019 namespace DAPTA { // Define namespace DAPTA
00020 
00021 namespace pico { // Define namespace pico
00022 
00023 template <typename V>
00024 FieldExchanger<V>::FieldExchanger(DomainCoupler<V>* coupler, std::vector<int>::iterator lGbegin, int nodes) {
00025    _coupler = coupler;
00026    //_coupler->registerSendBuffer<int>(RED, &(*lGbegin), nodes);
00027    _coupler->template registerSendBuffer<int>(RED, &(*lGbegin), nodes);
00028    _coupler->exchangeSubdomainDataIntra(RED); 
00029    
00030    //int importedNodes = _coupler->recvBufferSize<int>(RED);
00031    int importedNodes = _coupler->template recvBufferSize<int>(RED);
00032    assert(importedNodes>0);
00033    _importedNodeIDs.reserve(importedNodes);
00034    
00035    const int notUsed=0;
00036    int *tmp = _coupler->recvBuffer(RED, notUsed);
00037    
00038    for (int i=0; i<importedNodes; ++i) {
00039       std::vector<int>::iterator it = std::find(lGbegin, lGbegin+nodes, tmp[i]); 
00040       int localID = std::distance(lGbegin, it);
00041       if (localID<nodes) {
00042          _importedNodeIDs.push_back(localID);
00043       } else { 
00044          _importedNodeIDs.push_back(-1);
00045       }
00046    }
00047 }
00048 
00049 template <typename V>
00050 template <int NVAR, typename FT, template <typename> class OP>
00051 void FieldExchanger<V>::exchange(FT *field, int fsize) {    
00052    // NVAR - number of variables for each node
00053    // FT - type of the field (double/int)
00054    // OP - operation to be performend, e.g. std::plus<FT>
00055    
00056    _coupler->registerSendBuffer(RED, field, fsize);
00057    _coupler->exchangeSubdomainDataIntra(RED);
00058    
00059    const FT notUsed(0);
00060    FT *tmp = _coupler->recvBuffer(RED, notUsed);
00061    
00062    OP<FT> operation;
00063    std::vector<int>::iterator it = _importedNodeIDs.begin();
00064    std::vector<int>::iterator ite = _importedNodeIDs.end();
00065    for (int i=0; it!=ite; ++it, ++i) {
00066       if (*it==-1) continue;
00067       for (int j=0; j<NVAR; ++j) {
00068          assert((NVAR*(*it)+j)<fsize);
00069          field[NVAR*(*it)+j] = operation(field[NVAR*(*it)+j], tmp[NVAR*i+j]);
00070       }
00071    }
00072 }
00073    
00074 } // namespace pico
00075 
00076 } // namespace DAPTA

Generated on Tue May 29 17:13:49 2007 for DAPTA by  doxygen 1.5.1