MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / mtxReadSparseMatrix

Function mtxReadSparseMatrix

test/arrayfire_test.cpp:1259–1352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1257
1258#if defined(USE_MTX)
1259::testing::AssertionResult mtxReadSparseMatrix(af::array &out,
1260 const char *fileName) {
1261 FILE *fileHandle;
1262
1263 if ((fileHandle = fopen(fileName, "r")) == NULL) {
1264 return ::testing::AssertionFailure()
1265 << "Failed to open mtx file: " << fileName << "\n";
1266 }
1267
1268 MM_typecode matcode;
1269 if (mm_read_banner(fileHandle, &matcode)) {
1270 return ::testing::AssertionFailure()
1271 << "Could not process Matrix Market banner.\n";
1272 }
1273
1274 if (!(mm_is_matrix(matcode) && mm_is_sparse(matcode))) {
1275 return ::testing::AssertionFailure()
1276 << "Input mtx doesn't have a sparse matrix.\n";
1277 }
1278
1279 if (mm_is_integer(matcode)) {
1280 return ::testing::AssertionFailure() << "MTX file has integer data. \
1281 Integer sparse matrices are not supported in ArrayFire yet.\n";
1282 }
1283
1284 int M = 0, N = 0, nz = 0;
1285 if (mm_read_mtx_crd_size(fileHandle, &M, &N, &nz)) {
1286 return ::testing::AssertionFailure()
1287 << "Failed to read matrix dimensions.\n";
1288 }
1289
1290 if (mm_is_real(matcode)) {
1291 std::vector<int> I(nz);
1292 std::vector<int> J(nz);
1293 std::vector<float> V(nz);
1294
1295 for (int i = 0; i < nz; ++i) {
1296 int c, r;
1297 double v;
1298 int readCount = fscanf(fileHandle, "%d %d %lg\n", &r, &c, &v);
1299 if (readCount != 3) {
1300 fclose(fileHandle);
1301 return ::testing::AssertionFailure()
1302 << "\nEnd of file reached, expected more data, "
1303 << "following are some reasons this happens.\n"
1304 << "\t - use of template type that doesn't match "
1305 "data "
1306 "type\n"
1307 << "\t - the mtx file itself doesn't have enough "
1308 "data\n";
1309 }
1310 I[i] = r - 1;
1311 J[i] = c - 1;
1312 V[i] = (float)v;
1313 }
1314
1315 out = af::sparse(M, N, nz, V.data(), I.data(), J.data(), f32,
1316 AF_STORAGE_COO);

Callers 2

TESTFunction · 0.85
ARITH_TESTSFunction · 0.85

Calls 3

mm_read_bannerFunction · 0.85
mm_read_mtx_crd_sizeFunction · 0.85
sparseFunction · 0.85

Tested by

no test coverage detected