ACLVISPtclBaseDataSource.hpp 11.6 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
 * 
 * This program was prepared by PSI. 
 * All rights in the program are reserved by PSI.
 * Neither PSI nor the author(s)
 * makes any warranty, express or implied, or assumes any liability or
 * responsibility for the use of this software
 *
 * Visit www.amas.web.psi for more details
 *
 ***************************************************************************/

// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
 * 
 *
 * Visit http://people.web.psi.ch/adelmann/ for more details
 *
 ***************************************************************************/

// include files
#include "DataSource/ACLVISPtclBaseDataSource.h"
#include "DataSource/ACLVISOperations.h"
#include "DataSource/ACLVISDataConnect.h"
#include "DataSource/PtclAttribDataSource.h"
31
#include "Particle/IpplParticleBase.h"
gsell's avatar
gsell committed
32 33
#include "Message/Communicate.h"
#include "Utility/IpplInfo.h"
adelmann's avatar
adelmann committed
34
 
uldis_l's avatar
uldis_l committed
35

gsell's avatar
gsell committed
36 37 38 39 40


////////////////////////////////////////////////////////////////////////////
// constructor: name, connection method, transfer method, pbase
template<class PLayout>
41
ACLVISIpplParticleBaseDataSource<PLayout>::ACLVISIpplParticleBaseDataSource(const
gsell's avatar
gsell committed
42
								    char *nm,
43 44
         DataConnect *dc, int tm, IpplParticleBase<PLayout>& PB)
  : IpplParticleBaseDataSource(nm, dc, tm, &PB), MyIpplParticleBase(PB),
gsell's avatar
gsell committed
45 46 47
    IDMap(&IDMapA), NewIDMap(&IDMapB)
{

48
  // make sure we do not have a IpplParticleBase with positions greater than
gsell's avatar
gsell committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  // 3 dimensions
  CTAssert(PLayout::Dimension < 4);

  // then check to make sure we're not trying to make a connection that we
  // don't support in this class
  std::string filestring = "aclvis";
  if (std::string(dc->DSID()) != filestring) {
    ERRORMSG("Illegal DataConnect object for ACLVIS Data Object." << endl);
    ACLVISConnection = 0;
    Connection = 0;
  } else if (tm != DataSource::OUTPUT) {
    ERRORMSG("ACLVIS data connections may only be of type OUTPUT." << endl);
    ACLVISConnection = 0;
    Connection = 0;
  } else {
    ACLVISConnection = dynamic_cast<ACLVISDataConnect *>(dc);
    checkin();
  }
}


////////////////////////////////////////////////////////////////////////////
// destructor
template<class PLayout>
73
ACLVISIpplParticleBaseDataSource<PLayout>::~ACLVISIpplParticleBaseDataSource() {
uldis_l's avatar
uldis_l committed
74

gsell's avatar
gsell committed
75 76 77 78 79 80
}


////////////////////////////////////////////////////////////////////////////
// make a connection using the given attribute.  Return success.
template<class PLayout>
81
bool ACLVISIpplParticleBaseDataSource<PLayout>::connect_attrib(
gsell's avatar
gsell committed
82 83 84 85 86 87 88 89 90 91
	   ParticleAttribDataSource *pa) {

  // on parent node, establish connection to ACLVIS for this base+attrib pair
  if (getConnection()->onConnectNode() && connected())
    // call ACLVIS API to register this data object
    ACLVISConnection->getConnection()->connect((void *)pa,
		       (char *)(pa->name()),
		       (ReadParticleTool *)(pa->getConnectStorage()),
		       ParticleDataType);

92
  return IpplParticleBaseDataSource::connect_attrib(pa);
gsell's avatar
gsell committed
93 94 95 96 97 98 99
}


////////////////////////////////////////////////////////////////////////////
// disconnect from the external agency the connection involving this
// particle base and the given attribute.  Return success.
template<class PLayout>
100
bool ACLVISIpplParticleBaseDataSource<PLayout>::disconnect_attrib(
gsell's avatar
gsell committed
101 102 103 104 105 106 107 108 109 110
	   ParticleAttribDataSource *pa) {

  // on parent node, remove connection to ACLVIS for this base+attrib pair
  if (getConnection()->onConnectNode() && connected())
#ifdef IPPL_LUX
    ACLVISConnection->getConnection()->disconnect((char *)(pa->name()));
#else
    ACLVISConnection->getConnection()->disconnect((void *)pa);
#endif

111
  return IpplParticleBaseDataSource::disconnect_attrib(pa);
gsell's avatar
gsell committed
112 113 114 115
}


