Skip to content

Commit

Permalink
Made SGSparseVector inherited from SGReferencedData
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 24, 2012
1 parent d456ac5 commit 916983d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 59 deletions.
47 changes: 22 additions & 25 deletions src/shogun/features/SparseFeatures.cpp
Expand Up @@ -130,7 +130,7 @@ template<class ST> ST CSparseFeatures<ST>::get_feature(int32_t num, int32_t inde
ret+=sv.features[i].entry ;
}

free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);

return ret ;
}
Expand All @@ -154,7 +154,7 @@ template<class ST> ST* CSparseFeatures<ST>::get_full_feature_vector(int32_t num,
fv[sv.features[i].feat_index]= sv.features[i].entry;
}

free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);

return fv;
}
Expand All @@ -180,7 +180,7 @@ template<class ST> SGVector<ST> CSparseFeatures<ST>::get_full_feature_vector(int
dense.vector[sv.features[i].feat_index]= sv.features[i].entry;
}

free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);

return dense;
}
Expand All @@ -189,7 +189,7 @@ template<class ST> int32_t CSparseFeatures<ST>::get_nnz_features_for_vector(int3
{
SGSparseVector<ST> sv = get_sparse_feature_vector(num);
int32_t len=sv.num_feat_entries;
free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);
return len;
}

Expand All @@ -204,13 +204,10 @@ template<class ST> SGSparseVector<ST> CSparseFeatures<ST>::get_sparse_feature_ve
if (sparse_feature_matrix)
{
result=sparse_feature_matrix[real_num];
result.do_free=false;
return result;
}
else
{
result.do_free=false;

if (feature_cache)
{
result.features=feature_cache->lock_entry(num);
Expand All @@ -223,8 +220,8 @@ template<class ST> SGSparseVector<ST> CSparseFeatures<ST>::get_sparse_feature_ve
}
}

if (!result.features)
result.do_free=true;
//if (!result.features)
// result.do_free=true;

result.features=compute_sparse_feature_vector(num,
result.num_feat_entries, result.features);
Expand Down Expand Up @@ -321,7 +318,7 @@ template<class ST> ST CSparseFeatures<ST>::dense_dot(ST alpha, int32_t num, ST*
}
}

free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);
return result;
}

Expand Down Expand Up @@ -356,15 +353,15 @@ template<class ST> void CSparseFeatures<ST>::add_to_dense_vec(float64_t alpha, i
}
}

free_sparse_feature_vector(sv, num);
free_sparse_feature_vector(num);
}

template<class ST> void CSparseFeatures<ST>::free_sparse_feature_vector(SGSparseVector<ST> vec, int32_t num)
template<class ST> void CSparseFeatures<ST>::free_sparse_feature_vector(int32_t num)
{
if (feature_cache)
feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num));

vec.free_vector();
//vec.free_vector();
}

template<class ST> SGSparseVector<ST>* CSparseFeatures<ST>::get_sparse_feature_matrix(int32_t &num_feat, int32_t &num_vec)
Expand Down Expand Up @@ -423,7 +420,7 @@ template<class ST> SGSparseVector<ST>* CSparseFeatures<ST>::get_transposed(int32
for (int32_t i=0; i<sv.num_feat_entries; i++)
hist[sv.features[i].feat_index]++;

free_sparse_feature_vector(sv, v);
free_sparse_feature_vector(v);
}

// allocate room for future feature vectors
Expand All @@ -449,7 +446,7 @@ template<class ST> SGSparseVector<ST>* CSparseFeatures<ST>::get_transposed(int32
hist[vidx]++;
}

free_sparse_feature_vector(sv, v);
free_sparse_feature_vector(v);
}

SG_FREE(hist);
Expand Down Expand Up @@ -645,12 +642,12 @@ template<class ST> EFeatureClass CSparseFeatures<ST>::get_feature_class() const
return C_SPARSE;
}

template<class ST> void CSparseFeatures<ST>::free_feature_vector(SGSparseVector<ST> vec, int32_t num)
template<class ST> void CSparseFeatures<ST>::free_feature_vector(int32_t num)
{
if (feature_cache)
feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num));

