forked from clMathLibraries/clSPARSE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool-mult-sample.cpp
265 lines (211 loc) · 8.14 KB
/
bool-mult-sample.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* ************************************************************************
* Copyright 2015 Vratis, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ************************************************************************ */
/*! \file
* \brief Simple demonstration code for how to calculate a BoolSpMM multiplication
*/
#include <iostream>
#include <vector>
#include <assert.h>
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_MINIMUM_OPENCL_VERSION BUILD_CLVERSION
#define CL_HPP_TARGET_OPENCL_VERSION BUILD_CLVERSION
#include <CL/cl2.hpp>
#include "clSPARSE.h"
#include "clSPARSE-error.h"
/**
* \brief Sample Bool Sparse Matrix Matrix multiplication
* \details [C = A * A]
*
* A - [m x n] matrix in CSR format
*
* Program is executing by completing following steps:
* 1. Setup OpenCL environment
* 2. Setup GPU buffers
* 3. Init clSPARSE library
* 4. Execute algorithm clsparseBoolScsrSpGemm
* 5. Shutdown clSPARSE library & OpenCL
*
* usage:
*
* bool-mult-sample path/to/matrix/in/mtx/format.mtx
*
*/
int readAndCreateBoolCsrMatrix(clsparseBoolCsrMatrix* A, std::string matrix_path, clsparseCreateResult createResult, cl::Context context)
{
cl_int cl_status;
clsparseInitBoolCsrMatrix(A);
// Read matrix from file. Calculates the rowBlocks structures as well.
clsparseIdx_t nnz, row, col;
// read MM header to get the size of the matrix;
clsparseStatus fileError
= clsparseHeaderfromFile( &nnz, &row, &col, matrix_path.c_str( ) );
if( fileError != clsparseSuccess )
{
std::cout << "Could not read matrix market header from disk" << std::endl;
return -5;
}
A->num_nonzeros = nnz;
A->num_rows = row;
A->num_cols = col;
// Allocate memory for CSR matrix
A->col_indices = ::clCreateBuffer( context(), CL_MEM_READ_ONLY,
A->num_nonzeros * sizeof( clsparseIdx_t ), NULL, &cl_status );
A->row_pointer = ::clCreateBuffer( context(), CL_MEM_READ_ONLY,
( A->num_rows + 1 ) * sizeof( clsparseIdx_t ), NULL, &cl_status );
// Read matrix market file with explicit zero values included.
fileError = clsparseSBoolCsrMatrixfromFile( A, matrix_path.c_str( ), createResult.control, true );
// This function allocates memory for rowBlocks structure. If not called
// the structure will not be calculated and clSPARSE will run the vectorized
// version of SpMV instead of adaptive;
clsparseBoolCsrMetaCreate( A, createResult.control );
if (fileError != clsparseSuccess)
{
std::cout << "Problem with reading matrix from " << matrix_path
<< std::endl;
return -6;
}
}
int main (int argc, char* argv[])
{
//parse command line
std::string matrix_path;
if (argc < 2)
{
std::cout << "Not enough parameters. "
<< "Please specify path to matrix in mtx format as parameter"
<< std::endl;
return -1;
}
else
{
matrix_path = std::string(argv[1]);
}
std::cout << "Executing sample clSPARSE SpMM (y = A*A) C++" << std::endl;
std::cout << "Matrix will be read from: " << matrix_path << std::endl;
/** Step 1. Setup OpenCL environment; **/
// Init OpenCL environment;
cl_int cl_status;
// Get OpenCL platforms
std::vector<cl::Platform> platforms;
cl_status = cl::Platform::get(&platforms);
if (cl_status != CL_SUCCESS)
{
std::cout << "Problem with getting OpenCL platforms"
<< " [" << cl_status << "]" << std::endl;
return -2;
}
int platform_id = 0;
for (const auto& p : platforms)
{
std::cout << "Platform ID " << platform_id++ << " : "
<< p.getInfo<CL_PLATFORM_NAME>() << std::endl;
}
// Using first platform
platform_id = 0;
cl::Platform platform = platforms[platform_id];
// Get device from platform
std::vector<cl::Device> devices;
cl_status = platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
if (cl_status != CL_SUCCESS)
{
std::cout << "Problem with getting devices from platform"
<< " [" << platform_id << "] " << platform.getInfo<CL_PLATFORM_NAME>()
<< " error: [" << cl_status << "]" << std::endl;
}
std::cout << std::endl
<< "Getting devices from platform " << platform_id << std::endl;
cl_int device_id = 0;
for (const auto& device : devices)
{
std::cout << "Device ID " << device_id++ << " : "
<< device.getInfo<CL_DEVICE_NAME>() << std::endl;
}
// Using first device;
device_id = 0;
cl::Device device = devices[device_id];
// Create OpenCL context;
cl::Context context (device);
// Create OpenCL queue;
cl::CommandQueue queue(context, device);
/** Step 2. Setup GPU buffers **/
/** Step 3. Init clSPARSE library **/
clsparseStatus status = clsparseSetup();
if (status != clsparseSuccess)
{
std::cout << "Problem with executing clsparseSetup()" << std::endl;
return -3;
}
// Create clsparseControl object
clsparseCreateResult createResult = clsparseCreateControl( queue( ) );
CLSPARSE_V( createResult.status, "Failed to create clsparse control" );
clsparseBoolCsrMatrix A;
int error = readAndCreateBoolCsrMatrix(&A, matrix_path, createResult, context);
clsparseBoolCsrMatrix B;
int error1 = readAndCreateBoolCsrMatrix(&B, matrix_path, createResult, context);
clsparseBoolCsrMatrix C;
/**Step 4. Call the multiplication algorithm */
status = clsparseBoolScsrSpGemm(&A, &B, &C, createResult.control );
std::vector<int> csrRowPtrC_h((C.num_rows + 1), 0);
int run_status = clEnqueueReadBuffer(queue(),
C.row_pointer,
1,
0,
(C.num_rows + 1)*sizeof(cl_int),
csrRowPtrC_h.data(),
0,
0,
0);
std::vector<int> csrColIndC_h(C.num_nonzeros, 0);
run_status = clEnqueueReadBuffer(queue(),
C.col_indices,
1,
0,
C.num_nonzeros*sizeof(cl_int),
csrColIndC_h.data(),
0,
0,
0);
if (status != clsparseSuccess)
{
std::cout << "Problem with execution SpMV algorithm."
<< " Error: " << status << std::endl;
}
/** Step 5. Close & release resources */
status = clsparseReleaseControl( createResult.control );
if (status != clsparseSuccess)
{
std::cout << "Problem with releasing control object."
<< " Error: " << status << std::endl;
}
status = clsparseTeardown();
if (status != clsparseSuccess)
{
std::cout << "Problem with closing clSPARSE library."
<< " Error: " << status << std::endl;
}
//release mem;
clsparseBoolCsrMetaDelete( &A );
clReleaseMemObject ( A.col_indices );
clReleaseMemObject ( A.row_pointer );
clsparseBoolCsrMetaDelete( &B );
clReleaseMemObject ( B.col_indices );
clReleaseMemObject ( B.row_pointer );
std::cout << C.num_nonzeros << std::endl;
clReleaseMemObject ( C.col_indices );
clReleaseMemObject ( C.row_pointer );
std::cout << "Program completed successfully." << std::endl;
return 0;
}