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);