Skip to content

Commit

Permalink
[fix] set_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Daiki Adachi committed Oct 5, 2023
1 parent d3b7513 commit f5ca31a
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 96 deletions.
19 changes: 17 additions & 2 deletions include/monolish/common/monolish_coo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ template <typename Float> class COO {
* @param c col_index
* @param v value
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
Expand All @@ -523,13 +523,28 @@ template <typename Float> class COO {
* @param c col_index
* @param v value
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t rN, const size_t cN, const std::vector<int> &r,
const std::vector<int> &c, const size_t vsize, const Float *v);

/**
* @brief Set COO array from std::vector
* @param rN # of row
* @param cN # of column
* @param r row_index
* @param c col_index
* @param v value
* @note
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t rN, const size_t cN, const std::vector<int> &r,
const std::vector<int> &c, const size_t vsize, const Float v);

/**
* @brief get # of row
* @note
Expand Down
22 changes: 20 additions & 2 deletions include/monolish/common/monolish_crs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ template <typename Float> class CRS {
*elements (size nnz)
* @param value value index, which stores the non-zero elements (size nnz)
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
Expand All @@ -275,14 +275,32 @@ template <typename Float> class CRS {
*elements (size nnz)
* @param value value index, which stores the non-zero elements (size nnz)
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
const std::vector<int> &colind, const size_t vsize,
const Float *value);

/**
* @brief Set CRS array from std::vector
* @param M # of row
* @param N # of col
* @param rowptr row_ptr, which stores the starting points of the rows of the
*arrays value and col_ind (size M+1)
* @param colind col_ind, which stores the column numbers of the non-zero
*elements (size nnz)
* @param value value index, which stores the non-zero elements (size nnz)
* @note
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t M, const size_t N, const std::vector<int> &rowptr,
const std::vector<int> &colind, const size_t vsize,
const Float value);

/**
* @brief print all elements to standard I/O
* @param force_cpu Ignore device status and output CPU data
Expand Down
23 changes: 21 additions & 2 deletions include/monolish/common/monolish_dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ template <typename Float> class Dense {
* @param N # of col
* @param value value (size nnz)
* @note
* - # of computation: 1
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
Expand All @@ -300,12 +300,24 @@ template <typename Float> class Dense {
* @param N # of col
* @param value value (size nnz)
* @note
* - # of computation: 1
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t M, const size_t N, const Float *value);

/**
* @brief Set Dense array from std::vector
* @param M # of row
* @param N # of col
* @param value value (size nnz)
* @note
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const size_t M, const size_t N, const Float value);

/**
* @brief get # of row
* @note
Expand Down Expand Up @@ -609,6 +621,13 @@ template <typename Float> class Dense {
* - GPU acceleration: false
*/
void resize(size_t N, Float Val = 0) {
if (first + N < alloc_nnz) {
for (size_t i = val_nnz; i < N; ++i) {
begin()[i] = Val;
}
val_nnz = N;
return;
}
if (get_device_mem_stat()) {
throw std::runtime_error("Error, GPU matrix cant use resize");
}
Expand Down
19 changes: 17 additions & 2 deletions include/monolish/common/monolish_tensor_coo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ template <typename Float> class tensor_COO {
* @param indix index fo tensor
* @param v value
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
Expand All @@ -214,14 +214,29 @@ template <typename Float> class tensor_COO {
* @param vsize size of value
* @param v value
* @note
* - # of computation: 3
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const std::vector<size_t> &shape,
const std::vector<std::vector<size_t>> &index,
const size_t vsize, const Float *v);

/**
* @brief Set tensor_COO array from array
* @param shape shape of tensor
* @param indix index fo tensor
* @param vsize size of value
* @param v value
* @note
* - # of computation: 3nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const std::vector<size_t> &shape,
const std::vector<std::vector<size_t>> &index,
const size_t vsize, const Float v);

/**
* @brief get shape
* @note
Expand Down
15 changes: 13 additions & 2 deletions include/monolish/common/monolish_tensor_dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ template <typename Float> class tensor_Dense {
* @param shape shape of tensor
* @param value value (size nnz)
* @note
* - # of computation: 1
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
Expand All @@ -283,12 +283,23 @@ template <typename Float> class tensor_Dense {
* @param shape shape of tensor
* @param value value (size nnz)
* @note
* - # of computation: 1
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const std::vector<size_t> &shape, const Float *value);

/**
* @brief Set tensor_Dense array from array
* @param shape shape of tensor
* @param value value (size nnz)
* @note
* - # of computation: nnz
* - Multi-threading: false
* - GPU acceleration: false
**/
void set_ptr(const std::vector<size_t> &shape, const Float value);

/**
* @brief get shape
* @note
Expand Down
34 changes: 30 additions & 4 deletions src/utils/copy/copy_coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ template <typename T> void COO<T>::operator=(const matrix::COO<T> &mat) {
assert(monolish::util::is_same_device_mem_stat(*this, mat));

// value copy
internal::vcopy(get_nnz(), data(), mat.data(), get_device_mem_stat());
internal::vcopy(get_nnz(), mat.begin(), begin(), get_device_mem_stat());

logger.util_out();
}
template void COO<double>::operator=(const matrix::COO<double> &mat);
template void COO<float>::operator=(const matrix::COO<float> &mat);

template <typename T>
void COO<T>::set_ptr(const size_t rN, const size_t cN,
Expand All @@ -31,9 +33,8 @@ void COO<T>::set_ptr(const size_t rN, const size_t cN,
col_index = c;
row_index = r;
resize(vsize);
for (size_t i = 0; i < vsize; ++i) {
data()[i] = v[i];
}

internal::vcopy(get_nnz(), v, begin(), false);

rowN = rN;
colN = cN;
Expand All @@ -48,6 +49,31 @@ template void COO<float>::set_ptr(const size_t rN, const size_t cN,
const std::vector<int> &c, const size_t vsize,
const float *v);

template <typename T>
void COO<T>::set_ptr(const size_t rN, const size_t cN,
const std::vector<int> &r, const std::vector<int> &c,
const size_t vsize, const T v) {
Logger &logger = Logger::get_instance();
logger.util_in(monolish_func);
col_index = c;
row_index = r;
resize(vsize);

internal::vbroadcast(get_nnz(), v, begin(), false);

rowN = rN;
colN = cN;
logger.util_out();
}
template void COO<double>::set_ptr(const size_t rN, const size_t cN,
const std::vector<int> &r,
const std::vector<int> &c,
const size_t vsize, const double v);
template void COO<float>::set_ptr(const size_t rN, const size_t cN,
const std::vector<int> &r,
const std::vector<int> &c, const size_t vsize,
const float v);

template <typename T>
void COO<T>::set_ptr(const size_t rN, const size_t cN,
const std::vector<int> &r, const std::vector<int> &c,
Expand Down
53 changes: 35 additions & 18 deletions src/utils/copy/copy_crs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,8 @@ template <typename T> void CRS<T>::operator=(const CRS<T> &mat) {
assert(monolish::util::is_same_device_mem_stat(*this, mat));
val_create_flag = true;

if (mat.get_device_mem_stat() == true) {
#if MONOLISH_USE_NVIDIA_GPU
internal::vcopy(mat.row_ptr.size(), mat.row_ptr.data(), row_ptr.data(),
true);
internal::vcopy(mat.col_ind.size(), mat.col_ind.data(), col_ind.data(),
true);
internal::vcopy(mat.get_nnz(), mat.begin(), begin(), true);
#endif
} else {
internal::vcopy(mat.row_ptr.size(), mat.row_ptr.data(), row_ptr.data(),
false);
internal::vcopy(mat.col_ind.size(), mat.col_ind.data(), col_ind.data(),
false);
internal::vcopy(mat.get_nnz(), mat.begin(), begin(), false);
}
// value copy
internal::vcopy(get_nnz(), mat.begin(), begin(), get_device_mem_stat());

logger.util_out();
}
Expand All @@ -49,12 +36,13 @@ void CRS<T>::set_ptr(const size_t M, const size_t N,
row_ptr = rowptr;
val_create_flag = true;
resize(vsize);
for (size_t i = 0; i < vsize; ++i) {
data()[i] = value[i];
}

internal::vcopy(get_nnz(), value, begin(), false);

rowN = M;
colN = N;

compute_hash();
logger.util_out();
}
template void CRS<double>::set_ptr(const size_t M, const size_t N,
Expand All @@ -66,6 +54,35 @@ template void CRS<float>::set_ptr(const size_t M, const size_t N,
const std::vector<int> &colind,
const size_t vsize, const float *value);

template <typename T>
void CRS<T>::set_ptr(const size_t M, const size_t N,
const std::vector<int> &rowptr,
const std::vector<int> &colind, const size_t vsize,
const T value) {
Logger &logger = Logger::get_instance();
logger.util_in(monolish_func);
col_ind = colind;
row_ptr = rowptr;
val_create_flag = true;
resize(vsize);

internal::vbroadcast(get_nnz(), value, begin(), false);

rowN = M;
colN = N;

compute_hash();
logger.util_out();
}
template void CRS<double>::set_ptr(const size_t M, const size_t N,
const std::vector<int> &rowptr,
const std::vector<int> &colind,
const size_t vsize, const double value);
template void CRS<float>::set_ptr(const size_t M, const size_t N,
const std::vector<int> &rowptr,
const std::vector<int> &colind,
const size_t vsize, const float value);

template <typename T>
void CRS<T>::set_ptr(const size_t M, const size_t N,
const std::vector<int> &rowptr,
Expand Down
Loading

0 comments on commit f5ca31a

Please sign in to comment.