Mentions légales du service

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

Add GPU Faust fancy idx support in pyfaust wrapper.

Refactor fancy idx functions from TransformHelper<FPP,Cpu> into parent TransformHelperGen,
Update Transform(Helper)<FPP,GPU2> for fancy idx support.
parent 8ed1dfa4
Branches
Tags
No related merge requests found
Showing
with 152 additions and 99 deletions
......@@ -80,7 +80,6 @@ namespace Faust
#ifdef USE_GPU_MOD
FaustGPU<FPP> *gpu_faust;
#endif
void eval_fancy_idx_Transform();
public:
TransformHelper(const std::vector<MatGeneric<FPP,Cpu> *>& facts, const FPP lambda_ = (FPP)1.0, const bool optimizedCopy=false, const bool cloning_fact = true, const bool internal_call=false);
TransformHelper();
......@@ -148,7 +147,6 @@ namespace Faust
faust_unsigned_int* num_rows,
faust_unsigned_int* num_cols,
const bool transpose = false) const;
TransformHelper<FPP, Cpu>* fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols);
MatDense<FPP,Cpu> get_product();// const;
void get_product(MatDense<FPP,Cpu>& prod) const;
void save_mat_file(const char* filename) const;
......
......@@ -137,30 +137,7 @@ namespace Faust {
template<typename FPP>
TransformHelper<FPP,Cpu>::TransformHelper(TransformHelper<FPP,Cpu>* th, faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols): TransformHelper<FPP,Cpu>()
{
this->transform = th->transform; //do not remove this line, necessary for eval*()
this->copy_transconj_state(th);
this->is_sliced = false;
//TODO: check indices
// handleError("Faust::TransformHelper::TransformHelper(TransformHelper,Slice)", "Fancy indexing overflows a Faust dimension.");
unsigned int id0=0, id1=1;
this->fancy_num_cols = num_cols;
this->fancy_num_rows = num_rows;
if(this->is_transposed)
{
id0 = 1;
id1 = 0;
this->fancy_num_cols = num_rows;
this->fancy_num_rows = num_cols;
}
this->fancy_indices[id0] = new faust_unsigned_int[num_rows];
this->fancy_indices[id1] = new faust_unsigned_int[num_cols];
this->is_fancy_indexed= true;
memcpy(this->fancy_indices[id0], row_ids, num_rows*sizeof(faust_unsigned_int));
memcpy(this->fancy_indices[id1], col_ids, num_cols*sizeof(faust_unsigned_int));
eval_fancy_idx_Transform();
delete[] this->fancy_indices[0];
delete[] this->fancy_indices[1];
copy_mul_mode_state(th);
this->init_fancy_idx_transform(th, row_ids, num_rows, col_ids, num_cols);
}
template<typename FPP>
......@@ -931,58 +908,6 @@ namespace Faust {
Faust::conjugate(elts,*num_cols*(*num_rows));
}
template<typename FPP>
TransformHelper<FPP, Cpu>* TransformHelper<FPP,Cpu>::fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols)
{
return new TransformHelper<FPP,Cpu>(this, row_ids, num_rows, col_ids, num_cols);
}
template<typename FPP>
void TransformHelper<FPP, Cpu>::eval_fancy_idx_Transform()
{
bool cloning_fact = false;
faust_unsigned_int size = this->size();
std::vector<MatGeneric<FPP,Cpu>*> factors((size_t) size);
MatGeneric<FPP,Cpu>* gen_fac, *first_sub_fac, *last_sub_fac;
gen_fac = this->transform->get_fact(0, cloning_fact);
// first_sub_fac = gen_fac->get_rows(this->slices[0].start_id, this->slices[0].end_id-this->slices[0].start_id);
// first_sub_fac->Display();
first_sub_fac = gen_fac->get_rows(this->fancy_indices[0], this->fancy_num_rows);
if(cloning_fact)
delete gen_fac;
if(size > 1) {
gen_fac = this->transform->get_fact(size-1, cloning_fact);
// last_sub_fac = gen_fac->get_cols(this->slices[1].start_id, this->slices[1].end_id-this->slices[1].start_id);
last_sub_fac = gen_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols); // std::cout << "---" << std::endl;
// last_sub_fac->Display();
if(cloning_fact)
delete gen_fac;
factors.reserve(size);
factors.insert(factors.begin(), first_sub_fac);
if(size > 2)
{
auto it = factors.begin();
for(faust_unsigned_int i = 1; i < size-1; i++)
{
gen_fac = this->transform->get_fact(i, cloning_fact);
factors[i] = gen_fac;
}
}
factors.insert(factors.begin()+(size-1), last_sub_fac);
factors.resize(size);
}
else { //only one factor
last_sub_fac = first_sub_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols);
delete first_sub_fac;
factors[0] = last_sub_fac;
factors.resize(1);
}
this->transform = make_shared<Transform<FPP, Cpu>>(factors, 1.0, false, cloning_fact);
if(cloning_fact) {
for(faust_unsigned_int i = 0; i < size; i++)
delete factors[i];
}
}
......
......@@ -15,6 +15,7 @@ namespace Faust
public:
TransformHelper();
TransformHelper(TransformHelper<FPP,GPU2>* th, Slice s[2]);
TransformHelper(TransformHelper<FPP,GPU2>* th, faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols);
TransformHelper(const std::vector<MatGeneric<FPP,GPU2> *>& facts, const FPP lambda_ = (FPP)1.0, const bool optimizedCopy=false, const bool cloning_fact = true, const bool internal_call=false);
TransformHelper(const TransformHelper<FPP,Cpu>& cpu_t, int32_t dev_id=-1, void* stream=nullptr);
TransformHelper(const TransformHelper<FPP,GPU2>& th, bool transpose, bool conjugate);
......@@ -67,10 +68,6 @@ namespace Faust
TransformHelper<FPP,GPU2>* transpose();
TransformHelper<FPP,GPU2>* conjugate();
TransformHelper<FPP,GPU2>* adjoint();
// TransformHelper<FPP, GPU2>* slice(faust_unsigned_int start_row_id, faust_unsigned_int end_row_id,
// faust_unsigned_int start_col_id, faust_unsigned_int end_col_id);
TransformHelper<FPP, GPU2>* fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols);
TransformHelper<FPP,GPU2>* optimize_time(const bool transp=false, const bool inplace=false, const int nsamples=1);
TransformHelper<FPP,GPU2>* optimize(const bool transp=false);
TransformHelper<FPP,GPU2>* optimize_storage(const bool time=true);
......
......@@ -38,6 +38,12 @@ namespace Faust
this->init_sliced_transform(th, s);
}
template<typename FPP>
TransformHelper<FPP,GPU2>::TransformHelper(TransformHelper<FPP,GPU2>* th, faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols): TransformHelper<FPP,GPU2>()
{
this->init_fancy_idx_transform(th, row_ids, num_rows, col_ids, num_cols);
}
#ifndef IGNORE_TRANSFORM_HELPER_VARIADIC_TPL
template<typename FPP>
template<typename ... GList>
......@@ -455,19 +461,6 @@ namespace Faust
return t;
}
// template<typename FPP>
// TransformHelper<FPP, GPU2>* TransformHelper<FPP, GPU2>::slice(faust_unsigned_int start_row_id, faust_unsigned_int end_row_id,
// faust_unsigned_int start_col_id, faust_unsigned_int end_col_id)
// {
// }
template<typename FPP>
TransformHelper<FPP, GPU2>* TransformHelper<FPP,GPU2>::fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols)
{
throw std::runtime_error("fancy_index is yet to implement in Faust C++ core for GPU.");
return nullptr;
}
template<typename FPP>
faust_unsigned_int TransformHelper<FPP,GPU2>::getNBytes() const
{
......
......@@ -88,10 +88,12 @@ namespace Faust
int get_mul_order_opt_mode() const;
int get_Fv_mul_mode() const;
void eval_sliced_Transform();
void eval_fancy_idx_Transform();
TransformHelper<FPP, DEV>* slice(faust_unsigned_int start_row_id, faust_unsigned_int end_row_id,
faust_unsigned_int start_col_id, faust_unsigned_int end_col_id);
TransformHelper<FPP, DEV>* fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols);
protected:
void init_fancy_idx_transform(TransformHelper<FPP,DEV>* th, faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols);
void init_sliced_transform(TransformHelper<FPP,DEV>* th, Slice s[2]);
bool is_transposed;
bool is_conjugate;
......
......@@ -15,6 +15,36 @@ namespace Faust
this->is_conjugate = conjugate?!th->is_conjugate:th->is_conjugate;
}
template<typename FPP, FDevice DEV>
void TransformHelperGen<FPP,DEV>::init_fancy_idx_transform(TransformHelper<FPP,DEV>* th, faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols)
{
this->transform = th->transform; //do not remove this line, necessary for eval*()
this->copy_transconj_state(*th);
this->is_sliced = false;
//TODO: check indices
// handleError("Faust::TransformHelper::TransformHelper(TransformHelper,Slice)", "Fancy indexing overflows a Faust dimension.");
unsigned int id0=0, id1=1;
this->fancy_num_cols = num_cols;
this->fancy_num_rows = num_rows;
if(this->is_transposed)
{
id0 = 1;
id1 = 0;
this->fancy_num_cols = num_rows;
this->fancy_num_rows = num_cols;
}
this->fancy_indices[id0] = new faust_unsigned_int[num_rows];
this->fancy_indices[id1] = new faust_unsigned_int[num_cols];
this->is_fancy_indexed = true;
memcpy(this->fancy_indices[id0], row_ids, num_rows*sizeof(faust_unsigned_int));
memcpy(this->fancy_indices[id1], col_ids, num_cols*sizeof(faust_unsigned_int));
this->eval_fancy_idx_Transform();
delete[] this->fancy_indices[0];
delete[] this->fancy_indices[1];
copy_mul_mode_state(*th);
}
template<typename FPP, FDevice DEV>
void TransformHelperGen<FPP,DEV>::init_sliced_transform(TransformHelper<FPP,DEV>* th, Slice s[2])
......@@ -313,4 +343,57 @@ namespace Faust
return new TransformHelper<FPP, DEV>(dynamic_cast<TransformHelper<FPP, DEV>*>(this), s);
}
template<typename FPP, FDevice DEV>
TransformHelper<FPP, DEV>* TransformHelperGen<FPP,DEV>::fancy_index(faust_unsigned_int* row_ids, faust_unsigned_int num_rows, faust_unsigned_int* col_ids, faust_unsigned_int num_cols)
{
return new TransformHelper<FPP,DEV>(dynamic_cast<TransformHelper<FPP, DEV>*>(this), row_ids, num_rows, col_ids, num_cols);
}
template<typename FPP, FDevice DEV>
void TransformHelperGen<FPP, DEV>::eval_fancy_idx_Transform()
{
bool cloning_fact = false;
faust_unsigned_int size = this->size();
std::vector<MatGeneric<FPP,DEV>*> factors((size_t) size);
MatGeneric<FPP,DEV>* gen_fac, *first_sub_fac, *last_sub_fac;
gen_fac = this->transform->get_fact(0, cloning_fact);
// first_sub_fac = gen_fac->get_rows(this->slices[0].start_id, this->slices[0].end_id-this->slices[0].start_id);
// first_sub_fac->Display();
first_sub_fac = gen_fac->get_rows(this->fancy_indices[0], this->fancy_num_rows);
if(cloning_fact)
delete gen_fac;
if(size > 1) {
gen_fac = this->transform->get_fact(size-1, cloning_fact);
// last_sub_fac = gen_fac->get_cols(this->slices[1].start_id, this->slices[1].end_id-this->slices[1].start_id);
last_sub_fac = gen_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols); // std::cout << "---" << std::endl;
// last_sub_fac->Display();
if(cloning_fact)
delete gen_fac;
factors.reserve(size);
factors.insert(factors.begin(), first_sub_fac);
if(size > 2)
{
auto it = factors.begin();
for(faust_unsigned_int i = 1; i < size-1; i++)
{
gen_fac = this->transform->get_fact(i, cloning_fact);
factors[i] = gen_fac;
}
}
factors.insert(factors.begin()+(size-1), last_sub_fac);
factors.resize(size);
}
else { //only one factor
last_sub_fac = first_sub_fac->get_cols(this->fancy_indices[1], this->fancy_num_cols);
delete first_sub_fac;
factors[0] = last_sub_fac;
factors.resize(1);
}
this->transform = make_shared<Transform<FPP, DEV>>(factors, 1.0, false, cloning_fact);
if(cloning_fact) {
for(faust_unsigned_int i = 0; i < size; i++)
delete factors[i];
}
}
}
......@@ -101,7 +101,7 @@ class FaustCoreCpp
FaustCoreCpp<FPP,DEV>* slice(unsigned int, unsigned int, unsigned int, unsigned int) const;
FaustCoreCpp<FPP,DEV>* fancy_idx(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols);
unsigned long int num_cols) const;
bool save_mat_file(const char* filepath) const;
FaustCoreCpp<FPP,DEV>* optimize_storage(const bool time=false);
FaustCoreCpp<FPP,DEV>* optimize(const bool transp=false);
......@@ -147,6 +147,9 @@ class FaustCoreCppGPU: public FaustCoreCpp<FPP, GPU2>
FaustCoreCppGPU<FPP>* horzcat_gpu(FaustCoreCppGPU<FPP>* right) const;
FaustCoreCppGPU<FPP>* vertcat_gpu(FaustCoreCppGPU<FPP>* right) const;
FaustCoreCppGPU<FPP>* slice_gpu(unsigned int start_row_id, unsigned int end_row_id, unsigned int start_col_id, unsigned int end_col_id) const;
FaustCoreCppGPU<FPP>* fancy_idx_gpu(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols) const;
void device_gpu(char* dev) const;
static FaustCoreCppGPU<FPP>* randFaustGPU(unsigned int t,
unsigned int min_num_factors, unsigned int max_num_factors,
......
......@@ -323,7 +323,7 @@ FaustCoreCpp<FPP,DEV>* FaustCoreCpp<FPP,DEV>::left(const faust_unsigned_int id)
template<typename FPP, FDevice DEV>
FaustCoreCpp<FPP,DEV>* FaustCoreCpp<FPP,DEV>::fancy_idx(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols)
unsigned long int num_cols) const
{
Faust::TransformHelper<FPP,DEV>* th = this->transform->fancy_index(row_ids, num_rows, col_ids, num_cols);
FaustCoreCpp<FPP,DEV>* core = new FaustCoreCpp<FPP,DEV>(th);
......
......@@ -148,4 +148,12 @@ FaustCoreCppGPU<FPP>* FaustCoreCppGPU<FPP>::slice_gpu(unsigned int start_row_id,
{
return (FaustCoreCppGPU<FPP>*) FaustCoreCpp<FPP, GPU2>::slice(start_row_id, end_row_id, start_col_id, end_col_id);
}
template<typename FPP>
FaustCoreCppGPU<FPP>* FaustCoreCppGPU<FPP>::fancy_idx_gpu(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols) const
{
return (FaustCoreCppGPU<FPP>*) FaustCoreCpp<FPP, GPU2>::fancy_idx(row_ids, num_rows, col_ids, num_cols);
}
#endif
......@@ -87,7 +87,7 @@ cdef extern from "FaustCoreCpp.h":
unsigned int) const
FaustCoreCpp[FPP]* fancy_idx(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols)
unsigned long int num_cols) const
bool save_mat_file(const char* filepath) const
FaustCoreCpp[FPP]* optimize_time(const bool transp, const bool inplace,
const int nsamples)
......
......@@ -38,4 +38,7 @@ cdef extern from "FaustCoreCpp.h":
void device_gpu(char*) const
FaustCoreCppGPU[FPP]* slice_gpu(unsigned int, unsigned int, unsigned int,
unsigned int) const
FaustCoreCppGPU[FPP]* fancy_idx_gpu(unsigned long int* row_ids, unsigned long int
num_rows, unsigned long int* col_ids,
unsigned long int num_cols) const
......@@ -671,3 +671,44 @@ cdef class FaustCoreGPU:
core._isReal = self._isReal
return core
def fancy_idx(self, indices):
cdef unsigned long int[:] row_indices_view
cdef unsigned long int[:] col_indices_view
core = FaustCoreGPU(core=True)
# fancy indexing
#convert possible slice (on index 0 or 1 of out_indices) to
# an array of indices
for i in range(0,2):
if(isinstance(indices[i], slice)):
indices[i] = list(range(indices[i].start, indices[i].stop,
indices[i].step))
# it's possible that on certain architectures unsigned long int is a
# 4-bytes integer
# TODO: move to a cross-platform size type (like uint32/64_t from stdlib.h)
if(sizeof(unsigned long int) == 8):
dtype = np.uint64
elif(sizeof(unsigned long int) == 4):
dtype = np.uint32
row_indices = np.array(indices[0], dtype=dtype)
col_indices = np.array(indices[1], dtype=dtype)
row_indices_view = row_indices
col_indices_view = col_indices
# print("FaustCorePy.fancy_idx(), row_indices=", row_indices, " size=",
# row_indices.size)
# print("FaustCorePy.fancy_idx(), col_indices=", col_indices," size=",
# col_indices.size)
if(self._isReal):
core.core_faust_dbl = \
self.core_faust_dbl.fancy_idx_gpu(&row_indices_view[0], row_indices.size,
&col_indices_view[0], col_indices.size)
# else:
# core.core_faust_cplx = \
# self.core_faust_cplx.fancy_idx(&row_indices_view[0],
# row_indices.size,
# &col_indices_view[0],
# col_indices.size)
#
core._isReal = self._isReal
return core
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment