Skip to content

Commit

Permalink
few base classes and implementations for the log-det framework added
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday committed Jun 20, 2013
1 parent d0e18e5 commit cacb172
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/shogun/lib/computation/job/DenseExactLogJob.cpp
@@ -0,0 +1,92 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Soumyajit De
*/

#include <shogun/lib/common.h>

#ifdef HAVE_EIGEN3
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/lib/computation/job/ScalarResult.h>
#include <shogun/mathematics/logdet/DenseMatrixOperator.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/lib/computation/job/DenseExactLogJob.h>

using namespace Eigen;

namespace shogun
{

CDenseExactLogJob::CDenseExactLogJob()
: CIndependentJob(), m_log_operator(NULL)
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

CDenseExactLogJob::CDenseExactLogJob(CJobResultAggregator* aggregator)
: CIndependentJob(aggregator), m_log_operator(NULL)
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

CDenseExactLogJob::~CDenseExactLogJob()
{
SG_UNREF(m_log_operator);
SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

void CDenseExactLogJob::compute()
{
SG_DEBUG("Entering...\n")

// apply the log to m_vector
SGVector<float64_t> vec=m_log_operator->apply(m_vector);

// compute the vector-vector dot product using Eigen3
Map<VectorXd> v(vec.vector, vec.vlen);
Map<VectorXd> s(m_vector.vector, m_vector.vlen);

CScalarResult<float64_t>* result=new CScalarResult<float64_t>(s.dot(v));
SG_REF(result);
m_aggregator->submit_result(result);
SG_UNREF(result);

SG_DEBUG("Leaving...\n")
}

SGVector<float64_t> CDenseExactLogJob::get_vector() const
{
return m_vector;
}

void CDenseExactLogJob::set_vector(SGVector<float64_t> vec)
{
m_vector=vec;
}

CDenseMatrixOperator<float64_t>* CDenseExactLogJob::get_operator() const
{
return m_log_operator;
}

void CDenseExactLogJob::set_operator(CDenseMatrixOperator<float64_t>* op)
{
// DEBUG COMMENT: SG_REF should increase here!!
// Its important that we first increase the refcount and then
// decrease it. Param might be same, for example, and unref-ing it
// first may accidentally delete it!
// The following is surely a weird way, but works!
CDenseMatrixOperator<float64_t>* old_op=m_log_operator;
SG_UNREF(m_log_operator);
m_log_operator=op;
SG_REF(m_log_operator);
SG_UNREF(old_op);
}

}
#endif // HAVE_EIGEN3
70 changes: 70 additions & 0 deletions src/shogun/lib/computation/job/DenseExactLogJob.h
@@ -0,0 +1,70 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Soumyajit De
*/

#ifndef DENSE_EXACT_LOG_JOB_H_
#define DENSE_EXACT_LOG_JOB_H_

#include <shogun/lib/config.h>

#ifdef HAVE_EIGEN3
#include <shogun/lib/computation/job/IndependentJob.h>

namespace shogun
{
template<class T> class SGVector;
template<class T> class CDenseMatrixOperator;

/** @brief Class that represents the job of applying the log of
* a CDenseMatrixOperator on a real vector
*/
class CDenseExactLogJob : public CIndependentJob
{
public:
/** default constructor, no args */
CDenseExactLogJob();

/** default constructor, one arg */
CDenseExactLogJob(CJobResultAggregator* aggregator);

/** destructor */
virtual ~CDenseExactLogJob();

/** implementation of compute method for the job */
virtual void compute();

/** get the vector */
SGVector<float64_t> get_vector() const;

/** set the vector */
void set_vector(SGVector<float64_t> vec);

/** get the linear operator */
CDenseMatrixOperator<float64_t>* get_operator() const;

/** set the linear operator */
void set_operator(CDenseMatrixOperator<float64_t>* op);

/** @return object name */
virtual const char* get_name() const
{
return "CDenseExactLogJob";
}

private:
/** the log of a CDenseMatrixOperator<float64_t> */
CDenseMatrixOperator<float64_t>* m_log_operator;

/** the trace-sample */
SGVector<float64_t> m_vector;
};

}

#endif // HAVE_EIGEN3
#endif // DENSE_EXACT_LOG_JOB_H_
67 changes: 67 additions & 0 deletions src/shogun/lib/computation/job/IndependentJob.h
@@ -0,0 +1,67 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Soumyajit De
*/

#ifndef INDEPENDENT_JOB_H_
#define INDEPENDENT_JOB_H_

#include <shogun/lib/config.h>
#include <shogun/base/SGObject.h>
#include <shogun/lib/computation/job/JobResultAggregator.h>

namespace shogun
{

/** @brief Abstract base for general computation jobs to be registered
* in CIndependentComputationEngine. compute method produces a job result
* and submits it to the internal JobResultAggregator. Each set of jobs
* that form a result will share the same job result aggregator.
*/
class CIndependentJob : public CSGObject
{
public:
/** default constructor, no args */
CIndependentJob()
: CSGObject(), m_aggregator(NULL)
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

/** default constructor, one arg */
CIndependentJob(CJobResultAggregator* aggregator)
: CSGObject(), m_aggregator(aggregator)
{
SG_REF(m_aggregator);
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

/** destructor */
virtual ~CIndependentJob()
{
SG_UNREF(m_aggregator);
SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

/** abstract compute method that computes the job, creates a CJobResult,
* submits the result to the job result aggregator
*/
virtual void compute() = 0;

/** @return object name */
virtual const char* get_name() const
{
return "CIndependentJob";
}
protected:
/** the job result aggregator for the current job */
CJobResultAggregator* m_aggregator;
};

}

#endif // INDEPENDENT_JOB_H_
70 changes: 70 additions & 0 deletions src/shogun/lib/computation/job/JobResultAggregator.h
@@ -0,0 +1,70 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Soumyajit De
*/

#ifndef JOB_RESULT_AGGREGATOR_H_
#define JOB_RESULT_AGGREGATOR_H_

#include <shogun/lib/config.h>
#include <shogun/base/SGObject.h>
#include <shogun/lib/computation/job/JobResult.h>

namespace shogun
{

/** @brief Abstract base class that provides an interface for computing an
* aggeregation of the job results of independent computation jobs as
* they are submitted and also for finalizing the aggregation.
*/
class CJobResultAggregator : public CSGObject
{
public:
/** default constructor */
CJobResultAggregator()
: CSGObject(), m_result(NULL)
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

/** destructor */
virtual ~CJobResultAggregator()
{
SG_UNREF(m_result);
SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

/** abstract method that submits the result of an independent job, and
* computes the aggregation with the previously submitted result
* @param the result of an independent job
*/
virtual void submit_result(CJobResult* result) = 0;

/** abstract method that finalizes the aggregation and computes the result,
* its necessary to call finalize before getting the final result
*/
virtual void finalize() = 0;

/** @return the final result */
CJobResult* get_final_result() const
{
return m_result;
}

/** @return object name */
virtual const char* get_name() const
{
return "CJobResultAggregator";
}
protected:
/** the final job result */
CJobResult* m_result;
};

}

#endif // JOB_RESULT_AGGREGATOR_H_
78 changes: 78 additions & 0 deletions src/shogun/lib/computation/job/StoreScalarAggregator.cpp
@@ -0,0 +1,78 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Soumyajit De
*/

#include <shogun/lib/common.h>
#include <shogun/lib/computation/job/ScalarResult.h>
#include <shogun/lib/computation/job/StoreScalarAggregator.h>

namespace shogun
{
template<class T>
CStoreScalarAggregator<T>::CStoreScalarAggregator()
: CJobResultAggregator(), m_aggregate(static_cast<T>(0))
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
}

template<class T>
CStoreScalarAggregator<T>::~CStoreScalarAggregator()
{
SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
}

template<class T>
void CStoreScalarAggregator<T>::submit_result(CJobResult* result)
{
SG_GCDEBUG("Entering ...\n")

// check for proper typecast
// DEBUG COMMENT : the following does NOT increase refcount of result
CScalarResult<T>* new_result=dynamic_cast<CScalarResult<T>*>(result);
if (!new_result)
SG_ERROR("result is not of CScalarResult type!\n");
// aggregate it with previous
m_aggregate+=new_result->get_result();

SG_GCDEBUG("Leaving ...\n")
}

template<>
void CStoreScalarAggregator<bool>::submit_result(CJobResult* result)
{
SG_NOTIMPLEMENTED
}

template<>
void CStoreScalarAggregator<char>::submit_result(CJobResult* result)
{
SG_NOTIMPLEMENTED
}

template<class T>
void CStoreScalarAggregator<T>::finalize()
{
m_result=(CJobResult*)(new CScalarResult<T>(m_aggregate));
SG_REF(m_result);
}

template class CStoreScalarAggregator<bool>;
template class CStoreScalarAggregator<char>;
template class CStoreScalarAggregator<int8_t>;
template class CStoreScalarAggregator<uint8_t>;
template class CStoreScalarAggregator<int16_t>;
template class CStoreScalarAggregator<uint16_t>;
template class CStoreScalarAggregator<int32_t>;
template class CStoreScalarAggregator<uint32_t>;
template class CStoreScalarAggregator<int64_t>;
template class CStoreScalarAggregator<uint64_t>;
template class CStoreScalarAggregator<float32_t>;
template class CStoreScalarAggregator<float64_t>;
template class CStoreScalarAggregator<floatmax_t>;
template class CStoreScalarAggregator<complex64_t>;
}

0 comments on commit cacb172

Please sign in to comment.