/****************************************************************************
**  SCALASCA    http://www.scalasca.org/                                   **
*****************************************************************************
**  Copyright (c) 1998-2012                                                **
**  Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre          **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/


#include <cstddef>

#include <Synchronizer.h>

#include "SynchronizeTask.h"

using namespace std;
using namespace scout;

/*
 *---------------------------------------------------------------------------
 *
 * class SynchronizeTask
 *
 *---------------------------------------------------------------------------
 */

//--- Constructors & destructor ---------------------------------------------

/**
 * @brief Constructor. Creates a new synchronizer task.
 *
 * This constructor creates a new timestamp synchronization task working on
 * the given trace data object @p trace.
 *
 * @param  trace  Local trace data object (smart pointer)
 */
SynchronizeTask::SynchronizeTask(const LocalTracePtr& trace)
  : Task(),
    m_trace(trace),
    m_synchronizer(NULL)
{
}


/**
 * @brief Destructor.
 *
 * Destructor. Destroys the instance and releases all occupied resources.
 */
SynchronizeTask::~SynchronizeTask()
{
  delete m_synchronizer;
}


//--- Execution control -----------------------------------------------------

/**
 * @brief Executes the task.
 *
 * Performs the timestamp synchronization based on the controlled logical
 * clock algorithm.
 *
 * @return Always @em true
 */
bool SynchronizeTask::execute()
{
  int rank = m_trace->get_location()->get_process()->get_id();
  m_synchronizer = new Synchronizer(rank, *m_trace);
  m_synchronizer->synchronize();

  return true;
}


//--- Printing statistics ---------------------------------------------------

/**
 * @brief Prints some synchronizer-related statistics.
 *
 * This method prints some additional statistics about the timestamp
 * synchronization process. In case the synchronizer task has not been
 * called, it defaults to a no-op.
 */
void SynchronizeTask::print_statistics() const
{
  if (m_synchronizer)
    m_synchronizer->print_statistics();
}
