Actual source code: veccusp.c
petsc-3.8.4 2018-03-24
1: /*
2: Implementation of the sequential cusp vectors.
4: This file contains the code that can be compiled with a C
5: compiler. The companion file veccusp2.cu contains the code that
6: must be compiled with nvcc or a C++ compiler.
7: */
9: #define PETSC_SKIP_COMPLEX
10: #define PETSC_SKIP_SPINLOCK
12: #include <petscconf.h>
13: #include <petsc/private/vecimpl.h> /*I <petscvec.h> I*/
14: #include <../src/vec/vec/impls/dvecimpl.h>
15: #include <../src/vec/vec/impls/seq/seqcusp/cuspvecimpl.h>
18: /*
19: Allocates space for the vector array on the Host if it does not exist.
20: Does NOT change the PetscCUSPFlag for the vector
21: Does NOT zero the CUSP array
22: */
23: PetscErrorCode VecCUSPAllocateCheckHost(Vec v)
24: {
26: PetscScalar *array;
27: Vec_Seq *s = (Vec_Seq*)v->data;
28: PetscInt n = v->map->n;
31: if (!s) {
32: PetscNewLog((PetscObject)v,&s);
33: v->data = s;
34: }
35: if (!s->array) {
36: PetscMalloc1(n,&array);
37: PetscLogObjectMemory((PetscObject)v,n*sizeof(PetscScalar));
38: s->array = array;
39: s->array_allocated = array;
40: if (v->valid_GPU_array == PETSC_CUSP_UNALLOCATED) {
41: v->valid_GPU_array = PETSC_CUSP_CPU;
42: }
43: }
44: return(0);
45: }
47: PetscErrorCode VecCopy_SeqCUSP_Private(Vec xin,Vec yin)
48: {
49: PetscScalar *ya;
50: const PetscScalar *xa;
51: PetscErrorCode ierr;
54: VecCUSPAllocateCheckHost(xin);
55: VecCUSPAllocateCheckHost(yin);
56: if (xin != yin) {
57: VecGetArrayRead(xin,&xa);
58: VecGetArray(yin,&ya);
59: PetscMemcpy(ya,xa,xin->map->n*sizeof(PetscScalar));
60: VecRestoreArrayRead(xin,&xa);
61: VecRestoreArray(yin,&ya);
62: }
63: return(0);
64: }
66: PetscErrorCode VecSetRandom_SeqCUSP_Private(Vec xin,PetscRandom r)
67: {
69: PetscInt n = xin->map->n,i;
70: PetscScalar *xx;
73: VecGetArray(xin,&xx);
74: for (i=0; i<n; i++) {PetscRandomGetValue(r,&xx[i]);}
75: VecRestoreArray(xin,&xx);
76: return(0);
77: }
79: PetscErrorCode VecDestroy_SeqCUSP_Private(Vec v)
80: {
81: Vec_Seq *vs = (Vec_Seq*)v->data;
85: PetscObjectSAWsViewOff(v);
86: #if defined(PETSC_USE_LOG)
87: PetscLogObjectState((PetscObject)v,"Length=%D",v->map->n);
88: #endif
89: if (vs) {
90: if (vs->array_allocated) PetscFree(vs->array_allocated);
91: PetscFree(vs);
92: }
93: return(0);
94: }
96: PetscErrorCode VecResetArray_SeqCUSP_Private(Vec vin)
97: {
98: Vec_Seq *v = (Vec_Seq*)vin->data;
101: v->array = v->unplacedarray;
102: v->unplacedarray = 0;
103: return(0);
104: }
106: PetscErrorCode VecCUSPAllocateCheck_Public(Vec v)
107: {
111: VecCUSPAllocateCheck(v);
112: return(0);
113: }
115: PetscErrorCode VecCUSPCopyToGPU_Public(Vec v)
116: {
120: VecCUSPCopyToGPU(v);
121: return(0);
122: }
124: /*
125: VecCUSPCopyToGPUSome_Public - Copies certain entries down to the GPU from the CPU of a vector
127: Input Parameters:
128: . v - the vector
129: . indices - the requested indices, this should be created with CUSPIndicesCreate()
131: */
132: PetscErrorCode VecCUSPCopyToGPUSome_Public(Vec v,PetscCUSPIndices ci)
133: {
137: VecCUSPCopyToGPUSome(v,ci);
138: return(0);
139: }
141: /*
142: VecCUSPCopyFromGPUSome_Public - Copies certain entries up to the CPU from the GPU of a vector
144: Input Parameters:
145: + v - the vector
146: - indices - the requested indices, this should be created with CUSPIndicesCreate()
147: */
148: PetscErrorCode VecCUSPCopyFromGPUSome_Public(Vec v,PetscCUSPIndices ci)
149: {
153: VecCUSPCopyFromGPUSome(v,ci);
154: return(0);
155: }
157: PetscErrorCode VecSetRandom_SeqCUSP(Vec xin,PetscRandom r)
158: {
162: VecSetRandom_SeqCUSP_Private(xin,r);
163: xin->valid_GPU_array = PETSC_CUSP_CPU;
164: return(0);
165: }
167: PetscErrorCode VecResetArray_SeqCUSP(Vec vin)
168: {
172: VecCUSPCopyFromGPU(vin);
173: VecResetArray_SeqCUSP_Private(vin);
174: vin->valid_GPU_array = PETSC_CUSP_CPU;
175: return(0);
176: }
178: PetscErrorCode VecPlaceArray_SeqCUSP(Vec vin,const PetscScalar *a)
179: {
183: VecCUSPCopyFromGPU(vin);
184: VecPlaceArray_Seq(vin,a);
185: vin->valid_GPU_array = PETSC_CUSP_CPU;
186: return(0);
187: }
189: PetscErrorCode VecReplaceArray_SeqCUSP(Vec vin,const PetscScalar *a)
190: {
194: VecCUSPCopyFromGPU(vin);
195: VecReplaceArray_Seq(vin,a);
196: vin->valid_GPU_array = PETSC_CUSP_CPU;
197: return(0);
198: }
200: /*@
201: VecCreateSeqCUSP - Creates a standard, sequential array-style vector.
203: Collective on MPI_Comm
205: Input Parameter:
206: . comm - the communicator, should be PETSC_COMM_SELF
207: . n - the vector length
209: Output Parameter:
210: . V - the vector
212: Notes:
213: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
214: same type as an existing vector.
216: Level: intermediate
218: Concepts: vectors^creating sequential
220: .seealso: VecCreateMPI(), VecCreate(), VecDuplicate(), VecDuplicateVecs(), VecCreateGhost()
221: @*/
222: PetscErrorCode VecCreateSeqCUSP(MPI_Comm comm,PetscInt n,Vec *v)
223: {
227: VecCreate(comm,v);
228: VecSetSizes(*v,n,n);
229: VecSetType(*v,VECSEQCUSP);
230: return(0);
231: }
233: PetscErrorCode VecDuplicate_SeqCUSP(Vec win,Vec *V)
234: {
238: VecCreateSeqCUSP(PetscObjectComm((PetscObject)win),win->map->n,V);
239: PetscLayoutReference(win->map,&(*V)->map);
240: PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*V))->olist);
241: PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*V))->qlist);
242: (*V)->stash.ignorenegidx = win->stash.ignorenegidx;
243: return(0);
244: }
246: PETSC_EXTERN PetscErrorCode VecCreate_SeqCUSP(Vec V)
247: {
249: PetscMPIInt size;
252: MPI_Comm_size(PetscObjectComm((PetscObject)V),&size);
253: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot create VECSEQCUSP on more than one process");
254: VecCreate_Seq_Private(V,0);
255: PetscObjectChangeTypeName((PetscObject)V,VECSEQCUSP);
257: V->ops->dot = VecDot_SeqCUSP;
258: V->ops->norm = VecNorm_SeqCUSP;
259: V->ops->tdot = VecTDot_SeqCUSP;
260: V->ops->scale = VecScale_SeqCUSP;
261: V->ops->copy = VecCopy_SeqCUSP;
262: V->ops->set = VecSet_SeqCUSP;
263: V->ops->swap = VecSwap_SeqCUSP;
264: V->ops->axpy = VecAXPY_SeqCUSP;
265: V->ops->axpby = VecAXPBY_SeqCUSP;
266: V->ops->axpbypcz = VecAXPBYPCZ_SeqCUSP;
267: V->ops->pointwisemult = VecPointwiseMult_SeqCUSP;
268: V->ops->pointwisedivide = VecPointwiseDivide_SeqCUSP;
269: V->ops->setrandom = VecSetRandom_SeqCUSP;
270: V->ops->dot_local = VecDot_SeqCUSP;
271: V->ops->tdot_local = VecTDot_SeqCUSP;
272: V->ops->norm_local = VecNorm_SeqCUSP;
273: V->ops->mdot_local = VecMDot_SeqCUSP;
274: V->ops->maxpy = VecMAXPY_SeqCUSP;
275: V->ops->mdot = VecMDot_SeqCUSP;
276: V->ops->aypx = VecAYPX_SeqCUSP;
277: V->ops->waxpy = VecWAXPY_SeqCUSP;
278: V->ops->dotnorm2 = VecDotNorm2_SeqCUSP;
279: V->ops->placearray = VecPlaceArray_SeqCUSP;
280: V->ops->replacearray = VecReplaceArray_SeqCUSP;
281: V->ops->resetarray = VecResetArray_SeqCUSP;
282: V->ops->destroy = VecDestroy_SeqCUSP;
283: V->ops->duplicate = VecDuplicate_SeqCUSP;
284: V->ops->conjugate = VecConjugate_SeqCUSP;
285: V->ops->getlocalvector = VecGetLocalVector_SeqCUSP;
286: V->ops->restorelocalvector = VecRestoreLocalVector_SeqCUSP;
287: V->ops->getlocalvectorread = VecGetLocalVector_SeqCUSP;
288: V->ops->restorelocalvectorread = VecRestoreLocalVector_SeqCUSP;
290: VecCUSPAllocateCheck(V);
291: V->valid_GPU_array = PETSC_CUSP_GPU;
292: VecSet(V,0.0);
293: return(0);
294: }