1 #ifndef VIENNACL_COORDINATE_MATRIX_HPP_
2 #define VIENNACL_COORDINATE_MATRIX_HPP_
46 template <
typename CPU_MATRIX,
typename SCALARTYPE,
unsigned int ALIGNMENT>
47 void copy(
const CPU_MATRIX & cpu_matrix,
50 size_t group_num = 64;
53 if ( cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0 )
55 std::size_t num_entries = 0;
56 for (
typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
57 row_it != cpu_matrix.end1();
60 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
61 col_it != row_it.end();
69 std::cout <<
"Number of entries: " << num_entries << std::endl;
70 gpu_matrix.nonzeros_ = num_entries;
71 gpu_matrix.rows_ = cpu_matrix.size1();
72 gpu_matrix.cols_ = cpu_matrix.size2();
74 std::vector<cl_uint> coord_buffer(2*gpu_matrix.
internal_nnz());
75 std::vector<cl_uint> group_boundaries(group_num + 1);
76 std::vector<SCALARTYPE> elements(gpu_matrix.
internal_nnz());
78 std::size_t data_index = 0;
79 std::size_t current_fraction = 0;
81 for (
typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
82 row_it != cpu_matrix.end1();
85 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
86 col_it != row_it.end();
89 coord_buffer[2*data_index] =
static_cast<cl_uint
>(col_it.index1());
90 coord_buffer[2*data_index + 1] =
static_cast<cl_uint
>(col_it.index2());
91 elements[data_index] = *col_it;
95 if (data_index > (current_fraction + 1) /
static_cast<double>(group_num) * num_entries)
96 group_boundaries[++current_fraction] = data_index;
100 group_boundaries[group_num] = data_index;
118 template <
typename SCALARTYPE,
unsigned int ALIGNMENT>
119 void copy(
const std::vector< std::map<unsigned int, SCALARTYPE> > & cpu_matrix,
135 template <
typename CPU_MATRIX,
typename SCALARTYPE,
unsigned int ALIGNMENT>
137 CPU_MATRIX & cpu_matrix )
139 if ( gpu_matrix.
size1() > 0 && gpu_matrix.
size2() > 0 )
141 cpu_matrix.resize(gpu_matrix.
size1(), gpu_matrix.
size2(),
false);
144 std::vector<cl_uint> coord_buffer(2*gpu_matrix.
nnz());
145 std::vector<SCALARTYPE> elements(gpu_matrix.
nnz());
157 for (std::size_t index = 0; index < gpu_matrix.
nnz(); ++index)
159 cpu_matrix(coord_buffer[2*index], coord_buffer[2*index+1]) = elements[index];
169 template <
typename SCALARTYPE,
unsigned int ALIGNMENT>
171 std::vector< std::map<unsigned int, SCALARTYPE> > & cpu_matrix)
174 copy(gpu_matrix, temp);
186 template<
class SCALARTYPE,
unsigned int ALIGNMENT >
193 coordinate_matrix() : rows_(0), cols_(0), nonzeros_(0) { viennacl::linalg::kernels::coordinate_matrix<SCALARTYPE, ALIGNMENT>::init(); }
202 rows_(rows), cols_(cols), nonzeros_(nonzeros)
204 viennacl::linalg::kernels::coordinate_matrix<SCALARTYPE, ALIGNMENT>::init();
216 if (new_nonzeros > nonzeros_)
224 err = clEnqueueCopyBuffer(
viennacl::ocl::get_queue().
handle(), coord_buffer_old, coord_buffer_, 0, 0,
sizeof(cl_uint) * 2 * nonzeros_, 0, NULL, NULL);
231 err = clEnqueueCopyBuffer(
viennacl::ocl::get_queue().
handle(), coord_buffer_old, coord_buffer_, 0, nonzeros_,
sizeof(cl_uint) * 2 * temp.size(), 0, NULL, NULL);
233 err = clEnqueueCopyBuffer(
viennacl::ocl::get_queue().
handle(), elements_old, elements_, 0, nonzeros_,
sizeof(SCALARTYPE)*temp.size(), 0, NULL, NULL);
244 void resize(std::size_t new_size1, std::size_t new_size2,
bool preserve =
true)
246 assert (new_size1 > 0 && new_size2 > 0);
248 if (new_size1 < rows_ || new_size2 < cols_)
250 std::vector<std::map<unsigned int, SCALARTYPE> > stl_sparse_matrix;
252 stl_sparse_matrix.resize(rows_);
254 if (preserve && rows_ > 0)
257 stl_sparse_matrix.resize(new_size1);
259 std::cout <<
"Cropping STL matrix of size " << stl_sparse_matrix.size() << std::endl;
260 if (new_size2 < cols_ && rows_ > 0)
262 for (std::size_t i=0; i<stl_sparse_matrix.size(); ++i)
264 std::list<unsigned int> to_delete;
265 for (
typename std::map<unsigned int, SCALARTYPE>::iterator it = stl_sparse_matrix[i].begin();
266 it != stl_sparse_matrix[i].end();
269 if (it->first >= new_size2)
270 to_delete.push_back(it->first);
273 for (std::list<unsigned int>::iterator it = to_delete.begin(); it != to_delete.end(); ++it)
274 stl_sparse_matrix[i].erase(*it);
276 std::cout <<
"Cropping done..." << std::endl;
290 std::size_t
size1()
const {
return rows_; }
292 std::size_t
size2()
const {
return cols_; }
294 std::size_t
nnz()
const {
return nonzeros_; }
296 std::size_t
internal_nnz()
const {
return viennacl::tools::roundUpToNextMultiple<std::size_t>(nonzeros_, ALIGNMENT);; }
305 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
306 template <
typename CPU_MATRIX>
309 template <
typename CPU_MATRIX,
typename SCALARTYPE2,
unsigned int ALIGNMENT2>
323 std::size_t nonzeros_;