////////////////////////////////////////////////////////////////////////////
116
// check to see if the given ParticleAttrib is in this IpplParticleBase's
gsell's avatar
gsell committed
117 118
// list of registered attributes.  Return true if this is so.
template<class PLayout>
119
bool ACLVISIpplParticleBaseDataSource<PLayout>::has_attrib(
gsell's avatar
gsell committed
120 121
	    ParticleAttribBase *pa) {

122 123 124
  // go through the list of registered attributes in our IpplParticleBase ...
  typename IpplParticleBase<PLayout>::attrib_iterator attr=MyIpplParticleBase.begin();
  typename IpplParticleBase<PLayout>::attrib_iterator endattr=MyIpplParticleBase.end();
gsell's avatar
gsell committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
  for ( ; attr != endattr; ++attr)
    if (pa == *attr)
      return true;

  // if here, not found
  return false;
}


////////////////////////////////////////////////////////////////////////////
// Indicate to the receiver that we're allowing them time to manipulate the
// data (e.g., for a viz program, to rotate it, change representation, etc.)
// This should only return when the manipulation is done.
template<class PLayout>
139
void ACLVISIpplParticleBaseDataSource<PLayout>::interact(const char *str) {
uldis_l's avatar
uldis_l committed
140 141
  
  
gsell's avatar
gsell committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  // on parent node, hand off control to ACLVIS API.  If a command string
  // is given, instead call the viz API function to execute it as an
  // interactive command.
  if (getConnection()->onConnectNode() && connected()) {
    if (str != 0 && *str != 0) {
#ifndef IPPL_LUX
      ACLVISConnection->getConnection()->InterpretCommand(str);
#endif
    } else {
#ifdef IPPL_LUX
      ACLVISConnection->getConnection()->Interact();
#else
      ACLVISConnection->getConnection()->Interact(1);
#endif
    }
  }
}


////////////////////////////////////////////////////////////////////////////
// Update the object, that is, make sure the receiver of the data has a
// current and consistent snapshot of the current state.  Return success.
template<class PLayout>
166
bool ACLVISIpplParticleBaseDataSource<PLayout>::update() {
uldis_l's avatar
uldis_l committed
167 168
  
  
gsell's avatar
gsell committed
169 170 171 172 173 174 175 176 177 178 179 180

  //Inform dbgmsg("APB::update", INFORM_ALL_NODES);
  //dbgmsg << "Starting update on node " << Ippl::myNode() << endl;

  // only need to do update if we have connected attribs
  if (begin_attrib() == end_attrib())
    return true;

  // get the next tag for communication
  int n, tag = Ippl::Comm->next_tag(DS_PB_TAG, DS_CYCLE);
  unsigned N = Ippl::getNodes();
  unsigned myN = Ippl::myNode();
181 182
  unsigned locN = MyIpplParticleBase.getLocalNum();
  unsigned totN = MyIpplParticleBase.getTotalNum();
gsell's avatar
gsell committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  unsigned currIndx = 0;

  typename AttribList_t::iterator attr;
  typename AttribList_t::iterator endattr;

  // clear out the id list
  NewIDMap->erase(NewIDMap->begin(), NewIDMap->end());

  // when running in parallel, we must send all data to parent nodes
  //dbgmsg << "About to send all the messages out ..." << endl;
  for (n=0; n < getConnection()->getNodes(); ++n) {
    if (myN != n) {
      // put number of local particles into message
      Message *msg = new Message;
      msg->put(locN);

      if (locN > 0) {
	// put position and ID data into a message
201 202
	MyIpplParticleBase.R.putMessage(*msg, locN, 0);
	MyIpplParticleBase.ID.putMessage(*msg, locN, 0);
gsell's avatar
gsell committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	// put attribute data into a message
	for (attr=begin_attrib(), endattr=end_attrib();
	     attr != endattr;
	     ++attr)
	  (*attr)->putMessage(msg);
      }

      // send the data to parent
      //dbgmsg << "Sending " << locN << " particles to node " << n << endl;
      Ippl::Comm->send(msg, n, tag);
    }
  }

  // on the parent nodes, put in our own data and the data from other
  // nodes
  //dbgmsg << "About to receive in all the messages ..." << endl;
  if (getConnection()->onConnectNode()) {

    // on master nodes, prepare all the attributes for new data
    for (attr=begin_attrib(),endattr=end_attrib(); attr!=endattr; ++attr)
      (*attr)->prepare_data(true, totN);

    // process a message or local data from each node
    for (n=0; n < N; ++n) {
      if (myN == n) {
	// put in local data
	if (locN > 0) {
	  //dbgmsg << "Inserting local data ..." << endl;
	  for (attr=begin_attrib(),endattr=end_attrib();attr!=endattr;++attr){
	    (*attr)->insert_data(locN, currIndx, 0);
234 235
	    insert_pos(locN, currIndx, &(MyIpplParticleBase.R[0]),
		       &(MyIpplParticleBase.ID[0]), *attr);
gsell's avatar
gsell committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	  }
	}

	// increment index, which indicates where the next particles go
	currIndx += locN;

      } else {
	// now receive message from current other node
	Message *msg = Ippl::Comm->receive_block(n, tag);
	unsigned remN;
	msg->get(remN);

	// extract attrib data into agency-specific data structure
	if (remN > 0) {
	  // get coordinate attribute from message
	  typename PLayout::SingleParticlePos_t* posdata =
	    (typename PLayout::SingleParticlePos_t *)(msg->remove());
	  typename PLayout::Index_t* iddata =
	    (typename PLayout::Index_t *)(msg->remove());

	  // get non-position attribute data from message, and put all the
	  // data into the ACLVIS data structures
	  //dbgmsg<<"Inserting remote data from node " << n << " ..." << endl;
	  for (attr=begin_attrib(),endattr=end_attrib();attr!=endattr;++attr){
	    (*attr)->insert_data(remN, currIndx, msg);
	    insert_pos(remN, currIndx, posdata, iddata, *attr);
	  }

	  // free the memory used to store position and ID values
	  free(static_cast<void *>(posdata));
	  free(static_cast<void *>(iddata));

	  // increment index, which indicates where the next particles go
	  currIndx += remN;
	}

	// we're done with this node
	delete msg;
      }
    }

    // tell ACLVIS API there is new data
    for (attr=begin_attrib(),endattr=end_attrib(); attr!=endattr; ++attr) {
      // on parent node, call ACLVIS API to update data for each attrib.
      (*attr)->prepare_data(false, totN);
#ifdef IPPL_LUX
      ACLVISConnection->getConnection()->update((char *)((*attr)->name()));
#else
      ACLVISConnection->getConnection()->update((void *)(*attr));
#endif
    }
  }

  // swap ID lists
  IDMap_t *tmpmap = IDMap;
  IDMap = NewIDMap;
  NewIDMap = tmpmap;

  // report success
  return true;
}


