Actual source code: vector.c

petsc-master 2014-12-20
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>    /*I  "petscvec.h"   I*/

  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;

 19: extern PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
 22: /*@
 23:    VecStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
 24:        to be communicated to other processors during the VecAssemblyBegin/End() process

 26:     Not collective

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

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

 37:    Level: advanced

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

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

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

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

 59:    Logically Collective on Vec

 61:    Input Parameters:
 62: +  x - vector
 63: -  mapping - mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS()

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

 68:    Level: intermediate

 70:    Concepts: vector^setting values with local numbering

 72: seealso:  VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetValuesLocal(),
 73:            VecSetLocalToGlobalMapping(), VecSetValuesBlockedLocal()
 74: @*/
 75: PetscErrorCode  VecSetLocalToGlobalMapping(Vec x,ISLocalToGlobalMapping mapping)
 76: {


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

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

 96:    Not Collective

 98:    Input Parameter:
 99: .  X - the vector

101:    Output Parameter:
102: .  mapping - the mapping

104:    Level: advanced

106:    Concepts: vectors^local to global mapping
107:    Concepts: local to global mapping^for vectors

109: .seealso:  VecSetValuesLocal()
110: @*/
111: PetscErrorCode VecGetLocalToGlobalMapping(Vec X,ISLocalToGlobalMapping *mapping)
112: {
117:   *mapping = X->map->mapping;
118:   return(0);
119: }

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

127:    Collective on Vec

129:    Input Parameter:
130: .  vec - the vector

132:    Level: beginner

134:    Concepts: assembly^vectors

136: .seealso: VecAssemblyEnd(), VecSetValues()
137: @*/
138: PetscErrorCode  VecAssemblyBegin(Vec vec)
139: {

145:   VecStashViewFromOptions(vec,NULL,"-vec_view_stash");
146:   PetscLogEventBegin(VEC_AssemblyBegin,vec,0,0,0);
147:   if (vec->ops->assemblybegin) {
148:     (*vec->ops->assemblybegin)(vec);
149:   }
150:   PetscLogEventEnd(VEC_AssemblyBegin,vec,0,0,0);
151:   PetscObjectStateIncrease((PetscObject)vec);
152:   return(0);
153: }

157: /*@
158:    VecAssemblyEnd - Completes assembling the vector.  This routine should
159:    be called after VecAssemblyBegin().

161:    Collective on Vec

163:    Input Parameter:
164: .  vec - the vector

166:    Options Database Keys:
167: +  -vec_view - Prints vector in ASCII format
168: .  -vec_view ::ascii_matlab - Prints vector in ASCII MATLAB format to stdout
169: .  -vec_view matlab:filename - Prints vector in MATLAB format to matlaboutput.mat
170: .  -vec_view draw - Activates vector viewing using drawing tools
171: .  -display <name> - Sets display name (default is host)
172: .  -draw_pause <sec> - Sets number of seconds to pause after display
173: -  -vec_view socket - Activates vector viewing using a socket

175:    Level: beginner

177: .seealso: VecAssemblyBegin(), VecSetValues()
178: @*/
179: PetscErrorCode  VecAssemblyEnd(Vec vec)
180: {

185:   PetscLogEventBegin(VEC_AssemblyEnd,vec,0,0,0);
187:   if (vec->ops->assemblyend) {
188:     (*vec->ops->assemblyend)(vec);
189:   }
190:   PetscLogEventEnd(VEC_AssemblyEnd,vec,0,0,0);
191:   VecViewFromOptions(vec,NULL,"-vec_view");
192:   return(0);
193: }

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

200:    Logically Collective on Vec

202:    Input Parameters:
203: .  x, y  - the vectors

205:    Output Parameter:
206: .  w - the result

208:    Level: advanced

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

213:    Concepts: vector^pointwise multiply

215: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
216: @*/
217: PetscErrorCode  VecPointwiseMax(Vec w,Vec x,Vec y)
218: {

230:   if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
231:   if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

233:   (*w->ops->pointwisemax)(w,x,y);
234:   PetscObjectStateIncrease((PetscObject)w);
235:   return(0);
236: }


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

244:    Logically Collective on Vec

246:    Input Parameters:
247: .  x, y  - the vectors

249:    Output Parameter:
250: .  w - the result

252:    Level: advanced

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

257:    Concepts: vector^pointwise multiply

259: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
260: @*/
261: PetscErrorCode  VecPointwiseMin(Vec w,Vec x,Vec y)
262: {

274:   if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
275:   if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

277:   (*w->ops->pointwisemin)(w,x,y);
278:   PetscObjectStateIncrease((PetscObject)w);
279:   return(0);
280: }

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

287:    Logically Collective on Vec

289:    Input Parameters:
290: .  x, y  - the vectors

292:    Output Parameter:
293: .  w - the result

295:    Level: advanced

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

299:    Concepts: vector^pointwise multiply

301: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMax(), VecMaxPointwiseDivide()
302: @*/
303: PetscErrorCode  VecPointwiseMaxAbs(Vec w,Vec x,Vec y)
304: {

316:   if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
317:   if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

319:   (*w->ops->pointwisemaxabs)(w,x,y);
320:   PetscObjectStateIncrease((PetscObject)w);
321:   return(0);
322: }

326: /*@
327:    VecPointwiseDivide - Computes the componentwise division w = x/y.

329:    Logically Collective on Vec

331:    Input Parameters:
332: .  x, y  - the vectors

334:    Output Parameter:
335: .  w - the result

337:    Level: advanced

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

341:    Concepts: vector^pointwise divide

343: .seealso: VecPointwiseMult(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
344: @*/
345: PetscErrorCode  VecPointwiseDivide(Vec w,Vec x,Vec y)
346: {

358:   if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
359:   if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

361:   (*w->ops->pointwisedivide)(w,x,y);
362:   PetscObjectStateIncrease((PetscObject)w);
363:   return(0);
364: }


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

372:    Collective on Vec

374:    Input Parameters:
375: .  v - a vector to mimic

377:    Output Parameter:
378: .  newv - location to put new vector

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

384:    Use VecDestroy() to free the space. Use VecDuplicateVecs() to get several
385:    vectors.

387:    Level: beginner

389: .seealso: VecDestroy(), VecDuplicateVecs(), VecCreate(), VecCopy()
390: @*/
391: PetscErrorCode  VecDuplicate(Vec v,Vec *newv)
392: {

399:   (*v->ops->duplicate)(v,newv);
400:   PetscObjectStateIncrease((PetscObject)*newv);
401:   return(0);
402: }

406: /*@
407:    VecDestroy - Destroys a vector.

409:    Collective on Vec

411:    Input Parameters:
412: .  v  - the vector

414:    Level: beginner

416: .seealso: VecDuplicate(), VecDestroyVecs()
417: @*/
418: PetscErrorCode  VecDestroy(Vec *v)
419: {

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

427:   PetscObjectSAWsViewOff((PetscObject)*v);
428:   /* destroy the internal part */
429:   if ((*v)->ops->destroy) {
430:     (*(*v)->ops->destroy)(*v);
431:   }
432:   /* destroy the external/common part */
433:   PetscLayoutDestroy(&(*v)->map);
434:   PetscHeaderDestroy(v);
435:   return(0);
436: }

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

443:    Collective on Vec

445:    Input Parameters:
446: +  m - the number of vectors to obtain
447: -  v - a vector to mimic

449:    Output Parameter:
450: .  V - location to put pointer to array of vectors

452:    Notes:
453:    Use VecDestroyVecs() to free the space. Use VecDuplicate() to form a single
454:    vector.

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

461:    Level: intermediate

463: .seealso:  VecDestroyVecs(), VecDuplicate(), VecCreate(), VecDuplicateVecsF90()
464: @*/
465: PetscErrorCode  VecDuplicateVecs(Vec v,PetscInt m,Vec *V[])
466: {

473:   (*v->ops->duplicatevecs)(v, m,V);
474:   return(0);
475: }

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

482:    Collective on Vec

484:    Input Parameters:
485: +  vv - pointer to pointer to array of vector pointers
486: -  m - the number of vectors previously obtained

488:    Fortran Note:
489:    The Fortran interface is slightly different from that given below.
490:    See the Fortran chapter of the users manual

492:    Level: intermediate

494: .seealso: VecDuplicateVecs(), VecDestroyVecsf90()
495: @*/
496: PetscErrorCode  VecDestroyVecs(PetscInt m,Vec *vv[])
497: {

502:   if (!*vv) return(0);
505:   if (m < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of vectors %D",m);
506:   (*(**vv)->ops->destroyvecs)(m,*vv);
507:   *vv  = 0;
508:   return(0);
509: }

513: /*@C
514:    VecView - Views a vector object.

516:    Collective on Vec

518:    Input Parameters:
519: +  vec - the vector
520: -  viewer - an optional visualization context

522:    Notes:
523:    The available visualization contexts include
524: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
525: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
526:          output where only the first processor opens
527:          the file.  All other processors send their
528:          data to the first processor to print.

530:    You can change the format the vector is printed using the
531:    option PetscViewerSetFormat().

533:    The user can open alternative visualization contexts with
534: +    PetscViewerASCIIOpen() - Outputs vector to a specified file
535: .    PetscViewerBinaryOpen() - Outputs vector in binary to a
536:          specified file; corresponding input uses VecLoad()
537: .    PetscViewerDrawOpen() - Outputs vector to an X window display
538: -    PetscViewerSocketOpen() - Outputs vector to Socket viewer

540:    The user can call PetscViewerSetFormat() to specify the output
541:    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
542:    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
543: +    PETSC_VIEWER_DEFAULT - default, prints vector contents
544: .    PETSC_VIEWER_ASCII_MATLAB - prints vector contents in MATLAB format
545: .    PETSC_VIEWER_ASCII_INDEX - prints vector contents, including indices of vector elements
546: -    PETSC_VIEWER_ASCII_COMMON - prints vector contents, using a
547:          format common among all vector types

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

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

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

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

568:    Level: beginner

570:    Concepts: vector^printing
571:    Concepts: vector^saving to disk

573: .seealso: PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscDrawLGCreate(),
574:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), VecLoad(), PetscViewerCreate(),
575:           PetscRealView(), PetscScalarView(), PetscIntView()
576: @*/
577: PetscErrorCode  VecView(Vec vec,PetscViewer viewer)
578: {
579:   PetscErrorCode    ierr;
580:   PetscBool         iascii;
581:   PetscViewerFormat format;

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

593:   PetscLogEventBegin(VEC_View,vec,viewer,0,0);
594:   PetscViewerGetFormat(viewer,&format);
595:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
596:   if (iascii) {
597:     PetscInt rows,bs;

599:     PetscObjectPrintClassNamePrefixType((PetscObject)vec,viewer);
600:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
601:       PetscViewerASCIIPushTab(viewer);
602:       VecGetSize(vec,&rows);
603:       VecGetBlockSize(vec,&bs);
604:       if (bs != 1) {
605:         PetscViewerASCIIPrintf(viewer,"length=%D, bs=%D\n",rows,bs);
606:       } else {
607:         PetscViewerASCIIPrintf(viewer,"length=%D\n",rows);
608:       }
609:       PetscViewerASCIIPopTab(viewer);
610:     }
611:   }
612:   if (format == PETSC_VIEWER_NATIVE && vec->ops->viewnative) {
613:     (*vec->ops->viewnative)(vec,viewer);
614:   } else {
615:     (*vec->ops->view)(vec,viewer);
616:   }
617:   PetscLogEventEnd(VEC_View,vec,viewer,0,0);
618:   return(0);
619: }

621: #if defined(PETSC_USE_DEBUG)
622: #include <../src/sys/totalview/tv_data_display.h>
623: PETSC_UNUSED static int TV_display_type(const struct _p_Vec *v)
624: {
625:   const PetscScalar *values;
626:   char              type[32];
627:   PetscErrorCode    ierr;


630:   TV_add_row("Local rows", "int", &v->map->n);
631:   TV_add_row("Global rows", "int", &v->map->N);
632:   TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)v)->type_name);
633:   VecGetArrayRead((Vec)v,&values);
634:   PetscSNPrintf(type,32,"double[%d]",v->map->n);
635:   TV_add_row("values",type, values);
636:   VecRestoreArrayRead((Vec)v,&values);
637:   return TV_format_OK;
638: }
639: #endif

643: /*@
644:    VecGetSize - Returns the global number of elements of the vector.

646:    Not Collective

648:    Input Parameter:
649: .  x - the vector

651:    Output Parameters:
652: .  size - the global length of the vector

654:    Level: beginner

656:    Concepts: vector^local size

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

668:   (*x->ops->getsize)(x,size);
669:   return(0);
670: }

674: /*@
675:    VecGetLocalSize - Returns the number of elements of the vector stored
676:    in local memory. This routine may be implementation dependent, so use
677:    with care.

679:    Not Collective

681:    Input Parameter:
682: .  x - the vector

684:    Output Parameter:
685: .  size - the length of the local piece of the vector

687:    Level: beginner

689:    Concepts: vector^size

691: .seealso: VecGetSize()
692: @*/
693: PetscErrorCode  VecGetLocalSize(Vec x,PetscInt *size)
694: {

701:   (*x->ops->getlocalsize)(x,size);
702:   return(0);
703: }

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

714:    Not Collective

716:    Input Parameter:
717: .  x - the vector

719:    Output Parameters:
720: +  low - the first local element, pass in NULL if not interested
721: -  high - one more than the last local element, pass in NULL if not interested

723:    Note:
724:    The high argument is one more than the last element stored locally.

726:    Fortran: NULL_INTEGER should be used instead of NULL

728:    Level: beginner

730:    Concepts: ownership^of vectors
731:    Concepts: vector^ownership of elements

733: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRanges()
734: @*/
735: PetscErrorCode  VecGetOwnershipRange(Vec x,PetscInt *low,PetscInt *high)
736: {
742:   if (low)  *low  = x->map->rstart;
743:   if (high) *high = x->map->rend;
744:   return(0);
745: }

749: /*@C
750:    VecGetOwnershipRanges - Returns the range of indices owned by EACH processor,
751:    assuming that the vectors are laid out with the
752:    first n1 elements on the first processor, next n2 elements on the
753:    second, etc.  For certain parallel layouts this range may not be
754:    well defined.

756:    Not Collective

758:    Input Parameter:
759: .  x - the vector

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

764:    Note:
765:    The high argument is one more than the last element stored locally.

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

769:    Level: beginner

771:    Concepts: ownership^of vectors
772:    Concepts: vector^ownership of elements

774: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRange()
775: @*/
776: PetscErrorCode  VecGetOwnershipRanges(Vec x,const PetscInt *ranges[])
777: {

783:   PetscLayoutGetRanges(x->map,ranges);
784:   return(0);
785: }

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

792:    Collective on Vec

794:    Input Parameter:
795: +  x - the vector
796: .  op - the option
797: -  flag - turn the option on or off

799:    Supported Options:
800: +     VEC_IGNORE_OFF_PROC_ENTRIES, which causes VecSetValues() to ignore
801:           entries destined to be stored on a separate processor. This can be used
802:           to eliminate the global reduction in the VecAssemblyXXXX() if you know
803:           that you have only used VecSetValues() to set local elements
804: .     VEC_IGNORE_NEGATIVE_INDICES, which means you can pass negative indices
805:           in ix in calls to VecSetValues() or VecGetValues(). These rows are simply
806:           ignored.

808:    Level: intermediate

810: @*/
811: PetscErrorCode  VecSetOption(Vec x,VecOption op,PetscBool flag)
812: {

818:   if (x->ops->setoption) {
819:     (*x->ops->setoption)(x,op,flag);
820:   }
821:   return(0);
822: }

826: /* Default routines for obtaining and releasing; */
827: /* may be used by any implementation */
828: PetscErrorCode VecDuplicateVecs_Default(Vec w,PetscInt m,Vec *V[])
829: {
831:   PetscInt       i;

836:   if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
837:   PetscMalloc1(m,V);
838:   for (i=0; i<m; i++) {VecDuplicate(w,*V+i);}
839:   return(0);
840: }

844: PetscErrorCode VecDestroyVecs_Default(PetscInt m,Vec v[])
845: {
847:   PetscInt       i;

851:   if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
852:   for (i=0; i<m; i++) {VecDestroy(&v[i]);}
853:   PetscFree(v);
854:   return(0);
855: }

859: /*@
860:    VecResetArray - Resets a vector to use its default memory. Call this
861:    after the use of VecPlaceArray().

863:    Not Collective

865:    Input Parameters:
866: .  vec - the vector

868:    Level: developer

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

872: @*/
873: PetscErrorCode  VecResetArray(Vec vec)
874: {

880:   if (vec->ops->resetarray) {
881:     (*vec->ops->resetarray)(vec);
882:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot reset array in this type of vector");
883:   PetscObjectStateIncrease((PetscObject)vec);
884:   return(0);
885: }

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

893:   Collective on PetscViewer

895:   Input Parameters:
896: + newvec - the newly loaded vector, this needs to have been created with VecCreate() or
897:            some related function before a call to VecLoad().
898: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
899:            HDF5 file viewer, obtained from PetscViewerHDF5Open()

901:    Level: intermediate

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

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

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

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

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

924:   Notes for advanced users:
925:   Most users should not need to know the details of the binary storage
926:   format, since VecLoad() and VecView() completely hide these details.
927:   But for anyone who's interested, the standard binary matrix storage
928:   format is
929: .vb
930:      int    VEC_FILE_CLASSID
931:      int    number of rows
932:      PetscScalar *values of all entries
933: .ve

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

941:   Concepts: vector^loading from file

943: .seealso: PetscViewerBinaryOpen(), VecView(), MatLoad(), VecLoad()
944: @*/
945: PetscErrorCode  VecLoad(Vec newvec, PetscViewer viewer)
946: {
948:   PetscBool      isbinary,ishdf5;
949:   PetscViewerFormat format;

954:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
955:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
956:   if (!isbinary && !ishdf5) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

958:   PetscLogEventBegin(VEC_Load,viewer,0,0,0);
959:   if (!((PetscObject)newvec)->type_name && !newvec->ops->create) {
960:     VecSetType(newvec, VECSTANDARD);
961:   }
962:   PetscViewerGetFormat(viewer,&format);
963:   if (format == PETSC_VIEWER_NATIVE && newvec->ops->loadnative) {
964:     (*newvec->ops->loadnative)(newvec,viewer);
965:   } else {
966:     (*newvec->ops->load)(newvec,viewer);
967:   }
968:   PetscLogEventEnd(VEC_Load,viewer,0,0,0);
969:   return(0);
970: }


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

978:    Logically Collective on Vec

980:    Input Parameter:
981: .  vec - the vector

983:    Output Parameter:
984: .  vec - the vector reciprocal

986:    Level: intermediate

988:    Concepts: vector^reciprocal

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

992: @*/
993: PetscErrorCode  VecReciprocal(Vec vec)
994: {

1000:   if (vec->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1001:   if (!vec->ops->reciprocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vector does not support reciprocal operation");
1002:   (*vec->ops->reciprocal)(vec);
1003:   PetscObjectStateIncrease((PetscObject)vec);
1004:   return(0);
1005: }

1009: /*@C
1010:     VecSetOperation - Allows user to set a vector operation.

1012:    Logically Collective on Vec

1014:     Input Parameters:
1015: +   vec - the vector
1016: .   op - the name of the operation
1017: -   f - the function that provides the operation.

1019:    Level: advanced

1021:     Usage:
1022: $      PetscErrorCode userview(Vec,PetscViewer);
1023: $      VecCreateMPI(comm,m,M,&x);
1024: $      VecSetOperation(x,VECOP_VIEW,(void(*)(void))userview);

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

1032:     This function is not currently available from Fortran.

1034: .keywords: vector, set, operation

1036: .seealso: VecCreate(), MatShellSetOperation()
1037: @*/
1038: PetscErrorCode VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
1039: {
1042:   if (op == VECOP_VIEW && !vec->ops->viewnative) {
1043:     vec->ops->viewnative = vec->ops->view;
1044:   } else if (op == VECOP_LOAD && !vec->ops->loadnative) {
1045:     vec->ops->loadnative = vec->ops->load;
1046:   }
1047:   (((void(**)(void))vec->ops)[(int)op]) = f;
1048:   return(0);
1049: }


1054: /*@
1055:    VecStashSetInitialSize - sets the sizes of the vec-stash, that is
1056:    used during the assembly process to store values that belong to
1057:    other processors.

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

1061:    Input Parameters:
1062: +  vec   - the vector
1063: .  size  - the initial size of the stash.
1064: -  bsize - the initial size of the block-stash(if used).

1066:    Options Database Keys:
1067: +   -vecstash_initial_size <size> or <size0,size1,...sizep-1>
1068: -   -vecstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1>

1070:    Level: intermediate

1072:    Notes:
1073:      The block-stash is used for values set with VecSetValuesBlocked() while
1074:      the stash is used for values set with VecSetValues()

1076:      Run with the option -info and look for output of the form
1077:      VecAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
1078:      to determine the appropriate value, MM, to use for size and
1079:      VecAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
1080:      to determine the value, BMM to use for bsize

1082:    Concepts: vector^stash
1083:    Concepts: stash^vector

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

1087: @*/
1088: PetscErrorCode  VecStashSetInitialSize(Vec vec,PetscInt size,PetscInt bsize)
1089: {

1094:   VecStashSetInitialSize_Private(&vec->stash,size);
1095:   VecStashSetInitialSize_Private(&vec->bstash,bsize);
1096:   return(0);
1097: }

1101: /*@
1102:    VecConjugate - Conjugates a vector.

1104:    Logically Collective on Vec

1106:    Input Parameters:
1107: .  x - the vector

1109:    Level: intermediate

1111:    Concepts: vector^conjugate

1113: @*/
1114: PetscErrorCode  VecConjugate(Vec x)
1115: {
1116: #if defined(PETSC_USE_COMPLEX)

1122:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1123:   (*x->ops->conjugate)(x);
1124:   /* we need to copy norms here */
1125:   PetscObjectStateIncrease((PetscObject)x);
1126:   return(0);
1127: #else
1128:   return(0);
1129: #endif
1130: }

1134: /*@
1135:    VecPointwiseMult - Computes the componentwise multiplication w = x*y.

1137:    Logically Collective on Vec

1139:    Input Parameters:
1140: .  x, y  - the vectors

1142:    Output Parameter:
1143: .  w - the result

1145:    Level: advanced

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

1149:    Concepts: vector^pointwise multiply

1151: .seealso: VecPointwiseDivide(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
1152: @*/
1153: PetscErrorCode  VecPointwiseMult(Vec w, Vec x,Vec y)
1154: {

1166:   if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

1168:   PetscLogEventBegin(VEC_PointwiseMult,x,y,w,0);
1169:   (*w->ops->pointwisemult)(w,x,y);
1170:   PetscLogEventEnd(VEC_PointwiseMult,x,y,w,0);
1171:   PetscObjectStateIncrease((PetscObject)w);
1172:   return(0);
1173: }

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

1180:    Logically Collective on Vec

1182:    Input Parameters:
1183: +  x  - the vector
1184: -  rctx - the random number context, formed by PetscRandomCreate(), or NULL and
1185:           it will create one internally.

1187:    Output Parameter:
1188: .  x  - the vector

1190:    Example of Usage:
1191: .vb
1192:      PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
1193:      VecSetRandom(x,rctx);
1194:      PetscRandomDestroy(rctx);
1195: .ve

1197:    Level: intermediate

1199:    Concepts: vector^setting to random
1200:    Concepts: random^vector

1202: .seealso: VecSet(), VecSetValues(), PetscRandomCreate(), PetscRandomDestroy()
1203: @*/
1204: PetscErrorCode  VecSetRandom(Vec x,PetscRandom rctx)
1205: {
1207:   PetscRandom    randObj = NULL;

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

1215:   if (!rctx) {
1216:     MPI_Comm comm;
1217:     PetscObjectGetComm((PetscObject)x,&comm);
1218:     PetscRandomCreate(comm,&randObj);
1219:     PetscRandomSetFromOptions(randObj);
1220:     rctx = randObj;
1221:   }

1223:   PetscLogEventBegin(VEC_SetRandom,x,rctx,0,0);
1224:   (*x->ops->setrandom)(x,rctx);
1225:   PetscLogEventEnd(VEC_SetRandom,x,rctx,0,0);

1227:   PetscRandomDestroy(&randObj);
1228:   PetscObjectStateIncrease((PetscObject)x);
1229:   return(0);
1230: }

1234: /*@
1235:   VecZeroEntries - puts a 0.0 in each element of a vector

1237:   Logically Collective on Vec

1239:   Input Parameter:
1240: . vec - The vector

1242:   Level: beginner

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

1249: .keywords: Vec, set, options, database
1250: .seealso: VecCreate(),  VecSetOptionsPrefix(), VecSet(), VecSetValues()
1251: @*/
1252: PetscErrorCode  VecZeroEntries(Vec vec)
1253: {

1257:   VecSet(vec,0);
1258:   return(0);
1259: }

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

1267:   Collective on Vec

1269:   Input Parameter:
1270: . vec - The vector

1272:   Level: intermediate

1274: .keywords: Vec, set, options, database, type
1275: .seealso: VecSetFromOptions(), VecSetType()
1276: */
1277: static PetscErrorCode VecSetTypeFromOptions_Private(Vec vec)
1278: {
1279:   PetscBool      opt;
1280:   VecType        defaultType;
1281:   char           typeName[256];
1282:   PetscMPIInt    size;

1286:   if (((PetscObject)vec)->type_name) defaultType = ((PetscObject)vec)->type_name;
1287:   else {
1288:     MPI_Comm_size(PetscObjectComm((PetscObject)vec), &size);
1289:     if (size > 1) defaultType = VECMPI;
1290:     else defaultType = VECSEQ;
1291:   }

1293:   if (!VecRegisterAllCalled) {VecRegisterAll();}
1294:   PetscOptionsFList("-vec_type","Vector type","VecSetType",VecList,defaultType,typeName,256,&opt);
1295:   if (opt) {
1296:     VecSetType(vec, typeName);
1297:   } else {
1298:     VecSetType(vec, defaultType);
1299:   }
1300:   return(0);
1301: }

1305: /*@
1306:   VecSetFromOptions - Configures the vector from the options database.

1308:   Collective on Vec

1310:   Input Parameter:
1311: . vec - The vector

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

1316:   Level: beginner

1318:   Concepts: vectors^setting options
1319:   Concepts: vectors^setting type

1321: .keywords: Vec, set, options, database
1322: .seealso: VecCreate(), VecSetOptionsPrefix()
1323: @*/
1324: PetscErrorCode  VecSetFromOptions(Vec vec)
1325: {


1331:   PetscObjectOptionsBegin((PetscObject)vec);
1332:   /* Handle vector type options */
1333:   VecSetTypeFromOptions_Private(vec);

1335:   /* Handle specific vector options */
1336:   if (vec->ops->setfromoptions) {
1337:     (*vec->ops->setfromoptions)(vec);
1338:   }

1340:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
1341:   PetscObjectProcessOptionsHandlers((PetscObject)vec);
1342:   PetscOptionsEnd();
1343:   return(0);
1344: }

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

1351:   Collective on Vec

1353:   Input Parameters:
1354: + v - the vector
1355: . n - the local size (or PETSC_DECIDE to have it set)
1356: - N - the global size (or PETSC_DECIDE)

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

1362:   Level: intermediate

1364: .seealso: VecGetSize(), PetscSplitOwnership()
1365: @*/
1366: PetscErrorCode  VecSetSizes(Vec v, PetscInt n, PetscInt N)
1367: {

1373:   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);
1374:   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);
1375:   v->map->n = n;
1376:   v->map->N = N;
1377:   if (v->ops->create) {
1378:     (*v->ops->create)(v);
1379:     v->ops->create = 0;
1380:   }
1381:   return(0);
1382: }

1386: /*@
1387:    VecSetBlockSize - Sets the blocksize for future calls to VecSetValuesBlocked()
1388:    and VecSetValuesBlockedLocal().

1390:    Logically Collective on Vec

1392:    Input Parameter:
1393: +  v - the vector
1394: -  bs - the blocksize

1396:    Notes:
1397:    All vectors obtained by VecDuplicate() inherit the same blocksize.

1399:    Level: advanced

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

1403:   Concepts: block size^vectors
1404: @*/
1405: PetscErrorCode  VecSetBlockSize(Vec v,PetscInt bs)
1406: {

1411:   if (bs < 0 || bs == v->map->bs) return(0);
1413:   PetscLayoutSetBlockSize(v->map,bs);
1414:   v->bstash.bs = bs; /* use the same blocksize for the vec's block-stash */
1415:   return(0);
1416: }

1420: /*@
1421:    VecGetBlockSize - Gets the blocksize for the vector, i.e. what is used for VecSetValuesBlocked()
1422:    and VecSetValuesBlockedLocal().

1424:    Not Collective

1426:    Input Parameter:
1427: .  v - the vector

1429:    Output Parameter:
1430: .  bs - the blocksize

1432:    Notes:
1433:    All vectors obtained by VecDuplicate() inherit the same blocksize.

1435:    Level: advanced

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

1439:    Concepts: vector^block size
1440:    Concepts: block^vector

1442: @*/
1443: PetscErrorCode  VecGetBlockSize(Vec v,PetscInt *bs)
1444: {

1450:   PetscLayoutGetBlockSize(v->map,bs);
1451:   return(0);
1452: }

1456: /*@C
1457:    VecSetOptionsPrefix - Sets the prefix used for searching for all
1458:    Vec options in the database.

1460:    Logically Collective on Vec

1462:    Input Parameter:
1463: +  v - the Vec context
1464: -  prefix - the prefix to prepend to all option names

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

1470:    Level: advanced

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

1474: .seealso: VecSetFromOptions()
1475: @*/
1476: PetscErrorCode  VecSetOptionsPrefix(Vec v,const char prefix[])
1477: {

1482:   PetscObjectSetOptionsPrefix((PetscObject)v,prefix);
1483:   return(0);
1484: }

1488: /*@C
1489:    VecAppendOptionsPrefix - Appends to the prefix used for searching for all
1490:    Vec options in the database.

1492:    Logically Collective on Vec

1494:    Input Parameters:
1495: +  v - the Vec context
1496: -  prefix - the prefix to prepend to all option names

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

1502:    Level: advanced

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

1506: .seealso: VecGetOptionsPrefix()
1507: @*/
1508: PetscErrorCode  VecAppendOptionsPrefix(Vec v,const char prefix[])
1509: {

1514:   PetscObjectAppendOptionsPrefix((PetscObject)v,prefix);
1515:   return(0);
1516: }

1520: /*@C
1521:    VecGetOptionsPrefix - Sets the prefix used for searching for all
1522:    Vec options in the database.

1524:    Not Collective

1526:    Input Parameter:
1527: .  v - the Vec context

1529:    Output Parameter:
1530: .  prefix - pointer to the prefix string used

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

1535:    Level: advanced

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

1539: .seealso: VecAppendOptionsPrefix()
1540: @*/
1541: PetscErrorCode  VecGetOptionsPrefix(Vec v,const char *prefix[])
1542: {

1547:   PetscObjectGetOptionsPrefix((PetscObject)v,prefix);
1548:   return(0);
1549: }

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

1556:    Collective on Vec

1558:    Input Parameters:
1559: .  v - the Vec context

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

1565:    Level: advanced

1567: .keywords: Vec, setup

1569: .seealso: VecCreate(), VecDestroy()
1570: @*/
1571: PetscErrorCode  VecSetUp(Vec v)
1572: {
1573:   PetscMPIInt    size;

1578:   if (!((PetscObject)v)->type_name) {
1579:     MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);
1580:     if (size == 1) {
1581:       VecSetType(v, VECSEQ);
1582:     } else {
1583:       VecSetType(v, VECMPI);
1584:     }
1585:   }
1586:   return(0);
1587: }

