Date: 2026-03-01T05:25:24+00:00

Actual source code: bvec2.c

      1: /*
      2:    Implements the sequential vectors.
      3: */

      5: #include <../src/vec/vec/impls/dvecimpl.h>
      6: #include <../src/vec/vec/impls/mpi/pvecimpl.h>
      7: #include <petsc/private/glvisviewerimpl.h>
      8: #include <petsc/private/glvisvecimpl.h>
      9: #include <petscblaslapack.h>

     11: static PetscErrorCode VecPointwiseApply_Seq(Vec win, Vec xin, Vec yin, PetscScalar (*const func)(PetscScalar, PetscScalar))
     12: {
     13:   const PetscInt n = win->map->n;
     14:   PetscScalar   *ww, *xx, *yy; /* cannot make xx or yy const since might be ww */

     16:   PetscFunctionBegin;
     17:   PetscCall(VecGetArrayRead(xin, (const PetscScalar **)&xx));
     18:   PetscCall(VecGetArrayRead(yin, (const PetscScalar **)&yy));
     19:   PetscCall(VecGetArray(win, &ww));
     20:   for (PetscInt i = 0; i < n; ++i) ww[i] = func(xx[i], yy[i]);
     21:   PetscCall(VecRestoreArrayRead(xin, (const PetscScalar **)&xx));
     22:   PetscCall(VecRestoreArrayRead(yin, (const PetscScalar **)&yy));
     23:   PetscCall(VecRestoreArray(win, &ww));
     24:   PetscCall(PetscLogFlops(n));
     25:   PetscFunctionReturn(PETSC_SUCCESS);
     26: }

     28: static PetscScalar MaxRealPart(PetscScalar x, PetscScalar y)
     29: {
     30:   // use temporaries to avoid reevaluating side-effects
     31:   const PetscReal rx = PetscRealPart(x), ry = PetscRealPart(y);

     33:   return PetscMax(rx, ry);
     34: }

     36: PetscErrorCode VecPointwiseMax_Seq(Vec win, Vec xin, Vec yin)
     37: {
     38:   PetscFunctionBegin;
     39:   PetscCall(VecPointwiseApply_Seq(win, xin, yin, MaxRealPart));
     40:   PetscFunctionReturn(PETSC_SUCCESS);
     41: }

     43: static PetscScalar MinRealPart(PetscScalar x, PetscScalar y)
     44: {
     45:   // use temporaries to avoid reevaluating side-effects
     46:   const PetscReal rx = PetscRealPart(x), ry = PetscRealPart(y);

     48:   return PetscMin(rx, ry);
     49: }

     51: PetscErrorCode VecPointwiseMin_Seq(Vec win, Vec xin, Vec yin)
     52: {
     53:   PetscFunctionBegin;
     54:   PetscCall(VecPointwiseApply_Seq(win, xin, yin, MinRealPart));
     55:   PetscFunctionReturn(PETSC_SUCCESS);
     56: }

     58: static PetscScalar MaxAbs(PetscScalar x, PetscScalar y)
     59: {
     60:   return PetscMax(PetscAbsScalar(x), PetscAbsScalar(y));
     61: }

     63: PetscErrorCode VecPointwiseMaxAbs_Seq(Vec win, Vec xin, Vec yin)
     64: {
     65:   PetscFunctionBegin;
     66:   PetscCall(VecPointwiseApply_Seq(win, xin, yin, MaxAbs));
     67:   PetscFunctionReturn(PETSC_SUCCESS);
     68: }

     70: #include <../src/vec/vec/impls/seq/ftn-kernels/fxtimesy.h>

     72: PetscErrorCode VecPointwiseMult_Seq(Vec win, Vec xin, Vec yin)
     73: {
     74:   PetscInt     n = win->map->n, i;
     75:   PetscScalar *ww, *xx, *yy; /* cannot make xx or yy const since might be ww */

     77:   PetscFunctionBegin;
     78:   PetscCall(VecGetArrayRead(xin, (const PetscScalar **)&xx));
     79:   PetscCall(VecGetArrayRead(yin, (const PetscScalar **)&yy));
     80:   PetscCall(VecGetArray(win, &ww));
     81:   if (ww == xx) {
     82:     for (i = 0; i < n; i++) ww[i] *= yy[i];
     83:   } else if (ww == yy) {
     84:     for (i = 0; i < n; i++) ww[i] *= xx[i];
     85:   } else {
     86: #if defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
     87:     fortranxtimesy_(xx, yy, ww, &n);
     88: #else
     89:     for (i = 0; i < n; i++) ww[i] = xx[i] * yy[i];
     90: #endif
     91:   }
     92:   PetscCall(VecRestoreArrayRead(xin, (const PetscScalar **)&xx));
     93:   PetscCall(VecRestoreArrayRead(yin, (const PetscScalar **)&yy));
     94:   PetscCall(VecRestoreArray(win, &ww));
     95:   PetscCall(PetscLogFlops(n));
     96:   PetscFunctionReturn(PETSC_SUCCESS);
     97: }

     99: static PetscScalar ScalDiv(PetscScalar x, PetscScalar y)
    100: {
    101:   return y == 0.0 ? 0.0 : x / y;
    102: }

    104: PetscErrorCode VecPointwiseDivide_Seq(Vec win, Vec xin, Vec yin)
    105: {
    106:   PetscFunctionBegin;
    107:   PetscCall(VecPointwiseApply_Seq(win, xin, yin, ScalDiv));
    108:   PetscFunctionReturn(PETSC_SUCCESS);
    109: }

    111: PetscErrorCode VecSetRandom_Seq(Vec xin, PetscRandom r)
    112: {
    113:   PetscScalar *xx;

    115:   PetscFunctionBegin;
    116:   PetscCall(VecGetArrayWrite(xin, &xx));
    117:   PetscCall(PetscRandomGetValues(r, xin->map->n, xx));
    118:   PetscCall(VecRestoreArrayWrite(xin, &xx));
    119:   PetscFunctionReturn(PETSC_SUCCESS);
    120: }

    122: PetscErrorCode VecGetSize_Seq(Vec vin, PetscInt *size)
    123: {
    124:   PetscFunctionBegin;
    125:   *size = vin->map->n;
    126:   PetscFunctionReturn(PETSC_SUCCESS);
    127: }

    129: PetscErrorCode VecConjugate_Seq(Vec xin)
    130: {
    131:   PetscFunctionBegin;
    132:   if (PetscDefined(USE_COMPLEX)) {
    133:     const PetscInt n = xin->map->n;
    134:     PetscScalar   *x;

    136:     PetscCall(VecGetArray(xin, &x));
    137:     for (PetscInt i = 0; i < n; ++i) x[i] = PetscConj(x[i]);
    138:     PetscCall(VecRestoreArray(xin, &x));
    139:   }
    140:   PetscFunctionReturn(PETSC_SUCCESS);
    141: }

    143: PetscErrorCode VecResetArray_Seq(Vec vin)
    144: {
    145:   Vec_Seq *v = (Vec_Seq *)vin->data;

    147:   PetscFunctionBegin;
    148:   v->array         = v->unplacedarray;
    149:   v->unplacedarray = NULL;
    150:   PetscFunctionReturn(PETSC_SUCCESS);
    151: }

    153: PetscErrorCode VecCopy_Seq(Vec xin, Vec yin)
    154: {
    155:   PetscFunctionBegin;
    156:   if (xin != yin) {
    157:     const PetscScalar *xa;
    158:     PetscScalar       *ya;

    160:     PetscCall(VecGetArrayRead(xin, &xa));
    161:     PetscCall(VecGetArrayWrite(yin, &ya));
    162:     PetscCall(PetscArraycpy(ya, xa, xin->map->n));
    163:     PetscCall(VecRestoreArrayRead(xin, &xa));
    164:     PetscCall(VecRestoreArrayWrite(yin, &ya));
    165:   }
    166:   PetscFunctionReturn(PETSC_SUCCESS);
    167: }

    169: PetscErrorCode VecSwap_Seq(Vec xin, Vec yin)
    170: {
    171:   PetscFunctionBegin;
    172:   if (xin != yin) {
    173:     const PetscBLASInt one = 1;
    174:     PetscScalar       *ya, *xa;
    175:     PetscBLASInt       bn;

    177:     PetscCall(PetscBLASIntCast(xin->map->n, &bn));
    178:     PetscCall(VecGetArray(xin, &xa));
    179:     PetscCall(VecGetArray(yin, &ya));
    180:     PetscCallBLAS("BLASswap", BLASswap_(&bn, xa, &one, ya, &one));
    181:     PetscCall(VecRestoreArray(xin, &xa));
    182:     PetscCall(VecRestoreArray(yin, &ya));
    183:   }
    184:   PetscFunctionReturn(PETSC_SUCCESS);
    185: }

    187: PetscErrorCode VecNorm_Seq(Vec xin, NormType type, PetscReal *z)
    188: {
    189:   // use a local variable to ensure compiler doesn't think z aliases any of the other arrays
    190:   PetscReal      ztmp[] = {0.0, 0.0};
    191:   const PetscInt n      = xin->map->n;

    193:   PetscFunctionBegin;
    194:   if (n) {
    195:     const PetscScalar *xx;
    196:     const PetscBLASInt one = 1;
    197:     PetscBLASInt       bn  = 0;

    199:     PetscCall(PetscBLASIntCast(n, &bn));
    200:     PetscCall(VecGetArrayRead(xin, &xx));
    201:     if (type == NORM_2 || type == NORM_FROBENIUS) {
    202:     NORM_1_AND_2_DOING_NORM_2:
    203:       if (PetscDefined(USE_REAL___FP16)) {
    204:         PetscCallBLAS("BLASnrm2", ztmp[type == NORM_1_AND_2] = BLASnrm2_(&bn, xx, &one));
    205:       } else {
    206:         PetscCallBLAS("BLASdot", ztmp[type == NORM_1_AND_2] = PetscSqrtReal(PetscRealPart(BLASdot_(&bn, xx, &one, xx, &one))));
    207:       }
    208:       PetscCall(PetscLogFlops(2.0 * n - 1));
    209:     } else if (type == NORM_INFINITY) {
    210:       for (PetscInt i = 0; i < n; ++i) {
    211:         const PetscReal tmp = PetscAbsScalar(xx[i]);

    213:         /* check special case of tmp == NaN */
    214:         if ((tmp > ztmp[0]) || (tmp != tmp)) {
    215:           ztmp[0] = tmp;
    216:           if (tmp != tmp) break;
    217:         }
    218:       }
    219:     } else if (type == NORM_1 || type == NORM_1_AND_2) {
    220:       if (PetscDefined(USE_COMPLEX)) {
    221:         // BLASasum() returns the nonstandard 1 norm of the 1 norm of the complex entries so we
    222:         // provide a custom loop instead
    223:         for (PetscInt i = 0; i < n; ++i) ztmp[0] += PetscAbsScalar(xx[i]);
    224:       } else {
    225:         PetscCallBLAS("BLASasum", ztmp[0] = BLASasum_(&bn, xx, &one));
    226:       }
    227:       PetscCall(PetscLogFlops(n - 1.0));
    228:       /* slight reshuffle so we can skip getting the array again (but still log the flops) if we
    229:          do norm2 after this */
    230:       if (type == NORM_1_AND_2) goto NORM_1_AND_2_DOING_NORM_2;
    231:     }
    232:     PetscCall(VecRestoreArrayRead(xin, &xx));
    233:   }
    234:   z[0] = ztmp[0];
    235:   if (type == NORM_1_AND_2) z[1] = ztmp[1];
    236:   PetscFunctionReturn(PETSC_SUCCESS);
    237: }

    239: static PetscErrorCode VecView_Seq_ASCII(Vec xin, PetscViewer viewer)
    240: {
    241:   PetscInt           i, n = xin->map->n;
    242:   const char        *name;
    243:   PetscViewerFormat  format;
    244:   const PetscScalar *xv;

    246:   PetscFunctionBegin;
    247:   PetscCall(VecGetArrayRead(xin, &xv));
    248:   PetscCall(PetscViewerGetFormat(viewer, &format));
    249:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
    250:     PetscCall(PetscObjectGetName((PetscObject)xin, &name));
    251:     PetscCall(PetscViewerASCIIPrintf(viewer, "%s = [\n", name));
    252:     for (i = 0; i < n; i++) {
    253: #if defined(PETSC_USE_COMPLEX)
    254:       if (PetscImaginaryPart(xv[i]) > 0.0) {
    255:         PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e + %18.16ei\n", (double)PetscRealPart(xv[i]), (double)PetscImaginaryPart(xv[i])));
    256:       } else if (PetscImaginaryPart(xv[i]) < 0.0) {
    257:         PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e - %18.16ei\n", (double)PetscRealPart(xv[i]), -(double)PetscImaginaryPart(xv[i])));
    258:       } else {
    259:         PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e\n", (double)PetscRealPart(xv[i])));
    260:       }
    261: #else
    262:       PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e\n", (double)xv[i]));
    263: #endif
    264:     }
    265:     PetscCall(PetscViewerASCIIPrintf(viewer, "];\n"));
    266:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
    267:     for (i = 0; i < n; i++) {
    268: #if defined(PETSC_USE_COMPLEX)
    269:       PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e %18.16e\n", (double)PetscRealPart(xv[i]), (double)PetscImaginaryPart(xv[i])));
    270: #else
    271:       PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e\n", (double)xv[i]));
    272: #endif
    273:     }
    274:   } else if (format == PETSC_VIEWER_ASCII_PCICE) {
    275:     PetscInt bs, b;

    277:     PetscCall(VecGetBlockSize(xin, &bs));
    278:     PetscCheck(bs >= 1 && bs <= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PCICE can only handle up to 3D objects, but vector dimension is %" PetscInt_FMT, bs);
    279:     PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT "\n", xin->map->N / bs));
    280:     for (i = 0; i < n / bs; i++) {
    281:       PetscCall(PetscViewerASCIIPrintf(viewer, "%7" PetscInt_FMT "   ", i + 1));
    282:       for (b = 0; b < bs; b++) {
    283:         if (b > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " "));
    284: #if !defined(PETSC_USE_COMPLEX)
    285:         PetscCall(PetscViewerASCIIPrintf(viewer, "% 12.5E", (double)xv[i * bs + b]));
    286: #endif
    287:       }
    288:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
    289:     }
    290:   } else if (format == PETSC_VIEWER_ASCII_GLVIS) {
    291:     /* GLVis ASCII visualization/dump: this function mimics mfem::GridFunction::Save() */
    292:     const PetscScalar      *array;
    293:     PetscInt                i, n, vdim, ordering = 1; /* mfem::FiniteElementSpace::Ordering::byVDIM */
    294:     PetscContainer          glvis_container;
    295:     PetscViewerGLVisVecInfo glvis_vec_info;
    296:     PetscViewerGLVisInfo    glvis_info;

    298:     /* mfem::FiniteElementSpace::Save() */
    299:     PetscCall(VecGetBlockSize(xin, &vdim));
    300:     PetscCall(PetscViewerASCIIPrintf(viewer, "FiniteElementSpace\n"));
    301:     PetscCall(PetscObjectQuery((PetscObject)xin, "_glvis_info_container", (PetscObject *)&glvis_container));
    302:     PetscCheck(glvis_container, PetscObjectComm((PetscObject)xin), PETSC_ERR_PLIB, "Missing GLVis container");
    303:     PetscCall(PetscContainerGetPointer(glvis_container, (void **)&glvis_vec_info));
    304:     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", glvis_vec_info->fec_type));
    305:     PetscCall(PetscViewerASCIIPrintf(viewer, "VDim: %" PetscInt_FMT "\n", vdim));
    306:     PetscCall(PetscViewerASCIIPrintf(viewer, "Ordering: %" PetscInt_FMT "\n", ordering));
    307:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
    308:     /* mfem::Vector::Print() */
    309:     PetscCall(PetscObjectQuery((PetscObject)viewer, "_glvis_info_container", (PetscObject *)&glvis_container));
    310:     PetscCheck(glvis_container, PetscObjectComm((PetscObject)viewer), PETSC_ERR_PLIB, "Missing GLVis container");
    311:     PetscCall(PetscContainerGetPointer(glvis_container, (void **)&glvis_info));
    312:     if (glvis_info->enabled) {
    313:       PetscCall(VecGetLocalSize(xin, &n));
    314:       PetscCall(VecGetArrayRead(xin, &array));
    315:       for (i = 0; i < n; i++) {
    316:         PetscCall(PetscViewerASCIIPrintf(viewer, glvis_info->fmt, (double)PetscRealPart(array[i])));
    317:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
    318:       }
    319:       PetscCall(VecRestoreArrayRead(xin, &array));
    320:     }
    321:   } else if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
    322:     /* No info */
    323:   } else {
    324:     for (i = 0; i < n; i++) {
    325:       if (format == PETSC_VIEWER_ASCII_INDEX) PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ": ", i));
    326: #if defined(PETSC_USE_COMPLEX)
    327:       if (PetscImaginaryPart(xv[i]) > 0.0) {
    328:         PetscCall(PetscViewerASCIIPrintf(viewer, "%g + %g i\n", (double)PetscRealPart(xv[i]), (double)PetscImaginaryPart(xv[i])));
    329:       } else if (PetscImaginaryPart(xv[i]) < 0.0) {
    330:         PetscCall(PetscViewerASCIIPrintf(viewer, "%g - %g i\n", (double)PetscRealPart(xv[i]), -(double)PetscImaginaryPart(xv[i])));
    331:       } else {
    332:         PetscCall(PetscViewerASCIIPrintf(viewer, "%g\n", (double)PetscRealPart(xv[i])));
    333:       }
    334: #else
    335:       PetscCall(PetscViewerASCIIPrintf(viewer, "%g\n", (double)xv[i]));
    336: #endif
    337:     }
    338:   }
    339:   PetscCall(PetscViewerFlush(viewer));
    340:   PetscCall(VecRestoreArrayRead(xin, &xv));
    341:   PetscFunctionReturn(PETSC_SUCCESS);
    342: }

    344: #include <petscdraw.h>
    345: static PetscErrorCode VecView_Seq_Draw_LG(Vec xin, PetscViewer v)
    346: {
    347:   PetscDraw          draw;
    348:   PetscBool          isnull;
    349:   PetscDrawLG        lg;
    350:   PetscInt           i, c, bs = xin->map->bs, n = xin->map->n / bs;
    351:   const PetscScalar *xv;
    352:   PetscReal         *xx, *yy, xmin, xmax, h;
    353:   int                colors[] = {PETSC_DRAW_RED};
    354:   PetscViewerFormat  format;
    355:   PetscDrawAxis      axis;
    356:   const char        *name;

    358:   PetscFunctionBegin;
    359:   PetscCall(PetscViewerDrawGetDraw(v, 0, &draw));
    360:   PetscCall(PetscDrawIsNull(draw, &isnull));
    361:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

    363:   PetscCall(PetscObjectGetName((PetscObject)xin, &name));
    364:   PetscCall(PetscDrawSetTitle(draw, name));
    365:   PetscCall(PetscViewerGetFormat(v, &format));
    366:   PetscCall(PetscMalloc2(n, &xx, n, &yy));
    367:   PetscCall(VecGetArrayRead(xin, &xv));
    368:   for (c = 0; c < bs; c++) {
    369:     PetscCall(PetscViewerDrawGetDrawLG(v, c, &lg));
    370:     PetscCall(PetscDrawLGReset(lg));
    371:     PetscCall(PetscDrawLGSetDimension(lg, 1));
    372:     PetscCall(PetscDrawLGSetColors(lg, colors));
    373:     if (format == PETSC_VIEWER_DRAW_LG_XRANGE) {
    374:       PetscCall(PetscDrawLGGetAxis(lg, &axis));
    375:       PetscCall(PetscDrawAxisGetLimits(axis, &xmin, &xmax, NULL, NULL));
    376:       h = (xmax - xmin) / n;
    377:       for (i = 0; i < n; i++) xx[i] = i * h + 0.5 * h; /* cell center */
    378:     } else {
    379:       for (i = 0; i < n; i++) xx[i] = (PetscReal)i;
    380:     }
    381:     for (i = 0; i < n; i++) yy[i] = PetscRealPart(xv[c + i * bs]);

    383:     PetscCall(PetscDrawLGAddPoints(lg, n, &xx, &yy));
    384:     PetscCall(PetscDrawLGDraw(lg));
    385:     PetscCall(PetscDrawLGSave(lg));
    386:   }
    387:   PetscCall(VecRestoreArrayRead(xin, &xv));
    388:   PetscCall(PetscFree2(xx, yy));
    389:   PetscFunctionReturn(PETSC_SUCCESS);
    390: }

    392: static PetscErrorCode VecView_Seq_Draw(Vec xin, PetscViewer v)
    393: {
    394:   PetscDraw draw;
    395:   PetscBool isnull;

    397:   PetscFunctionBegin;
    398:   PetscCall(PetscViewerDrawGetDraw(v, 0, &draw));
    399:   PetscCall(PetscDrawIsNull(draw, &isnull));
    400:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

    402:   PetscCall(VecView_Seq_Draw_LG(xin, v));
    403:   PetscFunctionReturn(PETSC_SUCCESS);
    404: }

    406: static PetscErrorCode VecView_Seq_Binary(Vec xin, PetscViewer viewer)
    407: {
    408:   return VecView_Binary(xin, viewer);
    409: }

    411: #if defined(PETSC_HAVE_MATLAB)
    412: #include <petscmatlab.h>
    413:   #include <mat.h> /* MATLAB include file */
    414: PetscErrorCode VecView_Seq_Matlab(Vec vec, PetscViewer viewer)
    415: {
    416:   PetscInt           n;
    417:   const PetscScalar *array;

    419:   PetscFunctionBegin;
    420:   PetscCall(VecGetLocalSize(vec, &n));
    421:   PetscCall(PetscObjectName((PetscObject)vec));
    422:   PetscCall(VecGetArrayRead(vec, &array));
    423:   PetscCall(PetscViewerMatlabPutArray(viewer, n, 1, array, ((PetscObject)vec)->name));
    424:   PetscCall(VecRestoreArrayRead(vec, &array));
    425:   PetscFunctionReturn(PETSC_SUCCESS);
    426: }
    427: #endif

    429: PetscErrorCode VecView_Seq(Vec xin, PetscViewer viewer)
    430: {
    431:   PetscBool isdraw, isascii, issocket, isbinary;
    432: #if defined(PETSC_HAVE_MATHEMATICA)
    433:   PetscBool ismathematica;
    434: #endif
    435: #if defined(PETSC_HAVE_MATLAB)
    436:   PetscBool ismatlab;
    437: #endif
    438: #if defined(PETSC_HAVE_HDF5)
    439:   PetscBool ishdf5;
    440: #endif
    441:   PetscBool isglvis;
    442: #if defined(PETSC_HAVE_ADIOS)
    443:   PetscBool isadios;
    444: #endif

    446:   PetscFunctionBegin;
    447:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
    448:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
    449:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
    450:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
    451: #if defined(PETSC_HAVE_MATHEMATICA)
    452:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERMATHEMATICA, &ismathematica));
    453: #endif
    454: #if defined(PETSC_HAVE_HDF5)
    455:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
    456: #endif
    457: #if defined(PETSC_HAVE_MATLAB)
    458:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERMATLAB, &ismatlab));
    459: #endif
    460:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
    461: #if defined(PETSC_HAVE_ADIOS)
    462:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERADIOS, &isadios));
    463: #endif

    465:   if (isdraw) {
    466:     PetscCall(VecView_Seq_Draw(xin, viewer));
    467:   } else if (isascii) {
    468:     PetscCall(VecView_Seq_ASCII(xin, viewer));
    469:   } else if (isbinary) {
    470:     PetscCall(VecView_Seq_Binary(xin, viewer));
    471: #if defined(PETSC_HAVE_MATHEMATICA)
    472:   } else if (ismathematica) {
    473:     PetscCall(PetscViewerMathematicaPutVector(viewer, xin));
    474: #endif
    475: #if defined(PETSC_HAVE_HDF5)
    476:   } else if (ishdf5) {
    477:     PetscCall(VecView_MPI_HDF5(xin, viewer)); /* Reusing VecView_MPI_HDF5 ... don't want code duplication*/
    478: #endif
    479: #if defined(PETSC_HAVE_ADIOS)
    480:   } else if (isadios) {
    481:     PetscCall(VecView_MPI_ADIOS(xin, viewer)); /* Reusing VecView_MPI_ADIOS ... don't want code duplication*/
    482: #endif
    483: #if defined(PETSC_HAVE_MATLAB)
    484:   } else if (ismatlab) {
    485:     PetscCall(VecView_Seq_Matlab(xin, viewer));
    486: #endif
    487:   } else if (isglvis) PetscCall(VecView_GLVis(xin, viewer));
    488:   PetscFunctionReturn(PETSC_SUCCESS);
    489: }

    491: PetscErrorCode VecGetValues_Seq(Vec xin, PetscInt ni, const PetscInt ix[], PetscScalar y[])
    492: {
    493:   const PetscBool    ignorenegidx = xin->stash.ignorenegidx;
    494:   const PetscScalar *xx;

    496:   PetscFunctionBegin;
    497:   PetscCall(VecGetArrayRead(xin, &xx));
    498:   for (PetscInt i = 0; i < ni; ++i) {
    499:     if (ignorenegidx && (ix[i] < 0)) continue;
    500:     if (PetscDefined(USE_DEBUG)) {
    501:       PetscCheck(ix[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Out of range index value %" PetscInt_FMT " cannot be negative", ix[i]);
    502:       PetscCheck(ix[i] < xin->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Out of range index value %" PetscInt_FMT ", should be less than %" PetscInt_FMT, ix[i], xin->map->n);
    503:     }
    504:     y[i] = xx[ix[i]];
    505:   }
    506:   PetscCall(VecRestoreArrayRead(xin, &xx));
    507:   PetscFunctionReturn(PETSC_SUCCESS);
    508: }

    510: PetscErrorCode VecSetValues_Seq(Vec xin, PetscInt ni, const PetscInt ix[], const PetscScalar y[], InsertMode m)
    511: {
    512:   const PetscBool ignorenegidx = xin->stash.ignorenegidx;
    513:   PetscScalar    *xx;

    515:   PetscFunctionBegin;
    516:   // call to getarray (not e.g. getarraywrite() if m is INSERT_VALUES) is deliberate! If this
    517:   // is secretly a VECSEQCUDA it may have values currently on the device, in which case --
    518:   // unless we are replacing the entire array -- we need to copy them up
    519:   PetscCall(VecGetArray(xin, &xx));
    520:   for (PetscInt i = 0; i < ni; i++) {
    521:     if (ignorenegidx && (ix[i] < 0)) continue;
    522:     PetscScalar yv = y ? y[i] : 0.0;
    523:     if (PetscDefined(USE_DEBUG)) {
    524:       PetscCheck(ix[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Out of range index value %" PetscInt_FMT " cannot be negative", ix[i]);
    525:       PetscCheck(ix[i] < xin->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Out of range index value %" PetscInt_FMT ", should be less than %" PetscInt_FMT, ix[i], xin->map->n);
    526:     }
    527:     if (m == INSERT_VALUES) {
    528:       xx[ix[i]] = yv;
    529:     } else {
    530:       xx[ix[i]] += yv;
    531:     }
    532:   }
    533:   PetscCall(VecRestoreArray(xin, &xx));
    534:   PetscFunctionReturn(PETSC_SUCCESS);
    535: }

    537: PetscErrorCode VecSetValuesBlocked_Seq(Vec xin, PetscInt ni, const PetscInt ix[], const PetscScalar yin[], InsertMode m)
    538: {
    539:   PetscScalar *xx;
    540:   PetscInt     bs;

    542:   /* For optimization could treat bs = 2, 3, 4, 5 as special cases with loop unrolling */
    543:   PetscFunctionBegin;
    544:   PetscCall(VecGetBlockSize(xin, &bs));
    545:   PetscCall(VecGetArray(xin, &xx));
    546:   for (PetscInt i = 0; i < ni; ++i, yin += bs) {
    547:     const PetscInt start = bs * ix[i];

    549:     if (start < 0) continue;
    550:     PetscCheck(start < xin->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Out of range index value %" PetscInt_FMT ", should be less than %" PetscInt_FMT, start, xin->map->n);
    551:     for (PetscInt j = 0; j < bs; j++) {
    552:       if (m == INSERT_VALUES) {
    553:         xx[start + j] = yin ? yin[j] : 0.0;
    554:       } else {
    555:         xx[start + j] += yin ? yin[j] : 0.0;
    556:       }
    557:     }
    558:   }
    559:   PetscCall(VecRestoreArray(xin, &xx));
    560:   PetscFunctionReturn(PETSC_SUCCESS);
    561: }

    563: static PetscErrorCode VecResetPreallocationCOO_Seq(Vec x)
    564: {
    565:   Vec_Seq *vs = (Vec_Seq *)x->data;

    567:   PetscFunctionBegin;
    568:   if (vs) {
    569:     PetscCall(PetscFree(vs->jmap1)); /* Destroy old stuff */
    570:     PetscCall(PetscFree(vs->perm1));
    571:   }
    572:   PetscFunctionReturn(PETSC_SUCCESS);
    573: }

    575: PetscErrorCode VecSetPreallocationCOO_Seq(Vec x, PetscCount coo_n, const PetscInt coo_i[])
    576: {
    577:   PetscInt    m, *i;
    578:   PetscCount  k, nneg;
    579:   PetscCount *perm1, *jmap1;
    580:   Vec_Seq    *vs = (Vec_Seq *)x->data;

    582:   PetscFunctionBegin;
    583:   PetscCall(VecResetPreallocationCOO_Seq(x)); /* Destroy old stuff */
    584:   PetscCall(PetscMalloc1(coo_n, &i));
    585:   PetscCall(PetscArraycpy(i, coo_i, coo_n)); /* Make a copy since we'll modify it */
    586:   PetscCall(PetscMalloc1(coo_n, &perm1));
    587:   for (k = 0; k < coo_n; k++) perm1[k] = k;
    588:   PetscCall(PetscSortIntWithCountArray(coo_n, i, perm1));
    589:   for (k = 0; k < coo_n; k++) {
    590:     if (i[k] >= 0) break;
    591:   } /* Advance k to the first entry with a non-negative index */
    592:   nneg = k;

    594:   PetscCall(VecGetLocalSize(x, &m));
    595:   PetscCheck(!nneg || x->stash.ignorenegidx, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Found a negative index in VecSetPreallocateCOO() but VEC_IGNORE_NEGATIVE_INDICES was not set");
    596:   PetscCheck(!coo_n || i[coo_n - 1] < m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Found index (%" PetscInt_FMT ") greater than the size of the vector (%" PetscInt_FMT ") in VecSetPreallocateCOO()", i[coo_n - 1], m);

    598:   PetscCall(PetscCalloc1(m + 1, &jmap1));
    599:   for (; k < coo_n; k++) jmap1[i[k] + 1]++;         /* Count repeats of each entry */
    600:   for (k = 0; k < m; k++) jmap1[k + 1] += jmap1[k]; /* Transform jmap[] to CSR-like data structure */
    601:   PetscCall(PetscFree(i));

    603:   if (nneg) { /* Discard leading negative indices */
    604:     PetscCount *perm1_new;
    605:     PetscCall(PetscMalloc1(coo_n - nneg, &perm1_new));
    606:     PetscCall(PetscArraycpy(perm1_new, perm1 + nneg, coo_n - nneg));
    607:     PetscCall(PetscFree(perm1));
    608:     perm1 = perm1_new;
    609:   }

    611:   /* Record COO fields */
    612:   vs->coo_n = coo_n;
    613:   vs->tot1  = coo_n - nneg;
    614:   vs->jmap1 = jmap1; /* [m+1] */
    615:   vs->perm1 = perm1; /* [tot] */
    616:   PetscFunctionReturn(PETSC_SUCCESS);
    617: }

    619: PetscErrorCode VecSetValuesCOO_Seq(Vec x, const PetscScalar coo_v[], InsertMode imode)
    620: {
    621:   Vec_Seq          *vs    = (Vec_Seq *)x->data;
    622:   const PetscCount *perm1 = vs->perm1, *jmap1 = vs->jmap1;
    623:   PetscScalar      *xv;
    624:   PetscInt          m;

    626:   PetscFunctionBegin;
    627:   PetscCall(VecGetLocalSize(x, &m));
    628:   PetscCall(VecGetArray(x, &xv));
    629:   for (PetscInt i = 0; i < m; i++) {
    630:     PetscScalar sum = 0.0;
    631:     for (PetscCount j = jmap1[i]; j < jmap1[i + 1]; j++) sum += coo_v[perm1[j]];
    632:     xv[i] = (imode == INSERT_VALUES ? 0.0 : xv[i]) + sum;
    633:   }
    634:   PetscCall(VecRestoreArray(x, &xv));
    635:   PetscFunctionReturn(PETSC_SUCCESS);
    636: }

    638: PetscErrorCode VecDestroy_Seq(Vec v)
    639: {
    640:   Vec_Seq *vs = (Vec_Seq *)v->data;

    642:   PetscFunctionBegin;
    643:   PetscCall(PetscLogObjectState((PetscObject)v, "Length=%" PetscInt_FMT, v->map->n));
    644:   if (vs) PetscCall(PetscShmgetDeallocateArray((void **)&vs->array_allocated));
    645:   PetscCall(VecResetPreallocationCOO_Seq(v));
    646:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscMatlabEnginePut_C", NULL));
    647:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscMatlabEngineGet_C", NULL));
    648:   PetscCall(PetscFree(v->data));
    649:   PetscFunctionReturn(PETSC_SUCCESS);
    650: }

    652: PetscErrorCode VecSetOption_Seq(Vec v, VecOption op, PetscBool flag)
    653: {
    654:   PetscFunctionBegin;
    655:   if (op == VEC_IGNORE_NEGATIVE_INDICES) v->stash.ignorenegidx = flag;
    656:   PetscFunctionReturn(PETSC_SUCCESS);
    657: }

    659: // duplicate w to v. v is half-baked, potentially already with arrays allocated.
    660: static PetscErrorCode VecDuplicate_Seq_Private(Vec w, Vec v)
    661: {
    662:   PetscFunctionBegin;
    663:   PetscCall(VecSetType(v, ((PetscObject)w)->type_name));
    664:   PetscCall(PetscObjectListDuplicate(((PetscObject)w)->olist, &((PetscObject)v)->olist));
    665:   PetscCall(PetscFunctionListDuplicate(((PetscObject)w)->qlist, &((PetscObject)v)->qlist));

    667:   // Vec ops are not necessarily fully set by VecSetType(), e.g., see DMCreateGlobalVector_DA, so we copy w's to v
    668:   v->ops[0] = w->ops[0];
    669: #if defined(PETSC_HAVE_DEVICE)
    670:   v->boundtocpu        = w->boundtocpu;
    671:   v->bindingpropagates = w->bindingpropagates;
    672: #endif
    673:   PetscFunctionReturn(PETSC_SUCCESS);
    674: }

    676: PetscErrorCode VecDuplicate_Seq(Vec win, Vec *V)
    677: {
    678:   PetscFunctionBegin;
    679:   PetscCall(VecCreateWithLayout_Private(win->map, V));
    680:   PetscCall(VecDuplicate_Seq_Private(win, *V));
    681:   PetscFunctionReturn(PETSC_SUCCESS);
    682: }

    684: PetscErrorCode VecReplaceArray_Default_GEMV_Error(Vec v, const PetscScalar *a)
    685: {
    686:   PetscFunctionBegin;
    687:   PetscCheck(PETSC_FALSE, PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "VecReplaceArray() is not supported on the first Vec obtained from VecDuplicateVecs(). \
    688: You could either 1) use -vec_mdot_use_gemv 0 -vec_maxpy_use_gemv 0 to turn off an optimization to allow your current code to work or 2) use VecDuplicate() to duplicate the vector.");
    689:   (void)a;
    690:   PetscFunctionReturn(PETSC_SUCCESS);
    691: }

    693: static PetscErrorCode VecDuplicateVecs_Seq_GEMV(Vec w, PetscInt m, Vec *V[])
    694: {
    695:   PetscScalar *array;
    696:   PetscInt64   lda; // use 64-bit as we will do "m * lda"

    698:   PetscFunctionBegin;
    699:   PetscCall(PetscMalloc1(m, V));
    700:   VecGetLocalSizeAligned(w, 64, &lda); // get in lda the 64-bytes aligned local size
    701:   PetscCall(PetscCalloc1(m * lda, &array));
    702:   for (PetscInt i = 0; i < m; i++) {
    703:     Vec v;
    704:     PetscCall(VecCreateSeqWithLayoutAndArray_Private(w->map, PetscSafePointerPlusOffset(array, i * lda), &v));
    705:     PetscCall(VecDuplicate_Seq_Private(w, v));
    706:     (*V)[i] = v;
    707:   }
    708:   // so when the first vector is destroyed it will destroy the array
    709:   if (m) ((Vec_Seq *)(*V)[0]->data)->array_allocated = array;
    710:   // disable replacearray of the first vector, as freeing its memory also frees others in the group.
    711:   // But replacearray of others is ok, as they don't own their array.
    712:   if (m > 1) (*V)[0]->ops->replacearray = VecReplaceArray_Default_GEMV_Error;
    713:   PetscFunctionReturn(PETSC_SUCCESS);
    714: }

    716: static struct _VecOps DvOps = {
    717:   PetscDesignatedInitializer(duplicate, VecDuplicate_Seq), /* 1 */
    718:   PetscDesignatedInitializer(duplicatevecs, VecDuplicateVecs_Default),
    719:   PetscDesignatedInitializer(destroyvecs, VecDestroyVecs_Default),
    720:   PetscDesignatedInitializer(dot, VecDot_Seq),
    721:   PetscDesignatedInitializer(mdot, VecMDot_Seq),
    722:   PetscDesignatedInitializer(norm, VecNorm_Seq),
    723:   PetscDesignatedInitializer(tdot, VecTDot_Seq),
    724:   PetscDesignatedInitializer(mtdot, VecMTDot_Seq),
    725:   PetscDesignatedInitializer(scale, VecScale_Seq),
    726:   PetscDesignatedInitializer(copy, VecCopy_Seq), /* 10 */
    727:   PetscDesignatedInitializer(set, VecSet_Seq),
    728:   PetscDesignatedInitializer(swap, VecSwap_Seq),
    729:   PetscDesignatedInitializer(axpy, VecAXPY_Seq),
    730:   PetscDesignatedInitializer(axpby, VecAXPBY_Seq),
    731:   PetscDesignatedInitializer(maxpy, VecMAXPY_Seq),
    732:   PetscDesignatedInitializer(aypx, VecAYPX_Seq),
    733:   PetscDesignatedInitializer(waxpy, VecWAXPY_Seq),
    734:   PetscDesignatedInitializer(axpbypcz, VecAXPBYPCZ_Seq),
    735:   PetscDesignatedInitializer(pointwisemult, VecPointwiseMult_Seq),
    736:   PetscDesignatedInitializer(pointwisedivide, VecPointwiseDivide_Seq),
    737:   PetscDesignatedInitializer(setvalues, VecSetValues_Seq), /* 20 */
    738:   PetscDesignatedInitializer(assemblybegin, NULL),
    739:   PetscDesignatedInitializer(assemblyend, NULL),
    740:   PetscDesignatedInitializer(getarray, NULL),
    741:   PetscDesignatedInitializer(getsize, VecGetSize_Seq),
    742:   PetscDesignatedInitializer(getlocalsize, VecGetSize_Seq),
    743:   PetscDesignatedInitializer(restorearray, NULL),
    744:   PetscDesignatedInitializer(max, VecMax_Seq),
    745:   PetscDesignatedInitializer(min, VecMin_Seq),
    746:   PetscDesignatedInitializer(setrandom, VecSetRandom_Seq),
    747:   PetscDesignatedInitializer(setoption, VecSetOption_Seq), /* 30 */
    748:   PetscDesignatedInitializer(setvaluesblocked, VecSetValuesBlocked_Seq),
    749:   PetscDesignatedInitializer(destroy, VecDestroy_Seq),
    750:   PetscDesignatedInitializer(view, VecView_Seq),
    751:   PetscDesignatedInitializer(placearray, VecPlaceArray_Seq),
    752:   PetscDesignatedInitializer(replacearray, VecReplaceArray_Seq),
    753:   PetscDesignatedInitializer(dot_local, VecDot_Seq),
    754:   PetscDesignatedInitializer(tdot_local, VecTDot_Seq),
    755:   PetscDesignatedInitializer(norm_local, VecNorm_Seq),
    756:   PetscDesignatedInitializer(mdot_local, VecMDot_Seq),
    757:   PetscDesignatedInitializer(mtdot_local, VecMTDot_Seq), /* 40 */
    758:   PetscDesignatedInitializer(load, VecLoad_Default),
    759:   PetscDesignatedInitializer(reciprocal, VecReciprocal_Default),
    760:   PetscDesignatedInitializer(conjugate, VecConjugate_Seq),
    761:   PetscDesignatedInitializer(setlocaltoglobalmapping, NULL),
    762:   PetscDesignatedInitializer(getlocaltoglobalmapping, NULL),
    763:   PetscDesignatedInitializer(resetarray, VecResetArray_Seq),
    764:   PetscDesignatedInitializer(setfromoptions, NULL),
    765:   PetscDesignatedInitializer(maxpointwisedivide, VecMaxPointwiseDivide_Seq),
    766:   PetscDesignatedInitializer(pointwisemax, VecPointwiseMax_Seq),
    767:   PetscDesignatedInitializer(pointwisemaxabs, VecPointwiseMaxAbs_Seq),
    768:   PetscDesignatedInitializer(pointwisemin, VecPointwiseMin_Seq),
    769:   PetscDesignatedInitializer(getvalues, VecGetValues_Seq),
    770:   PetscDesignatedInitializer(sqrt, NULL),
    771:   PetscDesignatedInitializer(abs, NULL),
    772:   PetscDesignatedInitializer(exp, NULL),
    773:   PetscDesignatedInitializer(log, NULL),
    774:   PetscDesignatedInitializer(shift, NULL),
    775:   PetscDesignatedInitializer(create, NULL),
    776:   PetscDesignatedInitializer(stridegather, VecStrideGather_Default),
    777:   PetscDesignatedInitializer(stridescatter, VecStrideScatter_Default),
    778:   PetscDesignatedInitializer(dotnorm2, NULL),
    779:   PetscDesignatedInitializer(getsubvector, NULL),
    780:   PetscDesignatedInitializer(restoresubvector, NULL),
    781:   PetscDesignatedInitializer(getarrayread, NULL),
    782:   PetscDesignatedInitializer(restorearrayread, NULL),
    783:   PetscDesignatedInitializer(stridesubsetgather, VecStrideSubSetGather_Default),
    784:   PetscDesignatedInitializer(stridesubsetscatter, VecStrideSubSetScatter_Default),
    785:   PetscDesignatedInitializer(viewnative, VecView_Seq),
    786:   PetscDesignatedInitializer(loadnative, NULL),
    787:   PetscDesignatedInitializer(createlocalvector, NULL),
    788:   PetscDesignatedInitializer(getlocalvector, NULL),
    789:   PetscDesignatedInitializer(restorelocalvector, NULL),
    790:   PetscDesignatedInitializer(getlocalvectorread, NULL),
    791:   PetscDesignatedInitializer(restorelocalvectorread, NULL),
    792:   PetscDesignatedInitializer(bindtocpu, NULL),
    793:   PetscDesignatedInitializer(getarraywrite, NULL),
    794:   PetscDesignatedInitializer(restorearraywrite, NULL),
    795:   PetscDesignatedInitializer(getarrayandmemtype, NULL),
    796:   PetscDesignatedInitializer(restorearrayandmemtype, NULL),
    797:   PetscDesignatedInitializer(getarrayreadandmemtype, NULL),
    798:   PetscDesignatedInitializer(restorearrayreadandmemtype, NULL),
    799:   PetscDesignatedInitializer(getarraywriteandmemtype, NULL),
    800:   PetscDesignatedInitializer(restorearraywriteandmemtype, NULL),
    801:   PetscDesignatedInitializer(concatenate, NULL),
    802:   PetscDesignatedInitializer(sum, NULL),
    803:   PetscDesignatedInitializer(setpreallocationcoo, VecSetPreallocationCOO_Seq),
    804:   PetscDesignatedInitializer(setvaluescoo, VecSetValuesCOO_Seq),
    805:   PetscDesignatedInitializer(errorwnorm, NULL),
    806:   PetscDesignatedInitializer(maxpby, NULL),
    807: };

    809: /*
    810:   Create a VECSEQ with the given layout and array

    812:   Input Parameter:
    813: + map   - the layout
    814: - array - the array on host

    816:   Output Parameter:
    817: . V  - The vector object
    818: */
    819: PetscErrorCode VecCreateSeqWithLayoutAndArray_Private(PetscLayout map, const PetscScalar array[], Vec *V)
    820: {
    821:   PetscMPIInt size;

    823:   PetscFunctionBegin;
    824:   PetscCall(VecCreateWithLayout_Private(map, V));
    825:   PetscCallMPI(MPI_Comm_size(map->comm, &size));
    826:   PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQ on more than one process");
    827:   PetscCall(VecCreate_Seq_Private(*V, array));
    828:   PetscFunctionReturn(PETSC_SUCCESS);
    829: }

    831: /*
    832:       This is called by VecCreate_Seq() (i.e. VecCreateSeq()) and VecCreateSeqWithArray()
    833: */
    834: PetscErrorCode VecCreate_Seq_Private(Vec v, const PetscScalar array[])
    835: {
    836:   Vec_Seq  *s;
    837:   PetscBool mdot_use_gemv  = PETSC_TRUE;
    838:   PetscBool maxpy_use_gemv = PETSC_FALSE; // default is false as we saw bad performance with vendors' GEMV with tall skinny matrices.

    840:   PetscFunctionBegin;
    841:   PetscCall(PetscNew(&s));
    842:   v->ops[0] = DvOps;

    844:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_mdot_use_gemv", &mdot_use_gemv, NULL));
    845:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-vec_maxpy_use_gemv", &maxpy_use_gemv, NULL));

    847:   // allocate multiple vectors together
    848:   if (mdot_use_gemv || maxpy_use_gemv) v->ops[0].duplicatevecs = VecDuplicateVecs_Seq_GEMV;

    850:   if (mdot_use_gemv) {
    851:     v->ops[0].mdot        = VecMDot_Seq_GEMV;
    852:     v->ops[0].mdot_local  = VecMDot_Seq_GEMV;
    853:     v->ops[0].mtdot       = VecMTDot_Seq_GEMV;
    854:     v->ops[0].mtdot_local = VecMTDot_Seq_GEMV;
    855:   }
    856:   if (maxpy_use_gemv) v->ops[0].maxpy = VecMAXPY_Seq_GEMV;

    858:   v->data            = (void *)s;
    859:   v->petscnative     = PETSC_TRUE;
    860:   s->array           = (PetscScalar *)array;
    861:   s->array_allocated = NULL;
    862:   if (array) v->offloadmask = PETSC_OFFLOAD_CPU;

    864:   PetscCall(PetscLayoutSetUp(v->map));
    865:   PetscCall(PetscObjectChangeTypeName((PetscObject)v, VECSEQ));
    866: #if defined(PETSC_HAVE_MATLAB)
    867:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscMatlabEnginePut_C", VecMatlabEnginePut_Default));
    868:   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscMatlabEngineGet_C", VecMatlabEngineGet_Default));
    869: #endif
    870:   PetscFunctionReturn(PETSC_SUCCESS);
    871: }

    873: /*@
    874:   VecCreateSeqWithArray - Creates a standard,sequential array-style vector,
    875:   where the user provides the array space to store the vector values.

    877:   Collective

    879:   Input Parameters:
    880: + comm  - the communicator, should be `PETSC_COMM_SELF`
    881: . bs    - the block size
    882: . n     - the vector length
    883: - array - memory where the vector elements are to be stored.

    885:   Output Parameter:
    886: . V - the vector

    888:   Level: intermediate

    890:   Notes:
    891:   Use `VecDuplicate()` or `VecDuplicateVecs(`) to form additional vectors of the
    892:   same type as an existing vector.

    894:   If the user-provided array is` NULL`, then `VecPlaceArray()` can be used
    895:   at a later stage to SET the array for storing the vector values.

    897:   PETSc does NOT free the array when the vector is destroyed via `VecDestroy()`.
    898:   The user should not free the array until the vector is destroyed.

    900: .seealso: `VecCreateMPIWithArray()`, `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`,
    901:           `VecCreateGhost()`, `VecCreateSeq()`, `VecPlaceArray()`
    902: @*/
    903: PetscErrorCode VecCreateSeqWithArray(MPI_Comm comm, PetscInt bs, PetscInt n, const PetscScalar array[], Vec *V)
    904: {
    905:   PetscMPIInt size;

    907:   PetscFunctionBegin;
    908:   PetscCall(VecCreate(comm, V));
    909:   PetscCall(VecSetSizes(*V, n, n));
    910:   PetscCall(VecSetBlockSize(*V, bs));
    911:   PetscCallMPI(MPI_Comm_size(comm, &size));
    912:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQ on more than one process");
    913:   PetscCall(VecCreate_Seq_Private(*V, array));
    914:   PetscFunctionReturn(PETSC_SUCCESS);
    915: }