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