1589: /*
1590:     These currently expose the PetscScalar/PetscReal in updating the
1591:     cached norm. If we push those down into the implementation these
1592:     will become independent of PetscScalar/PetscReal
1593: */

1597: /*@
1598:    VecCopy - Copies a vector. y <- x

1600:    Logically Collective on Vec

1602:    Input Parameter:
1603: .  x - the vector

1605:    Output Parameter:
1606: .  y - the copy

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

1612:    Level: beginner

1614: .seealso: VecDuplicate()
1615: @*/
1616: PetscErrorCode  VecCopy(Vec x,Vec y)
1617: {
1618:   PetscBool      flgs[4];
1619:   PetscReal      norms[4] = {0.0,0.0,0.0,0.0};
1621:   PetscInt       i;

1628:   if (x == y) return(0);
1629:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1630:   if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths %d != %d", x->map->n, y->map->n);

1632: #if !defined(PETSC_USE_MIXED_PRECISION)
1633:   for (i=0; i<4; i++) {
1634:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],norms[i],flgs[i]);
1635:   }
1636: #endif

1638:   PetscLogEventBegin(VEC_Copy,x,y,0,0);
1639: #if defined(PETSC_USE_MIXED_PRECISION)
1640:   extern PetscErrorCode VecGetArray(Vec,double**);
1641:   extern PetscErrorCode VecRestoreArray(Vec,double**);
1642:   extern PetscErrorCode VecGetArray(Vec,float**);
1643:   extern PetscErrorCode VecRestoreArray(Vec,float**);
1644:   extern PetscErrorCode VecGetArrayRead(Vec,const double**);
1645:   extern PetscErrorCode VecRestoreArrayRead(Vec,const double**);
1646:   extern PetscErrorCode VecGetArrayRead(Vec,const float**);
1647:   extern PetscErrorCode VecRestoreArrayRead(Vec,const float**);
1648:   if ((((PetscObject)x)->precision == PETSC_PRECISION_SINGLE) && (((PetscObject)y)->precision == PETSC_PRECISION_DOUBLE)) {
1649:     PetscInt    i,n;
1650:     const float *xx;
1651:     double      *yy;
1652:     VecGetArrayRead(x,&xx);
1653:     VecGetArray(y,&yy);
1654:     VecGetLocalSize(x,&n);
1655:     for (i=0; i<n; i++) yy[i] = xx[i];
1656:     VecRestoreArrayRead(x,&xx);
1657:     VecRestoreArray(y,&yy);
1658:   } else if ((((PetscObject)x)->precision == PETSC_PRECISION_DOUBLE) && (((PetscObject)y)->precision == PETSC_PRECISION_SINGLE)) {
1659:     PetscInt     i,n;
1660:     float        *yy;
1661:     const double *xx;
1662:     VecGetArrayRead(x,&xx);
1663:     VecGetArray(y,&yy);
1664:     VecGetLocalSize(x,&n);
1665:     for (i=0; i<n; i++) yy[i] = (float) xx[i];
1666:     VecRestoreArrayRead(x,&xx);
1667:     VecRestoreArray(y,&yy);
1668:   } else {
1669:     (*x->ops->copy)(x,y);
1670:   }
1671: #else
1672:   (*x->ops->copy)(x,y);
1673: #endif

