Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 853db464 authored by hhakim's avatar hhakim
Browse files

Add MatSparse<FPP,GPU2>::add/sub with tests.

parent 0c353252
No related branches found
No related tags found
No related merge requests found
......@@ -310,6 +310,48 @@ void test_scal_mul()
delete cpu_sp_mat;
}
void test_scal_add()
{
cout << "test MatSparse<FPP,GPU2>::operator+=(const FPP &scal)" << endl;
auto nrows = 5;
auto ncols = 8;
double scalar = 55.;
auto cpu_sp_mat = Faust::MatSparse<double, Cpu>::randMat(nrows, ncols, .2);
MatDense<double, Cpu> cpu_ds_mat(*cpu_sp_mat);
Faust::MatSparse<double, Cpu> cpu_sp_mat2;
Faust::MatSparse<double, GPU2> gpu_sp_mat(*cpu_sp_mat);
gpu_sp_mat += scalar;
gpu_sp_mat.tocpu(cpu_sp_mat2);
for(int i=0;i<nrows;i++)
for(int j=0;j<ncols;j++)
{
assert(std::abs(cpu_sp_mat2(i,j) - cpu_ds_mat(i,j) - 55 )< 1e-6 || cpu_sp_mat2(i,j) == 0);
}
cout << "OK" << endl;
delete cpu_sp_mat;
}
void test_scal_sub()
{
cout << "test MatSparse<FPP,GPU2>::operator-=(const FPP &scal)" << endl;
auto nrows = 5;
auto ncols = 8;
double scalar = 55.;
auto cpu_sp_mat = Faust::MatSparse<double, Cpu>::randMat(nrows, ncols, .2);
MatDense<double, Cpu> cpu_ds_mat(*cpu_sp_mat);
Faust::MatSparse<double, Cpu> cpu_sp_mat2;
Faust::MatSparse<double, GPU2> gpu_sp_mat(*cpu_sp_mat);
gpu_sp_mat -= scalar;
gpu_sp_mat.tocpu(cpu_sp_mat2);
for(int i=0;i<nrows;i++)
for(int j=0;j<ncols;j++)
{
assert(std::abs(cpu_sp_mat2(i,j) - cpu_ds_mat(i,j) + 55)<1e-6|| cpu_sp_mat2(i,j) == 0);
}
cout << "OK" << endl;
delete cpu_sp_mat;
}
void test_is_equal()
{
cout << "test MatSparse<FPP,GPU2>::operator==(const MatSparse<FPP,GPU2>& mat)" << endl;
......@@ -342,6 +384,8 @@ int main()
test_clone();
test_copy();
test_scal_mul();
test_scal_add();
test_scal_sub();
test_set_id();
test_move();
test_is_equal();
......
......@@ -44,6 +44,14 @@ namespace Faust
void operator=(const MatSparse<FPP, Cpu>& mat);
void operator*=(const FPP& alpha);
void operator/=(const FPP& alpha);
/**
* Subtracts a scalar value ONLY to nonzeros (zeros stay zeros).
*/
void operator-=(const FPP& alpha);
/**
* Adds a scalar value ONLY to nonzeros (zeros stay zeros).
*/
void operator+=(const FPP& alpha);
bool operator==(const MatSparse<FPP, GPU2>& mat) const;
bool operator!=(const MatSparse<FPP, GPU2>& mat) const;
......
......@@ -99,6 +99,20 @@ namespace Faust
*this *= ((@FAUST_SCALAR_FOR_GM@)1)/scalar;
}
template<>
void MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>::operator+=(const @FAUST_SCALAR_FOR_GM@& scalar)
{
auto spm_funcs = ((gm_SparseMatFunc_@GM_SCALAR@*) this->spm_funcs);
spm_funcs->add_scalar(gpu_mat, reinterpret_cast<const @GM_REINTERPRET_CAST_SCALAR@*>(&scalar));
}
template<>
void MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>::operator-=(const @FAUST_SCALAR_FOR_GM@& scalar)
{
auto spm_funcs = ((gm_SparseMatFunc_@GM_SCALAR@*) this->spm_funcs);
spm_funcs->sub_scalar(gpu_mat, reinterpret_cast<const @GM_REINTERPRET_CAST_SCALAR@*>(&scalar));
}
template<>
void MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>::operator=(const MatSparse<@FAUST_SCALAR_FOR_GM@, Cpu>& mat)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment