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 /* printf(" 2. total_space_size : %d\n",a->total_space_size); */ 35 /* 36 PetscMPIInt rank; 37 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 38 printf("[%d] MatStashSpaceCreate space %p, next %p, total_space_size: %d, space->val %p\n",rank,*space,(*space)->next,(*space)->total_space_size,(*space)->val); 39 */ 40 PetscFunctionReturn(0); 41 } 42 43 /* Copy the values in space into arrays val, idx and idy. Then destroy space */ 44 #undef __FUNCT__ 45 #define __FUNCT__ "PetscMatStashSpaceContiguous" 46 PetscErrorCode PetscMatStashSpaceContiguous(PetscInt bs2,PetscMatStashSpace *space,PetscScalar *val,PetscInt *idx,PetscInt *idy) 47 { 48 PetscMatStashSpace a; 49 PetscErrorCode ierr; 50 51 PetscFunctionBegin; 52 while ((*space) != PETSC_NULL){ 53 a = (*space)->next; 54 ierr = PetscMemcpy(val,(*space)->val,((*space)->local_used*bs2)*sizeof(MatScalar));CHKERRQ(ierr); 55 val += bs2*(*space)->local_used; 56 ierr = PetscMemcpy(idx,(*space)->idx,((*space)->local_used)*sizeof(PetscInt));CHKERRQ(ierr); 57 idx += (*space)->local_used; 58 ierr = PetscMemcpy(idy,(*space)->idy,((*space)->local_used)*sizeof(PetscInt));CHKERRQ(ierr); 59 idy += (*space)->local_used; 60 61 ierr = PetscFree((*space)->space_head);CHKERRQ(ierr); 62 ierr = PetscFree(*space);CHKERRQ(ierr); 63 *space = a; 64 } 65 PetscFunctionReturn(0); 66 } 67 68 #undef __FUNCT__ 69 #define __FUNCT__ "PetscMatStashSpaceDestroy" 70 PetscErrorCode PetscMatStashSpaceDestroy(PetscMatStashSpace space) 71 { 72 PetscMatStashSpace a; 73 PetscErrorCode ierr; 74 75 PetscFunctionBegin; 76 while (space != PETSC_NULL){ 77 a = space->next; 78 ierr = PetscFree(space->space_head);CHKERRQ(ierr); 79 ierr = PetscFree(space);CHKERRQ(ierr); 80 space = a; 81 } 82 PetscFunctionReturn(0); 83 } 84