1675:   PetscObjectStateIncrease((PetscObject)y);
1676: #if !defined(PETSC_USE_MIXED_PRECISION)
1677:   for (i=0; i<4; i++) {
1678:     if (flgs[i]) {
1679:       PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],norms[i]);
1680:     }
1681:   }
1682: #endif

1684:   PetscLogEventEnd(VEC_Copy,x,y,0,0);
1685:   return(0);
1686: }

1690: /*@
1691:    VecSwap - Swaps the vectors x and y.

1693:    Logically Collective on Vec

1695:    Input Parameters:
1696: .  x, y  - the vectors

1698:    Level: advanced

1700:    Concepts: vector^swapping values

1702: @*/
1703: PetscErrorCode  VecSwap(Vec x,Vec y)
1704: {
1705:   PetscReal      normxs[4]={0.0,0.0,0.0,0.0},normys[4]={0.0,0.0,0.0,0.0};
1706:   PetscBool      flgxs[4],flgys[4];
1708:   PetscInt       i;

1716:   if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1717:   if (y->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1718:   if (x->map->N != y->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
1719:   if (x->map->n != y->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");

1721:   PetscLogEventBegin(VEC_Swap,x,y,0,0);
1722:   for (i=0; i<4; i++) {
1723:     PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],normxs[i],flgxs[i]);
1724:     PetscObjectComposedDataGetReal((PetscObject)y,NormIds[i],normys[i],flgys[i]);
1725:   }
1726:   (*x->ops->swap)(x,y);
1727:   PetscObjectStateIncrease((PetscObject)x);
1728:   PetscObjectStateIncrease((PetscObject)y);
1729:   for (i=0; i<4; i++) {
1730:     if (flgxs[i]) {
1731:       PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],normxs[i]);
1732:     }
1733:     if (flgys[i]) {
1734:       PetscObjectComposedDataSetReal((PetscObject)x,NormIds[i],normys[i]);
1735:     }
1736:   }
1737:   PetscLogEventEnd(VEC_Swap,x,y,0,0);
1738:   return(0);
1739: }

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

1746:   Collective on VecStash

1748:   Input Parameters:
1749: + obj   - the VecStash object
1750: . prefix - prefix to use for viewing, or NULL to use prefix of 'mat'
1751: - optionname - option to activate viewing

1753:   Level: intermediate

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

1757: */
1758: PetscErrorCode VecStashViewFromOptions(Vec obj,const char prefix[],const char optionname[])
1759: {
1760:   PetscErrorCode    ierr;
1761:   PetscViewer       viewer;
1762:   PetscBool         flg;
1763:   static PetscBool  incall = PETSC_FALSE;
1764:   PetscViewerFormat format;

1767:   if (incall) return(0);
1768:   incall = PETSC_TRUE;
1769:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),prefix,optionname,&viewer,&format,&flg);
1770:   if (flg) {
1771:     PetscViewerPushFormat(viewer,format);
1772:     VecStashView(obj,viewer);
1773:     PetscViewerPopFormat(viewer);
1774:     PetscViewerDestroy(&viewer);
1775:   }
1776:   incall = PETSC_FALSE;
1777:   return(0);
1778: }

1782: /*@
1783:    VecStashView - Prints the entries in the vector stash and block stash.

1785:    Collective on Vec

1787:    Input Parameters:
1788: +  v - the vector
1789: -  viewer - the viewer

1791:    Level: advanced

1793:    Concepts: vector^stash
1794:    Concepts: stash^vector

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

1798: @*/
1799: PetscErrorCode  VecStashView(Vec v,PetscViewer viewer)
1800: {
1802:   PetscMPIInt    rank;
1803:   PetscInt       i,j;
1804:   PetscBool      match;
1805:   VecStash       *s;
1806:   PetscScalar    val;


1813:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&match);
1814:   if (!match) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Stash viewer only works with ASCII viewer not %s\n",((PetscObject)v)->type_name);
1815:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1816:   MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
1817:   s    = &v->bstash;

