Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 98dd2a57 authored by hhakim's avatar hhakim
Browse files

Update to gpu_mod@1cd25163, new functions in MatDense<FPP,GPU2>: operator+=,...

Update to gpu_mod@1cd25163, new functions in MatDense<FPP,GPU2>: operator+=, sub, operator-=, multiplyRight, multiplyLeft, operator*= with corresponding tests.
parent f769d6ca
Branches
Tags
No related merge requests found
...@@ -13,6 +13,8 @@ double calc_err(MatDense<double, GPU2> &gpu_mat, MatDense<double, Cpu> &cpu_mat) ...@@ -13,6 +13,8 @@ double calc_err(MatDense<double, GPU2> &gpu_mat, MatDense<double, Cpu> &cpu_mat)
auto err_diff = cpu_mat; auto err_diff = cpu_mat;
err_diff -= gpu_mat_to_cpu; err_diff -= gpu_mat_to_cpu;
auto err = err_diff.norm()/cpu_mat.norm(); auto err = err_diff.norm()/cpu_mat.norm();
if(err > 1e-6)
cout << "calc_err > err: " << err << endl;
return err; return err;
} }
...@@ -81,6 +83,40 @@ void test_mul_gpu_dense() ...@@ -81,6 +83,40 @@ void test_mul_gpu_dense()
cout << "OK" << endl; cout << "OK" << endl;
} }
void test_mul_eq_op()
{
cout << "test_mul_eq_op *=" << endl;
// generate two random cpu dense mats: cpu_mat1 and cpu_mat2
// convert the two cpu mats to gpu mats
faust_unsigned_int nrows = 90, ncols = 90;
faust_unsigned_int nrows2 = 90, ncols2 = 150;
auto cpu_mat1 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
MatDense<double,GPU2> gpu_mat1(nrows, ncols, cpu_mat1->getData());
auto cpu_mat2 = Faust::MatDense<double,Cpu>::randMat(nrows2,ncols2);
MatDense<double,GPU2> gpu_mat2(nrows2, ncols2, cpu_mat2->getData());
// test *=
*cpu_mat1 *= *cpu_mat2;
gpu_mat1 *= *cpu_mat2;
assert(calc_err(gpu_mat1, *cpu_mat1) < 1e-6);
cout << "OK" << endl;
}
void test_mul_spm()
{
cout << "test_mul_spm()" << endl;
faust_unsigned_int nrows = 90, ncols = 50;
auto cpu_mat1 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
Faust::MatSparse<double, Cpu> *cpu_mat2_sp = Faust::MatSparse<double, Cpu>::randMat(ncols, nrows, .2);
MatDense<double,GPU2> gpu_mat1(nrows, ncols, cpu_mat1->getData());
gpu_mat1.multiplyLeft(*cpu_mat2_sp);
cpu_mat1->multiplyLeft(*cpu_mat2_sp);
// cout << gpu_mat1.tocpu().to_string(false, true) << endl;
// cout << "---" << endl;
// cout << cpu_mat1->to_string(false, true) << endl;
assert(calc_err(gpu_mat1, *cpu_mat1) < 1e-6);
cout << "OK" << endl;
}
void test_resize() void test_resize()
{ {
cout << "Test resize()" << endl; cout << "Test resize()" << endl;
...@@ -292,6 +328,7 @@ void test_add() ...@@ -292,6 +328,7 @@ void test_add()
cout << "test add" << endl; cout << "test add" << endl;
faust_unsigned_int nrows = 11, ncols = 9; faust_unsigned_int nrows = 11, ncols = 9;
auto lambda = 12.; auto lambda = 12.;
// test MatDense<double, GPU2> + MatDense<double, Cpu>
auto cpu_mat1 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols); auto cpu_mat1 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
auto cpu_mat2 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols); auto cpu_mat2 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
MatDense<double,GPU2> gpu_mat1(nrows, ncols, cpu_mat1->getData()); MatDense<double,GPU2> gpu_mat1(nrows, ncols, cpu_mat1->getData());
...@@ -302,27 +339,56 @@ void test_add() ...@@ -302,27 +339,56 @@ void test_add()
err_diff -= gpu_mat1_to_cpu; err_diff -= gpu_mat1_to_cpu;
auto err = err_diff.norm()/cpu_mat1->norm(); auto err = err_diff.norm()/cpu_mat1->norm();
assert(err < 1e-6); assert(err < 1e-6);
// test MatDense<double, GPU2> + MatSparse<double, Cpu>
Faust::MatSparse<double, Cpu> *cpu_mat3_sp = Faust::MatSparse<double, Cpu>::randMat(nrows, ncols, .2); Faust::MatSparse<double, Cpu> *cpu_mat3_sp = Faust::MatSparse<double, Cpu>::randMat(nrows, ncols, .2);
cpu_mat3_sp->Display(); // cpu_mat3_sp->Display();
cout << cpu_mat3_sp->to_string(false, true) << endl; // cout << cpu_mat3_sp->to_string(false, true) << endl;
assert(calc_err(gpu_mat1, *cpu_mat1) < 1e-6); assert(calc_err(gpu_mat1, *cpu_mat1) < 1e-6);
cout << "--- gpu_mat1 after" << endl; // cout << gpu_mat1.tocpu().to_string(false, true) << endl;
cout << gpu_mat1.tocpu().to_string(false, true) << endl; // cout << cpu_mat1->to_string(false, true) << endl;
cout << "--- cpu_mat1 before" << endl;
cout << cpu_mat1->to_string(false, true) << endl;
cout << "---" << endl;
cpu_mat1->add(*cpu_mat3_sp); cpu_mat1->add(*cpu_mat3_sp);
gpu_mat1.add(*cpu_mat3_sp); gpu_mat1.add(*cpu_mat3_sp);
cout << "--- cpu_mat1 after" << endl; // cout << cpu_mat1->to_string(false, true) << endl;
cout << cpu_mat1->to_string(false, true) << endl; // cout << gpu_mat1.tocpu().to_string(false, true) << endl;
cout << "--- gpu_mat1 after" << endl;
cout << gpu_mat1.tocpu().to_string(false, true) << endl;
auto err_add_sp = calc_err(gpu_mat1, *cpu_mat1); auto err_add_sp = calc_err(gpu_mat1, *cpu_mat1);
cout << err_add_sp << endl; // cout << err_add_sp << endl;
assert(err_add_sp < 1e-6); assert(err_add_sp < 1e-6);
cout << "OK" << endl; cout << "OK" << endl;
} }
void test_sub()
{
cout << "test sub" << endl;
faust_unsigned_int nrows = 11, ncols = 9;
auto lambda = 12.;
// test MatDense<double, GPU2> + MatDense<double, Cpu>
auto cpu_mat1 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
auto cpu_mat2 = Faust::MatDense<double,Cpu>::randMat(nrows,ncols);
MatDense<double,GPU2> gpu_mat1(nrows, ncols, cpu_mat1->getData());
cpu_mat1->sub(*cpu_mat2);
gpu_mat1.sub(*cpu_mat2);
auto gpu_mat1_to_cpu = gpu_mat1.tocpu();
auto err_diff = *cpu_mat1;
err_diff -= gpu_mat1_to_cpu;
auto err = err_diff.norm()/cpu_mat1->norm();
assert(err < 1e-6);
// test MatDense<double, GPU2> + MatSparse<double, Cpu>
Faust::MatSparse<double, Cpu> *cpu_mat3_sp = Faust::MatSparse<double, Cpu>::randMat(nrows, ncols, .2);
// cpu_mat3_sp->Display();
// cout << cpu_mat3_sp->to_string(false, true) << endl;
assert(calc_err(gpu_mat1, *cpu_mat1) < 1e-6);
// cout << gpu_mat1.tocpu().to_string(false, true) << endl;
// cout << cpu_mat1->to_string(false, true) << endl;
cpu_mat1->sub(*cpu_mat3_sp);
gpu_mat1.sub(*cpu_mat3_sp);
// cout << cpu_mat1->to_string(false, true) << endl;
// cout << gpu_mat1.tocpu().to_string(false, true) << endl;
auto err_sub_sp = calc_err(gpu_mat1, *cpu_mat1);
// cout << err_sub_sp << endl;
assert(err_sub_sp < 1e-6);
cout << "OK" << endl;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Faust::enable_gpu_mod(); Faust::enable_gpu_mod();
...@@ -340,5 +406,8 @@ int main(int argc, char** argv) ...@@ -340,5 +406,8 @@ int main(int argc, char** argv)
test_scalar_mul(); test_scalar_mul();
test_abs(); test_abs();
test_add(); test_add();
test_sub();
test_mul_eq_op();
test_mul_spm();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -20,14 +20,35 @@ namespace Faust ...@@ -20,14 +20,35 @@ namespace Faust
void add(const MatDense<FPP,Cpu> & A); void add(const MatDense<FPP,Cpu> & A);
void add(const MatDense<FPP,GPU2> & A); void add(const MatDense<FPP,GPU2> & A);
void add(const MatSparse<FPP,Cpu> & A); void add(const MatSparse<FPP,Cpu> & A);
void operator+=(const MatDense<FPP,GPU2> & A);
void operator+=(const MatDense<FPP,Cpu> & A);
void operator+=(const MatSparse<FPP,Cpu> & A);
//TODO: add(const MatSparse<FPP,GPU2>)
void sub(const MatDense<FPP,Cpu> & A);
void sub(const MatDense<FPP,GPU2> & A);
void sub(const MatSparse<FPP,Cpu> & A);
void operator-=(const MatDense<FPP,GPU2> & A);
void operator-=(const MatDense<FPP,Cpu> & A);
void operator-=(const MatSparse<FPP,Cpu> & A);
//TODO: sub(const MatSparse<FPP,GPU2>)
//TODO: void add(MatSparse<FPP,GPU2> const& A); //TODO: void add(MatSparse<FPP,GPU2> const& A);
// vec = this * vec // vec = this * vec
Vect<FPP, Cpu> multiply(const Vect<FPP, Cpu> &vec); Vect<FPP, Cpu> multiply(const Vect<FPP, Cpu> &vec);
// other = (*this) * other
void multiply(MatDense<FPP, GPU2> &other, const char op_this='N'); void multiply(MatDense<FPP, GPU2> &other, const char op_this='N');
// other = (*this) * other
void multiply(MatDense<FPP, Cpu> &other, const char op_this='N'); void multiply(MatDense<FPP, Cpu> &other, const char op_this='N');
// void multiply(MatSparse<FPP, Cpu> &other, MatDense<FPP, GPU2>& output, const char op_this='N'); // void multiply(MatSparse<FPP, Cpu> &other, MatDense<FPP, GPU2>& output, const char op_this='N');
void multiply(const MatSparse<FPP, Cpu> &other, MatDense<FPP, Cpu>& output, const char op_this='N'); void multiply(const MatSparse<FPP, Cpu> &other, MatDense<FPP, Cpu>& output, const char op_this='N');
void multiply(const MatSparse<FPP, Cpu> &other, MatDense<FPP, GPU2>& output, const char op_this='N'); void multiply(const MatSparse<FPP, Cpu> &other, MatDense<FPP, GPU2>& output, const char op_this='N');
//! \brief Replace (this) by (this) * A
void multiplyRight(const MatDense<FPP, Cpu>& A);
void multiplyRight(const MatDense<FPP, GPU2>& A);
//! \brief Replace (this) by S * (this)
void multiplyLeft(const MatSparse<FPP, Cpu>& S, const char transS='N');
void operator*=(const MatDense<FPP, GPU2> &other);
void operator*=(const MatDense<FPP, Cpu> &other);
// void operator*=(MatSparse<FPP, Cpu> &other);
void scalarMultiply(const FPP& lambda); void scalarMultiply(const FPP& lambda);
void resize(const faust_unsigned_int nbRow, const faust_unsigned_int nbCol); void resize(const faust_unsigned_int nbRow, const faust_unsigned_int nbCol);
void resize(const faust_unsigned_int nbRow){resize(nbRow,nbRow);} void resize(const faust_unsigned_int nbRow){resize(nbRow,nbRow);}
......
...@@ -124,6 +124,7 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::setZeros() ...@@ -124,6 +124,7 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::setZeros()
{ {
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs); auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->setzeros(gpu_mat); dsm_funcs->setzeros(gpu_mat);
this->isZeros = true;
} }
template<> template<>
...@@ -131,6 +132,7 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::setEyes() ...@@ -131,6 +132,7 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::setEyes()
{ {
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs); auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->seteyes(gpu_mat); dsm_funcs->seteyes(gpu_mat);
this->is_identity = true;
} }
template<> template<>
...@@ -225,3 +227,121 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::add(MatSparse<@FAUST_SCALAR_F ...@@ -225,3 +227,121 @@ void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::add(MatSparse<@FAUST_SCALAR_F
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs); auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->add_cpu_spm(this->gpu_mat, A.getNbRow(), A.getNbCol(), A.getNonZeros(), A.getRowPtr(), A.getColInd(), A.getValuePtr()); dsm_funcs->add_cpu_spm(this->gpu_mat, A.getNbRow(), A.getNbCol(), A.getNonZeros(), A.getRowPtr(), A.getColInd(), A.getValuePtr());
} }
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator+=(MatSparse<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
this->add(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator+=(MatDense<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
this->add(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator+=(MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> const& A)
{
this->add(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::sub(MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> const& A)
{
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->sub_gpu_dsm(this->gpu_mat, A.gpu_mat);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::sub(MatDense<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->sub_cpu_dsm(gpu_mat, const_cast<@FAUST_SCALAR_FOR_GM@*>(A.getData()), A.getNbRow(), A.getNbCol());
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::sub(MatSparse<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->sub_cpu_spm(this->gpu_mat, A.getNbRow(), A.getNbCol(), A.getNonZeros(), A.getRowPtr(), A.getColInd(), A.getValuePtr());
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator-=(MatSparse<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
this->sub(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator-=(MatDense<@FAUST_SCALAR_FOR_GM@,Cpu> const& A)
{
this->sub(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator-=(MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> const& A)
{
this->sub(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::multiplyRight(const MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> & A)
{
// other = this * other
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
dsm_funcs->mul_gpu_dsm_ext(this->gpu_mat, A.gpu_mat, this->gpu_mat, OP_NOTRANSP, OP_NOTRANSP);
Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> new_this(dim1, A.dim2, nullptr);
dsm_funcs->mul_gpu_dsm_ext(this->gpu_mat, A.gpu_mat, new_this.gpu_mat, OP_NOTRANSP, OP_NOTRANSP);
auto tmp = this->gpu_mat;
this->gpu_mat = new_this.gpu_mat;
new_this.gpu_mat = tmp;
this->dim2 = A.dim2;
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::multiplyRight(const MatDense<@FAUST_SCALAR_FOR_GM@,Cpu> & A)
{
// this = this * other
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> gpuA(A.getNbRow(), A.getNbCol(), A.getData());
Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> new_this(dim1, A.dim2, nullptr);
dsm_funcs->mul_gpu_dsm_ext(this->gpu_mat, gpuA.gpu_mat, new_this.gpu_mat, OP_NOTRANSP, OP_NOTRANSP);
auto tmp = this->gpu_mat;
this->gpu_mat = new_this.gpu_mat;
new_this.gpu_mat = tmp;
this->dim2 = A.dim2;
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator*=(const MatDense<@FAUST_SCALAR_FOR_GM@,GPU2> & A)
{
this->multiplyRight(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::operator*=(const MatDense<@FAUST_SCALAR_FOR_GM@,Cpu> & A)
{
this->multiplyRight(A);
}
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@, GPU2>::multiplyLeft(const MatSparse<@FAUST_SCALAR_FOR_GM@,Cpu> & S, const char transS/*='N'*/)
{
auto spm_funcs = ((gm_SparseMatFunc_@GM_SCALAR@*)this->spm_funcs);
auto dsm_funcs = ((gm_DenseMatFunc_@GM_SCALAR@*)this->dsm_funcs);
if(isZeros)
{
setZeros();
}
else
{
auto gpu_S = spm_funcs->togpu(S.getNbRow(), S.getNbCol(), S.getNonZeros(), S.getRowPtr(), S.getColInd(), S.getValuePtr());
auto gpu_out = spm_funcs->mul_gpu_dsm_ext(gpu_S, this->gpu_mat, nullptr, OP_NOTRANSP, OP_NOTRANSP);
dsm_funcs->free(this->gpu_mat);
spm_funcs->free(gpu_S);
this->gpu_mat = gpu_out;
this->dim1 = S.getNbRow();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment