// file: $isip/class/asr/Splitter/Splitter.h
// version: $Id: Splitter.h 10636 2007-01-26 22:18:09Z tm334 $
//

// make sure definitions are only made once
//
#ifndef ISIP_SPLITTER
#define ISIP_SPLITTER

#ifndef ISIP_STRING
#include <String.h>
#endif

#ifndef ISIP_VECTOR
#include <Vector.h>
#endif

#ifndef ISIP_VECTOR_DOUBLE
#include <VectorDouble.h>
#endif

#ifndef ISIP_FILENAME
#include <Filename.h>
#endif

#ifndef ISIP_SINGLE_LINKED_LIST
#include <SingleLinkedList.h>
#endif

#ifndef ISIP_SDB
#include <Sdb.h>
#endif

#ifndef ISIP_NAME_MAP
#include <NameMap.h>
#endif

#ifndef ISIP_MACHINE_DATABASE
#include <MachineDatabase.h>
#endif

// Splitter: a class used to split a file list and launch jobs on machines
//
class Splitter {
  
  //--------------------------------------------------------------------------
  //
  // public constants
  //
  //--------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------

  // define the algorithm choices
  //
  enum SPLIT_MODE { NONE = 0, SPEED, MEMORY, DEF_SPLIT_MODE = NONE };

  // define the static NameMap objects
  //
  static const NameMap SPLIT_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_SPLIT_MODE;
  static const String PARAM_MACHINE_DATABASE;
  static const String PARAM_CLUSTER_EMPIRE;
  static const String PARAM_CLUSTER_MAVERICK;
  static const String PARAM_CLUSTER_TALUS;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
  protected:

  // declare the split mode
  //
  SPLIT_MODE split_mode_d;

  // declare the machine database
  //
  MachineDatabase mdb_d;
  
  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_d;
  
  // a static memory manager
  //
  static MemoryManager mgr_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
  
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }
  
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods
  //
  boolean debug(const unichar* msg) const;

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG arg) {
    debug_level_d = arg;
    return true;
  }
  
  // method: destructor
  //
  ~Splitter() {
    clear();  
  }

  // method: default constructor
  //
  Splitter();

  // method: copy constructor
  //
  Splitter(const Splitter& arg) {
    assign(arg);
  }

  // method: assign
  //
  boolean assign(const Splitter& arg);
  
  // method: sofSize
  //
  long sofSize() const {
    return mdb_d.sofSize();
  }

  // method: read
  //
  boolean read(Sof& sof, long tag) {
    return read(sof, tag, name());
  }

  // other read methods
  //
  boolean read(Sof& sof, long tag, const String& name);
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT, boolean param = true,
		   boolean nested = false);
  
  // method: write
  //
  boolean write(Sof& sof, long tag) const {
    return write(sof, tag, name());
  }

  // other write methods
  //
  boolean write(Sof& sof, long tag, const String& name) const;
  boolean writeData(Sof& sof, const String& pname =  DEF_PARAM) const;
  
  // method: eq
  //
  boolean eq(const Splitter& arg) const;

  // method: new
  //
  static void* operator new(size_t arg) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t arg) {
    return mgr_d.getBlock(arg);
  }

  // method: delete
  //
  static void operator delete(void* arg) {
    mgr_d.release(arg);
  }

  // method: delete[]
  //
  static void operator delete[](void* arg) {
    mgr_d.releaseBlock(arg);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long arg) {
    return mgr_d.setGrow(arg);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //
  //---------------------------------------------------------------------------

  // method: getSplitMode
  //
  SPLIT_MODE getSplitMode() {
    return split_mode_d;
  }

  // method: setSplitMode
  //
  boolean setSplitMode(SPLIT_MODE arg) {
    return (split_mode_d = arg);
  }  

  // method: getMachineDatabase
  //
  MachineDatabase& getMachineDatabase() {
    return mdb_d;
  }
  
  // method: setMachineDatabase
  //
  boolean setMachineDatabase(MachineDatabase& arg) {
    return mdb_d.assign(arg);
  }
  
  // method to divide the main utterance list
  //
  boolean divideAndConquer(Filename& identifier_file,
			   Filename& machine_list,
			   Vector<SingleLinkedList<Filename> >& items);

  // method to generate the divided utterance lists
  //
  boolean createUtterLists(Filename& identifier_file,
			   Filename& identifier_list,
			   Filename& machine_list,
			   Filename& accumulator_list,
			   Vector<SingleLinkedList<Filename> >& items,
			   Vector<Filename>& identifier_lookup,
			   Vector<Machine>& machine_lookup,
			   Vector<Filename>& accumulator_lookup);
  
  // method to split a list of jobs and submit it to the cluster
  //
  boolean splitJobList(Filename& input_file,
				 Filename& cpus,
				 String& command_to_execute,
				 String& walltime,
				 String& batch_command,
                                 Sdb& sdb_list,
				 String& cluster_name);
  
  // method to poll all the log files created by PBS
  //
  boolean pollLogFile(Vector<String>& log_files);
  
  // method to accumulate all the log files created by PBS
  //
  boolean waitAndAccumulate(Vector<String>& log_files,
				      String& proc_id_str);
  
  // method to send email after the job completion
  //
  boolean sendEmail(Long& start_time, Long& finish_time,
			      String& begin_time,
			      String& end_time,
			      String& proc_id_str);
  
  // method to get the difference between two times
  //
  String getDiffTime(Long& start_time,
			       Long& finish_time);

  // method to generate script file
  //
  boolean generateScript(File& scripts_file,
				   String& scripts_filename,
			           String& out_scripts,
				   String& clustername,
				   int list_length,
				   int reminder_list,
				   Filename& inputfile,
				   Sdb& sdb_list,
				   long job_index,
				   String& command_to_execute,
				   String& wall_time);
  // method to generate the command for job submission
  //
  boolean generateCommand(String& log_filename,
				    String& scripts_filename,
				    String& command,
				    String& cluster_name);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:
  
};

//end of include file
//
#endif
