From 284683b00dce0c9ae8a7cbbbbb926f013ec88b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Thu, 30 Sep 2010 21:37:14 +0000 Subject: [PATCH] Add a routine for allocating a CSR matrix with known number of rows and non-zeros. Note that this routine is for allocation only. Caller must create and manage sparsity structure if this is being used in a global assembly process. This routine finds utility in MsMFEM BF construction. --- sparse_sys.c | 32 ++++++++++++++++++++++++++++++++ sparse_sys.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/sparse_sys.c b/sparse_sys.c index 544b54065..26691a613 100644 --- a/sparse_sys.c +++ b/sparse_sys.c @@ -37,6 +37,38 @@ csrmatrix_new_count_nnz(size_t m) } +/* Allocate CSR matrix, known nnz. Allocation only. Caller must + * build sparsity structure before using in global assembly. + * + * Returns fully allocated structure if successful and NULL otherwise. */ +/* ---------------------------------------------------------------------- */ +struct CSRMatrix * +csrmatrix_new_known_nnz(size_t m, size_t nnz) +/* ---------------------------------------------------------------------- */ +{ + struct CSRMatrix *new; + + new = malloc(1 * sizeof *new); + + if (new != NULL) { + new->ia = malloc((m + 1) * sizeof *new->ia); + new->ja = malloc(nnz * sizeof *new->ja); + new->sa = malloc(nnz * sizeof *new->sa); + + if ((new->ia == NULL) || (new->ja == NULL) || (new->sa == NULL)) { + csrmatrix_delete(new); + new = NULL; + } else { + new->m = m; + new->n = 0; + new->nnz = nnz; + } + } + + return new; +} + + /* ---------------------------------------------------------------------- */ size_t csrmatrix_new_elms_pushback(struct CSRMatrix *A) diff --git a/sparse_sys.h b/sparse_sys.h index 481043e95..1671a79d4 100644 --- a/sparse_sys.h +++ b/sparse_sys.h @@ -35,6 +35,9 @@ struct CSRMatrix struct CSRMatrix * csrmatrix_new_count_nnz(size_t m); +struct CSRMatrix * +csrmatrix_new_known_nnz(size_t m, size_t nnz); + size_t csrmatrix_new_elms_pushback(struct CSRMatrix *A);