// Exec_CrdAction::ProcessArgs() Exec::RetType Exec_CrdAction::ProcessArgs(CpptrajState& State, ArgList& argIn) { std::string setname = argIn.GetStringNext(); if (setname.empty()) { mprinterr("Error: %s: Specify COORDS dataset name.\n", argIn.Command()); return CpptrajState::ERR; } DataSet_Coords* CRD = (DataSet_Coords*)State.DSL().FindCoordsSet( setname ); if (CRD == 0) { mprinterr("Error: %s: No COORDS set with name %s found.\n", argIn.Command(), setname.c_str()); return CpptrajState::ERR; } mprintf("\tUsing set '%s'\n", CRD->legend()); // Start, stop, offset TrajFrameCounter frameCount; ArgList crdarg( argIn.GetStringKey("crdframes"), "," ); if (frameCount.CheckFrameArgs( CRD->Size(), crdarg )) return CpptrajState::ERR; frameCount.PrintInfoLine(CRD->legend()); ArgList actionargs = argIn.RemainingArgs(); actionargs.MarkArg(0); Cmd const& cmd = Command::SearchTokenType( DispatchObject::ACTION, actionargs.Command() ); if ( cmd.Empty() ) return CpptrajState::ERR; Action* act = (Action*)cmd.Alloc(); if (act == 0) return CpptrajState::ERR; CpptrajState::RetType err = DoCrdAction(State, actionargs, CRD, act, frameCount); delete act; return err; }
// DataSet_Coords_TRJ::UpdateTrjFrames() int DataSet_Coords_TRJ::UpdateTrjFrames(TrajFrameCounter const& count) { if (count.TotalReadFrames() > 0) IDX_.AddTraj( count.TotalReadFrames(), count.Start(), count.Offset() ); else { mprinterr("Error: Cannot use trajectories with unknown # of frames as data set.\n"); return 1; } return 0; }
Exec::RetType Exec_CrdAction::DoCrdAction(CpptrajState& State, ArgList& actionargs, DataSet_Coords* CRD, Action* act, TrajFrameCounter const& frameCount) const { Timer total_time; total_time.Start(); # ifdef MPI ActionInit state(State.DSL(), State.DFL(), trajComm_); # else ActionInit state(State.DSL(), State.DFL()); # endif if ( act->Init( actionargs, state, State.Debug() ) != Action::OK ) return CpptrajState::ERR; actionargs.CheckForMoreArgs(); // Set up frame and parm for COORDS. ActionSetup originalSetup( CRD->TopPtr(), CRD->CoordsInfo(), CRD->Size() ); Frame originalFrame = CRD->AllocateFrame(); ActionFrame frm( &originalFrame, 0 ); // Set up for this topology Action::RetType setup_ret = act->Setup( originalSetup ); if ( setup_ret == Action::ERR || setup_ret == Action::SKIP ) return CpptrajState::ERR; // Loop over all frames in COORDS. ProgressBar progress( frameCount.TotalReadFrames() ); int set = 0; for (int frame = frameCount.Start(); frame < frameCount.Stop(); frame += frameCount.Offset(), ++set) { progress.Update( set ); CRD->GetFrame( frame, originalFrame ); frm.SetTrajoutNum( set ); Action::RetType ret = act->DoAction( set, frm ); if (ret == Action::ERR) { mprinterr("Error: crdaction: Frame %i, set %i\n", frame + 1, set + 1); break; } // Check if frame was modified. If so, update COORDS. if ( ret == Action::MODIFY_COORDS ) CRD->SetCRD( frame, frm.Frm() ); } # ifdef MPI act->SyncAction(); # endif // Check if parm was modified. If so, update COORDS. if ( setup_ret == Action::MODIFY_TOPOLOGY ) { mprintf("Info: crdaction: Parm for %s was modified by action %s\n", CRD->legend(), actionargs.Command()); CRD->CoordsSetup( originalSetup.Top(), originalSetup.CoordInfo() ); } act->Print(); State.MasterDataFileWrite(); total_time.Stop(); mprintf("TIME: Total action execution time: %.4f seconds.\n", total_time.Total()); return CpptrajState::OK; }
/** Loop over all filenames in replica_filenames, set up TrajectoryIO. */ int TrajIOarray::SetupIOarray(ArgList& argIn, TrajFrameCounter& counter, CoordinateInfo& cInfo, Topology* trajParm) { // Sanity check if (!IOarray_.empty()) { mprinterr("Internal Error: SetupIOarray() has been called twice.\n"); return 1; } // Save arguments that have not been processed so they can be passed // to each replica in turn. Only the lowest replica will use argIn. ArgList argCopy( argIn ); bool lowestRep = true; int rep0Frames = TrajectoryIO::TRAJIN_UNK; // Total frames in replica 0 int totalFrames = TrajectoryIO::TRAJIN_UNK; // Total # frames to be read from ensemble TrajectoryFile::TrajFormatType lastRepFmt = TrajectoryFile::UNKNOWN_TRAJ; // Right now enforce that all replicas have the same metadata as lowest // replica, e.g. if replica 0 has temperature, replica 1 does too etc. for (File::NameArray::const_iterator repfile = replica_filenames_.begin(); repfile != replica_filenames_.end(); ++repfile) { // Detect format TrajectoryFile::TrajFormatType repformat = TrajectoryFile::UNKNOWN_TRAJ; TrajectoryIO* replica0 = TrajectoryFile::DetectFormat( *repfile, repformat ); if ( replica0 == 0 ) { mprinterr("Error: Could not set up replica file %s\n", repfile->full()); return 1; } if (repformat != lastRepFmt) mprintf("\tReading '%s' as %s\n", repfile->full(), TrajectoryFile::FormatString(repformat)); lastRepFmt = repformat; replica0->SetDebug( debug_ ); // Pushing replica0 here allows the destructor to handle it on errors IOarray_.push_back( replica0 ); // Process format-specific read args. Do not exit on error in case // replicas have different formats supporting different args. if (lowestRep) { replica0->processReadArgs( argIn ); } else { ArgList argtmp( argCopy ); replica0->processReadArgs( argtmp ); } // Set up replica for reading and get the number of frames. int nframes = replica0->setupTrajin( *repfile, trajParm ); if (nframes == TrajectoryIO::TRAJIN_ERR) { mprinterr("Error: Could not set up %s for reading.\n", repfile->full()); return 1; } // TODO: Do not allow unknown number of frames? if (lowestRep) { cInfo = replica0->CoordInfo(); rep0Frames = nframes; totalFrames = nframes; if (cInfo.ReplicaDimensions().Ndims() > 0) { mprintf("\tReplica dimensions:\n"); for (int rd = 0; rd < cInfo.ReplicaDimensions().Ndims(); rd++) mprintf("\t\t%i: %s\n", rd+1, cInfo.ReplicaDimensions().Description(rd)); } } else { // Check total frames in this replica against lowest rep. if (nframes != rep0Frames) mprintf("Warning: Replica %s frames (%i) does not match # frames in first replica (%i).\n", repfile->base(), nframes, rep0Frames); //if (repframes < 0) { // mprinterr("Error: RemdTraj: Unknown # of frames in replica.\n"); // return 1; //} if (nframes < totalFrames) { totalFrames = nframes; mprintf("Warning: Setting total # of frames to read from replica ensemble to %i\n", totalFrames); } // Check box info against lowest rep. if ( replica0->CoordInfo().HasBox() != cInfo.HasBox() ) { mprinterr("Error: Replica %s box info does not match first replica.\n", repfile->full()); return 1; } // TODO: Check specific box type // Check velocity info against lowest rep. if ( replica0->CoordInfo().HasVel() != cInfo.HasVel() ) { mprinterr("Error: Replica %s velocity info does not match first replica.\n", repfile->full()); return 1; } // Check # dimensions and types against lowest rep if ( replica0->CoordInfo().ReplicaDimensions() != cInfo.ReplicaDimensions() ) { mprinterr("Error: Replica %s dimension info does not match first replica.\n", repfile->full()); ReplicaDimArray const& thisRepDims = replica0->CoordInfo().ReplicaDimensions(); for (int rd = 0; rd < thisRepDims.Ndims(); rd++) mprinterr("\t\t%i: %s\n", rd+1, thisRepDims.Description(rd)); return 1; } // If temperature/time info does not match set to false. if (cInfo.HasTemp() != replica0->CoordInfo().HasTemp()) cInfo.SetTemperature( false ); if (cInfo.HasTime() != replica0->CoordInfo().HasTime()) cInfo.SetTime( false ); } lowestRep = false; } // Check how many frames will actually be read if (counter.CheckFrameArgs( totalFrames, argIn )) return 1; // Check for errors. if (IOarray_.empty()) { mprinterr("Error: No replica trajectories set up.\n"); return 1; } if (IOarray_.size() != replica_filenames_.size()) { // SANITY CHECK mprinterr("Error: Not all replica files were set up.\n"); return 1; } // Update ensemble size cInfo.SetEnsembleSize( (int)IOarray_.size() ); if (debug_ > 0) cInfo.PrintCoordInfo( replica_filenames_[0].full(), trajParm->c_str() ); return 0; }
/** Each rank only sets up file that it will process. */ int TrajIOarray::SetupIOarray(ArgList& argIn, TrajFrameCounter& counter, CoordinateInfo& cInfo, Topology* trajParm, Parallel::Comm const& ensComm, Parallel::Comm const& trajComm) { // Sanity check if (!IOarray_.empty()) { mprinterr("Internal Error: SetupIOarray() has been called twice.\n"); return 1; } // Detect format FileName const& repFname = replica_filenames_[ensComm.Rank()]; TrajectoryFile::TrajFormatType repformat = TrajectoryFile::UNKNOWN_TRAJ; TrajectoryIO* replica0 = TrajectoryFile::DetectFormat( repFname, repformat ); if ( replica0 == 0 ) { mprinterr("Error: Could not set up replica file %s\n", repFname.full()); return 1; } mprintf("\tReading '%s' as %s\n", repFname.full(), TrajectoryFile::FormatString(repformat)); replica0->SetDebug( debug_ ); // Construct the IOarray_ with blanks for all except this rank. for (int member = 0; member != ensComm.Size(); member++) if (member == ensComm.Rank()) IOarray_.push_back( replica0 ); else IOarray_.push_back( 0 ); // Process format-specific read args. replica0->processReadArgs( argIn ); // Set up replica for reading and get # frames int nframes = replica0->setupTrajin( repFname, trajParm ); if (nframes == TrajectoryIO::TRAJIN_ERR) { mprinterr("Error: Could not set up %s for reading.\n", repFname.full()); return 1; } // Set coordinate info cInfo = replica0->CoordInfo(); int totalFrames = nframes; if (cInfo.ReplicaDimensions().Ndims() > 0) { // TODO put in common routine mprintf("\tReplica dimensions:\n"); for (int rd = 0; rd < cInfo.ReplicaDimensions().Ndims(); rd++) mprintf("\t\t%i: %s\n", rd+1, cInfo.ReplicaDimensions().Description(rd)); } // Check # frames in all files, use lowest. Parallel::World().AllReduce( &totalFrames, &nframes, 1, MPI_INT, MPI_MIN ); if (totalFrames != nframes) { rprintf("Warning: Replica '%s' frames (%i) is > # frames in shortest replica.\n", repFname.full(), nframes); mprintf("Warning: Setting total # of frames to read from replica ensemble to %i\n", totalFrames); } if (trajComm.Master()) { static const int iSize = 6; static const char* iTitle[iSize] = {"box", "velocity", "temperature", "time", "force", "replica dimensions"}; // Check coordinate info of all files 0 1 2 3 4 5 std::vector<int> Info( iSize * ensComm.Size() ); // box, vel, temp, time, force, nRepDims int rank_info[iSize]; rank_info[0] = (int)cInfo.TrajBox().Type(); rank_info[1] = (int)cInfo.HasVel(); rank_info[2] = (int)cInfo.HasTemp(); rank_info[3] = (int)cInfo.HasTime(); rank_info[4] = (int)cInfo.HasForce(); rank_info[5] = cInfo.ReplicaDimensions().Ndims(); ensComm.AllGather( rank_info, iSize, MPI_INT, &Info[0] ); // TODO Should mismatches be errors instead? for (int midx = 0; midx != iSize; midx++) { for (int ridx = midx + iSize; ridx < (int)Info.size(); ridx += iSize) { if (Info[midx] != Info[ridx]) { rprintf("Warning: Replica %i %s info does not match first replica.\n", ridx/iSize, iTitle[midx]); } } } } // TODO: Put code below into a common routine with serial version // Check how many frames will actually be read if (counter.CheckFrameArgs( totalFrames, argIn )) return 1; // SANITY CHECK if (IOarray_.size() != replica_filenames_.size()) { mprinterr("Error: Not all replica files were set up.\n"); return 1; } // Update ensemble size cInfo.SetEnsembleSize( (int)IOarray_.size() ); if (debug_ > 0) cInfo.PrintCoordInfo( repFname.full(), trajParm->c_str() ); return 0; }
Exec::RetType Exec_CrdAction::DoCrdAction(CpptrajState& State, ArgList& actionargs, DataSet_Coords* CRD, Action* act, TrajFrameCounter const& frameCount) const { Timer total_time; total_time.Start(); # ifdef MPI ActionInit state(State.DSL(), State.DFL(), trajComm_); # else ActionInit state(State.DSL(), State.DFL()); # endif if ( act->Init( actionargs, state, State.Debug() ) != Action::OK ) return CpptrajState::ERR; actionargs.CheckForMoreArgs(); // Set up frame and parm for COORDS. ActionSetup originalSetup( CRD->TopPtr(), CRD->CoordsInfo(), CRD->Size() ); Frame originalFrame = CRD->AllocateFrame(); // Set up for this topology Action::RetType setup_ret = act->Setup( originalSetup ); if ( setup_ret == Action::ERR || setup_ret == Action::SKIP ) return CpptrajState::ERR; // If the topology was modified, we will need a new COORDS set. DataSet_Coords* crdOut = 0; if ( setup_ret == Action::MODIFY_TOPOLOGY ) { // This will not work for a TRJ set. switch ( CRD->Type() ) { case DataSet::TRAJ : mprinterr("Error: Cannot modify TRAJ data sets.\n"); break; case DataSet::COORDS : crdOut = (DataSet_Coords*)new DataSet_Coords_CRD(); break; case DataSet::REF_FRAME : crdOut = (DataSet_Coords*)new DataSet_Coords_REF(); break; default: crdOut = 0; // SANITY } if (crdOut == 0) return CpptrajState::ERR; mprintf("Info: crdaction: COORDS set '%s' will be modified by action '%s'\n", CRD->legend(), actionargs.Command()); if (frameCount.TotalReadFrames() != (int)CRD->Size()) mprintf("Info: crdaction: Previous size= %zu, new size is %i\n", CRD->Size(), frameCount.TotalReadFrames()); // Set up set, copy original metadata crdOut->SetMeta( CRD->Meta() ); if (crdOut->CoordsSetup( originalSetup.Top(), originalSetup.CoordInfo() )) return CpptrajState::ERR; DataSet::SizeArray mfArray(1, frameCount.TotalReadFrames()); if (crdOut->Allocate( mfArray )) return CpptrajState::ERR; } // Loop over all frames in COORDS. ProgressBar* progress = 0; if (State.ShowProgress()) progress = new ProgressBar( frameCount.TotalReadFrames() ); int set = 0; for (int frame = frameCount.Start(); frame < frameCount.Stop(); frame += frameCount.Offset(), ++set) { // Since Frame can be modified by actions, save original and use currentFrame ActionFrame frm( &originalFrame, set ); if (progress != 0) progress->Update( set ); CRD->GetFrame( frame, originalFrame ); Action::RetType ret = act->DoAction( set, frm ); if (ret == Action::ERR) { mprinterr("Error: crdaction: Frame %i, set %i\n", frame + 1, set + 1); break; } // Check if frame was modified. If so, update COORDS. if ( ret == Action::MODIFY_COORDS ) { if (crdOut != 0) crdOut->AddFrame( frm.Frm() ); else CRD->SetCRD( frame, frm.Frm() ); } } if (progress != 0) delete progress; # ifdef MPI act->SyncAction(); # endif // If topology was modified, replace old set with new. if ( setup_ret == Action::MODIFY_TOPOLOGY ) { mprintf("Info: crdaction: Topology for '%s' was modified by action '%s'\n", CRD->legend(), actionargs.Command()); State.DSL().RemoveSet( CRD ); State.DSL().AddSet( crdOut ); } act->Print(); State.MasterDataFileWrite(); total_time.Stop(); mprintf("TIME: Total action execution time: %.4f seconds.\n", total_time.Total()); return CpptrajState::OK; }