Mentions légales du service

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

Add cuMatArray::to_string, use it to refactor cuMatArray::display and add it to the C interface.

parent f1e6c967
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <type_traits> #include <type_traits>
#include <string>
#include <cstdio>
//TODO: function to get nrows, ncols of a list matrix //TODO: function to get nrows, ncols of a list matrix
//TODO: function to get nnz of a list sparse matrix //TODO: function to get nnz of a list sparse matrix
...@@ -45,6 +47,8 @@ struct cuMatArray ...@@ -45,6 +47,8 @@ struct cuMatArray
* Displays the the list of factors according to op. * Displays the the list of factors according to op.
*/ */
void display(gm_Op op=OP_NOTRANSP); void display(gm_Op op=OP_NOTRANSP);
std::string to_string(gm_Op op=OP_NOTRANSP);
/** /**
* Computes the chain matrix product in cu_array applying the op (transpose, etc.) to it and if out is not nullptr it receives the result. * Computes the chain matrix product in cu_array applying the op (transpose, etc.) to it and if out is not nullptr it receives the result.
* The product is computed from the right factor to the left. * The product is computed from the right factor to the left.
......
...@@ -35,7 +35,13 @@ int32_t cuMatArray<T>::ncols() const ...@@ -35,7 +35,13 @@ int32_t cuMatArray<T>::ncols() const
template<typename T> template<typename T>
void cuMatArray<T>::display(gm_Op op) void cuMatArray<T>::display(gm_Op op)
{ {
// std::cout << "gm::display" << std::endl; std::cout << to_string(op) << std::endl;
}
template<typename T>
std::string cuMatArray<T>::to_string(gm_Op op)
{
std::string out_str="";
std::vector<int> im(cu_array.size()); std::vector<int> im(cu_array.size());
for(int i=0;i<cu_array.size();i++) im[i] = i; for(int i=0;i<cu_array.size();i++) im[i] = i;
if(op != OP_NOTRANSP) if(op != OP_NOTRANSP)
...@@ -43,15 +49,18 @@ void cuMatArray<T>::display(gm_Op op) ...@@ -43,15 +49,18 @@ void cuMatArray<T>::display(gm_Op op)
for(int i : im) for(int i : im)
{ {
//#ifdef DEBUG //#ifdef DEBUG
std::cout << "- GPU FACTOR " << i << ((std::is_same<T,cuComplex>::value || std::is_same<T,cuDoubleComplex>::value)?" (complex)":" (real)") << (cu_array[i]->is_sparse?" SPARSE":" DENSE") << " size "; out_str += std::string("- GPU FACTOR ") + std::to_string(i) + ((std::is_same<T,cuComplex>::value || std::is_same<T,cuDoubleComplex>::value)?" (complex)":" (real)") + (cu_array[i]->is_sparse?" SPARSE":" DENSE") + " size ";
std::cout << (op == OP_NOTRANSP?cu_array[i]->nrows:cu_array[i]->ncols); out_str += std::to_string(op == OP_NOTRANSP?cu_array[i]->nrows:cu_array[i]->ncols);
std::cout << " x " << (op == OP_NOTRANSP?cu_array[i]->ncols:cu_array[i]->nrows); out_str += std::string(" x ") + std::to_string(op == OP_NOTRANSP?cu_array[i]->ncols:cu_array[i]->nrows);
std::cout << ", addr: " << cu_array[i]; out_str += std::string(", addr: ");
std::cout << ", density " << ((double)cu_array[i]->get_nnz())/cu_array[i]->nrows/cu_array[i]->ncols // std::to_string(static_cast<void*>(cu_array[i]));
<< ", nnz " << cu_array[i]->get_nnz() char addr[sizeof(void*)+1];
<< std::endl; std::sprintf(addr, "%p", cu_array[i]);
out_str += std::string((const char*)addr);
out_str += std::string(", density ") + std::to_string(((double)cu_array[i]->get_nnz())/cu_array[i]->nrows/cu_array[i]->ncols) + ", nnz " + std::to_string(cu_array[i]->get_nnz());
//#endif //#endif
} }
return out_str;
} }
template<typename T> template<typename T>
......
...@@ -594,6 +594,18 @@ extern "C" ...@@ -594,6 +594,18 @@ extern "C"
return static_cast<cuMatArray<@GM_SCALAR@>*>(array)->display(); return static_cast<cuMatArray<@GM_SCALAR@>*>(array)->display();
} }
__DYNLIB_ATTR__ const char* gm_MatArray_to_string_@GM_SCALAR@(gm_MatArray_t array)
{
//TODO: ideally a pre-allocated str should be passed by the caller (no allocation inside this function)
//TODO: until that the caller is responsible to free the returned memory buffer
std::string out_str = static_cast<cuMatArray<@GM_SCALAR@>*>(array)->to_string();
size_t len = out_str.size();
char * str_copy = (char*) malloc(len+1);
strncpy(str_copy, out_str.c_str(), len+1);
str_copy[len] = '\0';
return str_copy;
}
__DYNLIB_ATTR__ size_t gm_MatArray_get_total_nnz_@GM_SCALAR@(gm_MatArray_t array) __DYNLIB_ATTR__ size_t gm_MatArray_get_total_nnz_@GM_SCALAR@(gm_MatArray_t array)
{ {
return static_cast<cuMatArray<@GM_SCALAR@>*>(array)->get_total_nnz(); return static_cast<cuMatArray<@GM_SCALAR@>*>(array)->get_total_nnz();
......
...@@ -241,6 +241,7 @@ typedef void (*gm_MatArray_addgpu_anymat_@GM_SCALAR@_ptr)(gm_MatArray_t, void* a ...@@ -241,6 +241,7 @@ typedef void (*gm_MatArray_addgpu_anymat_@GM_SCALAR@_ptr)(gm_MatArray_t, void* a
typedef int32_t (*gm_MatArray_nrows_@GM_SCALAR@_ptr)(gm_MatArray_t); typedef int32_t (*gm_MatArray_nrows_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef int32_t (*gm_MatArray_ncols_@GM_SCALAR@_ptr)(gm_MatArray_t); typedef int32_t (*gm_MatArray_ncols_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef void (*gm_MatArray_display_@GM_SCALAR@_ptr)(gm_MatArray_t); typedef void (*gm_MatArray_display_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef const char* (*gm_MatArray_to_string_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef size_t (*gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr)(gm_MatArray_t); typedef size_t (*gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef void (*gm_MatArray_transpose_@GM_SCALAR@_ptr)(gm_MatArray_t); typedef void (*gm_MatArray_transpose_@GM_SCALAR@_ptr)(gm_MatArray_t);
typedef void (*gm_MatArray_scalar_mul_@GM_SCALAR@_ptr)(gm_MatArray_t, const @GM_SCALAR@* scalar); typedef void (*gm_MatArray_scalar_mul_@GM_SCALAR@_ptr)(gm_MatArray_t, const @GM_SCALAR@* scalar);
...@@ -280,6 +281,7 @@ struct gm_MatArrayFunc_@GM_SCALAR@ ...@@ -280,6 +281,7 @@ struct gm_MatArrayFunc_@GM_SCALAR@
gm_MatArray_nrows_@GM_SCALAR@_ptr nrows; gm_MatArray_nrows_@GM_SCALAR@_ptr nrows;
gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr get_total_nnz; gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr get_total_nnz;
gm_MatArray_display_@GM_SCALAR@_ptr display; gm_MatArray_display_@GM_SCALAR@_ptr display;
gm_MatArray_to_string_@GM_SCALAR@_ptr to_string;
/** Array computation functions */ /** Array computation functions */
gm_MatArray_matmul_@GM_SCALAR@_ptr chain_matmul; gm_MatArray_matmul_@GM_SCALAR@_ptr chain_matmul;
gm_MatArray_matmul_one_@GM_SCALAR@_ptr chain_matmul_one; gm_MatArray_matmul_one_@GM_SCALAR@_ptr chain_matmul_one;
......
...@@ -125,6 +125,7 @@ void load_marr_funcs_@GM_SCALAR@(void* gm_handle, gm_MatArrayFunc_@GM_SCALAR@ *m ...@@ -125,6 +125,7 @@ void load_marr_funcs_@GM_SCALAR@(void* gm_handle, gm_MatArrayFunc_@GM_SCALAR@ *m
marr_funcs->nrows = (gm_MatArray_nrows_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_nrows_@GM_SCALAR@"); marr_funcs->nrows = (gm_MatArray_nrows_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_nrows_@GM_SCALAR@");
marr_funcs->ncols = (gm_MatArray_ncols_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_ncols_@GM_SCALAR@"); marr_funcs->ncols = (gm_MatArray_ncols_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_ncols_@GM_SCALAR@");
marr_funcs->display = (gm_MatArray_display_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_display_@GM_SCALAR@"); marr_funcs->display = (gm_MatArray_display_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_display_@GM_SCALAR@");
marr_funcs->to_string = (gm_MatArray_to_string_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_to_string_@GM_SCALAR@");
marr_funcs->chain_matmul_one = (gm_MatArray_matmul_one_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_matmul_one_@GM_SCALAR@"); marr_funcs->chain_matmul_one = (gm_MatArray_matmul_one_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_matmul_one_@GM_SCALAR@");
marr_funcs->get_total_nnz = (gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_get_total_nnz_@GM_SCALAR@"); marr_funcs->get_total_nnz = (gm_MatArray_get_total_nnz_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_get_total_nnz_@GM_SCALAR@");
marr_funcs->transpose = (gm_MatArray_transpose_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_transpose_@GM_SCALAR@"); marr_funcs->transpose = (gm_MatArray_transpose_@GM_SCALAR@_ptr) gm_load_func_check_err(gm_handle, "gm_MatArray_transpose_@GM_SCALAR@");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment