PETSC_EXTERN PetscErrorCode VecCreate_Seq(Vec V) { Vec_Seq *s; PetscScalar *array; PetscErrorCode ierr; PetscInt n = PetscMax(V->map->n,V->map->N); PetscMPIInt size; PetscFunctionBegin; ierr = MPI_Comm_size(PetscObjectComm((PetscObject)V),&size);CHKERRQ(ierr); if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot create VECSEQ on more than one process"); #if !defined(PETSC_USE_MIXED_PRECISION) ierr = PetscMalloc1(n,&array);CHKERRQ(ierr); ierr = PetscLogObjectMemory((PetscObject)V, n*sizeof(PetscScalar));CHKERRQ(ierr); ierr = VecCreate_Seq_Private(V,array);CHKERRQ(ierr); s = (Vec_Seq*)V->data; s->array_allocated = array; ierr = VecSet(V,0.0);CHKERRQ(ierr); #else switch (((PetscObject)V)->precision) { case PETSC_PRECISION_SINGLE: { float *aarray; ierr = PetscMalloc1(n,&aarray);CHKERRQ(ierr); ierr = PetscLogObjectMemory((PetscObject)V, n*sizeof(float));CHKERRQ(ierr); ierr = PetscMemzero(aarray,n*sizeof(float));CHKERRQ(ierr); ierr = VecCreate_Seq_Private(V,aarray);CHKERRQ(ierr); s = (Vec_Seq*)V->data; s->array_allocated = (PetscScalar*)aarray; } break; case PETSC_PRECISION_DOUBLE: { double *aarray; ierr = PetscMalloc1(n,&aarray);CHKERRQ(ierr); ierr = PetscLogObjectMemory((PetscObject)V, n*sizeof(double));CHKERRQ(ierr); ierr = PetscMemzero(aarray,n*sizeof(double));CHKERRQ(ierr); ierr = VecCreate_Seq_Private(V,aarray);CHKERRQ(ierr); s = (Vec_Seq*)V->data; s->array_allocated = (PetscScalar*)aarray; } break; default: SETERRQ1(PetscObjectComm((PetscObject)V),PETSC_ERR_SUP,"No support for mixed precision %d",(int)(((PetscObject)V)->precision)); } #endif PetscFunctionReturn(0); }
/*@C VecCreateSeqWithArray - Creates a standard,sequential array-style vector, where the user provides the array space to store the vector values. Collective on MPI_Comm Input Parameter: + comm - the communicator, should be PETSC_COMM_SELF . bs - the block size . n - the vector length - array - memory where the vector elements are to be stored. Output Parameter: . V - the vector Notes: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the same type as an existing vector. If the user-provided array is NULL, then VecPlaceArray() can be used at a later stage to SET the array for storing the vector values. PETSc does NOT free the array when the vector is destroyed via VecDestroy(). The user should not free the array until the vector is destroyed. Level: intermediate Concepts: vectors^creating with array .seealso: VecCreateMPIWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost(), VecCreateSeq(), VecPlaceArray() @*/ PetscErrorCode VecCreateSeqWithArray(MPI_Comm comm,PetscInt bs,PetscInt n,const PetscScalar array[],Vec *V) { PetscErrorCode ierr; PetscMPIInt size; PetscFunctionBegin; ierr = VecCreate(comm,V);CHKERRQ(ierr); ierr = VecSetSizes(*V,n,n);CHKERRQ(ierr); ierr = VecSetBlockSize(*V,bs);CHKERRQ(ierr); ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot create VECSEQ on more than one process"); ierr = VecCreate_Seq_Private(*V,array);CHKERRQ(ierr); PetscFunctionReturn(0); }