xref: /petsc/src/mat/utils/matstashspace.c (revision fbf9dbe564678ed6eff1806adbc4c4f01b9743f4)
1 
2 #include <petsc/private/matimpl.h>
3 
4 /* Get new PetscMatStashSpace into the existing space */
5 PetscErrorCode PetscMatStashSpaceGet(PetscInt bs2, PetscInt n, PetscMatStashSpace *space)
6 {
7   PetscMatStashSpace a;
8 
9   PetscFunctionBegin;
10   if (!n) PetscFunctionReturn(PETSC_SUCCESS);
11 
12   PetscCall(PetscMalloc(sizeof(struct _MatStashSpace), &a));
13   PetscCall(PetscMalloc3(n * bs2, &(a->space_head), n, &a->idx, n, &a->idy));
14 
15   a->val              = a->space_head;
16   a->local_remaining  = n;
17   a->local_used       = 0;
18   a->total_space_size = 0;
19   a->next             = NULL;
20 
21   if (*space) {
22     (*space)->next      = a;
23     a->total_space_size = (*space)->total_space_size;
24   }
25   a->total_space_size += n;
26   *space = a;
27   PetscFunctionReturn(PETSC_SUCCESS);
28 }
29 
30 /* Copy the values in space into arrays val, idx and idy. Then destroy space */
31 PetscErrorCode PetscMatStashSpaceContiguous(PetscInt bs2, PetscMatStashSpace *space, PetscScalar *val, PetscInt *idx, PetscInt *idy)
32 {
33   PetscMatStashSpace a;
34 
35   PetscFunctionBegin;
36   while ((*space)) {
37     a = (*space)->next;
38     PetscCall(PetscArraycpy(val, (*space)->val, (*space)->local_used * bs2));
39     val += bs2 * (*space)->local_used;
40     PetscCall(PetscArraycpy(idx, (*space)->idx, (*space)->local_used));
41     idx += (*space)->local_used;
42     PetscCall(PetscArraycpy(idy, (*space)->idy, (*space)->local_used));
43     idy += (*space)->local_used;
44 
45     PetscCall(PetscFree3((*space)->space_head, (*space)->idx, (*space)->idy));
46     PetscCall(PetscFree(*space));
47     *space = a;
48   }
49   PetscFunctionReturn(PETSC_SUCCESS);
50 }
51 
52 PetscErrorCode PetscMatStashSpaceDestroy(PetscMatStashSpace *space)
53 {
54   PetscMatStashSpace a;
55 
56   PetscFunctionBegin;
57   while (*space) {
58     a = (*space)->next;
59     PetscCall(PetscFree3((*space)->space_head, (*space)->idx, (*space)->idy));
60     PetscCall(PetscFree((*space)));
61     *space = a;
62   }
63   *space = NULL;
64   PetscFunctionReturn(PETSC_SUCCESS);
65 }
66