Skip to content

Commit

Permalink
switch most uses of malloc to calloc
Browse files Browse the repository at this point in the history
  • Loading branch information
bodono committed Jan 12, 2022
1 parent 01d3e2e commit eab7ed6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
36 changes: 18 additions & 18 deletions linsys/cpu/direct/private.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static csc *form_kkt(const ScsMatrix *A, const ScsMatrix *P, scs_float *diag_p,
}

K->nz = count;
idx_mapping = (scs_int *)scs_malloc(K->nz * sizeof(scs_int));
idx_mapping = (scs_int *)scs_calloc(K->nz, sizeof(scs_int));
Kcsc = SCS(cs_compress)(K, idx_mapping);
for (i = 0; i < m + n; i++) {
diag_r_idxs[i] = idx_mapping[diag_r_idxs[i]];
Expand All @@ -146,18 +146,18 @@ static csc *form_kkt(const ScsMatrix *A, const ScsMatrix *P, scs_float *diag_p,
}

static scs_int _ldl_init(csc *A, scs_int *P, scs_float **info) {
*info = (scs_float *)scs_malloc(AMD_INFO * sizeof(scs_float));
*info = (scs_float *)scs_calloc(AMD_INFO, sizeof(scs_float));
return amd_order(A->n, A->p, A->i, P, (scs_float *)SCS_NULL, *info);
}

/* call only once */
static scs_int ldl_prepare(ScsLinSysWork *p) {
csc *kkt = p->kkt, *L = p->L;
scs_int n = kkt->n;
p->etree = (scs_int *)scs_malloc(n * sizeof(scs_int));
p->Lnz = (scs_int *)scs_malloc(n * sizeof(scs_int));
p->iwork = (scs_int *)scs_malloc(3 * n * sizeof(scs_int));
L->p = (scs_int *)scs_malloc((1 + n) * sizeof(scs_int));
p->etree = (scs_int *)scs_calloc(n, sizeof(scs_int));
p->Lnz = (scs_int *)scs_calloc(n, sizeof(scs_int));
p->iwork = (scs_int *)scs_calloc(3 * n, sizeof(scs_int));
L->p = (scs_int *)scs_calloc((1 + n), sizeof(scs_int));
L->nzmax = QDLDL_etree(n, kkt->p, kkt->i, p->iwork, p->Lnz, p->etree);
if (L->nzmax < 0) {
scs_printf("Error in elimination tree calculation.\n");
Expand All @@ -169,12 +169,12 @@ static scs_int ldl_prepare(ScsLinSysWork *p) {
return L->nzmax;
}

L->x = (scs_float *)scs_malloc(L->nzmax * sizeof(scs_float));
L->i = (scs_int *)scs_malloc(L->nzmax * sizeof(scs_int));
p->Dinv = (scs_float *)scs_malloc(n * sizeof(scs_float));
p->D = (scs_float *)scs_malloc(n * sizeof(scs_float));
p->bwork = (scs_int *)scs_malloc(n * sizeof(scs_int));
p->fwork = (scs_float *)scs_malloc(n * sizeof(scs_float));
L->x = (scs_float *)scs_calloc(L->nzmax, sizeof(scs_float));
L->i = (scs_int *)scs_calloc(L->nzmax, sizeof(scs_int));
p->Dinv = (scs_float *)scs_calloc(n, sizeof(scs_float));
p->D = (scs_float *)scs_calloc(n, sizeof(scs_float));
p->bwork = (scs_int *)scs_calloc(n, sizeof(scs_int));
p->fwork = (scs_float *)scs_calloc(n, sizeof(scs_float));
return L->nzmax;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ static scs_int *cs_pinv(scs_int const *p, scs_int n) {
if (!p) {
return SCS_NULL;
} /* p = SCS_NULL denotes identity */
pinv = (scs_int *)scs_malloc(n * sizeof(scs_int)); /* allocate result */
pinv = (scs_int *)scs_calloc(n, sizeof(scs_int)); /* allocate result */
if (!pinv) {
return SCS_NULL;
} /* out of memory */
Expand Down Expand Up @@ -308,7 +308,7 @@ static csc *permute_kkt(const ScsMatrix *A, const ScsMatrix *P,
amd_info(info);
#endif
Pinv = cs_pinv(p->perm, A->n + A->m);
idx_mapping = (scs_int *)scs_malloc(kkt->nzmax * sizeof(scs_int));
idx_mapping = (scs_int *)scs_calloc(kkt->nzmax, sizeof(scs_int));
kkt_perm = cs_symperm(kkt, Pinv, idx_mapping, 1);
for (i = 0; i < A->n + A->m; i++) {
p->diag_r_idxs[i] = idx_mapping[p->diag_r_idxs[i]];
Expand Down Expand Up @@ -346,10 +346,10 @@ ScsLinSysWork *SCS(init_lin_sys_work)(const ScsMatrix *A, const ScsMatrix *P,
p->m = A->m;
p->n = A->n;
p->diag_p = (scs_float *)scs_calloc(A->n, sizeof(scs_float));
p->perm = (scs_int *)scs_malloc(sizeof(scs_int) * n_plus_m);
p->L = (csc *)scs_malloc(sizeof(csc));
p->bp = (scs_float *)scs_malloc(n_plus_m * sizeof(scs_float));
p->diag_r_idxs = (scs_int *)scs_malloc(n_plus_m * sizeof(scs_int));
p->perm = (scs_int *)scs_calloc(sizeof(scs_int), n_plus_m);
p->L = (csc *)scs_calloc(1, sizeof(csc));
p->bp = (scs_float *)scs_calloc(n_plus_m, sizeof(scs_float));
p->diag_r_idxs = (scs_int *)scs_calloc(n_plus_m, sizeof(scs_int));
p->factorizations = 0;
p->L->m = n_plus_m;
p->L->n = n_plus_m;
Expand Down
16 changes: 8 additions & 8 deletions linsys/cpu/indirect/private.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ ScsLinSysWork *SCS(init_lin_sys_work)(const ScsMatrix *A, const ScsMatrix *P,
p->m = A->m;
p->n = A->n;

p->p = (scs_float *)scs_malloc((A->n) * sizeof(scs_float));
p->r = (scs_float *)scs_malloc((A->n) * sizeof(scs_float));
p->Gp = (scs_float *)scs_malloc((A->n) * sizeof(scs_float));
p->tmp = (scs_float *)scs_malloc((A->m) * sizeof(scs_float));
p->p = (scs_float *)scs_calloc((A->n), sizeof(scs_float));
p->r = (scs_float *)scs_calloc((A->n), sizeof(scs_float));
p->Gp = (scs_float *)scs_calloc((A->n), sizeof(scs_float));
p->tmp = (scs_float *)scs_calloc((A->m), sizeof(scs_float));

/* memory for A transpose */
p->At = (ScsMatrix *)scs_malloc(sizeof(ScsMatrix));
p->At = (ScsMatrix *)scs_calloc(1, sizeof(ScsMatrix));
p->At->m = A->n;
p->At->n = A->m;
p->At->i = (scs_int *)scs_malloc((A->p[A->n]) * sizeof(scs_int));
p->At->p = (scs_int *)scs_malloc((A->m + 1) * sizeof(scs_int));
p->At->x = (scs_float *)scs_malloc((A->p[A->n]) * sizeof(scs_float));
p->At->i = (scs_int *)scs_calloc((A->p[A->n]), sizeof(scs_int));
p->At->p = (scs_int *)scs_calloc((A->m + 1), sizeof(scs_int));
p->At->x = (scs_float *)scs_calloc((A->p[A->n]), sizeof(scs_float));
transpose(A, p);

/* preconditioner memory */
Expand Down
6 changes: 3 additions & 3 deletions linsys/csparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ csc *SCS(cs_spalloc)(scs_int m, scs_int n, scs_int nzmax, scs_int values,
A->n = n;
A->nzmax = nzmax = MAX(nzmax, 1);
A->nz = triplet ? 0 : -1; /* allocate triplet or comp.col */
A->p = (scs_int *)scs_malloc((triplet ? nzmax : n + 1) * sizeof(scs_int));
A->i = (scs_int *)scs_malloc(nzmax * sizeof(scs_int));
A->x = values ? (scs_float *)scs_malloc(nzmax * sizeof(scs_float)) : SCS_NULL;
A->p = (scs_int *)scs_calloc((triplet ? nzmax : n + 1), sizeof(scs_int));
A->i = (scs_int *)scs_calloc(nzmax, sizeof(scs_int));
A->x = values ? (scs_float *)scs_calloc(nzmax, sizeof(scs_float)) : SCS_NULL;
return (!A->p || !A->i || (values && !A->x)) ? SCS(cs_spfree)(A) : A;
}

Expand Down
14 changes: 7 additions & 7 deletions linsys/scs_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ scs_int SCS(copy_matrix)(ScsMatrix **dstp, const ScsMatrix *src) {
A->n = src->n;
A->m = src->m;
/* A values, size: NNZ A */
A->x = (scs_float *)scs_malloc(sizeof(scs_float) * Anz);
A->x = (scs_float *)scs_calloc(Anz, sizeof(scs_float));
/* A row index, size: NNZ A */
A->i = (scs_int *)scs_malloc(sizeof(scs_int) * Anz);
A->i = (scs_int *)scs_calloc(Anz, sizeof(scs_int));
/* A column pointer, size: n+1 */
A->p = (scs_int *)scs_malloc(sizeof(scs_int) * (src->n + 1));
A->p = (scs_int *)scs_calloc(src->n + 1, sizeof(scs_int));
if (!A->x || !A->i || !A->p) {
return 0;
}
Expand Down Expand Up @@ -336,10 +336,10 @@ ScsScaling *SCS(normalize_a_p)(ScsMatrix *P, ScsMatrix *A, scs_float *b,
scs_int i;
scs_float s;
ScsScaling *scal = (ScsScaling *)scs_calloc(1, sizeof(ScsScaling));
scs_float *Dt = (scs_float *)scs_malloc(A->m * sizeof(scs_float));
scs_float *Et = (scs_float *)scs_malloc(A->n * sizeof(scs_float));
scal->D = (scs_float *)scs_malloc(A->m * sizeof(scs_float));
scal->E = (scs_float *)scs_malloc(A->n * sizeof(scs_float));
scs_float *Dt = (scs_float *)scs_calloc(A->m, sizeof(scs_float));
scs_float *Et = (scs_float *)scs_calloc(A->n, sizeof(scs_float));
scal->D = (scs_float *)scs_calloc(A->m, sizeof(scs_float));
scal->E = (scs_float *)scs_calloc(A->n, sizeof(scs_float));

#if VERBOSITY > 5
SCS(timer) normalize_timer;
Expand Down
2 changes: 1 addition & 1 deletion src/scs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void free_work(ScsWork *w) {
}
SCS(free_sol)(w->xys_orig);
free_residuals(w->r_orig);
if (w->stgs->normalize) {
if (w->stgs && w->stgs->normalize) {
SCS(free_sol)(w->xys_normalized);
free_residuals(w->r_normalized);
}
Expand Down

0 comments on commit eab7ed6

Please sign in to comment.