////////////////////////////////////////////////////////////////////////////
// copy data out of the given coordinates iterator into the data structure
// for the given ParticleAttribDataSource which represents the agency-
// specific storage.  Arguments = number of particles, starting index,
// pointer to value, pointer to ID's, and ParticleAttribDataSource
template<class PLayout>
305
void ACLVISIpplParticleBaseDataSource<PLayout>::insert_pos(
gsell's avatar
gsell committed
306 307 308
		      unsigned N, unsigned sIndx,
		      typename PLayout::SingleParticlePos_t* data,
		      typename PLayout::Index_t* iddata,
uldis_l's avatar
uldis_l committed
309
		      ParticleAttribDataSource* pa) { 
gsell's avatar
gsell committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

  //  Inform dbgmsg("APB::insert_pos", INFORM_ALL_NODES);

  // get ACLVIS data storage location for particle coordinates
  ReadParticleTool *vizdata =
    static_cast<ReadParticleTool *>(pa->getConnectStorage());

  // copy in the data from the storage
  unsigned maxIndx = sIndx + N;
  for ( ; sIndx < maxIndx; ++sIndx, ++data, ++iddata) {
    // set position for the Nth particle
    ACLVISTraits<ReadParticleTool, typename PLayout::SingleParticlePos_t>::
      setCoord(vizdata, sIndx, *data);

    // find where this particle was located during the last update
    typename IDMap_t::iterator idloc = IDMap->find(*iddata);
    int prevloc = (-1);
    if (idloc != IDMap->end())
      prevloc = (*idloc).second;

    // set ID information for the Nth particle
    ACLVISTraits<ReadParticleTool, typename PLayout::Index_t>::
      setID(vizdata, sIndx, prevloc);

    // save where this item is located in the list
    (*NewIDMap)[*iddata] = sIndx;

    //    dbgmsg << "  Inserted particle position " << sIndx << " = ";
    //    dbgmsg << *data << " with ID = " << prevloc << endl;
  }
}


/***************************************************************************
 * $RCSfile: ACLVISPtclBaseDataSource.cpp,v $   $Author: adelmann $
 * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:24 $
 * IPPL_VERSION_ID: $Id: ACLVISPtclBaseDataSource.cpp,v 1.1.1.1 2003/01/23 07:40:24 adelmann Exp $ 
 ***************************************************************************/