Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
introduce ref counted SGMatrix
  • Loading branch information
Soeren Sonnenburg committed May 7, 2012
1 parent c132e97 commit 9d6bd98
Show file tree
Hide file tree
Showing 35 changed files with 88 additions and 201 deletions.
2 changes: 0 additions & 2 deletions src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -314,8 +314,6 @@ static bool matrix_to_numpy(PyObject* &obj, SGMatrix<type> sg_matrix, int typeco
((PyArrayObject*) obj)->flags |= NPY_OWNDATA;
}

sg_matrix.free_matrix();

return descr!=NULL;
}

Expand Down
21 changes: 10 additions & 11 deletions src/shogun/classifier/GaussianNaiveBayes.cpp
Expand Up @@ -17,32 +17,30 @@

using namespace shogun;

CGaussianNaiveBayes::CGaussianNaiveBayes() :
CMachine(), m_features(NULL), m_min_label(0),
m_num_classes(0), m_dim(0), m_means(),
m_variances(), m_label_prob(), m_rates()
CGaussianNaiveBayes::CGaussianNaiveBayes() : CMachine(), m_features(NULL),
m_min_label(0), m_num_classes(0), m_dim(0), m_means(), m_variances(),
m_label_prob(), m_rates()
{

};

CGaussianNaiveBayes::CGaussianNaiveBayes(CFeatures* train_examples, CLabels* train_labels) :
CMachine(), m_features(NULL), m_min_label(0),
m_num_classes(0), m_dim(0), m_means(),
m_variances(), m_label_prob(), m_rates()
CGaussianNaiveBayes::CGaussianNaiveBayes(CFeatures* train_examples,
CLabels* train_labels) : CMachine(), m_features(NULL),
m_min_label(0), m_num_classes(0), m_dim(0), m_means(),
m_variances(), m_label_prob(), m_rates()
{
ASSERT(train_examples->get_num_vectors() == train_labels->get_num_labels());
set_labels(train_labels);

if (!train_examples->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n");

set_features((CDotFeatures*)train_examples);
};

CGaussianNaiveBayes::~CGaussianNaiveBayes()
{
SG_UNREF(m_features);

m_means.destroy_matrix();
m_variances.destroy_matrix();
};

CFeatures* CGaussianNaiveBayes::get_features()
Expand Down Expand Up @@ -70,6 +68,7 @@ bool CGaussianNaiveBayes::train(CFeatures* data)
SG_ERROR("Specified features are not of type CDotFeatures\n");
set_features((CDotFeatures*) data);
}

// get int labels to train_labels and check length equality
ASSERT(m_labels);
SGVector<int32_t> train_labels = m_labels->get_int_labels();
Expand Down
10 changes: 2 additions & 8 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.free_matrix();
m_means.unref();

m_num_classes = 0;
}
Expand Down Expand Up @@ -130,9 +130,6 @@ CLabels* CQDA::apply()
CMath::display_vector(out->get_labels().vector, num_vecs, "Labels");
#endif

A.destroy_matrix();
X.destroy_matrix();

return out;
}

Expand Down Expand Up @@ -264,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.destroy_matrix();
buffer.unref();

CMath::vector_multiply(col, col, col, m_dim);
CMath::scale_vector(1.0/(m-1), col, m_dim);
Expand All @@ -281,8 +278,6 @@ bool CQDA::train_machine(CFeatures* data)

cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, n, n, n, 1.0,
M.matrix, n, rot_mat, n, 0.0, m_covs.get_matrix(k), n);

M.destroy_matrix();
}
}

Expand Down Expand Up @@ -341,7 +336,6 @@ bool CQDA::train_machine(CFeatures* data)
#endif

rotations.destroy_ndarray();
scalings.destroy_matrix();
SG_FREE(class_idxs);
SG_FREE(class_nums);
return true;
Expand Down
11 changes: 1 addition & 10 deletions src/shogun/clustering/GMM.cpp
Expand Up @@ -148,11 +148,7 @@ float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_chan
max_likelihood(alpha, min_cov);
}
else
{
alpha.matrix=SG_MALLOC(float64_t, num_vectors*m_components.vlen);
alpha.num_rows=num_vectors;
alpha.num_cols=m_components.vlen;
}
alpha=SGMatrix<float64_t>(num_vectors,m_components.vlen);

int32_t iter=0;
float64_t log_likelihood_prev=0;
Expand Down Expand Up @@ -196,8 +192,6 @@ float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_chan

SG_FREE(logPxy);
SG_FREE(logPx);
//SG_FREE(logPost);
alpha.free_matrix();

return log_likelihood_cur;
}
Expand Down Expand Up @@ -412,8 +406,6 @@ void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min
components.vector[1]->set_d(SGVector<float64_t>(CMath::compute_eigenvectors(c1.matrix, dim_n, dim_n), dim_n));
components.vector[1]->set_u(c1);

c2.destroy_matrix();

float64_t new_d=0;
for (int32_t i=0; i<dim_n; i++)
{
Expand Down Expand Up @@ -515,7 +507,6 @@ void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min
m_coefficients.vector[comp3]=coefficients.vector[2];

delete partial_candidate;
alpha.destroy_matrix();
SG_FREE(logPxy);
SG_FREE(logPx);
SG_FREE(init_logPxy);
Expand Down
4 changes: 0 additions & 4 deletions src/shogun/clustering/KMeans.cpp
Expand Up @@ -465,10 +465,6 @@ void CKMeans::store_model_features()
CSimpleFeatures<float64_t>* cluster_centers=new CSimpleFeatures<float64_t>(
mus);

/* reset mus variable to avoid interference with above features */
mus.do_free=false;
mus.free_matrix();

/* store cluster centers in lhs of distance variable */
CFeatures* rhs=distance->get_rhs();
distance->init(cluster_centers, rhs);
Expand Down
1 change: 0 additions & 1 deletion src/shogun/converter/DiffusionMaps.cpp
Expand Up @@ -149,7 +149,6 @@ CSimpleFeatures<float64_t>* CDiffusionMaps::embed_kernel(CKernel* kernel)
if (info)
SG_ERROR("Eigenproblem solving failed with %d code", info);

kernel_matrix.destroy_matrix();
SG_FREE(s_values);

return new CSimpleFeatures<float64_t>(new_feature_matrix);
Expand Down
2 changes: 0 additions & 2 deletions src/shogun/converter/Isomap.cpp
Expand Up @@ -122,12 +122,10 @@ SGMatrix<float64_t> CIsomap::isomap_distance(SGMatrix<float64_t> D_matrix)
N = D_matrix.num_cols;
if (D_matrix.num_cols!=D_matrix.num_rows)
{
D_matrix.destroy_matrix();
SG_ERROR("Given distance matrix is not square.\n");
}
if (m_k>=N)
{
D_matrix.destroy_matrix();
SG_ERROR("K parameter should be less than number of given vectors (k=%d, N=%d)\n", m_k, N);
}

Expand Down
3 changes: 0 additions & 3 deletions src/shogun/converter/KernelLocallyLinearEmbedding.cpp
Expand Up @@ -136,13 +136,10 @@ CSimpleFeatures<float64_t>* CKernelLocallyLinearEmbedding::embed_kernel(CKernel*
time->start();
SGMatrix<float64_t> M_matrix = construct_weight_matrix(kernel_matrix,neighborhood_matrix);
SG_DEBUG("Weights computation took %fs\n",time->cur_time_diff());
kernel_matrix.destroy_matrix();
neighborhood_matrix.destroy_matrix();

time->start();
SGMatrix<float64_t> nullspace = construct_embedding(M_matrix,m_target_dim);
SG_DEBUG("Embedding construction took %fs\n",time->cur_time_diff());
M_matrix.destroy_matrix();

delete time;

Expand Down
1 change: 0 additions & 1 deletion src/shogun/converter/LaplacianEigenmaps.cpp
Expand Up @@ -152,7 +152,6 @@ CSimpleFeatures<float64_t>* CLaplacianEigenmaps::embed_distance(CDistance* dista

// compute D
CSimpleFeatures<float64_t>* embedding = construct_embedding(features,W_sgmatrix);
W_sgmatrix.destroy_matrix();

return embedding;
}
Expand Down
2 changes: 0 additions & 2 deletions src/shogun/converter/LocallyLinearEmbedding.cpp
Expand Up @@ -243,15 +243,13 @@ CFeatures* CLocallyLinearEmbedding::apply(CFeatures* features)
time->start();
SGMatrix<float64_t> weight_matrix = construct_weight_matrix(simple_features,W_matrix,neighborhood_matrix);
SG_DEBUG("Weight matrix construction took %.5fs\n", time->cur_time_diff());
neighborhood_matrix.destroy_matrix();

// find null space of weight matrix
SG_DEBUG("Finding nullspace\n");
time->start();
SGMatrix<float64_t> new_feature_matrix = construct_embedding(weight_matrix,m_target_dim);
SG_DEBUG("Eigenproblem solving took %.5fs\n", time->cur_time_diff());
delete time;
weight_matrix.destroy_matrix();

SG_UNREF(features);
return (CFeatures*)(new CSimpleFeatures<float64_t>(new_feature_matrix));
Expand Down
5 changes: 0 additions & 5 deletions src/shogun/converter/MultidimensionalScaling.cpp
Expand Up @@ -125,8 +125,6 @@ CSimpleFeatures<float64_t>* CMultidimensionalScaling::embed_distance(CDistance*
else
feature_matrix = classic_embedding(distance_matrix);

distance_matrix.destroy_matrix();

return new CSimpleFeatures<float64_t>(feature_matrix);
}

Expand Down Expand Up @@ -307,8 +305,6 @@ SGMatrix<float64_t> CMultidimensionalScaling::landmark_embedding(SGMatrix<float6
}
SGMatrix<float64_t> lmk_feature_matrix = classic_embedding(lmk_dist_sgmatrix);

lmk_dist_sgmatrix.destroy_matrix();

// construct new feature matrix
float64_t* new_feature_matrix = SG_CALLOC(float64_t, m_target_dim*total_N);

Expand Down Expand Up @@ -392,7 +388,6 @@ SGMatrix<float64_t> CMultidimensionalScaling::landmark_embedding(SGMatrix<float6
run_triangulation_thread((void*)&single_thread_param);
#endif
// cleanup
lmk_feature_matrix.destroy_matrix();
SG_FREE(current_dist_to_lmks);
SG_FREE(mean_sq_dist_vector);
SG_FREE(to_process);
Expand Down
5 changes: 0 additions & 5 deletions src/shogun/converter/StochasticProximityEmbedding.cpp
Expand Up @@ -349,13 +349,8 @@ CSimpleFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDis
}

// Free memory
Yd.destroy_matrix();
if ( m_strategy == SPE_LOCAL )
{
ind1Neighbors.destroy_matrix();
neighbors_mat.destroy_matrix();
SG_FREE(ind2);
}

return new CSimpleFeatures< float64_t >(Y);
}
Expand Down
15 changes: 2 additions & 13 deletions src/shogun/distributions/Gaussian.cpp
Expand Up @@ -36,9 +36,6 @@ CGaussian::CGaussian(const SGVector<float64_t> mean, SGMatrix<float64_t> cov,
decompose_cov(cov);
init();
register_params();

if (cov.do_free)
cov.free_matrix();
}

void CGaussian::init()
Expand All @@ -59,7 +56,6 @@ void CGaussian::init()

CGaussian::~CGaussian()
{
m_u.destroy_matrix();
}

bool CGaussian::train(CFeatures* data)
Expand All @@ -71,16 +67,12 @@ bool CGaussian::train(CFeatures* data)
SG_ERROR("Specified features are not of type CDotFeatures\n");
set_features(data);
}
CDotFeatures* dotdata=(CDotFeatures *) data;

CDotFeatures* dotdata=(CDotFeatures *) data;
m_mean=dotdata->get_mean();
SGMatrix<float64_t> cov=dotdata->get_cov();

decompose_cov(cov);
cov.destroy_matrix();

init();

return true;
}

Expand Down Expand Up @@ -176,8 +168,6 @@ void CGaussian::set_cov(SGMatrix<float64_t> cov)
ASSERT(cov.num_rows==m_mean.vlen);
decompose_cov(cov);
init();
if (cov.do_free)
cov.free_matrix();
}

void CGaussian::set_d(const SGVector<float64_t> d)
Expand Down Expand Up @@ -239,8 +229,7 @@ void CGaussian::decompose_cov(SGMatrix<float64_t> cov)
switch (m_cov_type)
{
case FULL:
m_u.destroy_matrix();
m_u.matrix=SG_MALLOC(float64_t, cov.num_rows*cov.num_rows);
m_u=SGMatrix<float64_t>(cov.num_rows,cov.num_rows);
memcpy(m_u.matrix, cov.matrix, sizeof(float64_t)*cov.num_rows*cov.num_rows);

m_d.vector=CMath::compute_eigenvectors(m_u.matrix, cov.num_rows, cov.num_rows);
Expand Down
1 change: 0 additions & 1 deletion src/shogun/distributions/Gaussian.h
Expand Up @@ -192,7 +192,6 @@ class CGaussian : public CDistribution
*/
inline void set_u(SGMatrix<float64_t> u)
{
m_u.destroy_matrix();
m_u = u;
}

Expand Down
2 changes: 0 additions & 2 deletions src/shogun/evaluation/ClusteringEvaluation.cpp
Expand Up @@ -77,6 +77,4 @@ void CClusteringEvaluation::best_map(CLabels* predicted, CLabels* ground_truth)

for (int32_t i= 0; i < predicted_ilabels.vlen; ++i)
predicted->set_int_label(i, label_map[predicted_ilabels[i]]);

G.destroy_matrix();
}
2 changes: 0 additions & 2 deletions src/shogun/evaluation/ClusteringMutualInformation.cpp
Expand Up @@ -65,7 +65,5 @@ float64_t CClusteringMutualInformation::evaluate(CLabels* predicted, CLabels* gr
entropy_p += -G_colsum[i] * log(G_colsum[i])/log(2.);
}

