MCPcopy Create free account
hub / github.com/NGSolve/ngsolve / T_MatAdd

Function T_MatAdd

linalg/sparsematrix.cpp:1457–1533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1455
1456 template <typename T>
1457 shared_ptr<SparseMatrixTM<T>>
1458 T_MatAdd (T sa, const SparseMatrixTM<T> & mata,
1459 T sb, const SparseMatrixTM<T> & matb)
1460 {
1461 if (mata.Shape() != matb.Shape())
1462 throw Exception("MatAdd, A.shape = "
1463 +ToString(mata.Height())+"x"+ToString(mata.Width())
1464 + " != "
1465 +ToString(matb.Height())+"x"+ToString(matb.Width())
1466 + " = B.shape");
1467
1468 Array<int> cnt(mata.Height());
1469 cnt = 0;
1470
1471 ParallelForRange
1472 (mata.Height(), [&] (IntRange r)
1473 {
1474 Array<BaseSparseMatrix::ColIdx*> ptrs(2);
1475 Array<int> sizes(2);
1476 for (int i : r)
1477 {
1478 ptrs[0] = mata.GetRowIndices(i).Addr(0);
1479 ptrs[1] = matb.GetRowIndices(i).Addr(0);
1480 sizes[0] = mata.GetRowIndices(i).Size();
1481 sizes[1] = matb.GetRowIndices(i).Size();
1482 int cnti = 0;
1483 MergeArrays(ptrs, sizes, [&cnti] (int col) { cnti++; } );
1484 cnt[i] = cnti;
1485 }
1486 },
1487 TasksPerThread(10));
1488
1489
1490 auto sum = make_shared<SparseMatrix<T>>(cnt, matb.Width());
1491 sum->AsVector() = 0.0;
1492
1493 // fill col-indices
1494 ParallelForRange
1495 (mata.Height(), [&] (IntRange r)
1496 {
1497 Array<BaseSparseMatrix::ColIdx*> ptrs(2);
1498 Array<int> sizes(2);
1499 for (int i : r)
1500 {
1501 BaseSparseMatrix::ColIdx * ptr = sum->GetRowIndices(i).Addr(0);
1502 ptrs[0] = mata.GetRowIndices(i).Addr(0);
1503 ptrs[1] = matb.GetRowIndices(i).Addr(0);
1504 sizes[0] = mata.GetRowIndices(i).Size();
1505 sizes[1] = matb.GetRowIndices(i).Size();
1506 MergeArrays(ptrs, sizes, [&ptr] (int col)
1507 {
1508 *ptr = col;
1509 ptr++;
1510 } );
1511 }
1512 }, TasksPerThread(10));
1513
1514

Calls 9

ToStringFunction · 0.85
MergeArraysFunction · 0.85
AddrMethod · 0.80
ShapeMethod · 0.45
HeightMethod · 0.45
WidthMethod · 0.45
SizeMethod · 0.45
AsVectorMethod · 0.45
RangeMethod · 0.45

Tested by

no test coverage detected