Skip to content

Commit

Permalink
dense matrix -> attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahdhn committed Jul 7, 2024
1 parent d0b4eab commit 8d987ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
44 changes: 7 additions & 37 deletions apps/MCF/mcf_sparse_matrix.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void mcf_rxmesh_cusolver_chol(rxmesh::RXMeshStatic& rx,
constexpr uint32_t blockThreads = 256;

uint32_t num_vertices = rx.get_num_vertices();
auto coords = rx.get_input_vertex_coordinates();

auto coords = rx.get_input_vertex_coordinates();

SparseMatrix<float> A_mat(rx);
DenseMatrix<float> B_mat(num_vertices, 3);
Expand Down Expand Up @@ -190,44 +191,13 @@ void mcf_rxmesh_cusolver_chol(rxmesh::RXMeshStatic& rx,
// Solving the linear system using chol factorization and no reordering
A_mat.spmat_linear_solve(B_mat, *X_mat, Solver::CHOL, Reorder::NONE);

// move the results to the host
X_mat->move(rxmesh::DEVICE, rxmesh::HOST);

const T tol = 0.001;
T tmp_tol = tol;
bool passed = true;
rx.for_each_vertex(HOST, [&](const VertexHandle vh) {
uint32_t v_id = rx.map_to_global(vh);
uint32_t v_linear_id = rx.linear_id(vh);


for (uint32_t i = 0; i < 3; ++i) {
tmp_tol =
std::abs(((*X_mat)(v_linear_id, i) - ground_truth[v_id][i]) /
ground_truth[v_id][i]);

if (tmp_tol > tol) {
RXMESH_WARN("val: {}, truth: {}, tol: {}\n",
(*X_mat)(v_linear_id, i),
ground_truth[v_id][i],
tmp_tol);
passed = false;
break;
}
}
});


rx.for_each_vertex(HOST, [&](const VertexHandle vh) {
uint32_t v_linear_id = rx.linear_id(vh);

for (uint32_t i = 0; i < 3; ++i) {
(*coords)(vh, i) = (*X_mat)(v_linear_id, i);
}
});
// copy the results to attributes
coords->from_matrix(X_mat.get());

// rx.export_obj("mcf_rxmesh_chol.obj", *coords);
// rx.get_polyscope_mesh()->updateVertexPositions(*coords);
// polyscope::show();

EXPECT_TRUE(passed);
rx.get_polyscope_mesh()->updateVertexPositions(*coords);
polyscope::show();
}
46 changes: 45 additions & 1 deletion include/rxmesh/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Attribute : public AttributeBase
* rows represent the number of mesh elements of this attribute and number
* of columns is the number of attributes
*/
std::shared_ptr<DenseMatrix<T>> to_matrix()
std::shared_ptr<DenseMatrix<T>> to_matrix() const
{
std::shared_ptr<DenseMatrix<T>> mat =
std::make_shared<DenseMatrix<T>>(rows(), cols());
Expand Down Expand Up @@ -227,6 +227,50 @@ class Attribute : public AttributeBase
return mat;
}

/**
* @brief copy a dense matrix to this attribute. The copying happens on the
* host side, i.e., we copy the content of mat which is on the host to this
* attribute on the host side
* @param mat
*/
void from_matrix(DenseMatrix<T>* mat)
{
assert(mat->rows() == rows());
assert(mat->cols() == cols());


if constexpr (std::is_same_v<HandleT, VertexHandle>) {
m_rxmesh->for_each_vertex(HOST, [&](const VertexHandle vh) {
uint32_t i = m_rxmesh->linear_id(vh);

for (uint32_t j = 0; j < cols(); ++j) {
this->operator()(vh, j) = (*mat)(i, j);
}
});
}

if constexpr (std::is_same_v<HandleT, EdgeHandle>) {
m_rxmesh->for_each_edge(HOST, [&](const EdgeHandle eh) {
uint32_t i = m_rxmesh->linear_id(eh);

for (uint32_t j = 0; j < cols(); ++j) {
this->operator()(eh, j) = (*mat)(i, j);
}
});
}

if constexpr (std::is_same_v<HandleT, FaceHandle>) {
m_rxmesh->for_each_face(HOST, [&](const FaceHandle fh) {
uint32_t i = m_rxmesh->linear_id(fh);

for (uint32_t j = 0; j < cols(); ++j) {
this->operator()(fh, j) = (*mat)(i, j);
}
});
}
}


/**
* @brief get the number of elements in a patch. The element type
* corresponds to the template HandleT
Expand Down

0 comments on commit 8d987ec

Please sign in to comment.