Mentions légales du service

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

Manage GPU memory references with RefManager in Transform<FPP,GPU2>.

parent 327322f4
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ namespace Faust ...@@ -23,7 +23,7 @@ namespace Faust
} }
template<> template<>
Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::Transform() : gpu_mat_arr(nullptr) Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::Transform() : gpu_mat_arr(nullptr), dtor_delete_data(false), dtor_disabled(false), data(std::vector<Faust::MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>*>())
{ {
} }
...@@ -32,24 +32,32 @@ namespace Faust ...@@ -32,24 +32,32 @@ namespace Faust
{ {
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0));
if(gpu_mat_arr != nullptr) if(gpu_mat_arr != nullptr)
marr_funcs->free(this->gpu_mat_arr, /* del mats*/ true); marr_funcs->free(this->gpu_mat_arr, /* del mats*/ false); // remove gpu mats individually
gpu_mat_arr = nullptr; gpu_mat_arr = nullptr;
if(! this->dtor_disabled)
{
for (int i=0;i<data.size();i++)
if(this->dtor_delete_data)
delete data[i];
else
ref_man.release(data[i]);
}
} }
template<> template<>
void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::push_back(const MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>* M, bool copying/*=true*/) void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::push_back(const MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>* M, bool copying/*=true*/)
{ {
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0));
auto pushed_M = M;
if(copying)
pushed_M = M->clone();
if(gpu_mat_arr == nullptr) if(gpu_mat_arr == nullptr)
gpu_mat_arr = marr_funcs->create(); gpu_mat_arr = marr_funcs->create();
if(copying) marr_funcs->addgpu_anymat(gpu_mat_arr, pushed_M->get_gpu_mat_ptr());
marr_funcs->addgpu_anymat(gpu_mat_arr, M->clone()->get_gpu_mat_ptr()); data.push_back(const_cast<MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>*>(pushed_M));
else if(!dtor_delete_data) ref_man.acquire(const_cast<MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>*>(pushed_M));
marr_funcs->addgpu_anymat(gpu_mat_arr, M->get_gpu_mat_ptr());
} }
template<> template<>
int32_t Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::size() const int32_t Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::size() const
{ {
...@@ -61,26 +69,30 @@ namespace Faust ...@@ -61,26 +69,30 @@ namespace Faust
template<> template<>
MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>* Faust::Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::get_fact(int32_t id, bool cloning_fact) const MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>* Faust::Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::get_fact(int32_t id, bool cloning_fact) const
{ {
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); // auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0));
auto is_sparse = marr_funcs->is_sparse_at(this->gpu_mat_arr, id); // auto is_sparse = marr_funcs->is_sparse_at(this->gpu_mat_arr, id);
MatGeneric<@FAUST_SCALAR_FOR_GM@, GPU2>* M = nullptr; // MatGeneric<@FAUST_SCALAR_FOR_GM@, GPU2>* M = nullptr;
if(is_sparse) // if(is_sparse)
{ // {
M = new MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>(); // M = new MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>();
M->set_gpu_mat_ptr(marr_funcs->get_spm(this->gpu_mat_arr, id)); // M->set_gpu_mat_ptr(marr_funcs->get_spm(this->gpu_mat_arr, id));
} // }
else // else
{ // {
M = new MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>(); // M = new MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>();
M->set_gpu_mat_ptr(marr_funcs->get_dsm(this->gpu_mat_arr, id)); // M->set_gpu_mat_ptr(marr_funcs->get_dsm(this->gpu_mat_arr, id));
} // }
// if(cloning_fact)
// {
// auto tmp = M;
// M = M->clone();
// delete tmp;
// }
// return M;
if(cloning_fact) if(cloning_fact)
{ return data[id]->clone();
auto tmp = M; else
M = M->clone(); return data[id];
delete tmp;
}
return M;
} }
template<> template<>
...@@ -104,6 +116,7 @@ namespace Faust ...@@ -104,6 +116,7 @@ namespace Faust
auto sM = dynamic_cast<const MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>*>(&M); auto sM = dynamic_cast<const MatSparse<@FAUST_SCALAR_FOR_GM@,GPU2>*>(&M);
*sfact = *sM; *sfact = *sM;
} }
// fact->set_id(M.is_id()); //TODO: in MatGeneric<FPP,GPU2> add is_id/set_id
} }
template<> template<>
...@@ -120,7 +133,17 @@ namespace Faust ...@@ -120,7 +133,17 @@ namespace Faust
{ {
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0));
if(gpu_mat_arr != nullptr) if(gpu_mat_arr != nullptr)
marr_funcs->free(this->gpu_mat_arr, /* del mats*/ true); marr_funcs->free(this->gpu_mat_arr, /* del mats*/ false);
for (int i=0;i<data.size();i++)
{
if(dtor_delete_data)
delete data[i];
else
{
ref_man.release(data[i]);
}
}
data.resize(0);
gpu_mat_arr = marr_funcs->create(); gpu_mat_arr = marr_funcs->create();
} }
...@@ -146,10 +169,12 @@ namespace Faust ...@@ -146,10 +169,12 @@ namespace Faust
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0));
if(gpu_mat_arr == nullptr) if(gpu_mat_arr == nullptr)
gpu_mat_arr = marr_funcs->create(); gpu_mat_arr = marr_funcs->create();
auto ins_M = const_cast<MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>*>(M);
if(copying) if(copying)
marr_funcs->insert_anymat(gpu_mat_arr, M->clone()->get_gpu_mat_ptr(), id); ins_M = M->clone();
else marr_funcs->insert_anymat(gpu_mat_arr, ins_M->get_gpu_mat_ptr(), id);
marr_funcs->insert_anymat(gpu_mat_arr, M->get_gpu_mat_ptr(), id); data.insert(data.begin()+id, ins_M);
if(!dtor_delete_data) ref_man.acquire(ins_M);
} }
template<> template<>
...@@ -166,6 +191,8 @@ namespace Faust ...@@ -166,6 +191,8 @@ namespace Faust
marr_funcs->remove_mat(gpu_mat_arr, id); marr_funcs->remove_mat(gpu_mat_arr, id);
else else
throw std::runtime_error("gpu_mat_arr is nullptr"); throw std::runtime_error("gpu_mat_arr is nullptr");
if(!dtor_delete_data) ref_man.release(*(data.begin()+id));
data.erase(data.begin()+id);
} }
template<> template<>
...@@ -182,16 +209,13 @@ namespace Faust ...@@ -182,16 +209,13 @@ namespace Faust
marr_funcs->transpose(gpu_mat_arr); marr_funcs->transpose(gpu_mat_arr);
else else
throw std::runtime_error("gpu_mat_arr is nullptr"); throw std::runtime_error("gpu_mat_arr is nullptr");
std::reverse(data.begin(),data.end());
} }
template<> template<>
void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::pop_back() void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::pop_back()
{ {
auto marr_funcs = GPUModHandler::get_singleton()->marr_funcs((@FAUST_SCALAR_FOR_GM@)(0)); this->erase(size()-1);
if(gpu_mat_arr != nullptr)
marr_funcs->remove_mat(gpu_mat_arr, size()-1);
else
throw std::runtime_error("gpu_mat_arr is nullptr");
} }
template<> template<>
...@@ -216,8 +240,6 @@ namespace Faust ...@@ -216,8 +240,6 @@ namespace Faust
marr_funcs->display(gpu_mat_arr); marr_funcs->display(gpu_mat_arr);
} }
template<> template<>
void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::get_product(MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>& M, const char opThis/*='N'*/, const bool isConj/*=false*/) const void Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::get_product(MatDense<@FAUST_SCALAR_FOR_GM@,GPU2>& M, const char opThis/*='N'*/, const bool isConj/*=false*/) const
{ {
...@@ -433,6 +455,13 @@ namespace Faust ...@@ -433,6 +455,13 @@ namespace Faust
return Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::iterator(*this, size()); return Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::iterator(*this, size());
} }
template<>
Faust::RefManager Faust::Transform<@FAUST_SCALAR_FOR_GM@,GPU2>::ref_man([](void *fact)
{
//#ifdef DEBUG
std::cout << "Faust::Transform delete_fact" << std::endl;
//#endif
delete static_cast<MatGeneric<@FAUST_SCALAR_FOR_GM@,GPU2>*>(fact);
});
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "faust_MatGeneric_gpu.h" #include "faust_MatGeneric_gpu.h"
#include "faust_MatSparse_gpu.h" #include "faust_MatSparse_gpu.h"
#include "faust_MatDense_gpu.h" #include "faust_MatDense_gpu.h"
#include "faust_RefManager.h"
#include <vector> #include <vector>
namespace Faust namespace Faust
...@@ -15,6 +16,11 @@ namespace Faust ...@@ -15,6 +16,11 @@ namespace Faust
class Transform<FPP,GPU2> class Transform<FPP,GPU2>
{ {
gm_MatArray_t gpu_mat_arr; gm_MatArray_t gpu_mat_arr;
std::vector<MatGeneric<FPP,GPU2>*> data; // same factors as in gpu_mat_arr
// just pointer copies
bool dtor_delete_data;
bool dtor_disabled;
static RefManager ref_man;
public: public:
Transform(); Transform();
Transform(const std::vector<MatGeneric<FPP,GPU2>*> &factors); Transform(const std::vector<MatGeneric<FPP,GPU2>*> &factors);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment