Mentions légales du service

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

Add GPU elementwise multiplication for MatDense and a unit test.

Update to gpu_mod@379f19c4
parent d534cc08
No related branches found
No related tags found
No related merge requests found
Subproject commit befc13821982df29cc542f8912d281a72880b867 Subproject commit 379f19c4a8ec5d6170da6ba23847c9ac73ea1987
...@@ -460,6 +460,32 @@ void test_mul_vec() ...@@ -460,6 +460,32 @@ void test_mul_vec()
assert(err < 1e-3); assert(err < 1e-3);
} }
void test_eltwise_mul_vec()
{
cout << "test MatDense<FPP,GPU2>::eltwise_mul(const Vect<FPP,GPU2> &vec)" << endl;
auto size = 2048;
auto nrows = 1024;
auto ncols = size;
auto vec_mat = MatDense<FPP, Cpu>::randMat(size, 1);
Faust::Vect<FPP, Cpu> v_cpu(size, vec_mat->getData());
Faust::Vect<FPP, GPU2> v_gpu(size, vec_mat->getData());
auto cpu_mat = Faust::MatDense<FPP,Cpu>::randMat(nrows,ncols);
MatDense<FPP,GPU2> gpu_mat(nrows, ncols, cpu_mat->getData());
gpu_mat.eltwise_mul(v_gpu);
auto cpu_mat_out = gpu_mat.tocpu();
cpu_mat_out.Display();
// test each element
for(int i=0; i < cpu_mat->getNbRow();i++)
for(int j=0; j < cpu_mat->getNbCol(); j++)
{
auto test_elt = cpu_mat_out.getData()[j*cpu_mat_out.getNbRow()+i];
auto ref_elt = cpu_mat->getData()[j*cpu_mat_out.getNbRow()+i] * v_cpu[i];
assert(std::abs(test_elt - ref_elt) < 1e-6);
}
delete cpu_mat;
delete vec_mat;
}
void test_gpu_matsparse2matdense() void test_gpu_matsparse2matdense()
{ {
cout << "test MatDense<FPP,GPU2>::MatDense<FPP,GPU2>(MatSparse<FPP,GPU2>&)" << endl; cout << "test MatDense<FPP,GPU2>::MatDense<FPP,GPU2>(MatSparse<FPP,GPU2>&)" << endl;
...@@ -504,6 +530,7 @@ void test_real() ...@@ -504,6 +530,7 @@ void test_real()
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Faust::enable_gpu_mod(); Faust::enable_gpu_mod();
test_eltwise_mul_vec();
test_real(); test_real();
test_mul_gpu_dense(); test_mul_gpu_dense();
test_resize(); test_resize();
......
...@@ -103,6 +103,13 @@ namespace Faust ...@@ -103,6 +103,13 @@ namespace Faust
return out_v; return out_v;
} }
template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::eltwise_mul(const Vect<@FAUST_SCALAR_FOR_GM@, GPU2> &gpu_vec)
{
auto dsm_funcs = GPUModHandler::get_singleton()->dsm_funcs(@FAUST_SCALAR_FOR_GM@(0));
dsm_funcs->elt_wise_mul(this->gpu_mat, gpu_vec.gpu_mat);
}
template<> template<>
void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::multiply(const MatSparse<@FAUST_SCALAR_FOR_GM@, Cpu> &other, MatDense<@FAUST_SCALAR_FOR_GM@,Cpu>& output, const char op_this) void Faust::MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>::multiply(const MatSparse<@FAUST_SCALAR_FOR_GM@, Cpu> &other, MatDense<@FAUST_SCALAR_FOR_GM@,Cpu>& output, const char op_this)
{ {
......
...@@ -65,6 +65,11 @@ namespace Faust ...@@ -65,6 +65,11 @@ namespace Faust
//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);
/**
* *this = *this * vec (element-wise multiplication)
* possible broadcasting
*/
void eltwise_mul(const Vect<FPP, GPU2> &vec);
// other = (*this) * other // other = (*this) * other
void multiply(const MatDense<FPP, GPU2> &other, const char op_this='N'); void multiply(const MatDense<FPP, GPU2> &other, const char op_this='N');
// other = (*this) * other // other = (*this) * other
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment