/
csrmatrixgpu.cpp
187 lines (163 loc) · 6.12 KB
/
csrmatrixgpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "include/csrmatrixgpu.hpp"
CsrMatrixGpu::CsrMatrixGpu(const size_t numrows, const size_t numcols):
_numrows(numrows), _numcols(numcols),
_colind(nullptr), _values(nullptr)
{
malloc_cuda(&_rowptr, (numrows+1)*sizeof(size_t));
}
CsrMatrixGpu::CsrMatrixGpu(const size_t size):
_numrows(size), _numcols(size),
_colind(nullptr), _values(nullptr)
{
malloc_cuda(&_rowptr, (size+1)*sizeof(size_t));
}
CsrMatrixGpu::CsrMatrixGpu(const CsrMatrixGpu& other):
_numrows(other._numrows), _numcols(other._numcols)
{
size_t numvalues{0};
memcpy_cuda(&numvalues, other._rowptr+_numrows, sizeof(size_t), d2h);
malloc_cuda(&_rowptr, (_numrows+1)*sizeof(size_t));
memcpy_cuda(_rowptr, other._rowptr, (_numrows+1)*sizeof(size_t), d2d);
malloc_cuda(&_colind, numvalues*sizeof(size_t));
memcpy_cuda(_colind, other._colind, numvalues*sizeof(size_t), d2d);
malloc_cuda(&_values, numvalues*sizeof(float));
memcpy_cuda(_values, other._values, numvalues*sizeof(float), d2d);
}
/*
CsrMatrixGpu::CsrMatrixGpu(CsrMatrixGpu&& other):
_numrows(other._numrows), _numcols(other._numcols)
{
std::cout << "copy and delete" << std::endl;
_rowptr = other._rowptr;
_colind = other._colind;
_values = other._values;
other._numrows = 0;
other._numcols = 0;
other._rowptr = nullptr;
other._colind = nullptr;
other._values = nullptr;
}
*/
CsrMatrixGpu::~CsrMatrixGpu()
{
free_cuda(_rowptr);
free_cuda(_colind);
free_cuda(_values);
}
void CsrMatrixGpu::set_local(const size_t row, const size_t col, const float val)
{
assert(row < _numrows && col < _numcols);
size_t pos_to_insert(_rowptr[row]);
while (_colind[pos_to_insert] < col && pos_to_insert < _rowptr[row+1])
++pos_to_insert;
assert(_colind[pos_to_insert] == col && pos_to_insert < _rowptr[row+1]);
_values[pos_to_insert] = val;
}
void CsrMatrixGpu::add_local(const size_t row, const size_t col, const float val)
{
assert(row < _numrows && col < _numcols);
size_t pos_to_insert(_rowptr[row]);
while (_colind[pos_to_insert] < col && pos_to_insert < _rowptr[row+1])
++pos_to_insert;
assert(_colind[pos_to_insert] == col && pos_to_insert < _rowptr[row+1]);
_values[pos_to_insert] += val;
}
void CsrMatrixGpu::add_local_atomic(const size_t row, const size_t col, const float val)
{
assert(row < _numrows && col < _numcols);
size_t pos_to_insert(_rowptr[row]);
while (_colind[pos_to_insert] < col && pos_to_insert < _rowptr[row+1])
++pos_to_insert;
assert(_colind[pos_to_insert] == col && pos_to_insert < _rowptr[row+1]);
_values[pos_to_insert] += val;
//atomicAdd(&_values[pos_to_insert], val);
}
void CsrMatrixGpu::print_local_data(const size_t firstindex) const
{
size_t* h_rowptr = new size_t[_numrows+1];
memcpy_cuda(h_rowptr, _rowptr, (_numrows+1)*sizeof(size_t), d2h);
size_t* h_colind = new size_t[h_rowptr[_numrows]];
float* h_values = new float[h_rowptr[_numrows]];
memcpy_cuda(h_colind, _colind, h_rowptr[_numrows]*sizeof(size_t), d2h);
memcpy_cuda(h_values, _values, h_rowptr[_numrows]*sizeof(float), d2h);
for (size_t row(0), current_pos(0); row < _numrows; ++row)
{
std::cout << row+firstindex << ": ";
for (size_t col(h_rowptr[row]); col < h_rowptr[row+1]; ++col, ++current_pos)
std::cout << h_values[current_pos] << "(" << h_colind[current_pos]+firstindex << "), ";
std::cout << std::endl;
}
delete[] h_rowptr;
delete[] h_colind;
delete[] h_values;
}
void CsrMatrixGpu::createStructure(const Triangle1* const elements, const size_t num_elem)
{
const size_t max_rowlength(20);
size_t* num_nonzeros = new size_t[_numrows];
for (size_t i(0); i < _numrows; ++i)
num_nonzeros[i] = 0;
size_t* colind = new size_t[max_rowlength*_numrows];
for (size_t i(0); i < num_elem; ++i)
{
size_t nodes[3];
nodes[0] = elements[i].nodeA;
nodes[1] = elements[i].nodeB;
nodes[2] = elements[i].nodeC;
for (size_t node1(0); node1 < 3; ++node1)
{
for (size_t node2(0); node2 < 3; ++node2)
{
size_t a(nodes[node1]);
size_t b(nodes[node2]);
size_t j(0);
while (j < num_nonzeros[a] && colind[a*max_rowlength + j] != b)
++j;
if (num_nonzeros[a] == j)
{
++(num_nonzeros[a]);
assert(num_nonzeros[a] <= max_rowlength);
colind[a*max_rowlength + j] = b;
}
}
}
}
for (size_t i(0); i < _numrows; ++i)
for (size_t a(num_nonzeros[i]-1); a > 0; --a)
for (size_t b(0); b < a; ++b)
if (colind[i*max_rowlength + b] > colind[i*max_rowlength + b+1])
{
size_t tmp(colind[i*max_rowlength + b]);
colind[i*max_rowlength + b] = colind[i*max_rowlength + b+1];
colind[i*max_rowlength + b+1] = tmp;
}
size_t* h_rowptr = new size_t[_numrows+1];
size_t num_values(0);
for (size_t i(0); i < _numrows; ++i)
{
h_rowptr[i] = num_values;
num_values += num_nonzeros[i];
}
h_rowptr[_numrows] = num_values;
free_cuda(_colind);
malloc_cuda(&_colind, num_values*sizeof(size_t));
size_t* h_colind = new size_t[num_values];
size_t current_pos(0);
for (size_t row(0); row < _numrows; ++row)
for (size_t col(0); col < num_nonzeros[row]; ++col)
h_colind[current_pos++] = colind[row*max_rowlength + col];
free_cuda(_values);
malloc_cuda(&_values, num_values*sizeof(float));
float* h_values = new float[num_values];
for (size_t i(0); i < num_values; ++i)
h_values[i] = 0.0;
memcpy_cuda(_colind, h_colind, num_values*sizeof(size_t), h2d);
memcpy_cuda(_rowptr, h_rowptr, (_numrows+1)*sizeof(size_t), h2d);
memcpy_cuda(_values, h_values, num_values*sizeof(float), h2d);
delete[] num_nonzeros;
delete[] colind;
delete[] h_rowptr;
delete[] h_colind;
delete[] h_values;
//cudaDeviceSynchronize(); // needed?
}