Skip to content

Commit

Permalink
make unref() protected
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed May 9, 2012
1 parent 608f708 commit 49462e7
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/shogun/classifier/QDA.cpp
Expand Up @@ -61,7 +61,7 @@ void CQDA::cleanup()

m_covs.free_ndarray();
m_M.free_ndarray();
m_means.unref();
m_means=SGMatrix<float64_t>();

m_num_classes = 0;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ bool CQDA::train_machine(CFeatures* data)
wrap_dgesvd(jobu, jobvt, m, n, buffer.matrix, lda, col, NULL, ldu,
rot_mat, ldvt, &info);
ASSERT(info == 0);
buffer.unref();
buffer=SGMatrix<float64_t>();

CMath::vector_multiply(col, col, col, m_dim);
CMath::scale_vector(1.0/(m-1), col, m_dim);
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/clustering/GMM.cpp
Expand Up @@ -106,8 +106,8 @@ void CGMM::cleanup()
for (int32_t i = 0; i < m_components.vlen; i++)
SG_UNREF(m_components.vector[i]);

m_components.unref();
m_coefficients.unref();
m_components=SGVector<CGaussian*>();
m_coefficients=SGVector<float64_t>();
}

bool CGMM::train(CFeatures* data)
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/DenseFeatures.cpp
Expand Up @@ -64,7 +64,7 @@ template<class ST> void CDenseFeatures<ST>::free_features()
template<class ST> void CDenseFeatures<ST>::free_feature_matrix()
{
m_subset_stack->remove_all_subsets();
feature_matrix.unref();
feature_matrix=SGMatrix<ST>();
num_vectors = 0;
num_features = 0;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ template<class ST> void CDenseFeatures<ST>::free_feature_vector(ST* feat_vec, in
template<class ST> void CDenseFeatures<ST>::free_feature_vector(SGVector<ST> vec, int32_t num)
{
free_feature_vector(vec.vector, num, false);
vec.unref();
vec=SGVector<ST>();
}

template<class ST> void CDenseFeatures<ST>::vector_subset(int32_t* idx, int32_t idx_len)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/Labels.cpp
Expand Up @@ -176,7 +176,7 @@ void CLabels::load(CFile* loader)
remove_subset();

SG_SET_LOCALE_C;
labels.unref();
labels=SGVector<float64_t>();
ASSERT(loader);
loader->get_vector(labels.vector, labels.vlen);
SG_RESET_LOCALE;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/TOPFeatures.cpp
Expand Up @@ -71,7 +71,7 @@ void CTOPFeatures::set_models(CHMM* p, CHMM* n)
neg=n;
set_num_vectors(0);

feature_matrix.unref();
feature_matrix=SGMatrix<float64_t>();

if (pos && pos->get_observations())
set_num_vectors(pos->get_observations()->get_num_vectors());
Expand Down
11 changes: 10 additions & 1 deletion src/shogun/kernel/CustomKernel.cpp
Expand Up @@ -89,6 +89,15 @@ CCustomKernel::CCustomKernel(SGMatrix<float64_t> km)
SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
}

CCustomKernel::CCustomKernel(SGMatrix<float32_t> km)
: CKernel(10), upper_diagonal(false)
{
SG_DEBUG("Entering CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
init();
set_full_kernel_matrix_from_full(km);
SG_DEBUG("Leaving CCustomKernel::CCustomKernel(SGMatrix<float64_t>)\n");
}

CCustomKernel::~CCustomKernel()
{
SG_DEBUG("Entering CCustomKernel::~CCustomKernel()\n");
Expand Down Expand Up @@ -131,7 +140,7 @@ void CCustomKernel::cleanup_custom()
remove_all_row_subsets();
remove_all_col_subsets();

kmatrix.unref();
kmatrix=SGMatrix<float32_t>();
upper_diagonal=false;

SG_DEBUG("Leaving CCustomKernel::cleanup_custom()\n");
Expand Down
13 changes: 10 additions & 3 deletions src/shogun/kernel/CustomKernel.h
Expand Up @@ -54,6 +54,15 @@ class CCustomKernel: public CKernel
*/
CCustomKernel(SGMatrix<float64_t> km);

/** constructor
*
* sets full kernel matrix from full kernel matrix
* (from double precision floats)
*
* @param km kernel matrix
*/
CCustomKernel(SGMatrix<float32_t> km);

/**
*
*/
Expand Down Expand Up @@ -280,11 +289,9 @@ class CCustomKernel: public CKernel
upper_diagonal = false;

for (int64_t i=0; i<int64_t(rows) * cols; i++)
{
kmatrix.matrix[i]=full_kernel_matrix.matrix[i];
}

dummy_init(rows, cols);
dummy_init(kmatrix.num_rows, kmatrix.num_cols);
return true;
}

Expand Down
53 changes: 26 additions & 27 deletions src/shogun/lib/SGReferencedData.h
Expand Up @@ -67,6 +67,32 @@ class SGReferencedData

#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", *m_refcount, this);
#endif
return *m_refcount;
}
#endif //USE_REFERENCE_COUNTING

protected:
void copy_refcount(const SGReferencedData &orig)
{
m_refcount=orig.m_refcount;
}

#ifdef USE_REFERENCE_COUNTING
/** increase reference counter
*
* @return reference count
*/
int32_t ref()
{
if (m_refcount == NULL)
{
return -1;
}

++(*m_refcount);
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", *m_refcount, this);
#endif
return *m_refcount;
}
Expand Down Expand Up @@ -106,33 +132,6 @@ class SGReferencedData
return c;
}
}

