Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_sparsity function to distributed2d #559

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/C-interface/bml_introspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ bml_get_sparsity(
case csr:
return bml_get_sparsity_csr(A, threshold);
break;
#ifdef DO_MPI
case distributed2d:
return bml_get_sparsity_distributed2d(A, threshold);
break;
#endif
default:
LOG_ERROR("unknown matrix type in bml_get_sparsity\n");
break;
Expand Down
28 changes: 28 additions & 0 deletions src/C-interface/distributed2d/bml_introspection_distributed2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../bml_logger.h"
#include "../bml_logger.h"
#include "../bml_types.h"
#include "../bml_parallel.h"
#include "bml_introspection_distributed2d.h"
#include "bml_types_distributed2d.h"

Expand Down Expand Up @@ -96,3 +97,30 @@ bml_get_local_matrix_distributed2d(
return NULL;
}
}

/** Return the sparsity of a matrix.
*
* Note that the the sparsity of a matrix is defined
* as NumberOfZeroes/N*N where N is the matrix dimension.
* The density of matrix A will be defined as 1-sparsity(A)
*
* \ingroup introspection_group_C
*
* \param A The bml matrix.
* \param threshold The threshold used to compute the sparsity.
* \return The sparsity of A.
*/
double
bml_get_sparsity_distributed2d(
bml_matrix_distributed2d_t * A,
double threshold)
{
double sp = bml_get_sparsity(A->matrix, threshold);
int nloc = A->N / A->nprows;
sp *= nloc * nloc;

bml_sumRealReduce(&sp);
sp /= (A->N * A->N);

return sp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ int bml_get_M_distributed2d(

bml_matrix_t *bml_get_local_matrix_distributed2d(
bml_matrix_distributed2d_t * A);

double bml_get_sparsity_distributed2d(
bml_matrix_distributed2d_t * A,
double threshold);
#endif
1 change: 1 addition & 0 deletions tests/C-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ set(testlist-mpi
convert
copy
diagonalize
get_sparsity
import_export
introspection
io_matrix
Expand Down
41 changes: 27 additions & 14 deletions tests/C-tests/get_sparsity_typed.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,49 @@ int TYPED_FUNC(
double threshold = 0.1;
REAL_T *A_dense;

A = bml_random_matrix(matrix_type, matrix_precision, N, M, sequential);
bml_distribution_mode_t distrib_mode = sequential;
#ifdef DO_MPI
if (bml_getNRanks() > 1)
{
LOG_INFO("Use distributed matrix\n");
distrib_mode = distributed;
}
#endif

A = bml_random_matrix(matrix_type, matrix_precision, N, M, distrib_mode);

sparsity = bml_get_sparsity(A, threshold);

// Form bml to dense_array
A_dense = bml_export_to_dense(A, dense_row_major);

for (int i = 0; i < N; i++)
if (bml_getMyRank() == 0)
{
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
{
if (ABS(A_dense[ROWMAJOR(i, j, N, N)]) > threshold)
for (int j = 0; j < N; j++)
{
nnzs++;
if (ABS(A_dense[ROWMAJOR(i, j, N, N)]) > threshold)
{
nnzs++;
}
}
}
}

sparsity_ref = (1.0 - (double) nnzs / ((double) (N * N)));
sparsity_ref = (1.0 - (double) nnzs / ((double) (N * N)));

if (fabs(sparsity - sparsity_ref) > 1e-12)
{
LOG_ERROR("bml_get_sparsity is corrupted\n");
return -1;
}
if (fabs(sparsity - sparsity_ref) > 1e-12)
{
LOG_ERROR("bml_get_sparsity is corrupted\n");
return -1;
}

printf("Sparsity = %f\n", sparsity);
printf("Sparsity = %f\n", sparsity);
}

bml_deallocate(&A);
bml_free_memory(A_dense);
if (bml_getMyRank() == 0)
bml_free_memory(A_dense);

LOG_INFO("bml_get_sparsity passed\n");

Expand Down