Skip to content

Commit

Permalink
Merge pull request #3724 from MikeLing/issuefix-3700
Browse files Browse the repository at this point in the history
Refactoring for shogun::memcpy
  • Loading branch information
lambday committed Mar 20, 2017
2 parents eac86c9 + c12a120 commit c7640fa
Show file tree
Hide file tree
Showing 84 changed files with 214 additions and 213 deletions.
6 changes: 3 additions & 3 deletions src/shogun/base/DynArray.h
Expand Up @@ -394,7 +394,7 @@ template <class T> class DynArray
array=SG_MALLOC(T, p_array_size);
else
array=(T*) malloc(p_array_size*sizeof(T));
memcpy(array, p_array, p_array_size*sizeof(T));
sg_memcpy(array, p_array, p_array_size*sizeof(T));
}
else
array=p_array;
Expand All @@ -420,7 +420,7 @@ template <class T> class DynArray
array=SG_MALLOC(T, p_array_size);
else
array=(T*) malloc(p_array_size*sizeof(T));
memcpy(array, p_array, p_array_size*sizeof(T));
sg_memcpy(array, p_array, p_array_size*sizeof(T));

num_elements=p_array_size;
current_num_elements=p_num_elements;
Expand Down Expand Up @@ -500,7 +500,7 @@ template <class T> class DynArray
array=(T*) malloc(sizeof(T)*orig.num_elements);
}

memcpy(array, orig.array, sizeof(T)*orig.num_elements);
sg_memcpy(array, orig.array, sizeof(T)*orig.num_elements);
num_elements=orig.num_elements;
current_num_elements=orig.current_num_elements;

Expand Down
6 changes: 3 additions & 3 deletions src/shogun/base/Parameter.cpp
Expand Up @@ -2918,7 +2918,7 @@ void Parameter::set_from_parameters(Parameter* params)

/* copy parameter data, size in memory is equal because of same type */
if (dest!=source)
memcpy(dest, source, own->m_datatype.get_size());
sg_memcpy(dest, source, own->m_datatype.get_size());
}
}

Expand Down Expand Up @@ -3354,7 +3354,7 @@ bool TParameter::copy_ptype(EPrimitiveType ptype, void* source, void* target)
{
SG_SDEBUG("entering TParameter::copy_ptype()\n");

/* rather than using memcpy, use the cumbersome way here and cast all types.
/* rather than using sg_memcpy, use the cumbersome way here and cast all types.
* This makes it so much easier to debug code.
* Copy full stype if this is too slow */
switch (ptype)
Expand Down Expand Up @@ -3637,7 +3637,7 @@ bool TParameter::copy_stype(EStructType stype, EPrimitiveType ptype,
*
* Therefore, this code is very close to the the equals code for
* stypes. If it turns out to be too slow (which I doubt), stypes can be
* copied with memcpy over the full memory blocks */
* copied with sg_memcpy over the full memory blocks */

switch (stype)
{
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/classifier/svm/NewtonSVM.cpp
Expand Up @@ -269,7 +269,7 @@ void CNewtonSVM::line_search_linear(float64_t* weights, float64_t* d, float64_t*
do
{
SGVector<float64_t>::vector_multiply(temp1, Y.vector, Xd, x_n);
memcpy(temp1forout, temp1, sizeof(float64_t)*x_n);
sg_memcpy(temp1forout, temp1, sizeof(float64_t)*x_n);
SGVector<float64_t>::scale_vector(t, temp1forout, x_n);
SGVector<float64_t>::add(outz, 1.0, out, -1.0, temp1forout, x_n);

Expand Down Expand Up @@ -342,7 +342,7 @@ void CNewtonSVM::obj_fun_linear(float64_t* weights, float64_t* out,

//create copy of w0
float64_t* w0=SG_MALLOC(float64_t, x_d+1);
memcpy(w0, weights, sizeof(float64_t)*(x_d));
sg_memcpy(w0, weights, sizeof(float64_t)*(x_d));
w0[x_d]=0; //do not penalize b

//create copy of out
Expand All @@ -353,7 +353,7 @@ void CNewtonSVM::obj_fun_linear(float64_t* weights, float64_t* out,
float64_t p1=SGVector<float64_t>::sum(out1, x_n)/2;
float64_t C1;
float64_t* w0copy=SG_MALLOC(float64_t, x_d+1);
memcpy(w0copy, w0, sizeof(float64_t)*(x_d+1));
sg_memcpy(w0copy, w0, sizeof(float64_t)*(x_d+1));
SGVector<float64_t>::scale_vector(0.5, w0copy, x_d+1);
cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1, x_d+1, lambda,
w0, x_d+1, w0copy, x_d+1, 0.0, &C1, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/svm/OnlineLibLinear.cpp
Expand Up @@ -57,7 +57,7 @@ COnlineLibLinear::COnlineLibLinear(COnlineLibLinear *mch)
if (w_dim > 0)
{
w = SG_MALLOC(float32_t, w_dim);
memcpy(w, mch->w, w_dim*sizeof(float32_t));
sg_memcpy(w, mch->w, w_dim*sizeof(float32_t));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/svm/SVM.cpp
Expand Up @@ -301,7 +301,7 @@ float64_t* CSVM::get_linear_term_array()
return NULL;
float64_t* a = SG_MALLOC(float64_t, m_linear_term.vlen);

memcpy(a, m_linear_term.vector,
sg_memcpy(a, m_linear_term.vector,
m_linear_term.vlen*sizeof(float64_t));

return a;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/vw/cache/VwNativeCacheWriter.cpp
Expand Up @@ -109,7 +109,7 @@ void CVwNativeCacheWriter::cache_tag(v_array<char> tag)
buf.buf_write(c, sizeof(vw_size_t)+tag.index());
*(vw_size_t*)c = tag.index();
c += sizeof(vw_size_t);
memcpy(c, tag.begin, tag.index());
sg_memcpy(c, tag.begin, tag.index());
c += tag.index();

buf.set(c);
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/clustering/GMM.cpp
Expand Up @@ -72,19 +72,19 @@ CGMM::CGMM(vector<CGaussian*> components, SGVector<float64_t> coefficients, bool

SGVector<float64_t> old_mean=components[i]->get_mean();
SGVector<float64_t> new_mean(old_mean.vlen);
memcpy(new_mean.vector, old_mean.vector, old_mean.vlen*sizeof(float64_t));
sg_memcpy(new_mean.vector, old_mean.vector, old_mean.vlen*sizeof(float64_t));
m_components[i]->set_mean(new_mean);

SGVector<float64_t> old_d=components[i]->get_d();
SGVector<float64_t> new_d(old_d.vlen);
memcpy(new_d.vector, old_d.vector, old_d.vlen*sizeof(float64_t));
sg_memcpy(new_d.vector, old_d.vector, old_d.vlen*sizeof(float64_t));
m_components[i]->set_d(new_d);

if (components[i]->get_cov_type()==FULL)
{
SGMatrix<float64_t> old_u=components[i]->get_u();
SGMatrix<float64_t> new_u(old_u.num_rows, old_u.num_cols);
memcpy(new_u.matrix, old_u.matrix, old_u.num_rows*old_u.num_cols*sizeof(float64_t));
sg_memcpy(new_u.matrix, old_u.matrix, old_u.num_rows*old_u.num_cols*sizeof(float64_t));
m_components[i]->set_u(new_u);
}

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/distributions/Gaussian.cpp
Expand Up @@ -224,7 +224,7 @@ float64_t CGaussian::compute_log_PDF(SGVector<float64_t> point)
ASSERT(m_mean.vector && m_d.vector)
ASSERT(point.vlen == m_mean.vlen)
float64_t* difference=SG_MALLOC(float64_t, m_mean.vlen);
memcpy(difference, point.vector, sizeof(float64_t)*m_mean.vlen);
sg_memcpy(difference, point.vector, sizeof(float64_t)*m_mean.vlen);

for (int32_t i = 0; i < m_mean.vlen; i++)
difference[i] -= m_mean.vector[i];
Expand Down Expand Up @@ -339,7 +339,7 @@ void CGaussian::decompose_cov(SGMatrix<float64_t> cov)
{
case FULL:
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);
sg_memcpy(m_u.matrix, cov.matrix, sizeof(float64_t)*cov.num_rows*cov.num_rows);

m_d.vector=SGMatrix<float64_t>::compute_eigenvectors(m_u.matrix, cov.num_rows, cov.num_rows);
m_d.vlen=cov.num_rows;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/distributions/PositionalPWM.cpp
Expand Up @@ -223,6 +223,6 @@ SGMatrix<float64_t> CPositionalPWM::get_scoring(int32_t d)
int32_t rows=CMath::pow((int32_t) m_w.num_rows,d);
int32_t cols=m_w.num_cols;
float64_t* scoring_matrix = SG_MALLOC(float64_t, rows*cols);
memcpy(scoring_matrix,m_poim.vector+offs,rows*cols*sizeof(float64_t));
sg_memcpy(scoring_matrix,m_poim.vector+offs,rows*cols*sizeof(float64_t));
return SGMatrix<float64_t>(scoring_matrix,rows,cols);
}
Expand Up @@ -47,7 +47,7 @@ SGVector<float64_t> CProbabilityDistribution::sample() const
{
SGMatrix<float64_t> s=sample(1);
SGVector<float64_t> result(m_dimension);
memcpy(result.vector, s.matrix, m_dimension*sizeof(float64_t));
sg_memcpy(result.vector, s.matrix, m_dimension*sizeof(float64_t));
return result;
}

Expand All @@ -65,7 +65,7 @@ float64_t CProbabilityDistribution::log_pdf(SGVector<float64_t> sample_vec) cons
m_dimension);

SGMatrix<float64_t> s(m_dimension, 1);
memcpy(s.matrix, sample_vec.vector, m_dimension*sizeof(float64_t));
sg_memcpy(s.matrix, sample_vec.vector, m_dimension*sizeof(float64_t));
return log_pdf_multiple(s)[0];
}

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/CrossValidationMKLStorage.cpp
Expand Up @@ -76,7 +76,7 @@ void CCrossValidationMKLStorage::update_trained_machine(
m_current_fold_index, first_idx);

/* copy memory */
memcpy(&m_mkl_weights.matrix[first_idx], w.vector,
sg_memcpy(&m_mkl_weights.matrix[first_idx], w.vector,
w.vlen*sizeof(float64_t));

SG_UNREF(kernel);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/CrossValidationPrintOutput.cpp
Expand Up @@ -136,7 +136,7 @@ char* CCrossValidationPrintOutput::append_tab_to_string(const char* string)
/* allocate memory, concatenate and add termination character */
index_t len=strlen(string);
char* new_prefix=SG_MALLOC(char, len+2);
memcpy(new_prefix, string, sizeof(char)*len);
sg_memcpy(new_prefix, string, sizeof(char)*len);
new_prefix[len]='\t';
new_prefix[len+1]='\0';

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/SplittingStrategy.cpp
Expand Up @@ -90,7 +90,7 @@ SGVector<index_t> CSplittingStrategy::generate_subset_indices(index_t subset_idx
SGVector<index_t> result(num_elements, true);

/* copy data */
memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements);
sg_memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements);

SG_UNREF(to_copy);

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/Alphabet.cpp
Expand Up @@ -663,7 +663,7 @@ void CAlphabet::copy_histogram(CAlphabet* a)
h.vlen, sizeof(histogram)/sizeof(histogram[0]));
}

memcpy(histogram, h.vector, sizeof(histogram));
sg_memcpy(histogram, h.vector, sizeof(histogram));
}

const char* CAlphabet::get_alphabet_name(EAlphabet alphabet)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/DataGenerator.cpp
Expand Up @@ -153,7 +153,7 @@ SGMatrix<float64_t> CDataGenerator::generate_gaussians(index_t m, index_t n, ind
for (index_t j = 0; j < m; ++j)
{
SGVector<float64_t> v = g->sample();
memcpy((result.matrix+j*result.num_rows+i*m*dim), v.vector, dim*sizeof(float64_t));
sg_memcpy((result.matrix+j*result.num_rows+i*m*dim), v.vector, dim*sizeof(float64_t));
SG_FREE(v.vector);
}

Expand Down
12 changes: 6 additions & 6 deletions src/shogun/features/DenseFeatures.cpp
Expand Up @@ -143,7 +143,7 @@ template<class ST> ST* CDenseFeatures<ST>::get_feature_vector(int32_t num, int32
// note: tmp_feat_after should be checked as it is used by memcpy
if (tmp_feat_after)
{
memcpy(feat, tmp_feat_after, sizeof(ST) * tmp_len);
sg_memcpy(feat, tmp_feat_after, sizeof(ST) * tmp_len);
SG_FREE(tmp_feat_after);

len = tmp_len;
Expand All @@ -170,7 +170,7 @@ template<class ST> void CDenseFeatures<ST>::set_feature_vector(SGVector<ST> vect
SG_ERROR(
"Vector not of length %d (has %d)\n", num_features, vector.vlen);

memcpy(&feature_matrix.matrix[real_num * int64_t(num_features)], vector.vector,
sg_memcpy(&feature_matrix.matrix[real_num * int64_t(num_features)], vector.vector,
int64_t(num_features) * sizeof(ST));
}

Expand Down Expand Up @@ -230,7 +230,7 @@ template<class ST> void CDenseFeatures<ST>::vector_subset(int32_t* idx, int32_t
if (i == ii)
continue;

memcpy(&feature_matrix.matrix[int64_t(num_features) * i],
sg_memcpy(&feature_matrix.matrix[int64_t(num_features) * i],
&feature_matrix.matrix[int64_t(num_features) * ii],
num_features * sizeof(ST));
old_ii = ii;
Expand Down Expand Up @@ -299,7 +299,7 @@ void CDenseFeatures<ST>::copy_feature_matrix(SGMatrix<ST> target, index_t column
{
auto src=feature_matrix.matrix;
auto dest=target.matrix+int64_t(num_features)*column_offset;
shogun::memcpy(dest, src, feature_matrix.size()*sizeof(ST));
sg_memcpy(dest, src, feature_matrix.size()*sizeof(ST));
}
else
{
Expand All @@ -308,7 +308,7 @@ void CDenseFeatures<ST>::copy_feature_matrix(SGMatrix<ST> target, index_t column
auto real_i=m_subset_stack->subset_idx_conversion(i);
auto src=feature_matrix.matrix+real_i*int64_t(num_features);
auto dest=target.matrix+int64_t(num_features)*(column_offset+i);
shogun::memcpy(dest, src, num_features*sizeof(ST));
sg_memcpy(dest, src, num_features*sizeof(ST));
}
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ template<class ST> CFeatures* CDenseFeatures<ST>::copy_subset(SGVector<index_t>
for (index_t i=0; i<indices.vlen; ++i)
{
index_t real_idx=m_subset_stack->subset_idx_conversion(indices.vector[i]);
memcpy(&feature_matrix_copy.matrix[i*num_features],
sg_memcpy(&feature_matrix_copy.matrix[i*num_features],
&feature_matrix.matrix[real_idx*num_features],
num_features*sizeof(ST));
}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/LBPPyrDotFeatures.cpp
Expand Up @@ -162,7 +162,7 @@ uint32_t* CLBPPyrDotFeatures::get_image(int32_t index, int32_t& width, int32_t&
uint32_t* image = images->get_feature_vector(index, len, do_free);
uint32_t* img;
img = SG_MALLOC(uint32_t, len);
memcpy(img, image, len * sizeof(uint32_t));
sg_memcpy(img, image, len * sizeof(uint32_t));
images->free_feature_vector(image, index, do_free);
width = image_width;
height = image_height;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/RealFileFeatures.cpp
Expand Up @@ -73,7 +73,7 @@ CRealFileFeatures::CRealFileFeatures(const CRealFileFeatures & orig)
if (orig.labels && get_num_vectors())
{
labels=SG_MALLOC(int32_t, get_num_vectors());
memcpy(labels, orig.labels, sizeof(int32_t)*get_num_vectors());
sg_memcpy(labels, orig.labels, sizeof(int32_t)*get_num_vectors());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/SparseFeatures.cpp
Expand Up @@ -141,7 +141,7 @@ template<class ST> SGSparseVector<ST> CSparseFeatures<ST>::get_sparse_feature_ve

if (tmp_feat_after)
{
memcpy(result.features, tmp_feat_after,
sg_memcpy(result.features, tmp_feat_after,
sizeof(SGSparseVectorEntry<ST>)*tmp_len);

SG_FREE(tmp_feat_after);
Expand Down
18 changes: 9 additions & 9 deletions src/shogun/features/StringFeatures.cpp
Expand Up @@ -100,7 +100,7 @@ template<class ST> CStringFeatures<ST>::CStringFeatures(const CStringFeatures &
{
features[i].string=SG_MALLOC(ST, orig.features[i].slen);
features[i].slen=orig.features[i].slen;
memcpy(features[i].string, orig.features[i].string, sizeof(ST)*orig.features[i].slen);
sg_memcpy(features[i].string, orig.features[i].string, sizeof(ST)*orig.features[i].slen);
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ template<class ST> SGVector<ST> CStringFeatures<ST>::get_feature_vector(int32_t
bool free_vec;
ST* vec=get_feature_vector(num, l, free_vec);
ST* dst=SG_MALLOC(ST, l);
memcpy(dst, vec, l*sizeof(ST));
sg_memcpy(dst, vec, l*sizeof(ST));
free_feature_vector(vec, num, free_vec);
return SGVector<ST>(dst, l, true);
}
Expand All @@ -262,7 +262,7 @@ template<class ST> void CStringFeatures<ST>::set_feature_vector(SGVector<ST> vec
cleanup_feature_vector(num);
features[num].slen=vector.vlen;
features[num].string=SG_MALLOC(ST, vector.vlen);
memcpy(features[num].string, vector.vector, vector.vlen*sizeof(ST));
sg_memcpy(features[num].string, vector.vector, vector.vlen*sizeof(ST));

determine_maximum_string_length();
}
Expand Down Expand Up @@ -921,7 +921,7 @@ template<class ST> bool CStringFeatures<ST>::set_features(SGString<ST>* p_featur

// TODO remove copying
features = SG_MALLOC(SGString<ST>,p_num_vectors);
memcpy(features,p_features,sizeof(SGString<ST>)*p_num_vectors);
sg_memcpy(features,p_features,sizeof(SGString<ST>)*p_num_vectors);
num_vectors = p_num_vectors;
max_string_length = p_max_string_length;

Expand Down Expand Up @@ -949,7 +949,7 @@ template<class ST> bool CStringFeatures<ST>::append_features(CStringFeatures<ST>
int32_t real_i = sf->m_subset_stack->subset_idx_conversion(i);
int32_t length=sf->features[real_i].slen;
new_features[i].string=SG_MALLOC(ST, length);
memcpy(new_features[i].string, sf->features[real_i].string, length);
sg_memcpy(new_features[i].string, sf->features[real_i].string, length);
new_features[i].slen=length;
}
return append_features(new_features, sf_num_str,
Expand Down Expand Up @@ -1042,7 +1042,7 @@ template<class ST> SGString<ST>* CStringFeatures<ST>::copy_features(int32_t& num
ST* vec=get_feature_vector(i, len, free_vec);
new_feat[i].string=SG_MALLOC(ST, len);
new_feat[i].slen=len;
memcpy(new_feat[i].string, vec, ((size_t) len) * sizeof(ST));
sg_memcpy(new_feat[i].string, vec, ((size_t) len) * sizeof(ST));
free_feature_vector(vec, i, free_vec);
}

Expand Down Expand Up @@ -1482,7 +1482,7 @@ template<class ST> ST* CStringFeatures<ST>::get_zero_terminated_string_copy(SGSt
{
int32_t l=str.slen;
ST* s=SG_MALLOC(ST, l+1);
memcpy(s, str.string, sizeof(ST)*l);
sg_memcpy(s, str.string, sizeof(ST)*l);
s[l]='\0';
return s;
}
Expand Down Expand Up @@ -1660,7 +1660,7 @@ template<class ST> CFeatures* CStringFeatures<ST>::copy_subset(
/* copy string */
SGString<ST> current_string=features[real_idx];
SGString<ST> string_copy(current_string.slen);
memcpy(string_copy.string, current_string.string,
sg_memcpy(string_copy.string, current_string.string,
current_string.slen*sizeof(ST));
list_copy.strings[i]=string_copy;
}
Expand Down Expand Up @@ -1697,7 +1697,7 @@ template<class ST> ST* CStringFeatures<ST>::compute_feature_vector(int32_t num,
return NULL;

ST* target=SG_MALLOC(ST, len);
memcpy(target, features[real_num].string, len*sizeof(ST));
sg_memcpy(target, features[real_num].string, len*sizeof(ST));
return target;
}

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/streaming/StreamingDenseFeatures.cpp
Expand Up @@ -317,7 +317,7 @@ CFeatures* CStreamingDenseFeatures<T>::get_streamed_features(
SGMatrix<T> so_far(matrix.num_rows, i);

/* copy */
memcpy(so_far.matrix, matrix.matrix,
sg_memcpy(so_far.matrix, matrix.matrix,
so_far.num_rows*so_far.num_cols*sizeof(T));

matrix=so_far;
Expand All @@ -343,7 +343,7 @@ CFeatures* CStreamingDenseFeatures<T>::get_streamed_features(
vec.vlen, matrix.num_rows);

/* copy vector into matrix */
memcpy(&matrix.matrix[current_vector.vlen*i], vec.vector,
sg_memcpy(&matrix.matrix[current_vector.vlen*i], vec.vector,
vec.vlen*sizeof(T));

/* clean up */
Expand Down

0 comments on commit c7640fa

Please sign in to comment.