vec.free_vector();
//vec.free_vector();
}

template<class ST> int64_t CSparseFeatures<ST>::get_num_nonzero_entries()
Expand All @@ -676,7 +673,7 @@ template<class ST> float64_t* CSparseFeatures<ST>::compute_squared(float64_t* sq
for (int32_t j=0; j<vec.num_feat_entries; j++)
sq[i]+=vec.features[j].entry*vec.features[j].entry;

free_feature_vector(vec, i);
free_feature_vector(i);
}

return sq;
Expand Down Expand Up @@ -736,8 +733,8 @@ template<class ST> float64_t CSparseFeatures<ST>::compute_squared_norm(
}
}

((CSparseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a);
((CSparseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b);
((CSparseFeatures<float64_t>*) lhs)->free_feature_vector(idx_a);
((CSparseFeatures<float64_t>*) rhs)->free_feature_vector(idx_b);

return CMath::abs(result);
}
Expand Down Expand Up @@ -1008,8 +1005,8 @@ template<class ST> float64_t CSparseFeatures<ST>::dot(int32_t vec_idx1,
float64_t result=sparse_dot(1, avec.features, avec.num_feat_entries,
bvec.features, bvec.num_feat_entries);

free_sparse_feature_vector(avec, vec_idx1);
sf->free_sparse_feature_vector(bvec, vec_idx2);
free_sparse_feature_vector(vec_idx1);
sf->free_sparse_feature_vector(vec_idx2);

return result;
}
Expand All @@ -1031,7 +1028,7 @@ template<class ST> float64_t CSparseFeatures<ST>::dense_dot(int32_t vec_idx1, co
result+=vec2[sv.features[i].feat_index]*sv.features[i].entry;
}

free_sparse_feature_vector(sv, vec_idx1);
free_sparse_feature_vector(vec_idx1);

return result;
}
Expand Down Expand Up @@ -1075,7 +1072,7 @@ template<class ST> void CSparseFeatures<ST>::free_feature_iterator(void* iterato
return;

sparse_feature_iterator* it=(sparse_feature_iterator*) iterator;
free_sparse_feature_vector(it->sv, it->vector_index);
free_sparse_feature_vector(it->vector_index);
SG_FREE(it);
}

Expand All @@ -1099,7 +1096,7 @@ template<class ST> CFeatures* CSparseFeatures<ST>::copy_subset(SGVector<index_t>
memcpy(matrix_copy.sparse_matrix[i].features, current.features,
sizeof(SGSparseVectorEntry<ST>)*current.num_feat_entries);

free_sparse_feature_vector(current, index);
free_sparse_feature_vector(index);
}

return new CSparseFeatures<ST>(matrix_copy);
Expand Down
5 changes: 2 additions & 3 deletions src/shogun/features/SparseFeatures.h
Expand Up @@ -208,10 +208,9 @@ template <class ST> class CSparseFeatures : public CDotFeatures
*
* possible with subset
*
* @param vec feature vector to free
* @param num index of this vector in the cache
*/
void free_sparse_feature_vector(SGSparseVector<ST> vec, int32_t num);
void free_sparse_feature_vector(int32_t num);

/** get the pointer to the sparse feature matrix
* num_feat,num_vectors are returned by reference
Expand Down Expand Up @@ -357,7 +356,7 @@ template <class ST> class CSparseFeatures : public CDotFeatures
* @param vec feature vector to free
* @param num index of vector in cache
*/
void free_feature_vector(SGSparseVector<ST> vec, int32_t num);
void free_feature_vector(int32_t num);

