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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/* ************************************************************************
* Copyright (C) 2018-2021 Advanced Micro Devices, Inc. All rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ************************************************************************ */
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
struct mtx_header
{
char banner[16];
char array[16];
char coord[16];
char data[16];
char type[16];
int symmetric;
};
bool read_mtx_header(FILE* f, int& nrow, int& ncol, int& nnz, mtx_header& header)
{
char line[1024];
// Check for banner
if(!fgets(line, 1024, f))
{
return false;
}
// Extract banner
if(sscanf(line,
"%s %s %s %s %s",
header.banner,
header.array,
header.coord,
header.data,
header.type)
!= 5)
{
return false;
}
// Convert to lower case
for(char* p = header.array; *p != '\0'; *p = tolower(*p), p++)
;
for(char* p = header.coord; *p != '\0'; *p = tolower(*p), p++)
;
for(char* p = header.data; *p != '\0'; *p = tolower(*p), p++)
;
for(char* p = header.type; *p != '\0'; *p = tolower(*p), p++)
;
// Check banner
if(strncmp(line, "%%MatrixMarket", 14))
{
return false;
}
// Check array type
if(strcmp(header.array, "matrix"))
{
return false;
}
// Check coord
if(strcmp(header.coord, "coordinate"))
{
return false;
}
// Check data
if(strcmp(header.data, "real") && strcmp(header.data, "complex")
&& strcmp(header.data, "integer") && strcmp(header.data, "pattern"))
{
return false;
}
// Check type
if(strcmp(header.type, "general") && strcmp(header.type, "symmetric")
&& strcmp(header.type, "hermitian"))
{
return false;
}
// Symmetric flag
header.symmetric = !strcmp(header.type, "symmetric") || !strcmp(header.type, "hermitian");
// Skip comments
while(fgets(line, 1024, f))
{
if(line[0] != '%')
{
break;
}
}
// Read dimensions
sscanf(line, "%d %d %d", &nrow, &ncol, &nnz);
return true;
}
void set_value(double& dst, double rsrc, double isrc)
{
dst = rsrc;
}
void set_value(std::complex<double>& dst, double rsrc, double isrc)
{
dst = std::complex<double>(rsrc, isrc);
}
template <typename T>
bool read_mtx_matrix(FILE* f,
const mtx_header& header,
int nrow,
int ncol,
int& nnz,
std::vector<int>& row_ind,
std::vector<int>& col_ind,
std::vector<T>& val)
{
// Cache for line
char line[1024];
// Read unsorted data
std::vector<int> unsorted_row(header.symmetric ? 2 * nnz : nnz);
std::vector<int> unsorted_col(header.symmetric ? 2 * nnz : nnz);
std::vector<T> unsorted_val(header.symmetric ? 2 * nnz : nnz);
// Read entries
int idx = 0;
while(fgets(line, 1024, f))
{
if(idx >= (header.symmetric ? 2 * nnz : nnz))
{
return false;
}
int irow;
int icol;
double rval;
double ival;
if(!strcmp(header.data, "pattern"))
{
sscanf(line, "%d %d", &irow, &icol);
rval = 1.0;
}
else
{
if(!strcmp(header.data, "complex"))
{
sscanf(line, "%d %d %lg %lg", &irow, &icol, &rval, &ival);
}
else
{
sscanf(line, "%d %d %lg", &irow, &icol, &rval);
}
}
--irow;
--icol;
unsorted_row[idx] = irow;
unsorted_col[idx] = icol;
set_value(unsorted_val[idx], rval, ival);
++idx;
if(header.symmetric && irow != icol)
{
if(idx >= (header.symmetric ? 2 * nnz : nnz))
{
return false;
}
unsorted_row[idx] = icol;
unsorted_col[idx] = irow;
set_value(unsorted_val[idx], rval, ival);
++idx;
}
}
// Store "real" number of non-zero entries
nnz = idx;
// Sort by row and column index
std::vector<int> perm(nnz);
for(int i = 0; i < nnz; ++i)
{
perm[i] = i;
}
std::sort(perm.begin(), perm.end(), [&](const int& a, const int& b) {
if(unsorted_row[a] < unsorted_row[b])
{
return true;
}
else if(unsorted_row[a] == unsorted_row[b])
{
return (unsorted_col[a] < unsorted_col[b]);
}
else
{
return false;
}
});
// Resize arrays
row_ind.resize(nnz);
col_ind.resize(nnz);
val.resize(nnz);
for(int i = 0; i < nnz; ++i)
{
row_ind[i] = unsorted_row[perm[i]];
col_ind[i] = unsorted_col[perm[i]];
val[i] = unsorted_val[perm[i]];
}
return true;
}
template <typename T>
bool write_bin_matrix(
const char* filename, int m, int n, int nnz, const int* ptr, const int* col, const T* val)
{
std::ofstream out(filename, std::ios::out | std::ios::binary);
if(!out.is_open())
{
return false;
}
// Header
out << "#rocALUTION binary csr file" << std::endl;
// rocALUTION version
int version = 10602;
out.write((char*)&version, sizeof(int));
// Data
out.write((char*)&m, sizeof(int));
out.write((char*)&n, sizeof(int));
out.write((char*)&nnz, sizeof(int));
out.write((char*)ptr, (m + 1) * sizeof(int));
out.write((char*)col, nnz * sizeof(int));
out.write((char*)val, nnz * sizeof(T));
out.close();
return true;
}
bool coo_to_csr(int m, int nnz, const int* src_row, std::vector<int>& dst_ptr)
{
dst_ptr.resize(m + 1, 0);
// Compute nnz entries per row
for(int i = 0; i < nnz; ++i)
{
++dst_ptr[src_row[i] + 1];
}
// Exclusive scan
for(int i = 0; i < m; ++i)
{
dst_ptr[i + 1] += dst_ptr[i];
}
return true;
}
int main(int argc, char* argv[])
{
if(argc < 3)
{
std::cerr << argv[0] << " <matrix.mtx> <matrix.csr>" << std::endl;
return -1;
}
// Matrix dimensions
int m;
int n;
int nnz;
// Matrix mtx header
mtx_header header;
// Open file for reading
FILE* f = fopen(argv[1], "r");
if(!f)
{
std::cerr << "Cannot open [read] .mtx file " << argv[1] << std::endl;
return -1;
}
if(!read_mtx_header(f, m, n, nnz, header))
{
std::cerr << "Cannot read .mtx header from " << argv[1] << std::endl;
return -1;
}
std::vector<int> row_ptr;
std::vector<int> row_ind;
std::vector<int> col_ind;
std::vector<double> rval;
std::vector<std::complex<double>> cval;
bool status;
if(!strcmp(header.data, "complex"))
{
status = read_mtx_matrix(f, header, m, n, nnz, row_ind, col_ind, cval);
}
else
{
status = read_mtx_matrix(f, header, m, n, nnz, row_ind, col_ind, rval);
}
if(!status)
{
std::cerr << "Cannot read .mtx data from " << argv[1] << std::endl;
return -1;
}
// Close file
fclose(f);
if(!coo_to_csr(m, nnz, row_ind.data(), row_ptr))
{
std::cerr << "Cannot convert " << argv[1] << " from COO to CSR." << std::endl;
return -1;
}
if(!strcmp(header.data, "complex"))
{
status = write_bin_matrix(argv[2], m, n, nnz, row_ptr.data(), col_ind.data(), cval.data());
}
else
{
status = write_bin_matrix(argv[2], m, n, nnz, row_ptr.data(), col_ind.data(), rval.data());
}
if(!status)
{
std::cerr << "Cannot open [write] " << argv[2] << std::endl;
return -1;
}
return 0;
}
|