Actual source code: vector.c

petsc-3.8.3 2017-12-09
Report Typos and Errors

  2: /*
  3:      Provides the interface functions for vector operations that do NOT have PetscScalar/PetscReal in the signature
  4:    These are the vector functions the user calls.
  5: */
  6:  #include <petsc/private/vecimpl.h>

  8: /* Logging support */
  9: PetscClassId  VEC_CLASSID;
 10: PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot;
 11: PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY;
 12: PetscLogEvent VEC_MTDot, VEC_NormBarrier, VEC_MAXPY, VEC_Swap, VEC_AssemblyBegin, VEC_ScatterBegin, VEC_ScatterEnd;
 13: PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier;
 14: PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication,VEC_ReduceBegin,VEC_ReduceEnd,VEC_Ops;
 15: PetscLogEvent VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_CUSPCopyFromGPU, VEC_CUSPCopyToGPU;
 16: PetscLogEvent VEC_CUSPCopyFromGPUSome, VEC_CUSPCopyToGPUSome;
 17: PetscLogEvent VEC_ViennaCLCopyFromGPU, VEC_ViennaCLCopyToGPU;
 18: PetscLogEvent VEC_CUDACopyFromGPU, VEC_CUDACopyToGPU;
 19: PetscLogEvent VEC_CUDACopyFromGPUSome, VEC_CUDACopyToGPUSome;

 21: /*@
 22:    VecStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
 23:        to be communicated to other processors during the VecAssemblyBegin/End() process

 25:     Not collective

 27:    Input Parameter:
 28: .   vec - the vector

 30:    Output Parameters:
 31: +   nstash   - the size of the stash
 32: .   reallocs - the number of additional mallocs incurred.
 33: .   bnstash   - the size of the block stash
 34: -   breallocs - the number of additional mallocs incurred.in the block stash

 36:    Level: advanced

 38: .seealso: VecAssemblyBegin(), VecAssemblyEnd(), Vec, VecStashSetInitialSize(), VecStashView()

 40: @*/
 41: PetscErrorCode  VecStashGetInfo(Vec vec,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
 42: {

 46:   VecStashGetInfo_Private(&vec->stash,nstash,reallocs);
 47:   VecStashGetInfo_Private(&vec->bstash,bnstash,breallocs);
 48:   return(0);
 49: }

 51: /*@
 52:    VecSetLocalToGlobalMapping - Sets a local numbering to global numbering used
 53:    by the routine VecSetValuesLocal() to allow users to insert vector entries
 54:    using a local (per-processor) numbering.

 56:    Logically Collective on Vec

 58:    Input Parameters:
 59: +  x - vector
 60: -  mapping - mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS()

 62:    Notes:
 63:    All vectors obtained with VecDuplicate() from this vector inherit the same mapping.

 65:    Level: intermediate

 67:    Concepts: vector^setting values with local numbering

 69: seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetValuesLocal(),
 70:            VecSetLocalToGlobalMapping(), VecSetValuesBlockedLocal()
 71: @*/
 72: PetscErrorCode  VecSetLocalToGlobalMapping(Vec x,ISLocalToGlobalMapping mapping)
 73: {


 80:   if (x->ops->setlocaltoglobalmapping) {
 81:     (*x->ops->setlocaltoglobalmapping)(x,mapping);
 82:   } else {
 83:     PetscLayoutSetISLocalToGlobalMapping(x->map,mapping);
 84:   }
 85:   return(0);
 86: }

 88: /*@
 89:    VecGetLocalToGlobalMapping - Gets the local-to-global numbering set by VecSetLocalToGlobalMapping()

 91:    Not Collective

 93:    Input Parameter:
 94: .  X - the vector

 96:    Output Parameter:
 97: .  mapping - the mapping

 99:    Level: advanced

101:    Concepts: vectors^local to global mapping
102:    Concepts: local to global mapping^for vectors

104: .seealso:  VecSetValuesLocal()
105: @*/
106: PetscErrorCode VecGetLocalToGlobalMapping(Vec X,ISLocalToGlobalMapping *mapping)
107: {
112:   *mapping = X->map->mapping;
113:   return(0);
114: }

116: /*@
117:    VecAssemblyBegin - Begins assembling the vector.  This routine should
118:    be called after completing all calls to VecSetValues().

120:    Collective on Vec

122:    Input Parameter:
123: .  vec - the vector

125:    Level: beginner

127:    Concepts: assembly^vectors

129: .seealso: VecAssemblyEnd(), VecSetValues()
130: @*/
131: PetscErrorCode  VecAssemblyBegin(Vec vec)
132: {

138:   VecStashViewFromOptions(vec,NULL,"-vec_view_stash");
139:   PetscLogEventBegin(VEC_AssemblyBegin,vec,0,0,0);
140:   if (vec->ops->assemblybegin) {
141:     (*vec->ops->assemblybegin)(vec);
142:   }
143:   PetscLogEventEnd(VEC_AssemblyBegin,vec,0,0,0);
144:   PetscObjectStateIncrease((PetscObject)vec);
145:   return(0);
146: }

148: /*@
149:    VecAssemblyEnd - Completes assembling the vector.  This routine should
150:    be called after VecAssemblyBegin().

152:    Collective on Vec

154:    Input Parameter:
155: .  vec - the vector

157:    Options Database Keys:
158: +  -vec_view - Prints vector in ASCII format
159: .  -vec_view ::ascii_matlab - Prints vector in ASCII MATLAB format to stdout
160: .  -vec_view matlab:filename - Prints vector in MATLAB format to matlaboutput.mat
161: .  -vec_view draw - Activates vector viewing using drawing tools
162: .  -display <name> - Sets display name (default is host)
163: .  -draw_pause <sec> - Sets number of seconds to pause after display
164: -  -vec_view socket - Activates vector viewing using a socket

166:    Level: beginner

168: .seealso: VecAssemblyBegin(), VecSetValues()
169: @*/
170: PetscErrorCode  VecAssemblyEnd(Vec vec)
171: {

176:   PetscLogEventBegin(VEC_AssemblyEnd,vec,0,0,0);
178:   if (vec->ops->assemblyend) {
179:     (*vec->ops->assemblyend)(vec);
180:   }
181:   PetscLogEventEnd(VEC_AssemblyEnd,vec,0,0,0);
182:   VecViewFromOptions(vec,NULL,"-vec_view");
183:   return(0);
184: }

186: /*@
187:    VecPointwiseMax - Computes the componentwise maximum w_i = max(x_i, y_i).

189:    Logically Collective on Vec

191:    Input Parameters:
192: .  x, y  - the vectors

194:    Output Parameter:
195: .  w - the result

197:    Level: advanced

199:    Notes: any subset of the x, y, and w may be the same vector.
200:           For complex numbers compares only the real part

202:    Concepts: vector^pointwise multiply

204: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
205: @*/
206: PetscErrorCode  VecPointwiseMax(Vec w,Vec x,Vec y)
207: {

219:   VecCheckSameSize(w,1,x,2);
220:   VecCheckSameSize(w,1,y,3);
221:   (*w->ops->pointwisemax)(w,x,y);
222:   PetscObjectStateIncrease((PetscObject)w);
223:   return(0);
224: }


227: /*@
228:    VecPointwiseMin - Computes the componentwise minimum w_i = min(x_i, y_i).

230:    Logically Collective on Vec

232:    Input Parameters:
233: .  x, y  - the vectors

235:    Output Parameter:
236: .  w - the result

238:    Level: advanced

240:    Notes: any subset of the x, y, and w may be the same vector.
241:           For complex numbers compares only the real part

243:    Concepts: vector^pointwise multiply

245: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
246: @*/
247: PetscErrorCode  VecPointwiseMin(Vec w,Vec x,Vec y)
248: {

260:   VecCheckSameSize(w,1,x,2);
261:   VecCheckSameSize(w,1,y,3);
262:   (*w->ops->pointwisemin)(w,x,y);
263:   PetscObjectStateIncrease((PetscObject)w);
264:   return(0);
265: }

267: /*@
268:    VecPointwiseMaxAbs - Computes the componentwise maximum of the absolute values w_i = max(abs(x_i), abs(y_i)).

270:    Logically Collective on Vec

272:    Input Parameters:
273: .  x, y  - the vectors

275:    Output Parameter:
276: .  w - the result

278:    Level: advanced

280:    Notes: any subset of the x, y, and w may be the same vector.

282:    Concepts: vector^pointwise multiply

284: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMax(), VecMaxPointwiseDivide()
285: @*/
286: PetscErrorCode  VecPointwiseMaxAbs(Vec w,Vec x,Vec y)
287: {

299:   VecCheckSameSize(w,1,x,2);
300:   VecCheckSameSize(w,1,y,3);
301:   (*w->ops->pointwisemaxabs)(w,x,y);
302:   PetscObjectStateIncrease((PetscObject)w);
303:   return(0);
304: }

306: /*@
307:    VecPointwiseDivide - Computes the componentwise division w = x/y.

309:    Logically Collective on Vec

311:    Input Parameters:
312: .  x, y  - the vectors

314:    Output Parameter:
315: .  w - the result

317:    Level: advanced

319:    Notes: any subset of the x, y, and w may be the same vector.

321:    Concepts: vector^pointwise divide

323: .seealso: VecPointwiseMult(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
324: @*/
325: PetscErrorCode  VecPointwiseDivide(Vec w,Vec x,Vec y)
326: {

338:   VecCheckSameSize(w,1,x,2);
339:   VecCheckSameSize(w,1,y,3);
340:   (*w->ops->pointwisedivide)(w,x,y);
341:   PetscObjectStateIncrease((PetscObject)w);
342:   return(0);
343: }


346: /*@
347:    VecDuplicate - Creates a new vector of the same type as an existing vector.

349:    Collective on Vec

351:    Input Parameters:
352: .  v - a vector to mimic

354:    Output Parameter:
355: .  newv - location to put new vector

357:    Notes:
358:    VecDuplicate() DOES NOT COPY the vector entries, but rather allocates storage
359:    for the new vector.  Use VecCopy() to copy a vector.

361:    Use VecDestroy() to free the space. Use VecDuplicateVecs() to get several
362:    vectors.

364:    Level: beginner

366: .seealso: VecDestroy(), VecDuplicateVecs(), VecCreate(), VecCopy()
367: @*/
368: PetscErrorCode  VecDuplicate(Vec v,Vec *newv)
369: {

376:   (*v->ops->duplicate)(v,newv);
377:   PetscObjectStateIncrease((PetscObject)*newv);
378:   return(0);
379: }

381: /*@
382:    VecDestroy - Destroys a vector.

384:    Collective on Vec

386:    Input Parameters:
387: .  v  - the vector

389:    Level: beginner

391: .seealso: VecDuplicate(), VecDestroyVecs()
392: @*/
393: PetscErrorCode  VecDestroy(Vec *v)
394: {

398:   if (!*v) return(0);
400:   if (--((PetscObject)(*v))->refct > 0) {*v = 0; return(0);}

402:   PetscObjectSAWsViewOff((PetscObject)*v);
403:   /* destroy the internal part */
404:   if ((*v)->ops->destroy) {
405:     (*(*v)->ops->destroy)(*v);
406:   }
407:   /* destroy the external/common part */
408:   PetscLayoutDestroy(&(*v)->map);
409:   PetscHeaderDestroy(v);
410:   return(0);
411: }

413: /*@C
414:    VecDuplicateVecs - Creates several vectors of the same type as an existing vector.

416:    Collective on Vec

418:    Input Parameters:
419: +  m - the number of vectors to obtain
420: -  v - a vector to mimic

422:    Output Parameter:
423: .  V - location to put pointer to array of vectors

425:    Notes:
426:    Use VecDestroyVecs() to free the space. Use VecDuplicate() to form a single
427:    vector.

429:    Fortran Note:
430:    The Fortran interface is slightly different from that given below, it
431:    requires one to pass in V a Vec (integer) array of size at least m.
432:    See the Fortran chapter of the users manual and petsc/src/vec/vec/examples for details.

434:    Level: intermediate

436: .seealso:  VecDestroyVecs(), VecDuplicate(), VecCreate(), VecDuplicateVecsF90()
437: @*/
438: PetscErrorCode  VecDuplicateVecs(Vec v,PetscInt m,Vec *V[])
439: {

446:   (*v->ops->duplicatevecs)(v,m,V);
447:   return(0);
448: }

450: /*@C
451:    VecDestroyVecs - Frees a block of vectors obtained with VecDuplicateVecs().

453:    Collective on Vec

455:    Input Parameters:
456: +  vv - pointer to pointer to array of vector pointers, if NULL no vectors are destroyed
457: -  m - the number of vectors previously obtained, if zero no vectors are destroyed

459:    Fortran Note:
460:    The Fortran interface is slightly different from that given below.
461:    See the Fortran chapter of the users manual

463:    Level: intermediate

465: .seealso: VecDuplicateVecs(), VecDestroyVecsf90()
466: @*/
467: PetscErrorCode  VecDestroyVecs(PetscInt m,Vec *vv[])
468: {

473:   if (m < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of vectors %D",m);
474:   if (!m || !*vv) {*vv  = NULL; return(0);}
477:   (*(**vv)->ops->destroyvecs)(m,*vv);
478:   *vv  = NULL;
479:   return(0);
480: }

482: /*@C
483:    VecView - Views a vector object.

485:    Collective on Vec

487:    Input Parameters:
488: +  vec - the vector
489: -  viewer - an optional visualization context

491:    Notes:
492:    The available visualization contexts include
493: +     PETSC_VIEWER_STDOUT_SELF - for sequential vectors
494: .     PETSC_VIEWER_STDOUT_WORLD - for parallel vectors created on PETSC_COMM_WORLD
495: -     PETSC_VIEWER_STDOUT_(comm) - for parallel vectors created on MPI communicator comm

497:    You can change the format the vector is printed using the
498:    option PetscViewerPushFormat().

500:    The user can open alternative visualization contexts with
501: +    PetscViewerASCIIOpen() - Outputs vector to a specified file
502: .    PetscViewerBinaryOpen() - Outputs vector in binary to a
503:          specified file; corresponding input uses VecLoad()
504: .    PetscViewerDrawOpen() - Outputs vector to an X window display
505: -    PetscViewerSocketOpen() - Outputs vector to Socket viewer

507:    The user can call PetscViewerPushFormat() to specify the output
508:    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
509:    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
510: +    PETSC_VIEWER_DEFAULT - default, prints vector contents
511: .    PETSC_VIEWER_ASCII_MATLAB - prints vector contents in MATLAB format
512: .    PETSC_VIEWER_ASCII_INDEX - prints vector contents, including indices of vector elements
513: -    PETSC_VIEWER_ASCII_COMMON - prints vector contents, using a
514:          format common among all vector types

516:    Notes: You can pass any number of vector objects, or other PETSc objects to the same viewer.

518:    Notes for binary viewer: If you pass multiply vectors to a binary viewer you can read them back in in the same order
519: $     with VecLoad().
520: $
521: $    If the blocksize of the vector is greater than one then you must provide a unique prefix to
522: $    the vector with PetscObjectSetOptionsPrefix((PetscObject)vec,"uniqueprefix"); BEFORE calling VecView() on the
523: $    vector to be stored and then set that same unique prefix on the vector that you pass to VecLoad(). The blocksize
524: $    information is stored in an ASCII file with the same name as the binary file plus a ".info" appended to the
525: $    filename. If you copy the binary file, make sure you copy the associated .info file with it.

527:    Notes for HDF5 Viewer: the name of the Vec (given with PetscObjectSetName() is the name that is used
528: $    for the object in the HDF5 file. If you wish to store the same vector to the HDF5 viewer (with different values,
529: $    obviously) several times, you must change its name each time before calling the VecView(). The name you use
530: $    here should equal the name that you use in the Vec object that you use with VecLoad().

532:    See the manual page for VecLoad() on the exact format the binary viewer stores
533:    the values in the file.

535:    Level: beginner

537:    Concepts: vector^printing
538:    Concepts: vector^saving to disk

540: .seealso: PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscDrawLGCreate(),
541:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), VecLoad(), PetscViewerCreate(),
542:           PetscRealView(), PetscScalarView(), PetscIntView()
543: @*/
544: PetscErrorCode  VecView(Vec vec,PetscViewer viewer)
545: {
546:   PetscErrorCode    ierr;
547:   PetscBool         iascii;
548:   PetscViewerFormat format;

553:   if (!viewer) {
554:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)vec),&viewer);
555:   }
558:   if (vec->stash.n || vec->bstash.n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call VecAssemblyBegin/End() before viewing this vector");

560:   PetscLogEventBegin(VEC_View,vec,viewer,0,0);
561:   PetscViewerGetFormat(viewer,&format);
562:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
563:   if (iascii) {
564:     PetscInt rows,bs;

566:     PetscObjectPrintClassNamePrefixType((PetscObject)vec,viewer);
567:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
568:       PetscViewerASCIIPushTab(viewer);
569:       VecGetSize(vec,&rows);
570:       VecGetBlockSize(vec,&bs);
571:       if (bs != 1) {
572:         PetscViewerASCIIPrintf(viewer,"length=%D, bs=%D\n",rows,bs);
573:       } else {
574:         PetscViewerASCIIPrintf(viewer,"length=%D\n",rows);
575:       }
576:       PetscViewerASCIIPopTab(viewer);
577:     }
578:   }
579:   VecLockPush(vec);
580:   if (format == PETSC_VIEWER_NATIVE && vec->ops->viewnative) {
581:     (*vec->ops->viewnative)(vec,viewer);
582:   } else {
583:     (*vec->ops->view)(vec,viewer);
584:   }
585:   VecLockPop(vec);
586:   PetscLogEventEnd(VEC_View,vec,viewer,0,0);
587:   return(0);
588: }

590: #if defined(PETSC_USE_DEBUG)
591: #include <../src/sys/totalview/tv_data_display.h>
592: PETSC_UNUSED static int TV_display_type(const struct _p_Vec *v)
593: {
594:   const PetscScalar *values;
595:   char              type[32];
596:   PetscErrorCode    ierr;


599:   TV_add_row("Local rows", "int", &v->map->n);
600:   TV_add_row("Global rows", "int", &v->map->N);
601:   TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)v)->type_name);
602:   VecGetArrayRead((Vec)v,&values);
603:   PetscSNPrintf(type,32,"double[%d]",v->map->n);
604:   TV_add_row("values",type, values);
605:   VecRestoreArrayRead((Vec)v,&values);
606:   return TV_format_OK;
607: }
608: #endif

610: /*@
611:    VecGetSize - Returns the global number of elements of the vector.

613:    Not Collective

615:    Input Parameter:
616: .  x - the vector

618:    Output Parameters:
619: .  size - the global length of the vector

621:    Level: beginner

623:    Concepts: vector^local size

625: .seealso: VecGetLocalSize()
626: @*/
627: PetscErrorCode  VecGetSize(Vec x,PetscInt *size)
628: {

635:   (*x->ops->getsize)(x,size);
636:   return(0);
637: }

639: /*@
640:    VecGetLocalSize - Returns the number of elements of the vector stored
641:    in local memory. This routine may be implementation dependent, so use
642:    with care.

644:    Not Collective

646:    Input Parameter:
647: .  x - the vector

649:    Output Parameter:
650: .  size - the length of the local piece of the vector

652:    Level: beginner

654:    Concepts: vector^size

656: .seealso: VecGetSize()
657: @*/
658: PetscErrorCode  VecGetLocalSize(Vec x,PetscInt *size)
659: {

666:   (*x->ops->getlocalsize)(x,size);
667:   return(0);
668: }

670: /*@C
671:    VecGetOwnershipRange - Returns the range of indices owned by
672:    this processor, assuming that the vectors are laid out with the
673:    first n1 elements on the first processor, next n2 elements on the
674:    second, etc.  For certain parallel layouts this range may not be
675:    well defined.

677:    Not Collective

679:    Input Parameter:
680: .  x - the vector

682:    Output Parameters:
683: +  low - the first local element, pass in NULL if not interested
684: -  high - one more than the last local element, pass in NULL if not interested

686:    Note:
687:    The high argument is one more than the last element stored locally.

689:    Fortran: NULL_INTEGER should be used instead of NULL

691:    Level: beginner

693:    Concepts: ownership^of vectors
694:    Concepts: vector^ownership of elements

696: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRanges()
697: @*/
698: PetscErrorCode  VecGetOwnershipRange(Vec x,PetscInt *low,PetscInt *high)
699: {
705:   if (low)  *low  = x->map->rstart;
706:   if (high) *high = x->map->rend;
707:   return(0);
708: }

710: /*@C
711:    VecGetOwnershipRanges - Returns the range of indices owned by EACH processor,
712:    assuming that the vectors are laid out with the
713:    first n1 elements on the first processor, next n2 elements on the
714:    second, etc.  For certain parallel layouts this range may not be
715:    well defined.

717:    Not Collective

719:    Input Parameter:
720: .  x - the vector

722:    Output Parameters:
723: .  range - array of length size+1 with the start and end+1 for each process

725:    Note:
726:    The high argument is one more than the last element stored locally.

728:    Fortran: You must PASS in an array of length size+1

730:    Level: beginner

732:    Concepts: ownership^of vectors
733:    Concepts: vector^ownership of elements

735: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRange()
736: @*/
737: PetscErrorCode  VecGetOwnershipRanges(Vec x,const PetscInt *ranges[])
738: {

744:   PetscLayoutGetRanges(x->map,ranges);
745:   return(0);
746: }

748: /*@
749:    VecSetOption - Sets an option for controling a vector's behavior.

751:    Collective on Vec

753:    Input Parameter:
754: +  x - the vector
755: .  op - the option
756: -  flag - turn the option on or off

758:    Supported Options:
759: +     VEC_IGNORE_OFF_PROC_ENTRIES, which causes VecSetValues() to ignore
760:           entries destined to be stored on a separate processor. This can be used
761:           to eliminate the global reduction in the VecAssemblyXXXX() if you know
762:           that you have only used VecSetValues() to set local elements
763: .     VEC_IGNORE_NEGATIVE_INDICES, which means you can pass negative indices
764:           in ix in calls to VecSetValues() or VecGetValues(). These rows are simply
765:           ignored.
766: -     VEC_SUBSET_OFF_PROC_ENTRIES, which causes VecAssemblyBegin() to assume that the off-process
767:           entries will always be a subset (possibly equal) of the off-process entries set on the
768:           first assembly.  This reuses the communication pattern, thus avoiding a global reduction.
769:           Subsequent assemblies setting off-process values should use the same InsertMode as the
770:           first assembly.

772:    Developer Note:
773:    The InsertMode restriction could be removed by packing the stash messages out of place.

775:    Level: intermediate

777: @*/
778: PetscErrorCode  VecSetOption(Vec x,VecOption op,PetscBool flag)
779: {

785:   if (x->ops->setoption) {
786:     (*x->ops->setoption)(x,op,flag);
787:   }
788:   return(0);
789: }

791: /* Default routines for obtaining and releasing; */
792: /* may be used by any implementation */
793: PetscErrorCode VecDuplicateVecs_Default(Vec w,PetscInt m,Vec *V[])
794: {
796:   PetscInt       i;

801:   if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
802:   PetscMalloc1(m,V);
803:   for (i=0; i<m; i++) {VecDuplicate(w,*V+i);}
804:   return(0);
805: }

807: PetscErrorCode VecDestroyVecs_Default(PetscInt m,Vec v[])
808: {
810:   PetscInt       i;

814:   for (i=0; i<m; i++) {VecDestroy(&v[i]);}
815:   PetscFree(v);
816:   return(0);
817: }

819: /*@
820:    VecResetArray - Resets a vector to use its default memory. Call this
821:    after the use of VecPlaceArray().

823:    Not Collective

825:    Input Parameters:
826: .  vec - the vector

828:    Level: developer

830: .seealso: VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecPlaceArray()

832: @*/
833: PetscErrorCode  VecResetArray(Vec vec)
834: {

840:   if (vec->ops->resetarray) {
841:     (*vec->ops->resetarray)(vec);
842:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot reset array in this type of vector");
843:   PetscObjectStateIncrease((PetscObject)vec);
844:   return(0);
845: }

847: /*@C
848:   VecLoad - Loads a vector that has been stored in binary or HDF5 format
849:   with VecView().

851:   Collective on PetscViewer

853:   Input Parameters:
854: + newvec - the newly loaded vector, this needs to have been created with VecCreate() or
855:            some related function before a call to VecLoad().
856: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
857:            HDF5 file viewer, obtained from PetscViewerHDF5Open()

859:    Level: intermediate

861:   Notes:
862:   Defaults to the standard Seq or MPI Vec, if you want some other type of Vec call VecSetFromOptions()
863:   before calling this.

865:   The input file must contain the full global vector, as
866:   written by the routine VecView().

868:   If the type or size of newvec is not set before a call to VecLoad, PETSc
869:   sets the type and the local and global sizes. If type and/or
870:   sizes are already set, then the same are used.

872:   If using binary and the blocksize of the vector is greater than one then you must provide a unique prefix to
873:   the vector with PetscObjectSetOptionsPrefix((PetscObject)vec,"uniqueprefix"); BEFORE calling VecView() on the
874:   vector to be stored and then set that same unique prefix on the vector that you pass to VecLoad(). The blocksize
875:   information is stored in an ASCII file with the same name as the binary file plus a ".info" appended to the
876:   filename. If you copy the binary file, make sure you copy the associated .info file with it.

878:   If using HDF5, you must assign the Vec the same name as was used in the Vec
879:   that was stored in the file using PetscObjectSetName(). Otherwise you will
880:   get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT"

882:   Notes for advanced users:
883:   Most users should not need to know the details of the binary storage
884:   format, since VecLoad() and VecView() completely hide these details.
885:   But for anyone who's interested, the standard binary matrix storage
886:   format is
887: .vb
888:      int    VEC_FILE_CLASSID
889:      int    number of rows
890:      PetscScalar *values of all entries
891: .ve

893:    In addition, PETSc automatically does the byte swapping for
894: machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
895: linux, Windows and the paragon; thus if you write your own binary
896: read/write routines you have to swap the bytes; see PetscBinaryRead()
897: and PetscBinaryWrite() to see how this may be done.

899:   Concepts: vector^loading from file

901: .seealso: PetscViewerBinaryOpen(), VecView(), MatLoad(), VecLoad()
902: @*/
903: PetscErrorCode  VecLoad(Vec newvec, PetscViewer viewer)
904: {
906:   PetscBool      isbinary,ishdf5;
907:   PetscViewerFormat format;

912:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
913:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
914:   if (!isbinary && !ishdf5) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

916:   PetscLogEventBegin(VEC_Load,viewer,0,0,0);
917:   if (!((PetscObject)newvec)->type_name && !newvec->ops->create) {
918:     VecSetType(newvec, VECSTANDARD);
919:   }
920:   PetscViewerGetFormat(viewer,&format);
921:   if (format == PETSC_VIEWER_NATIVE && newvec->ops->loadnative) {
922:     (*newvec->ops->loadnative)(newvec,viewer);
923:   } else {
924:     (*newvec->ops->load)(newvec,viewer);
925:   }
926:   PetscLogEventEnd(VEC_Load,viewer,0,0,0);
927:   return(0);
928: }


931: /*@
932:    VecReciprocal - Replaces each component of a vector by its reciprocal.

934:    Logically Collective on Vec

936:    Input Parameter:
937: .  vec - the vector

939:    Output Parameter:
940: .  vec - the vector reciprocal

942:    Level: intermediate

944:    Concepts: vector^reciprocal

946: .seealso: VecLog(), VecExp(), VecSqrtAbs()

948: @*/
949: PetscErrorCode  VecReciprocal(Vec vec)
950: {

956:   if (vec->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
957:   if (!vec->ops->reciprocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vector does not support reciprocal operation");
958:   (*vec->ops->reciprocal)(vec);
959:   PetscObjectStateIncrease((PetscObject)vec);
960:   return(0);
961: }

963: /*@C
964:     VecSetOperation - Allows user to set a vector operation.

966:    Logically Collective on Vec

968:     Input Parameters:
969: +   vec - the vector
970: .   op - the name of the operation
971: -   f - the function that provides the operation.

973:    Level: advanced

975:     Usage:
976: $      PetscErrorCode userview(Vec,PetscViewer);
977: $      VecCreateMPI(comm,m,M,&x);
978: $      VecSetOperation(x,VECOP_VIEW,(void(*)(void))userview);

980:     Notes:
981:     See the file include/petscvec.h for a complete list of matrix
982:     operations, which all have the form VECOP_<OPERATION>, where
983:     <OPERATION> is the name (in all capital letters) of the
984:     user interface routine (e.g., VecView() -> VECOP_VIEW).

986:     This function is not currently available from Fortran.

988: .keywords: vector, set, operation

990: .seealso: VecCreate(), MatShellSetOperation()
991: @*/
992: PetscErrorCode VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
993: {
996:   if (op == VECOP_VIEW && !vec->ops->viewnative) {
997:     vec->ops->viewnative = vec->ops->view;
998:   } else if (op == VECOP_LOAD && !vec->ops->loadnative) {
999:     vec->ops->loadnative = vec->ops->load;
1000:   }
1001:   (((void(**)(void))vec->ops)[(int)op]) = f;
1002:   return(0);
1003: }


1006: /*@
1007:    VecStashSetInitialSize - sets the sizes of the vec-stash, that is
1008:    used during the assembly process to store values that belong to
1009:    other processors.

1011:    Not Collective, different processes can have different size stashes

1013:    Input Parameters:
1014: +  vec   - the vector
1015: .  size  - the initial size of the stash.
1016: -  bsize - the initial size of the block-stash(if used).

1018:    Options Database Keys:
1019: +   -vecstash_initial_size <size> or <size0,size1,...sizep-1>
1020: -   -vecstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1>

1022:    Level: intermediate

1024:    Notes:
1025:      The block-stash is used for values set with VecSetValuesBlocked() while
1026:      the stash is used for values set with VecSetValues()

1028:      Run with the option -info and look for output of the form
1029:      VecAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
1030:      to determine the appropriate value, MM, to use for size and
1031:      VecAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
1032:      to determine the value, BMM to use for bsize

1034:    Concepts: vector^stash
1035:    Concepts: stash^vector

1037: .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked(), VecStashView()

1039: @*/
1040: PetscErrorCode  VecStashSetInitialSize(Vec vec,PetscInt size,PetscInt bsize)
1041: {

1046:   VecStashSetInitialSize_Private(&vec->stash,size);
1047:   VecStashSetInitialSize_Private(&vec->bstash,bsize);
1048:   return(0);
1049: }

1051: /*@
1052:    VecConjugate - Conjugates a vector.

1054:    Logically Collective on Vec

1056:    Input Parameters:
1057: .  x - the vector

1059:    Level: intermediate

1061:    Concepts: vector^conjugate

1063: @*/
1064: PetscErrorCode  VecConjugate(Vec x)
1065: {
1066: #if defined(PETSC_USE_COMPLEX)

1072:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1073:   (*x->ops->conjugate)(x);
1074:   /* we need to copy norms here */
1075:   PetscObjectStateIncrease((PetscObject)x);
1076:   return(0);
1077: #else
1078:   return(0);
1079: #endif
1080: }

1082: /*@
1083:    VecPointwiseMult - Computes the componentwise multiplication w = x*y.

1085:    Logically Collective on Vec

1087:    Input Parameters:
1088: .  x, y  - the vectors

1090:    Output Parameter:
1091: .  w - the result

1093:    Level: advanced

1095:    Notes: any subset of the x, y, and w may be the same vector.

1097:    Concepts: vector^pointwise multiply

1099: .seealso: VecPointwiseDivide(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
1100: @*/
1101: PetscErrorCode  VecPointwiseMult(Vec w, Vec x,Vec y)
1102: {

1114:   VecCheckSameSize(w,1,x,2);
1115:   VecCheckSameSize(w,2,y,3);
1116:   PetscLogEventBegin(VEC_PointwiseMult,x,y,w,0);
1117:   (*w->ops->pointwisemult)(w,x,y);
1118:   PetscLogEventEnd(VEC_PointwiseMult,x,y,w,0);
1119:   PetscObjectStateIncrease((PetscObject)w);
1120:   return(0);
1121: }

1123: /*@
1124:    VecSetRandom - Sets all components of a vector to random numbers.

1126:    Logically Collective on Vec

1128:    Input Parameters:
1129: +  x  - the vector
1130: -  rctx - the random number context, formed by PetscRandomCreate(), or NULL and
1131:           it will create one internally.

1133:    Output Parameter:
1134: .  x  - the vector

1136:    Example of Usage:
1137: .vb
1138:      PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
1139:      VecSetRandom(x,rctx);
1140:      PetscRandomDestroy(rctx);
1141: .ve

1143:    Level: intermediate

1145:    Concepts: vector^setting to random
1146:    Concepts: random^vector

1148: .seealso: VecSet(), VecSetValues(), PetscRandomCreate(), PetscRandomDestroy()
1149: @*/
1150: PetscErrorCode  VecSetRandom(Vec x,PetscRandom rctx)
1151: {
1153:   PetscRandom    randObj = NULL;

1159:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");

1161:   if (!rctx) {
1162:     MPI_Comm comm;
1163:     PetscObjectGetComm((PetscObject)x,&comm);
1164:     PetscRandomCreate(comm,&randObj);
1165:     PetscRandomSetFromOptions(randObj);
1166:     rctx = randObj;
1167:   }

1169:   PetscLogEventBegin(VEC_SetRandom,x,rctx,0,0);
1170:   (*x->ops->setrandom)(x,rctx);
1171:   PetscLogEventEnd(VEC_SetRandom,x,rctx,0,0);

1173:   PetscRandomDestroy(&randObj);
1174:   PetscObjectStateIncrease((PetscObject)x);
1175:   return(0);
1176: }

1178: /*@
1179:   VecZeroEntries - puts a 0.0 in each element of a vector

1181:   Logically Collective on Vec

1183:   Input Parameter:
1184: . vec - The vector

1186:   Level: beginner

1188:   Developer Note: This routine does not need to exist since the exact functionality is obtained with
1189:      VecSet(vec,0);  I guess someone added it to mirror the functionality of MatZeroEntries() but Mat is nothing
1190:      like a Vec (one is an operator and one is an element of a vector space, yeah yeah dual blah blah blah) so
1191:      this routine should not exist.

1193: .keywords: Vec, set, options, database
1194: .seealso: VecCreate(),  VecSetOptionsPrefix(), VecSet(), VecSetValues()
1195: @*/
1196: PetscErrorCode  VecZeroEntries(Vec vec)
1197: {

1201:   VecSet(vec,0);
1202:   return(0);
1203: }

1205: /*
1206:   VecSetTypeFromOptions_Private - Sets the type of vector from user options. Defaults to a PETSc sequential vector on one
1207:   processor and a PETSc MPI vector on more than one processor.

1209:   Collective on Vec

1211:   Input Parameter:
1212: . vec - The vector

1214:   Level: intermediate

1216: .keywords: Vec, set, options, database, type
1217: .seealso: VecSetFromOptions(), VecSetType()
1218: */
1219: static PetscErrorCode VecSetTypeFromOptions_Private(PetscOptionItems *PetscOptionsObject,Vec vec)
1220: {
1221:   PetscBool      opt;
1222:   VecType        defaultType;
1223:   char           typeName[256];
1224:   PetscMPIInt    size;

1228:   if (((PetscObject)vec)->type_name) defaultType = ((PetscObject)vec)->type_name;
1229:   else {
1230:     MPI_Comm_size(PetscObjectComm((PetscObject)vec), &size);
1231:     if (size > 1) defaultType = VECMPI;
1232:     else defaultType = VECSEQ;
1233:   }

1235:   VecRegisterAll();
1236:   PetscOptionsFList("-vec_type","Vector type","VecSetType",VecList,defaultType,typeName,256,&opt);
1237:   if (opt) {
1238:     VecSetType(vec, typeName);
1239:   } else {
1240:     VecSetType(vec, defaultType);
1241:   }
1242:   return(0);
1243: }

1245: /*@
1246:   VecSetFromOptions - Configures the vector from the options database.

1248:   Collective on Vec

1250:   Input Parameter:
1251: . vec - The vector

1253:   Notes:  To see all options, run your program with the -help option, or consult the users manual.
1254:           Must be called after VecCreate() but before the vector is used.

1256:   Level: beginner

1258:   Concepts: vectors^setting options
1259:   Concepts: vectors^setting type

1261: .keywords: Vec, set, options, database
1262: .seealso: VecCreate(), VecSetOptionsPrefix()
1263: @*/
1264: PetscErrorCode  VecSetFromOptions(Vec vec)
1265: {


1271:   PetscObjectOptionsBegin((PetscObject)vec);
1272:   /* Handle vector type options */
1273:   VecSetTypeFromOptions_Private(PetscOptionsObject,vec);

1275:   /* Handle specific vector options */
1276:   if (vec->ops->setfromoptions) {
1277:     (*vec->ops->setfromoptions)(PetscOptionsObject,vec);
1278:   }

1280:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
1281:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)vec);
1282:   PetscOptionsEnd();
1283:   return(0);
1284: }

1286: /*@
1287:   VecSetSizes - Sets the local and global sizes, and checks to determine compatibility

1289:   Collective on Vec

1291:   Input Parameters:
1292: + v - the vector
1293: . n - the local size (or PETSC_DECIDE to have it set)
1294: - N - the global size (or PETSC_DECIDE)

1296:   Notes:
1297:   n and N cannot be both PETSC_DECIDE
1298:   If one processor calls this with N of PETSC_DECIDE then all processors must, otherwise the program will hang.

1300:   Level: intermediate

1302: .seealso: VecGetSize(), PetscSplitOwnership()
1303: @*/
1304: PetscErrorCode  VecSetSizes(Vec v, PetscInt n, PetscInt N)
1305: {

1311:   if (N >= 0 && n > N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local size %D cannot be larger than global size %D",n,N);
1312:   if ((v->map->n >= 0 || v->map->N >= 0) && (v->map->n != n || v->map->N != N)) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset vector sizes to %D local %D global after previously setting them to %D local %D global",n,N,v->map->n,v->map->N);
1313:   v->map->n = n;
1314:   v->map->N = N;
1315:   if (v->ops->create) {
1316:     (*v->ops->create)(v);
1317:     v->ops->create = 0;
1318:   }
1319:   return(0);
1320: }

1322: /*@
1323:    VecSetBlockSize - Sets the blocksize for future calls to VecSetValuesBlocked()
1324:    and VecSetValuesBlockedLocal().

1326:    Logically Collective on Vec

1328:    Input Parameter:
1329: +  v - the vector
1330: -  bs - the blocksize

1332:    Notes:
1333:    All vectors obtained by VecDuplicate() inherit the same blocksize.

1335:    Level: advanced

1337: .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecGetBlockSize()

1339:   Concepts: block size^vectors
1340: @*/
1341: PetscErrorCode  VecSetBlockSize(Vec v,PetscInt bs)
1342: {

1347:   if (bs < 0 || bs == v->map->bs) return(0);
1349:   PetscLayoutSetBlockSize(v->map,bs);
1350:   v->bstash.bs = bs; /* use the same blocksize for the vec's block-stash */
1351:   return(0);
1352: }

1354: /*@
1355:    VecGetBlockSize - Gets the blocksize for the vector, i.e. what is used for VecSetValuesBlocked()
1356:    and VecSetValuesBlockedLocal().

1358:    Not Collective

1360:    Input Parameter:
1361: .  v - the vector

1363:    Output Parameter:
1364: .  bs - the blocksize

1366:    Notes:
1367:    All vectors obtained by VecDuplicate() inherit the same blocksize.

1369:    Level: advanced

1371: .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecSetBlockSize()

1373:    Concepts: vector^block size
1374:    Concepts: block^vector

1376: @*/
1377: PetscErrorCode  VecGetBlockSize(Vec v,PetscInt *bs)
1378: {

1384:   PetscLayoutGetBlockSize(v->map,bs);
1385:   return(0);
1386: }

1388: /*@C
1389:    VecSetOptionsPrefix - Sets the prefix used for searching for all
1390:    Vec options in the database.

1392:    Logically Collective on Vec

1394:    Input Parameter:
1395: +  v - the Vec context
1396: -  prefix - the prefix to prepend to all option names

1398:    Notes:
1399:    A hyphen (-) must NOT be given at the beginning of the prefix name.
1400:    The first character of all runtime options is AUTOMATICALLY the hyphen.

1402:    Level: advanced

1404: .keywords: Vec, set, options, prefix, database

1406: .seealso: VecSetFromOptions()
1407: @*/
1408: PetscErrorCode  VecSetOptionsPrefix(Vec v,const char prefix[])
1409: {

1414:   PetscObjectSetOptionsPrefix((PetscObject)v,prefix);
1415:   return(0);
1416: }

1418: /*@C
1419:    VecAppendOptionsPrefix - Appends to the prefix used for searching for all
1420:    Vec options in the database.

1422:    Logically Collective on Vec

1424:    Input Parameters:
1425: +  v - the Vec context
1426: -  prefix - the prefix to prepend to all option names

1428:    Notes:
1429:    A hyphen (-) must NOT be given at the beginning of the prefix name.
1430:    The first character of all runtime options is AUTOMATICALLY the hyphen.

1432:    Level: advanced

1434: .keywords: Vec, append, options, prefix, database

1436: .seealso: VecGetOptionsPrefix()
1437: @*/
1438: PetscErrorCode  VecAppendOptionsPrefix(Vec v,const char prefix[])
1439: {

1444:   PetscObjectAppendOptionsPrefix((PetscObject)v,prefix);
1445:   return(0);
1446: }

1448: /*@C
1449:    VecGetOptionsPrefix - Sets the prefix used for searching for all
1450:    Vec options in the database.

1452:    Not Collective

1454:    Input Parameter:
1455: .  v - the Vec context

1457:    Output Parameter:
1458: .  prefix - pointer to the prefix string used

1460:    Notes: On the fortran side, the user should pass in a string 'prefix' of
1461:    sufficient length to hold the prefix.

1463:    Level: advanced

1465: .keywords: Vec, get, options, prefix, database

1467: .seealso: VecAppendOptionsPrefix()
1468: @*/
1469: PetscErrorCode  VecGetOptionsPrefix(Vec v,const char *prefix[])
1470: {

1475:   PetscObjectGetOptionsPrefix((PetscObject)v,prefix);
1476:   return(0);
1477: }

1479: /*@
1480:    VecSetUp - Sets up the internal vector data structures for the later use.

1482:    Collective on Vec

1484:    Input Parameters:
1485: .  v - the Vec context

1487:    Notes:
1488:    For basic use of the Vec classes the user need not explicitly call
1489:    VecSetUp(), since these actions will happen automatically.

1491:    Level: advanced

1493: .keywords: Vec, setup

1495: .seealso: VecCreate(), VecDestroy()
1496: @*/
1497: PetscErrorCode  VecSetUp(Vec v)
1498: {
1499:   PetscMPIInt    size;

1504:   if (!((PetscObject)v)->type_name) {
1505:     MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);
1506:     if (size == 1) {
1507:       VecSetType(v, VECSEQ);
1508:     } else {
1509:       VecSetType(v, VECMPI);
1510:     }
1511:   }
1512:   return(0);
1513: }

1515: /*
1516:     These currently expose the PetscScalar/PetscReal in updating the
1517:     cached norm. If we push those down into the implementation these
1518:     will become independent of PetscScalar/PetscReal
1519: */

1521: /*@
1522:    VecCopy - Copies a vector. y <- x

1524:    Logically Collective on Vec

1526:    Input Parameter:
1527: .  x - the vector

1529:    Output Parameter:
1530: .  y - the copy

1532:    Notes:
1533:    For default parallel PETSc vectors, both x and y must be distributed in
1534:    the same manner; local copies are done.

1536:    Developer Notes:
1538:    of the vectors to be sequential and one to be parallel so long as both have the same
1539:    local sizes. This is used in some internal functions in PETSc.

1541:    Level: beginner

1543: .seealso: VecDuplicate()
1544: @*/
1545: PetscErrorCode  VecCopy(Vec x,Vec y)
1546: {
1547:   PetscBool      flgs[4];
1548:   PetscReal      norms[4] = {0.0,0.0,0.0,0.0};
1550:   PetscInt       i;

1557:   if (x == y) return(0);
1558:   VecCheckSameLocalSize(x,1,y,2);
1559:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1560:   VecLocked(y,2);

1562: #if !defined(PETSC_USE_MIXED_PRECISION)
1563:   for (i=0; i<4; i++) {
1564:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],norms[i],flgs[i]);
1565:   }
1566: #endif

1568:   PetscLogEventBegin(VEC_Copy,x,y,0,0);
1569: #if defined(PETSC_USE_MIXED_PRECISION)
1570:   extern PetscErrorCode VecGetArray(Vec,double**);
1571:   extern PetscErrorCode VecRestoreArray(Vec,double**);
1572:   extern PetscErrorCode VecGetArray(Vec,float**);
1573:   extern PetscErrorCode VecRestoreArray(Vec,float**);
1574:   extern PetscErrorCode VecGetArrayRead(Vec,const double**);
1575:   extern PetscErrorCode VecRestoreArrayRead(Vec,const double**);
1576:   extern PetscErrorCode VecGetArrayRead(Vec,const float**);
1577:   extern PetscErrorCode VecRestoreArrayRead(Vec,const float**);
1578:   if ((((PetscObject)x)->precision == PETSC_PRECISION_SINGLE) && (((PetscObject)y)->precision == PETSC_PRECISION_DOUBLE)) {
1579:     PetscInt    i,n;
1580:     const float *xx;
1581:     double      *yy;
1582:     VecGetArrayRead(x,&xx);
1583:     VecGetArray(y,&yy);
1584:     VecGetLocalSize(x,&n);
1585:     for (i=0; i<n; i++) yy[i] = xx[i];
1586:     VecRestoreArrayRead(x,&xx);
1587:     VecRestoreArray(y,&yy);
1588:   } else if ((((PetscObject)x)->precision == PETSC_PRECISION_DOUBLE) && (((PetscObject)y)->precision == PETSC_PRECISION_SINGLE)) {
1589:     PetscInt     i,n;
1590:     float        *yy;
1591:     const double *xx;
1592:     VecGetArrayRead(x,&xx);
1593:     VecGetArray(y,&yy);
1594:     VecGetLocalSize(x,&n);
1595:     for (i=0; i<n; i++) yy[i] = (float) xx[i];
1596:     VecRestoreArrayRead(x,&xx);
1597:     VecRestoreArray(y,&yy);
1598:   } else {
1599:     (*x->ops->copy)(x,y);
1600:   }
1601: #else
1602:   (*x->ops->copy)(x,y);
1603: #endif

1605:   PetscObjectStateIncrease((PetscObject)y);
1606: #if !defined(PETSC_USE_MIXED_PRECISION)
1607:   for (i=0; i<4; i++) {
1608:     if (flgs[i]) {
1609:       PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],norms[i]);
1610:     }
1611:   }
1612: #endif

1614:   PetscLogEventEnd(VEC_Copy,x,y,0,0);
1615:   return(0);
1616: }

1618: /*@
1619:    VecSwap - Swaps the vectors x and y.

1621:    Logically Collective on Vec

1623:    Input Parameters:
1624: .  x, y  - the vectors

1626:    Level: advanced

1628:    Concepts: vector^swapping values

1630: @*/
1631: PetscErrorCode  VecSwap(Vec x,Vec y)
1632: {
1633:   PetscReal      normxs[4]={0.0,0.0,0.0,0.0},normys[4]={0.0,0.0,0.0,0.0};
1634:   PetscBool      flgxs[4],flgys[4];
1636:   PetscInt       i;

1644:   VecCheckSameSize(x,1,y,2);
1645:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1646:   if (y->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");

1648:   PetscLogEventBegin(VEC_Swap,x,y,0,0);
1649:   for (i=0; i<4; i++) {
1650:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],normxs[i],flgxs[i]);
1651:     PetscObjectComposedDataGetReal((PetscObject)y,NormIds[i],normys[i],flgys[i]);
1652:   }
1653:   (*x->ops->swap)(x,y);
1654:   PetscObjectStateIncrease((PetscObject)x);
1655:   PetscObjectStateIncrease((PetscObject)y);
1656:   for (i=0; i<4; i++) {
1657:     if (flgxs[i]) {
1658:       PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],normxs[i]);
1659:     }
1660:     if (flgys[i]) {
1661:       PetscObjectComposedDataSetReal((PetscObject)x,NormIds[i],normys[i]);
1662:     }
1663:   }
1664:   PetscLogEventEnd(VEC_Swap,x,y,0,0);
1665:   return(0);
1666: }

1668: /*
1669:   VecStashViewFromOptions - Processes command line options to determine if/how an VecStash object is to be viewed. 

1671:   Collective on VecStash

1673:   Input Parameters:
1674: + obj   - the VecStash object
1675: . bobj - optional other object that provides the prefix
1676: - optionname - option to activate viewing

1678:   Level: intermediate

1680:   Developer Note: This cannot use PetscObjectViewFromOptions() because it takes a Vec as an argument but does not use VecView

1682: */
1683: PetscErrorCode VecStashViewFromOptions(Vec obj,PetscObject bobj,const char optionname[])
1684: {
1685:   PetscErrorCode    ierr;
1686:   PetscViewer       viewer;
1687:   PetscBool         flg;
1688:   PetscViewerFormat format;
1689:   char              *prefix;

1692:   prefix = bobj ? bobj->prefix : ((PetscObject)obj)->prefix;
1693:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),prefix,optionname,&viewer,&format,&flg);
1694:   if (flg) {
1695:     PetscViewerPushFormat(viewer,format);
1696:     VecStashView(obj,viewer);
1697:     PetscViewerPopFormat(viewer);
1698:     PetscViewerDestroy(&viewer);
1699:   }
1700:   return(0);
1701: }

1703: /*@
1704:    VecStashView - Prints the entries in the vector stash and block stash.

1706:    Collective on Vec

1708:    Input Parameters:
1709: +  v - the vector
1710: -  viewer - the viewer

1712:    Level: advanced

1714:    Concepts: vector^stash
1715:    Concepts: stash^vector

1717: .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked()

1719: @*/
1720: PetscErrorCode  VecStashView(Vec v,PetscViewer viewer)
1721: {
1723:   PetscMPIInt    rank;
1724:   PetscInt       i,j;
1725:   PetscBool      match;
1726:   VecStash       *s;
1727:   PetscScalar    val;


1734:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&match);
1735:   if (!match) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Stash viewer only works with ASCII viewer not %s\n",((PetscObject)v)->type_name);
1736:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1737:   MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
1738:   s    = &v->bstash;

1740:   /* print block stash */
1741:   PetscViewerASCIIPushSynchronized(viewer);
1742:   PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector Block stash size %D block size %D\n",rank,s->n,s->bs);
1743:   for (i=0; i<s->n; i++) {
1744:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D ",rank,s->idx[i]);
1745:     for (j=0; j<s->bs; j++) {
1746:       val = s->array[i*s->bs+j];
1747: #if defined(PETSC_USE_COMPLEX)
1748:       PetscViewerASCIISynchronizedPrintf(viewer,"(%18.16e %18.16e) ",PetscRealPart(val),PetscImaginaryPart(val));
1749: #else
1750:       PetscViewerASCIISynchronizedPrintf(viewer,"%18.16e ",val);
1751: #endif
1752:     }
1753:     PetscViewerASCIISynchronizedPrintf(viewer,"\n");
1754:   }
1755:   PetscViewerFlush(viewer);

1757:   s = &v->stash;

1759:   /* print basic stash */
1760:   PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector stash size %D\n",rank,s->n);
1761:   for (i=0; i<s->n; i++) {
1762:     val = s->array[i];
1763: #if defined(PETSC_USE_COMPLEX)
1764:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D (%18.16e %18.16e) ",rank,s->idx[i],PetscRealPart(val),PetscImaginaryPart(val));
1765: #else
1766:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D %18.16e\n",rank,s->idx[i],val);
1767: #endif
1768:   }
1769:   PetscViewerFlush(viewer);
1770:   PetscViewerASCIIPopSynchronized(viewer);
1771:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1772:   return(0);
1773: }

1775: PetscErrorCode PetscOptionsGetVec(PetscOptions options,const char prefix[],const char key[],Vec v,PetscBool *set)
1776: {
1777:   PetscInt       i,N,rstart,rend;
1779:   PetscScalar    *xx;
1780:   PetscReal      *xreal;
1781:   PetscBool      iset;

1784:   VecGetOwnershipRange(v,&rstart,&rend);
1785:   VecGetSize(v,&N);
1786:   PetscCalloc1(N,&xreal);
1787:   PetscOptionsGetRealArray(options,prefix,key,xreal,&N,&iset);
1788:   if (iset) {
1789:     VecGetArray(v,&xx);
1790:     for (i=rstart; i<rend; i++) xx[i-rstart] = xreal[i];
1791:     VecRestoreArray(v,&xx);
1792:   }
1793:   PetscFree(xreal);
1794:   if (set) *set = iset;
1795:   return(0);
1796: }

1798: /*@
1799:    VecGetLayout - get PetscLayout describing vector layout

1801:    Not Collective

1803:    Input Arguments:
1804: .  x - the vector

1806:    Output Arguments:
1807: .  map - the layout

1809:    Level: developer

1811: .seealso: VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1812: @*/
1813: PetscErrorCode VecGetLayout(Vec x,PetscLayout *map)
1814: {

1818:   *map = x->map;
1819:   return(0);
1820: }

1822: /*@
1823:    VecSetLayout - set PetscLayout describing vector layout

1825:    Not Collective

1827:    Input Arguments:
1828: +  x - the vector
1829: -  map - the layout

1831:    Notes:
1832:    It is normally only valid to replace the layout with a layout known to be equivalent.

1834:    Level: developer

1836: .seealso: VecGetLayout(), VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1837: @*/
1838: PetscErrorCode VecSetLayout(Vec x,PetscLayout map)
1839: {

1844:   PetscLayoutReference(map,&x->map);
1845:   return(0);
1846: }

1848: PetscErrorCode VecSetInf(Vec xin)
1849: {
1850:   PetscInt       i,n = xin->map->n;
1851:   PetscScalar    *xx;
1852:   PetscScalar    zero=0.0,one=1.0,inf=one/zero;

1856:   VecGetArray(xin,&xx);
1857:   for (i=0; i<n; i++) xx[i] = inf;
1858:   VecRestoreArray(xin,&xx);
1859:   return(0);
1860: }