1819:   /* print block stash */
1820:   PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
1821:   PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector Block stash size %D block size %D\n",rank,s->n,s->bs);
1822:   for (i=0; i<s->n; i++) {
1823:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D ",rank,s->idx[i]);
1824:     for (j=0; j<s->bs; j++) {
1825:       val = s->array[i*s->bs+j];
1826: #if defined(PETSC_USE_COMPLEX)
1827:       PetscViewerASCIISynchronizedPrintf(viewer,"(%18.16e %18.16e) ",PetscRealPart(val),PetscImaginaryPart(val));
1828: #else
1829:       PetscViewerASCIISynchronizedPrintf(viewer,"%18.16e ",val);
1830: #endif
1831:     }
1832:     PetscViewerASCIISynchronizedPrintf(viewer,"\n");
1833:   }
1834:   PetscViewerFlush(viewer);

1836:   s = &v->stash;

1838:   /* print basic stash */
1839:   PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector stash size %D\n",rank,s->n);
1840:   for (i=0; i<s->n; i++) {
1841:     val = s->array[i];
1842: #if defined(PETSC_USE_COMPLEX)
1843:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D (%18.16e %18.16e) ",rank,s->idx[i],PetscRealPart(val),PetscImaginaryPart(val));
1844: #else
1845:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D %18.16e\n",rank,s->idx[i],val);
1846: #endif
1847:   }
1848:   PetscViewerFlush(viewer);
1849:   PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);

1851:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1852:   return(0);
1853: }

1857: PetscErrorCode PetscOptionsVec(const char key[],const char text[],const char man[],Vec v,PetscBool *set)
1858: {
1859:   PetscInt       i,N,rstart,rend;
1861:   PetscScalar    *xx;
1862:   PetscReal      *xreal;
1863:   PetscBool      iset;

1866:   VecGetOwnershipRange(v,&rstart,&rend);
1867:   VecGetSize(v,&N);
1868:   PetscCalloc1(N,&xreal);
1869:   PetscOptionsRealArray(key,text,man,xreal,&N,&iset);
1870:   if (iset) {
1871:     VecGetArray(v,&xx);
1872:     for (i=rstart; i<rend; i++) xx[i-rstart] = xreal[i];
1873:     VecRestoreArray(v,&xx);
1874:   }
1875:   PetscFree(xreal);
1876:   if (set) *set = iset;
1877:   return(0);
1878: }

1882: /*@
1883:    VecGetLayout - get PetscLayout describing vector layout

1885:    Not Collective

1887:    Input Arguments:
1888: .  x - the vector

1890:    Output Arguments:
1891: .  map - the layout

1893:    Level: developer

1895: .seealso: VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1896: @*/
1897: PetscErrorCode VecGetLayout(Vec x,PetscLayout *map)
1898: {

1902:   *map = x->map;
1903:   return(0);
1904: }

1908: /*@
1909:    VecSetLayout - set PetscLayout describing vector layout

1911:    Not Collective

1913:    Input Arguments:
1914: +  x - the vector
1915: -  map - the layout

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

1920:    Level: developer

1922: .seealso: VecGetLayout(), VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1923: @*/
1924: PetscErrorCode VecSetLayout(Vec x,PetscLayout map)
1925: {

1930:   PetscLayoutReference(map,&x->map);
1931:   return(0);
1932: }