G.destroy_matrix();

return mutual_info / std::max(entropy_g, entropy_p);
}
1 change: 0 additions & 1 deletion src/shogun/features/BinnedDotFeatures.cpp
Expand Up @@ -37,7 +37,6 @@ CBinnedDotFeatures::CBinnedDotFeatures(CSimpleFeatures<float64_t>* sf, SGMatrix<
CBinnedDotFeatures::~CBinnedDotFeatures()
{
SG_UNREF(m_features);
m_bins.destroy_matrix();
}

int32_t CBinnedDotFeatures::get_dim_feature_space() const
Expand Down
10 changes: 2 additions & 8 deletions src/shogun/features/DotFeatures.cpp
Expand Up @@ -283,21 +283,15 @@ void* CDotFeatures::dense_dot_range_helper(void* p)

SGMatrix<float64_t> CDotFeatures::get_computed_dot_feature_matrix()
{
SGMatrix<float64_t> m;

int64_t offs=0;
int32_t num=get_num_vectors();
int32_t dim=get_dim_feature_space();
ASSERT(num>0);
ASSERT(dim>0);

int64_t sz=((uint64_t) num)* dim;

m.do_free=true;
m.num_cols=num;
m.num_rows=dim;
m.matrix=SG_MALLOC(float64_t, sz);
memset(m.matrix, 0, sz*sizeof(float64_t));
SGMatrix<float64_t> m(dim, num);
m.zero();

for (int32_t i=0; i<num; i++)
{
Expand Down
9 changes: 2 additions & 7 deletions src/shogun/features/SparseFeatures.cpp
Expand Up @@ -473,15 +473,10 @@ template<class ST> void CSparseFeatures<ST>::set_sparse_feature_matrix(SGSparseM

template<class ST> SGMatrix<ST> CSparseFeatures<ST>::get_full_feature_matrix()
{
SGMatrix<ST> full;
SGMatrix<ST> full(num_features, get_num_vectors());
full.zero();

SG_INFO( "converting sparse features to full feature matrix of %ld x %ld entries\n", num_vectors, num_features);
full.num_rows=num_features;
full.num_cols=get_num_vectors();
full.do_free=true;
full.matrix=SG_MALLOC(ST, int64_t(num_features)*get_num_vectors());

memset(full.matrix, 0, size_t(num_features)*size_t(get_num_vectors())*sizeof(ST));

for (int32_t v=0; v<full.num_cols; v++)
{
Expand Down
7 changes: 1 addition & 6 deletions src/shogun/kernel/CustomKernel.cpp
Expand Up @@ -131,13 +131,8 @@ void CCustomKernel::cleanup_custom()
remove_all_row_subsets();
remove_all_col_subsets();

if (m_free_km)
SG_FREE(kmatrix.matrix);

kmatrix.matrix=NULL;
kmatrix.unref();
upper_diagonal=false;
kmatrix.num_cols=0;
kmatrix.num_rows=0;

SG_DEBUG("Leaving CCustomKernel::cleanup_custom()\n");
}
Expand Down

0 comments on commit 9d6bd98

Please sign in to comment.