/** get number of non-zero entries in sparse feature matrix
*
Expand Down
10 changes: 5 additions & 5 deletions src/shogun/features/SparsePolyFeatures.cpp
Expand Up @@ -67,7 +67,7 @@ int32_t CSparsePolyFeatures::get_nnz_features_for_vector(int32_t num)
int32_t vlen;
SGSparseVector<float64_t> vec=m_feat->get_sparse_feature_vector(num);
vlen=vec.num_feat_entries;
m_feat->free_feature_vector(vec, num);
m_feat->free_feature_vector(num);
return vlen*(vlen+1)/2;
}

Expand Down Expand Up @@ -128,8 +128,8 @@ float64_t CSparsePolyFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t v
vec1.num_feat_entries, vec2.features, vec2.num_feat_entries);
result=CMath::pow(result, m_degree);

m_feat->free_feature_vector(vec1, vec_idx1);
pf->m_feat->free_feature_vector(vec2, vec_idx2);
m_feat->free_feature_vector(vec_idx1);
pf->m_feat->free_feature_vector(vec_idx2);

return result;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ float64_t CSparsePolyFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2
if (m_normalize)
result/=m_normalization_values[vec_idx1];

m_feat->free_feature_vector(vec, vec_idx1);
m_feat->free_feature_vector(vec_idx1);
return result;
}

Expand Down Expand Up @@ -228,7 +228,7 @@ void CSparsePolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, fl
else if (m_degree==3)
SG_NOTIMPLEMENTED;

m_feat->free_feature_vector(vec, vec_idx1);
m_feat->free_feature_vector(vec_idx1);
}

void CSparsePolyFeatures::store_normalization_values()
Expand Down
58 changes: 32 additions & 26 deletions src/shogun/lib/SGSparseVector.h
Expand Up @@ -15,6 +15,7 @@

#include <shogun/lib/config.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGReferencedData.h>
#include <map>

namespace shogun
Expand All @@ -31,53 +32,62 @@ template <class T> struct SGSparseVectorEntry
};

/** @brief template class SGSparseVector */
template <class T> class SGSparseVector
template <class T> class SGSparseVector : public SGReferencedData
{
public:
/** default constructor */
SGSparseVector() :
num_feat_entries(0), features(NULL), do_free(false) {}
SGSparseVector() : SGReferencedData(false)
{
init_data();
}

/** constructor for setting params */
SGSparseVector(SGSparseVectorEntry<T>* feats, index_t num_entries,
index_t index, bool free_v=false) :
num_feat_entries(num_entries), features(feats),
do_free(free_v)
bool ref_counting=true) :
SGReferencedData(ref_counting),
num_feat_entries(num_entries), features(feats)
{
}

/** constructor to create new vector in memory */
SGSparseVector(index_t num_entries, index_t index, bool free_v=false) :
num_feat_entries(num_entries), do_free(free_v)
SGSparseVector(index_t num_entries, bool ref_counting=true) :
SGReferencedData(ref_counting),
num_feat_entries(num_entries)
{
features=SG_MALLOC(SGSparseVectorEntry<T>, num_feat_entries);
features = SG_MALLOC(SGSparseVectorEntry<T>, num_feat_entries);
}

/** copy constructor */
SGSparseVector(const SGSparseVector& orig) :
num_feat_entries(orig.num_feat_entries),
features(orig.features), do_free(orig.do_free)
SGReferencedData(orig)
{
copy_data(orig);
}

/** free vector */
void free_vector()
virtual ~SGSparseVector()
{
if (do_free)
SG_FREE(features);
unref();
}

features=NULL;
do_free=false;
num_feat_entries=0;
protected:

virtual void copy_data(const SGReferencedData& orig)
{
num_feat_entries = ((SGSparseVector*)(&orig))->num_feat_entries;
features = ((SGSparseVector*)(&orig))->features;
}

/** destroy vector */
void destroy_vector()
virtual void init_data()
{
do_free=true;
free_vector();
num_feat_entries = 0;
features = NULL;
}

virtual void free_data()
{
num_feat_entries = 0;
SG_FREE(features);
}

public:
/** number of feature entries */
Expand All @@ -86,10 +96,6 @@ template <class T> class SGSparseVector
/** features */
SGSparseVectorEntry<T>* features;

/** whether vector needs to be freed */
bool do_free;


};

}
Expand Down

0 comments on commit 916983d

Please sign in to comment.