#endif //USE_REFERENCE_COUNTING

protected:
void copy_refcount(const SGReferencedData &orig)
{
m_refcount=orig.m_refcount;
}

#ifdef USE_REFERENCE_COUNTING
/** increase reference counter
*
* @return reference count
*/
int32_t ref()
{
if (m_refcount == NULL)
{
return -1;
}

++(*m_refcount);
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", *m_refcount, this);
#endif
return *m_refcount;
}
#endif //USE_REFERENCE_COUNTING

/** needs to be overridden to copy data */
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/machine/KernelMachine.cpp
Expand Up @@ -189,8 +189,8 @@ SGVector<float64_t> CKernelMachine::get_alphas()

bool CKernelMachine::create_new_model(int32_t num)
{
m_alpha.unref();
m_svs.unref();
m_alpha=SGVector<float64_t>();
m_svs=SGVector<int32_t>();

m_bias=0;

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/machine/KernelMulticlassMachine.h
Expand Up @@ -30,7 +30,7 @@ class CKernelMulticlassMachine : public CMulticlassMachine
/** default constructor */
CKernelMulticlassMachine() : CMulticlassMachine(), m_kernel(NULL)
{
m_parameters->add((CSGObject**)&m_kernel,"m_kernel");
SG_ADD((CSGObject**)&m_kernel,"m_kernel", "The kernel", MS_AVAILABLE);
}

/** standard constructor
Expand All @@ -43,7 +43,7 @@ class CKernelMulticlassMachine : public CMulticlassMachine
CMulticlassMachine(strategy,(CMachine*)machine,labs), m_kernel(NULL)
{
set_kernel(kernel);
m_parameters->add((CSGObject**)&m_kernel,"m_kernel");
SG_ADD((CSGObject**)&m_kernel,"m_kernel", "The kernel", MS_AVAILABLE);
}

/** destructor */
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/multiclass/ConjugateIndex.cpp
Expand Up @@ -58,7 +58,7 @@ void CConjugateIndex::clean_classes()
if (m_classes)
{
for (int32_t i=0; i<m_num_classes; i++)
m_classes[i].unref();
m_classes[i]=SGMatrix<float64_t>();

delete[] m_classes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/HomogeneousKernelMap.cpp
Expand Up @@ -57,7 +57,7 @@ bool CHomogeneousKernelMap::init (CFeatures* features)

void CHomogeneousKernelMap::cleanup ()
{
m_table.unref();
m_table=SGVector<float64_t>();
}


Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/KernelPCA.cpp
Expand Up @@ -51,7 +51,7 @@ void CKernelPCA::init()

void CKernelPCA::cleanup()
{
m_transformation_matrix.unref();
m_transformation_matrix = SGMatrix<float64_t>();

if (m_init_features)
SG_UNREF(m_init_features);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/PCA.cpp
Expand Up @@ -172,7 +172,7 @@ bool CPCA::init(CFeatures* features)

void CPCA::cleanup()
{
m_transformation_matrix.unref();
m_transformation_matrix=SGMatrix<float64_t>();
}

SGMatrix<float64_t> CPCA::apply_to_feature_matrix(CFeatures* features)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/ui/GUIClassifier.cpp
Expand Up @@ -1474,7 +1474,7 @@ CLabels* CGUIClassifier::classify_byte_linear()
testfeatures->get_feature_type() != F_BYTE )
{
SG_ERROR("testfeatures not of class STRING type BYTE\n") ;
return false ;
return NULL;
}

((CWDSVMOcas*) classifier)->set_features((CStringFeatures<uint8_t>*) testfeatures);
Expand Down

0 comments on commit 49462e7

Please sign in to comment.