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

Function toCooVector

test/arrayfire_test.cpp:1487–1551  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1485
1486template<typename T>
1487vector<sparseCooValue<T>> toCooVector(const af::array &arr) {
1488 vector<sparseCooValue<T>> out;
1489 if (arr.issparse()) {
1490 switch (sparseGetStorage(arr)) {
1491 case AF_STORAGE_COO: {
1492 dim_t nnz = sparseGetNNZ(arr);
1493 vector<int> row(nnz), col(nnz);
1494 vector<T> values(nnz);
1495 sparseGetValues(arr).host(values.data());
1496 sparseGetRowIdx(arr).host(row.data());
1497 sparseGetColIdx(arr).host(col.data());
1498 out.reserve(nnz);
1499 for (int i = 0; i < nnz; i++) {
1500 out.emplace_back(row[i], col[i], values[i]);
1501 }
1502 } break;
1503 case AF_STORAGE_CSR: {
1504 dim_t nnz = sparseGetNNZ(arr);
1505 vector<int> row(arr.dims(0) + 1), col(nnz);
1506 vector<T> values(nnz);
1507 sparseGetValues(arr).host(values.data());
1508 sparseGetRowIdx(arr).host(row.data());
1509 sparseGetColIdx(arr).host(col.data());
1510 out.reserve(nnz);
1511 for (int i = 0; i < row.size() - 1; i++) {
1512 for (int r = row[i]; r < row[i + 1]; r++) {
1513 out.emplace_back(i, col[r], values[r]);
1514 }
1515 }
1516 } break;
1517 case AF_STORAGE_CSC: {
1518 dim_t nnz = sparseGetNNZ(arr);
1519 vector<int> row(nnz), col(arr.dims(1) + 1);
1520 vector<T> values(nnz);
1521 sparseGetValues(arr).host(values.data());
1522 sparseGetRowIdx(arr).host(row.data());
1523 sparseGetColIdx(arr).host(col.data());
1524 out.reserve(nnz);
1525 for (int i = 0; i < col.size() - 1; i++) {
1526 for (int c = col[i]; c < col[i + 1]; c++) {
1527 out.emplace_back(row[c], i, values[c]);
1528 }
1529 }
1530 } break;
1531 default: throw std::logic_error("NOT SUPPORTED");
1532 }
1533 } else {
1534 vector<T> values(arr.elements());
1535 arr.host(values.data());
1536 int M = arr.dims(0), N = arr.dims(1);
1537 for (int j = 0; j < N; j++) {
1538 for (int i = 0; i < M; i++) {
1539 if (std::fpclassify(real(values[j * M + i])) == FP_ZERO) {
1540 out.emplace_back(i, j, values[j * M + i]);
1541 }
1542 }
1543 }
1544 }

Callers

nothing calls this directly

Calls 13

sparseGetStorageFunction · 0.85
sparseGetNNZFunction · 0.85
sparseGetValuesFunction · 0.85
sparseGetRowIdxFunction · 0.85
sparseGetColIdxFunction · 0.85
fpclassifyFunction · 0.85
beginFunction · 0.85
endFunction · 0.85
hostMethod · 0.80
realFunction · 0.70
sortFunction · 0.50
dimsMethod · 0.45

Tested by

no test coverage detected