Actual source code: vecviennacl.cxx

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

  5: #include <petsc/private/vecimpl.h>
  6: #include <../src/vec/vec/impls/dvecimpl.h>
  7: #include <../src/vec/vec/impls/seq/seqviennacl/viennaclvecimpl.h>

  9: #include "viennacl/linalg/inner_prod.hpp"
 10: #include "viennacl/linalg/norm_1.hpp"
 11: #include "viennacl/linalg/norm_2.hpp"
 12: #include "viennacl/linalg/norm_inf.hpp"

 14: #if defined(VIENNACL_WITH_OPENCL)
 15:   #include "viennacl/ocl/backend.hpp"
 16: #endif

 18: PETSC_EXTERN PetscErrorCode VecViennaCLGetArray(Vec v, ViennaCLVector **a)
 19: {
 20:   PetscFunctionBegin;
 21:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 22:   *a = 0;
 23:   PetscCall(VecViennaCLCopyToGPU(v));
 24:   *a = ((Vec_ViennaCL *)v->spptr)->GPUarray;
 25:   ViennaCLWaitForGPU();
 26:   PetscFunctionReturn(PETSC_SUCCESS);
 27: }

 29: PETSC_EXTERN PetscErrorCode VecViennaCLRestoreArray(Vec v, ViennaCLVector **a)
 30: {
 31:   PetscFunctionBegin;
 32:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 33:   v->offloadmask = PETSC_OFFLOAD_GPU;

 35:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
 36:   PetscFunctionReturn(PETSC_SUCCESS);
 37: }

 39: PETSC_EXTERN PetscErrorCode VecViennaCLGetArrayRead(Vec v, const ViennaCLVector **a)
 40: {
 41:   PetscFunctionBegin;
 42:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 43:   *a = 0;
 44:   PetscCall(VecViennaCLCopyToGPU(v));
 45:   *a = ((Vec_ViennaCL *)v->spptr)->GPUarray;
 46:   ViennaCLWaitForGPU();
 47:   PetscFunctionReturn(PETSC_SUCCESS);
 48: }

 50: PETSC_EXTERN PetscErrorCode VecViennaCLRestoreArrayRead(Vec v, const ViennaCLVector **a)
 51: {
 52:   PetscFunctionBegin;
 53:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 54:   PetscFunctionReturn(PETSC_SUCCESS);
 55: }

 57: PETSC_EXTERN PetscErrorCode VecViennaCLGetArrayWrite(Vec v, ViennaCLVector **a)
 58: {
 59:   PetscFunctionBegin;
 60:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 61:   *a = 0;
 62:   PetscCall(VecViennaCLAllocateCheck(v));
 63:   *a = ((Vec_ViennaCL *)v->spptr)->GPUarray;
 64:   ViennaCLWaitForGPU();
 65:   PetscFunctionReturn(PETSC_SUCCESS);
 66: }

 68: PETSC_EXTERN PetscErrorCode VecViennaCLRestoreArrayWrite(Vec v, ViennaCLVector **a)
 69: {
 70:   PetscFunctionBegin;
 71:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
 72:   v->offloadmask = PETSC_OFFLOAD_GPU;

 74:   PetscCall(PetscObjectStateIncrease((PetscObject)v));
 75:   PetscFunctionReturn(PETSC_SUCCESS);
 76: }

 78: PETSC_EXTERN PetscErrorCode PetscViennaCLInit()
 79: {
 80:   char      string[20];
 81:   PetscBool flg, flg_cuda, flg_opencl, flg_openmp;

 83:   PetscFunctionBegin;
 84:   /* ViennaCL backend selection: CUDA, OpenCL, or OpenMP */
 85:   PetscCall(PetscOptionsGetString(NULL, NULL, "-viennacl_backend", string, sizeof(string), &flg));
 86:   if (flg) {
 87:     try {
 88:       PetscCall(PetscStrcasecmp(string, "cuda", &flg_cuda));
 89:       PetscCall(PetscStrcasecmp(string, "opencl", &flg_opencl));
 90:       PetscCall(PetscStrcasecmp(string, "openmp", &flg_openmp));

 92:       /* A default (sequential) CPU backend is always available - even if OpenMP is not enabled. */
 93:       if (flg_openmp) viennacl::backend::default_memory_type(viennacl::MAIN_MEMORY);
 94: #if PetscDefined(HAVE_CUDA)
 95:       else if (flg_cuda) viennacl::backend::default_memory_type(viennacl::CUDA_MEMORY);
 96: #endif
 97: #if PetscDefined(HAVE_OPENCL)
 98:       else if (flg_opencl) viennacl::backend::default_memory_type(viennacl::OPENCL_MEMORY);
 99: #endif
100:       else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: Backend not recognized or available: %s.\n Pass -viennacl_view to see available backends for ViennaCL.", string);
101:     } catch (std::exception const &ex) {
102:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
103:     }
104:   }

106: #if PetscDefined(HAVE_OPENCL)
107:   /* ViennaCL OpenCL device type configuration */
108:   PetscCall(PetscOptionsGetString(NULL, NULL, "-viennacl_opencl_device_type", string, sizeof(string), &flg));
109:   if (flg) {
110:     try {
111:       PetscCall(PetscStrcasecmp(string, "cpu", &flg));
112:       if (flg) viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_CPU);

114:       PetscCall(PetscStrcasecmp(string, "gpu", &flg));
115:       if (flg) viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_GPU);

117:       PetscCall(PetscStrcasecmp(string, "accelerator", &flg));
118:       if (flg) viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_ACCELERATOR);
119:     } catch (std::exception const &ex) {
120:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
121:     }
122:   }
123: #endif

125:   /* Print available backends */
126:   PetscCall(PetscOptionsHasName(NULL, NULL, "-viennacl_view", &flg));
127:   if (flg) {
128:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "ViennaCL backends available: "));
129: #if PetscDefined(HAVE_CUDA)
130:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "CUDA, "));
131: #endif
132: #if PetscDefined(HAVE_OPENCL)
133:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenCL, "));
134: #endif
135: #if PetscDefined(HAVE_OPENMP)
136:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP "));
137: #else
138:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP (1 thread) "));
139: #endif
140:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n"));

142:     /* Print selected backends */
143:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "ViennaCL backend  selected: "));
144: #if PetscDefined(HAVE_CUDA)
145:     if (viennacl::backend::default_memory_type() == viennacl::CUDA_MEMORY) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "CUDA "));
146: #endif
147: #if PetscDefined(HAVE_OPENCL)
148:     if (viennacl::backend::default_memory_type() == viennacl::OPENCL_MEMORY) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenCL "));
149: #endif
150: #if PetscDefined(HAVE_OPENMP)
151:     if (viennacl::backend::default_memory_type() == viennacl::MAIN_MEMORY) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP "));
152: #else
153:     if (viennacl::backend::default_memory_type() == viennacl::MAIN_MEMORY) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP (sequential - consider reconfiguration: --with-openmp=1) "));
154: #endif
155:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n"));
156:   }
157:   PetscFunctionReturn(PETSC_SUCCESS);
158: }

160: /*
161:     Allocates space for the vector array on the Host if it does not exist.
162:     Does NOT change the PetscViennaCLFlag for the vector
163:     Does NOT zero the ViennaCL array
164:  */
165: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v)
166: {
167:   PetscScalar *array;
168:   Vec_Seq     *s;
169:   PetscInt     n = v->map->n;

171:   PetscFunctionBegin;
172:   s = (Vec_Seq *)v->data;
173:   PetscCall(VecViennaCLAllocateCheck(v));
174:   if (s->array == 0) {
175:     PetscCall(PetscMalloc1(n, &array));
176:     s->array           = array;
177:     s->array_allocated = array;
178:   }
179:   PetscFunctionReturn(PETSC_SUCCESS);
180: }

182: /*
183:     Allocates space for the vector array on the GPU if it does not exist.
184:     Does NOT change the PetscViennaCLFlag for the vector
185:     Does NOT zero the ViennaCL array

187:  */
188: PetscErrorCode VecViennaCLAllocateCheck(Vec v)
189: {
190:   PetscFunctionBegin;
191:   if (!v->spptr) {
192:     try {
193:       v->spptr                                       = new Vec_ViennaCL;
194:       ((Vec_ViennaCL *)v->spptr)->GPUarray_allocated = new ViennaCLVector((PetscBLASInt)v->map->n);
195:       ((Vec_ViennaCL *)v->spptr)->GPUarray           = ((Vec_ViennaCL *)v->spptr)->GPUarray_allocated;

197:     } catch (std::exception const &ex) {
198:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
199:     }
200:   }
201:   PetscFunctionReturn(PETSC_SUCCESS);
202: }

204: /* Copies a vector from the CPU to the GPU unless we already have an up-to-date copy on the GPU */
205: PetscErrorCode VecViennaCLCopyToGPU(Vec v)
206: {
207:   PetscFunctionBegin;
208:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
209:   PetscCall(VecViennaCLAllocateCheck(v));
210:   if (v->map->n > 0) {
211:     if (v->offloadmask == PETSC_OFFLOAD_CPU) {
212:       PetscCall(PetscLogEventBegin(VEC_ViennaCLCopyToGPU, v, 0, 0, 0));
213:       try {
214:         ViennaCLVector *vec = ((Vec_ViennaCL *)v->spptr)->GPUarray;
215:         viennacl::fast_copy(*(PetscScalar **)v->data, *(PetscScalar **)v->data + v->map->n, vec->begin());
216:         ViennaCLWaitForGPU();
217:       } catch (std::exception const &ex) {
218:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
219:       }
220:       PetscCall(PetscLogCpuToGpu(v->map->n * sizeof(PetscScalar)));
221:       PetscCall(PetscLogEventEnd(VEC_ViennaCLCopyToGPU, v, 0, 0, 0));
222:       v->offloadmask = PETSC_OFFLOAD_BOTH;
223:     }
224:   }
225:   PetscFunctionReturn(PETSC_SUCCESS);
226: }

228: /*
229:      VecViennaCLCopyFromGPU - Copies a vector from the GPU to the CPU unless we already have an up-to-date copy on the CPU
230: */
231: PetscErrorCode VecViennaCLCopyFromGPU(Vec v)
232: {
233:   PetscFunctionBegin;
234:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
235:   PetscCall(VecViennaCLAllocateCheckHost(v));
236:   if (v->offloadmask == PETSC_OFFLOAD_GPU) {
237:     PetscCall(PetscLogEventBegin(VEC_ViennaCLCopyFromGPU, v, 0, 0, 0));
238:     try {
239:       ViennaCLVector *vec = ((Vec_ViennaCL *)v->spptr)->GPUarray;
240:       viennacl::fast_copy(vec->begin(), vec->end(), *(PetscScalar **)v->data);
241:       ViennaCLWaitForGPU();
242:     } catch (std::exception const &ex) {
243:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
244:     }
245:     PetscCall(PetscLogGpuToCpu(v->map->n * sizeof(PetscScalar)));
246:     PetscCall(PetscLogEventEnd(VEC_ViennaCLCopyFromGPU, v, 0, 0, 0));
247:     v->offloadmask = PETSC_OFFLOAD_BOTH;
248:   }
249:   PetscFunctionReturn(PETSC_SUCCESS);
250: }

252: /* Copy on CPU */
253: static PetscErrorCode VecCopy_SeqViennaCL_Private(Vec xin, Vec yin)
254: {
255:   PetscScalar       *ya;
256:   const PetscScalar *xa;

258:   PetscFunctionBegin;
259:   PetscCall(VecViennaCLAllocateCheckHost(xin));
260:   PetscCall(VecViennaCLAllocateCheckHost(yin));
261:   if (xin != yin) {
262:     PetscCall(VecGetArrayRead(xin, &xa));
263:     PetscCall(VecGetArray(yin, &ya));
264:     PetscCall(PetscArraycpy(ya, xa, xin->map->n));
265:     PetscCall(VecRestoreArrayRead(xin, &xa));
266:     PetscCall(VecRestoreArray(yin, &ya));
267:   }
268:   PetscFunctionReturn(PETSC_SUCCESS);
269: }

271: static PetscErrorCode VecSetRandom_SeqViennaCL_Private(Vec xin, PetscRandom r)
272: {
273:   PetscInt     n = xin->map->n, i;
274:   PetscScalar *xx;

276:   PetscFunctionBegin;
277:   PetscCall(VecGetArray(xin, &xx));
278:   for (i = 0; i < n; i++) PetscCall(PetscRandomGetValue(r, &xx[i]));
279:   PetscCall(VecRestoreArray(xin, &xx));
280:   PetscFunctionReturn(PETSC_SUCCESS);
281: }

283: static PetscErrorCode VecDestroy_SeqViennaCL_Private(Vec v)
284: {
285:   Vec_Seq *vs = (Vec_Seq *)v->data;

287:   PetscFunctionBegin;
288:   PetscCall(PetscObjectSAWsViewOff(v));
289:   PetscCall(PetscLogObjectState((PetscObject)v, "Length=%" PetscInt_FMT, v->map->n));
290:   PetscCall(PetscFree(vs->array_allocated));
291:   PetscCall(PetscFree(vs));
292:   PetscFunctionReturn(PETSC_SUCCESS);
293: }

295: static PetscErrorCode VecResetArray_SeqViennaCL_Private(Vec vin)
296: {
297:   Vec_Seq *v = (Vec_Seq *)vin->data;

299:   PetscFunctionBegin;
300:   v->array         = v->unplacedarray;
301:   v->unplacedarray = 0;
302:   PetscFunctionReturn(PETSC_SUCCESS);
303: }

305: /*MC
306:    VECSEQVIENNACL - VECSEQVIENNACL = "seqviennacl" - The basic sequential vector, modified to use ViennaCL

308:    Options Database Keys:
309: . -vec_type seqviennacl - sets the vector type to VECSEQVIENNACL during a call to VecSetFromOptions()

311:   Level: beginner

313: .seealso: `VecCreate()`, `VecSetType()`, `VecSetFromOptions()`, `VecCreateSeqWithArray()`, `VECMPI`, `VecType`, `VecCreateMPI()`, `VecCreateSeq()`
314: M*/

316: PetscErrorCode VecAYPX_SeqViennaCL(Vec yin, PetscScalar alpha, Vec xin)
317: {
318:   const ViennaCLVector *xgpu;
319:   ViennaCLVector       *ygpu;

321:   PetscFunctionBegin;
322:   PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
323:   PetscCall(VecViennaCLGetArray(yin, &ygpu));
324:   PetscCall(PetscLogGpuTimeBegin());
325:   try {
326:     if (alpha != 0.0 && xin->map->n > 0) {
327:       *ygpu = *xgpu + alpha * *ygpu;
328:       PetscCall(PetscLogGpuFlops(2.0 * yin->map->n));
329:     } else {
330:       *ygpu = *xgpu;
331:     }
332:     ViennaCLWaitForGPU();
333:   } catch (std::exception const &ex) {
334:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
335:   }
336:   PetscCall(PetscLogGpuTimeEnd());
337:   PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
338:   PetscCall(VecViennaCLRestoreArray(yin, &ygpu));
339:   PetscFunctionReturn(PETSC_SUCCESS);
340: }

342: PetscErrorCode VecAXPY_SeqViennaCL(Vec yin, PetscScalar alpha, Vec xin)
343: {
344:   const ViennaCLVector *xgpu;
345:   ViennaCLVector       *ygpu;

347:   PetscFunctionBegin;
348:   if (alpha != 0.0 && xin->map->n > 0) {
349:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
350:     PetscCall(VecViennaCLGetArray(yin, &ygpu));
351:     PetscCall(PetscLogGpuTimeBegin());
352:     try {
353:       *ygpu += alpha * *xgpu;
354:       ViennaCLWaitForGPU();
355:     } catch (std::exception const &ex) {
356:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
357:     }
358:     PetscCall(PetscLogGpuTimeEnd());
359:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
360:     PetscCall(VecViennaCLRestoreArray(yin, &ygpu));
361:     PetscCall(PetscLogGpuFlops(2.0 * yin->map->n));
362:   }
363:   PetscFunctionReturn(PETSC_SUCCESS);
364: }

366: PetscErrorCode VecPointwiseDivide_SeqViennaCL(Vec win, Vec xin, Vec yin)
367: {
368:   const ViennaCLVector *xgpu, *ygpu;
369:   ViennaCLVector       *wgpu;

371:   PetscFunctionBegin;
372:   if (xin->map->n > 0) {
373:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
374:     PetscCall(VecViennaCLGetArrayRead(yin, &ygpu));
375:     PetscCall(VecViennaCLGetArrayWrite(win, &wgpu));
376:     PetscCall(PetscLogGpuTimeBegin());
377:     try {
378:       *wgpu = viennacl::linalg::element_div(*xgpu, *ygpu);
379:       ViennaCLWaitForGPU();
380:     } catch (std::exception const &ex) {
381:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
382:     }
383:     PetscCall(PetscLogGpuTimeEnd());
384:     PetscCall(PetscLogGpuFlops(win->map->n));
385:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
386:     PetscCall(VecViennaCLRestoreArrayRead(yin, &ygpu));
387:     PetscCall(VecViennaCLRestoreArrayWrite(win, &wgpu));
388:   }
389:   PetscFunctionReturn(PETSC_SUCCESS);
390: }

392: PetscErrorCode VecWAXPY_SeqViennaCL(Vec win, PetscScalar alpha, Vec xin, Vec yin)
393: {
394:   const ViennaCLVector *xgpu, *ygpu;
395:   ViennaCLVector       *wgpu;

397:   PetscFunctionBegin;
398:   if (alpha == 0.0 && xin->map->n > 0) PetscCall(VecCopy_SeqViennaCL(yin, win));
399:   else {
400:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
401:     PetscCall(VecViennaCLGetArrayRead(yin, &ygpu));
402:     PetscCall(VecViennaCLGetArrayWrite(win, &wgpu));
403:     PetscCall(PetscLogGpuTimeBegin());
404:     if (alpha == 1.0) {
405:       try {
406:         *wgpu = *ygpu + *xgpu;
407:       } catch (std::exception const &ex) {
408:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
409:       }
410:       PetscCall(PetscLogGpuFlops(win->map->n));
411:     } else if (alpha == -1.0) {
412:       try {
413:         *wgpu = *ygpu - *xgpu;
414:       } catch (std::exception const &ex) {
415:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
416:       }
417:       PetscCall(PetscLogGpuFlops(win->map->n));
418:     } else {
419:       try {
420:         *wgpu = *ygpu + alpha * *xgpu;
421:       } catch (std::exception const &ex) {
422:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
423:       }
424:       PetscCall(PetscLogGpuFlops(2 * win->map->n));
425:     }
426:     ViennaCLWaitForGPU();
427:     PetscCall(PetscLogGpuTimeEnd());
428:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
429:     PetscCall(VecViennaCLRestoreArrayRead(yin, &ygpu));
430:     PetscCall(VecViennaCLRestoreArrayWrite(win, &wgpu));
431:   }
432:   PetscFunctionReturn(PETSC_SUCCESS);
433: }

435: /*
436:  * Operation x = x + sum_i alpha_i * y_i for vectors x, y_i and scalars alpha_i
437:  *
438:  * ViennaCL supports a fast evaluation of x += alpha * y and x += alpha * y + beta * z,
439:  * hence there is an iterated application of these until the final result is obtained
440:  */
441: PetscErrorCode VecMAXPY_SeqViennaCL(Vec xin, PetscInt nv, const PetscScalar *alpha, Vec *y)
442: {
443:   PetscInt j;

445:   PetscFunctionBegin;
446:   for (j = 0; j < nv; ++j) {
447:     if (j + 1 < nv) {
448:       PetscCall(VecAXPBYPCZ_SeqViennaCL(xin, alpha[j], alpha[j + 1], 1.0, y[j], y[j + 1]));
449:       ++j;
450:     } else {
451:       PetscCall(VecAXPY_SeqViennaCL(xin, alpha[j], y[j]));
452:     }
453:   }
454:   ViennaCLWaitForGPU();
455:   PetscFunctionReturn(PETSC_SUCCESS);
456: }

458: PetscErrorCode VecDot_SeqViennaCL(Vec xin, Vec yin, PetscScalar *z)
459: {
460:   const ViennaCLVector *xgpu, *ygpu;

462:   PetscFunctionBegin;
463:   if (xin->map->n > 0) {
464:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
465:     PetscCall(VecViennaCLGetArrayRead(yin, &ygpu));
466:     PetscCall(PetscLogGpuTimeBegin());
467:     try {
468:       *z = viennacl::linalg::inner_prod(*xgpu, *ygpu);
469:     } catch (std::exception const &ex) {
470:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
471:     }
472:     ViennaCLWaitForGPU();
473:     PetscCall(PetscLogGpuTimeEnd());
474:     if (xin->map->n > 0) PetscCall(PetscLogGpuFlops(2.0 * xin->map->n - 1));
475:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
476:     PetscCall(VecViennaCLRestoreArrayRead(yin, &ygpu));
477:   } else *z = 0.0;
478:   PetscFunctionReturn(PETSC_SUCCESS);
479: }

481: /*
482:  * Operation z[j] = dot(x, y[j])
483:  *
484:  * We use an iterated application of dot() for each j. For small ranges of j this is still faster than an allocation of extra memory in order to use gemv().
485:  */
486: PetscErrorCode VecMDot_SeqViennaCL(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
487: {
488:   PetscInt                                                n = xin->map->n, i;
489:   const ViennaCLVector                                   *xgpu, *ygpu;
490:   Vec                                                    *yyin = (Vec *)yin;
491:   std::vector<viennacl::vector_base<PetscScalar> const *> ygpu_array(nv);

493:   PetscFunctionBegin;
494:   if (xin->map->n > 0) {
495:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
496:     for (i = 0; i < nv; i++) {
497:       PetscCall(VecViennaCLGetArrayRead(yyin[i], &ygpu));
498:       ygpu_array[i] = ygpu;
499:     }
500:     PetscCall(PetscLogGpuTimeBegin());
501:     viennacl::vector_tuple<PetscScalar> y_tuple(ygpu_array);
502:     ViennaCLVector                      result = viennacl::linalg::inner_prod(*xgpu, y_tuple);
503:     viennacl::copy(result.begin(), result.end(), z);
504:     for (i = 0; i < nv; i++) PetscCall(VecViennaCLRestoreArrayRead(yyin[i], &ygpu));
505:     ViennaCLWaitForGPU();
506:     PetscCall(PetscLogGpuTimeEnd());
507:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
508:     PetscCall(PetscLogGpuFlops(PetscMax(nv * (2.0 * n - 1), 0.0)));
509:   } else {
510:     for (i = 0; i < nv; i++) z[i] = 0.0;
511:   }
512:   PetscFunctionReturn(PETSC_SUCCESS);
513: }

515: static PetscErrorCode VecMTDot_SeqViennaCL(Vec xin, PetscInt nv, const Vec yin[], PetscScalar *z)
516: {
517:   PetscFunctionBegin;
518:   /* Since complex case is not supported at the moment, this is the same as VecMDot_SeqViennaCL */
519:   PetscCall(VecMDot_SeqViennaCL(xin, nv, yin, z));
520:   ViennaCLWaitForGPU();
521:   PetscFunctionReturn(PETSC_SUCCESS);
522: }

524: PetscErrorCode VecSet_SeqViennaCL(Vec xin, PetscScalar alpha)
525: {
526:   ViennaCLVector *xgpu;

528:   PetscFunctionBegin;
529:   if (xin->map->n > 0) {
530:     PetscCall(VecViennaCLGetArrayWrite(xin, &xgpu));
531:     PetscCall(PetscLogGpuTimeBegin());
532:     try {
533:       *xgpu = viennacl::scalar_vector<PetscScalar>(xgpu->size(), alpha);
534:       ViennaCLWaitForGPU();
535:     } catch (std::exception const &ex) {
536:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
537:     }
538:     PetscCall(PetscLogGpuTimeEnd());
539:     PetscCall(VecViennaCLRestoreArrayWrite(xin, &xgpu));
540:   }
541:   PetscFunctionReturn(PETSC_SUCCESS);
542: }

544: PetscErrorCode VecScale_SeqViennaCL(Vec xin, PetscScalar alpha)
545: {
546:   ViennaCLVector *xgpu;

548:   PetscFunctionBegin;
549:   if (alpha == 0.0 && xin->map->n > 0) {
550:     PetscCall(VecSet_SeqViennaCL(xin, alpha));
551:     PetscCall(PetscLogGpuFlops(xin->map->n));
552:   } else if (alpha != 1.0 && xin->map->n > 0) {
553:     PetscCall(VecViennaCLGetArray(xin, &xgpu));
554:     PetscCall(PetscLogGpuTimeBegin());
555:     try {
556:       *xgpu *= alpha;
557:       ViennaCLWaitForGPU();
558:     } catch (std::exception const &ex) {
559:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
560:     }
561:     PetscCall(PetscLogGpuTimeEnd());
562:     PetscCall(VecViennaCLRestoreArray(xin, &xgpu));
563:     PetscCall(PetscLogGpuFlops(xin->map->n));
564:   }
565:   PetscFunctionReturn(PETSC_SUCCESS);
566: }

568: PetscErrorCode VecTDot_SeqViennaCL(Vec xin, Vec yin, PetscScalar *z)
569: {
570:   PetscFunctionBegin;
571:   /* Since complex case is not supported at the moment, this is the same as VecDot_SeqViennaCL */
572:   PetscCall(VecDot_SeqViennaCL(xin, yin, z));
573:   ViennaCLWaitForGPU();
574:   PetscFunctionReturn(PETSC_SUCCESS);
575: }

577: PetscErrorCode VecCopy_SeqViennaCL(Vec xin, Vec yin)
578: {
579:   const ViennaCLVector *xgpu;
580:   ViennaCLVector       *ygpu;

582:   PetscFunctionBegin;
583:   if (xin != yin && xin->map->n > 0) {
584:     if (xin->offloadmask == PETSC_OFFLOAD_GPU) {
585:       PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
586:       PetscCall(VecViennaCLGetArrayWrite(yin, &ygpu));
587:       PetscCall(PetscLogGpuTimeBegin());
588:       try {
589:         *ygpu = *xgpu;
590:         ViennaCLWaitForGPU();
591:       } catch (std::exception const &ex) {
592:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
593:       }
594:       PetscCall(PetscLogGpuTimeEnd());
595:       PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
596:       PetscCall(VecViennaCLRestoreArrayWrite(yin, &ygpu));

598:     } else if (xin->offloadmask == PETSC_OFFLOAD_CPU) {
599:       /* copy in CPU if we are on the CPU*/
600:       PetscCall(VecCopy_SeqViennaCL_Private(xin, yin));
601:       ViennaCLWaitForGPU();
602:     } else if (xin->offloadmask == PETSC_OFFLOAD_BOTH) {
603:       /* if xin is valid in both places, see where yin is and copy there (because it's probably where we'll want to next use it) */
604:       if (yin->offloadmask == PETSC_OFFLOAD_CPU) {
605:         /* copy in CPU */
606:         PetscCall(VecCopy_SeqViennaCL_Private(xin, yin));
607:         ViennaCLWaitForGPU();
608:       } else if (yin->offloadmask == PETSC_OFFLOAD_GPU) {
609:         /* copy in GPU */
610:         PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
611:         PetscCall(VecViennaCLGetArrayWrite(yin, &ygpu));
612:         PetscCall(PetscLogGpuTimeBegin());
613:         try {
614:           *ygpu = *xgpu;
615:           ViennaCLWaitForGPU();
616:         } catch (std::exception const &ex) {
617:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
618:         }
619:         PetscCall(PetscLogGpuTimeEnd());
620:         PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
621:         PetscCall(VecViennaCLRestoreArrayWrite(yin, &ygpu));
622:       } else if (yin->offloadmask == PETSC_OFFLOAD_BOTH) {
623:         /* xin and yin are both valid in both places (or yin was unallocated before the earlier call to allocatecheck
624:            default to copy in GPU (this is an arbitrary choice) */
625:         PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
626:         PetscCall(VecViennaCLGetArrayWrite(yin, &ygpu));
627:         PetscCall(PetscLogGpuTimeBegin());
628:         try {
629:           *ygpu = *xgpu;
630:           ViennaCLWaitForGPU();
631:         } catch (std::exception const &ex) {
632:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
633:         }
634:         PetscCall(PetscLogGpuTimeEnd());
635:         PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
636:         PetscCall(VecViennaCLRestoreArrayWrite(yin, &ygpu));
637:       } else {
638:         PetscCall(VecCopy_SeqViennaCL_Private(xin, yin));
639:         ViennaCLWaitForGPU();
640:       }
641:     }
642:   }
643:   PetscFunctionReturn(PETSC_SUCCESS);
644: }

646: PetscErrorCode VecSwap_SeqViennaCL(Vec xin, Vec yin)
647: {
648:   ViennaCLVector *xgpu, *ygpu;

650:   PetscFunctionBegin;
651:   if (xin != yin && xin->map->n > 0) {
652:     PetscCall(VecViennaCLGetArray(xin, &xgpu));
653:     PetscCall(VecViennaCLGetArray(yin, &ygpu));
654:     PetscCall(PetscLogGpuTimeBegin());
655:     try {
656:       viennacl::swap(*xgpu, *ygpu);
657:       ViennaCLWaitForGPU();
658:     } catch (std::exception const &ex) {
659:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
660:     }
661:     PetscCall(PetscLogGpuTimeEnd());
662:     PetscCall(VecViennaCLRestoreArray(xin, &xgpu));
663:     PetscCall(VecViennaCLRestoreArray(yin, &ygpu));
664:   }
665:   PetscFunctionReturn(PETSC_SUCCESS);
666: }

668: // y = alpha * x + beta * y
669: PetscErrorCode VecAXPBY_SeqViennaCL(Vec yin, PetscScalar alpha, PetscScalar beta, Vec xin)
670: {
671:   PetscScalar           a = alpha, b = beta;
672:   const ViennaCLVector *xgpu;
673:   ViennaCLVector       *ygpu;

675:   PetscFunctionBegin;
676:   if (a == 0.0 && xin->map->n > 0) PetscCall(VecScale_SeqViennaCL(yin, beta));
677:   else if (b == 1.0 && xin->map->n > 0) PetscCall(VecAXPY_SeqViennaCL(yin, alpha, xin));
678:   else if (a == 1.0 && xin->map->n > 0) PetscCall(VecAYPX_SeqViennaCL(yin, beta, xin));
679:   else if (b == 0.0 && xin->map->n > 0) {
680:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
681:     PetscCall(VecViennaCLGetArray(yin, &ygpu));
682:     PetscCall(PetscLogGpuTimeBegin());
683:     try {
684:       *ygpu = *xgpu * alpha;
685:       ViennaCLWaitForGPU();
686:     } catch (std::exception const &ex) {
687:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
688:     }
689:     PetscCall(PetscLogGpuTimeEnd());
690:     PetscCall(PetscLogGpuFlops(xin->map->n));
691:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
692:     PetscCall(VecViennaCLRestoreArray(yin, &ygpu));
693:   } else if (xin->map->n > 0) {
694:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
695:     PetscCall(VecViennaCLGetArray(yin, &ygpu));
696:     PetscCall(PetscLogGpuTimeBegin());
697:     try {
698:       *ygpu = *xgpu * alpha + *ygpu * beta;
699:       ViennaCLWaitForGPU();
700:     } catch (std::exception const &ex) {
701:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
702:     }
703:     PetscCall(PetscLogGpuTimeEnd());
704:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
705:     PetscCall(VecViennaCLRestoreArray(yin, &ygpu));
706:     PetscCall(PetscLogGpuFlops(3.0 * xin->map->n));
707:   }
708:   PetscFunctionReturn(PETSC_SUCCESS);
709: }

711: /* operation  z = alpha * x + beta *y + gamma *z*/
712: PetscErrorCode VecAXPBYPCZ_SeqViennaCL(Vec zin, PetscScalar alpha, PetscScalar beta, PetscScalar gamma, Vec xin, Vec yin)
713: {
714:   PetscInt              n = zin->map->n;
715:   const ViennaCLVector *xgpu, *ygpu;
716:   ViennaCLVector       *zgpu;

718:   PetscFunctionBegin;
719:   PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
720:   PetscCall(VecViennaCLGetArrayRead(yin, &ygpu));
721:   PetscCall(VecViennaCLGetArray(zin, &zgpu));
722:   if (alpha == 0.0 && xin->map->n > 0) {
723:     PetscCall(PetscLogGpuTimeBegin());
724:     try {
725:       if (beta == 0.0) {
726:         *zgpu = gamma * *zgpu;
727:         ViennaCLWaitForGPU();
728:         PetscCall(PetscLogGpuFlops(1.0 * n));
729:       } else if (gamma == 0.0) {
730:         *zgpu = beta * *ygpu;
731:         ViennaCLWaitForGPU();
732:         PetscCall(PetscLogGpuFlops(1.0 * n));
733:       } else {
734:         *zgpu = beta * *ygpu + gamma * *zgpu;
735:         ViennaCLWaitForGPU();
736:         PetscCall(PetscLogGpuFlops(3.0 * n));
737:       }
738:     } catch (std::exception const &ex) {
739:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
740:     }
741:     PetscCall(PetscLogGpuTimeEnd());
742:     PetscCall(PetscLogGpuFlops(3.0 * n));
743:   } else if (beta == 0.0 && xin->map->n > 0) {
744:     PetscCall(PetscLogGpuTimeBegin());
745:     try {
746:       if (gamma == 0.0) {
747:         *zgpu = alpha * *xgpu;
748:         ViennaCLWaitForGPU();
749:         PetscCall(PetscLogGpuFlops(1.0 * n));
750:       } else {
751:         *zgpu = alpha * *xgpu + gamma * *zgpu;
752:         ViennaCLWaitForGPU();
753:         PetscCall(PetscLogGpuFlops(3.0 * n));
754:       }
755:     } catch (std::exception const &ex) {
756:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
757:     }
758:     PetscCall(PetscLogGpuTimeEnd());
759:   } else if (gamma == 0.0 && xin->map->n > 0) {
760:     PetscCall(PetscLogGpuTimeBegin());
761:     try {
762:       *zgpu = alpha * *xgpu + beta * *ygpu;
763:       ViennaCLWaitForGPU();
764:     } catch (std::exception const &ex) {
765:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
766:     }
767:     PetscCall(PetscLogGpuTimeEnd());
768:     PetscCall(PetscLogGpuFlops(3.0 * n));
769:   } else if (xin->map->n > 0) {
770:     PetscCall(PetscLogGpuTimeBegin());
771:     try {
772:       /* Split operation into two steps. This is not completely ideal, but avoids temporaries (which are far worse) */
773:       if (gamma != 1.0) *zgpu *= gamma;
774:       *zgpu += alpha * *xgpu + beta * *ygpu;
775:       ViennaCLWaitForGPU();
776:     } catch (std::exception const &ex) {
777:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
778:     }
779:     PetscCall(PetscLogGpuTimeEnd());
780:     PetscCall(VecViennaCLRestoreArray(zin, &zgpu));
781:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
782:     PetscCall(VecViennaCLRestoreArrayRead(yin, &ygpu));
783:     PetscCall(PetscLogGpuFlops(5.0 * n));
784:   }
785:   PetscFunctionReturn(PETSC_SUCCESS);
786: }

788: PetscErrorCode VecPointwiseMult_SeqViennaCL(Vec win, Vec xin, Vec yin)
789: {
790:   PetscInt              n = win->map->n;
791:   const ViennaCLVector *xgpu, *ygpu;
792:   ViennaCLVector       *wgpu;

794:   PetscFunctionBegin;
795:   if (xin->map->n > 0) {
796:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
797:     PetscCall(VecViennaCLGetArrayRead(yin, &ygpu));
798:     PetscCall(VecViennaCLGetArray(win, &wgpu));
799:     PetscCall(PetscLogGpuTimeBegin());
800:     try {
801:       *wgpu = viennacl::linalg::element_prod(*xgpu, *ygpu);
802:       ViennaCLWaitForGPU();
803:     } catch (std::exception const &ex) {
804:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
805:     }
806:     PetscCall(PetscLogGpuTimeEnd());
807:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
808:     PetscCall(VecViennaCLRestoreArrayRead(yin, &ygpu));
809:     PetscCall(VecViennaCLRestoreArray(win, &wgpu));
810:     PetscCall(PetscLogGpuFlops(n));
811:   }
812:   PetscFunctionReturn(PETSC_SUCCESS);
813: }

815: PetscErrorCode VecNorm_SeqViennaCL(Vec xin, NormType type, PetscReal *z)
816: {
817:   PetscInt              n = xin->map->n;
818:   PetscBLASInt          bn;
819:   const ViennaCLVector *xgpu;

821:   PetscFunctionBegin;
822:   if (xin->map->n > 0) {
823:     PetscCall(PetscBLASIntCast(n, &bn));
824:     PetscCall(VecViennaCLGetArrayRead(xin, &xgpu));
825:     if (type == NORM_2 || type == NORM_FROBENIUS) {
826:       PetscCall(PetscLogGpuTimeBegin());
827:       try {
828:         *z = viennacl::linalg::norm_2(*xgpu);
829:         ViennaCLWaitForGPU();
830:       } catch (std::exception const &ex) {
831:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
832:       }
833:       PetscCall(PetscLogGpuTimeEnd());
834:       PetscCall(PetscLogGpuFlops(PetscMax(2.0 * n - 1, 0.0)));
835:     } else if (type == NORM_INFINITY) {
836:       PetscCall(PetscLogGpuTimeBegin());
837:       try {
838:         *z = viennacl::linalg::norm_inf(*xgpu);
839:         ViennaCLWaitForGPU();
840:       } catch (std::exception const &ex) {
841:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
842:       }
843:       PetscCall(PetscLogGpuTimeEnd());
844:     } else if (type == NORM_1) {
845:       PetscCall(PetscLogGpuTimeBegin());
846:       try {
847:         *z = viennacl::linalg::norm_1(*xgpu);
848:         ViennaCLWaitForGPU();
849:       } catch (std::exception const &ex) {
850:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
851:       }
852:       PetscCall(PetscLogGpuTimeEnd());
853:       PetscCall(PetscLogGpuFlops(PetscMax(n - 1.0, 0.0)));
854:     } else if (type == NORM_1_AND_2) {
855:       PetscCall(PetscLogGpuTimeBegin());
856:       try {
857:         *z       = viennacl::linalg::norm_1(*xgpu);
858:         *(z + 1) = viennacl::linalg::norm_2(*xgpu);
859:         ViennaCLWaitForGPU();
860:       } catch (std::exception const &ex) {
861:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
862:       }
863:       PetscCall(PetscLogGpuTimeEnd());
864:       PetscCall(PetscLogGpuFlops(PetscMax(2.0 * n - 1, 0.0)));
865:       PetscCall(PetscLogGpuFlops(PetscMax(n - 1.0, 0.0)));
866:     }
867:     PetscCall(VecViennaCLRestoreArrayRead(xin, &xgpu));
868:   } else if (type == NORM_1_AND_2) {
869:     *z       = 0.0;
870:     *(z + 1) = 0.0;
871:   } else *z = 0.0;
872:   PetscFunctionReturn(PETSC_SUCCESS);
873: }

875: PetscErrorCode VecSetRandom_SeqViennaCL(Vec xin, PetscRandom r)
876: {
877:   PetscFunctionBegin;
878:   PetscCall(VecSetRandom_SeqViennaCL_Private(xin, r));
879:   xin->offloadmask = PETSC_OFFLOAD_CPU;
880:   PetscFunctionReturn(PETSC_SUCCESS);
881: }

883: PetscErrorCode VecResetArray_SeqViennaCL(Vec vin)
884: {
885:   PetscFunctionBegin;
886:   PetscCheckTypeNames(vin, VECSEQVIENNACL, VECMPIVIENNACL);
887:   PetscCall(VecViennaCLCopyFromGPU(vin));
888:   PetscCall(VecResetArray_SeqViennaCL_Private(vin));
889:   vin->offloadmask = PETSC_OFFLOAD_CPU;
890:   PetscFunctionReturn(PETSC_SUCCESS);
891: }

893: PetscErrorCode VecPlaceArray_SeqViennaCL(Vec vin, const PetscScalar *a)
894: {
895:   PetscFunctionBegin;
896:   PetscCheckTypeNames(vin, VECSEQVIENNACL, VECMPIVIENNACL);
897:   PetscCall(VecViennaCLCopyFromGPU(vin));
898:   PetscCall(VecPlaceArray_Seq(vin, a));
899:   vin->offloadmask = PETSC_OFFLOAD_CPU;
900:   PetscFunctionReturn(PETSC_SUCCESS);
901: }

903: PetscErrorCode VecReplaceArray_SeqViennaCL(Vec vin, const PetscScalar *a)
904: {
905:   PetscFunctionBegin;
906:   PetscCheckTypeNames(vin, VECSEQVIENNACL, VECMPIVIENNACL);
907:   PetscCall(VecViennaCLCopyFromGPU(vin));
908:   PetscCall(VecReplaceArray_Seq(vin, a));
909:   vin->offloadmask = PETSC_OFFLOAD_CPU;
910:   PetscFunctionReturn(PETSC_SUCCESS);
911: }

913: /*@
914:   VecCreateSeqViennaCL - Creates a standard, sequential array-style vector.

916:   Collective

918:   Input Parameters:
919: + comm - the communicator, should be PETSC_COMM_SELF
920: - n    - the vector length

922:   Output Parameter:
923: . v - the vector

925:   Notes:
926:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
927:   same type as an existing vector.

929:   Level: intermediate

931: .seealso: `VecCreateMPI()`, `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`, `VecCreateGhost()`
932: @*/
933: PetscErrorCode VecCreateSeqViennaCL(MPI_Comm comm, PetscInt n, Vec *v)
934: {
935:   PetscFunctionBegin;
936:   PetscCall(VecCreate(comm, v));
937:   PetscCall(VecSetSizes(*v, n, n));
938:   PetscCall(VecSetType(*v, VECSEQVIENNACL));
939:   PetscFunctionReturn(PETSC_SUCCESS);
940: }

942: /*@
943:   VecCreateSeqViennaCLWithArray - Creates a viennacl sequential array-style vector,
944:   where the user provides the array space to store the vector values.

946:   Collective

948:   Input Parameters:
949: + comm  - the communicator, should be PETSC_COMM_SELF
950: . bs    - the block size
951: . n     - the vector length
952: - array - viennacl array where the vector elements are to be stored.

954:   Output Parameter:
955: . V - the vector

957:   Notes:
958:   Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
959:   same type as an existing vector.

961:   If the user-provided array is NULL, then VecViennaCLPlaceArray() can be used
962:   at a later stage to SET the array for storing the vector values.

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

967:   Level: intermediate

969: .seealso: `VecCreateMPIViennaCLWithArray()`, `VecCreate()`, `VecDuplicate()`, `VecDuplicateVecs()`,
970:           `VecCreateGhost()`, `VecCreateSeq()`, `VecCUDAPlaceArray()`, `VecCreateSeqWithArray()`,
971:           `VecCreateMPIWithArray()`
972: @*/
973: PETSC_EXTERN PetscErrorCode VecCreateSeqViennaCLWithArray(MPI_Comm comm, PetscInt bs, PetscInt n, const ViennaCLVector *array, Vec *V) PeNS
974: {
975:   PetscMPIInt size;

977:   PetscFunctionBegin;
978:   PetscCall(VecCreate(comm, V));
979:   PetscCall(VecSetSizes(*V, n, n));
980:   PetscCall(VecSetBlockSize(*V, bs));
981:   PetscCallMPI(MPI_Comm_size(comm, &size));
982:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQ on more than one process");
983:   PetscCall(VecCreate_SeqViennaCL_Private(*V, array));
984:   PetscFunctionReturn(PETSC_SUCCESS);
985: }

987: /*@
988:   VecCreateSeqViennaCLWithArrays - Creates a ViennaCL sequential vector, where
989:   the user provides the array space to store the vector values.

991:   Collective

993:   Input Parameters:
994: + comm        - the communicator, should be PETSC_COMM_SELF
995: . bs          - the block size
996: . n           - the vector length
997: . cpuarray    - CPU memory where the vector elements are to be stored.
998: - viennaclvec - ViennaCL vector where the Vec entries are to be stored on the device.

1000:   Output Parameter:
1001: . V - the vector

1003:   Notes:
1004:   If both cpuarray and viennaclvec are provided, the caller must ensure that
1005:   the provided arrays have identical values.

1007:   PETSc does NOT free the provided arrays when the vector is destroyed via
1008:   VecDestroy(). The user should not free the array until the vector is
1009:   destroyed.

1011:   Level: intermediate

1013: .seealso: `VecCreateMPIViennaCLWithArrays()`, `VecCreate()`, `VecCreateSeqWithArray()`,
1014:           `VecViennaCLPlaceArray()`, `VecPlaceArray()`, `VecCreateSeqCUDAWithArrays()`,
1015:           `VecViennaCLAllocateCheckHost()`
1016: @*/
1017: PetscErrorCode VecCreateSeqViennaCLWithArrays(MPI_Comm comm, PetscInt bs, PetscInt n, const PetscScalar cpuarray[], const ViennaCLVector *viennaclvec, Vec *V) PeNS
1018: {
1019:   PetscMPIInt size;

1021:   PetscFunctionBegin;
1022:   PetscCallMPI(MPI_Comm_size(comm, &size));
1023:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQ on more than one process");

1025:   // set V's viennaclvec to be viennaclvec, do not allocate memory on host yet.
1026:   PetscCall(VecCreateSeqViennaCLWithArray(comm, bs, n, viennaclvec, V));

1028:   if (cpuarray && viennaclvec) {
1029:     Vec_Seq *s        = (Vec_Seq *)((*V)->data);
1030:     s->array          = (PetscScalar *)cpuarray;
1031:     (*V)->offloadmask = PETSC_OFFLOAD_BOTH;
1032:   } else if (cpuarray) {
1033:     Vec_Seq *s        = (Vec_Seq *)((*V)->data);
1034:     s->array          = (PetscScalar *)cpuarray;
1035:     (*V)->offloadmask = PETSC_OFFLOAD_CPU;
1036:   } else if (viennaclvec) {
1037:     (*V)->offloadmask = PETSC_OFFLOAD_GPU;
1038:   } else {
1039:     (*V)->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
1040:   }
1041:   PetscFunctionReturn(PETSC_SUCCESS);
1042: }

1044: /*@
1045:   VecViennaCLPlaceArray - Replace the viennacl vector in a Vec with
1046:   the one provided by the user. This is useful to avoid a copy.

1048:   Not Collective

1050:   Input Parameters:
1051: + vin - the vector
1052: - a   - the ViennaCL vector

1054:   Notes:
1055:   You can return to the original viennacl vector with a call to `VecViennaCLResetArray()`.
1056:   It is not possible to use `VecViennaCLPlaceArray()` and `VecPlaceArray()` at the same time on
1057:   the same vector.

1059:   Level: intermediate

1061: .seealso: `VecPlaceArray()`, `VecSetValues()`, `VecViennaCLResetArray()`,
1062:           `VecCUDAPlaceArray()`,

1064: @*/
1065: PETSC_EXTERN PetscErrorCode VecViennaCLPlaceArray(Vec vin, const ViennaCLVector *a)
1066: {
1067:   PetscFunctionBegin;
1068:   PetscCheckTypeNames(vin, VECSEQVIENNACL, VECMPIVIENNACL);
1069:   PetscCall(VecViennaCLCopyToGPU(vin));
1070:   PetscCheck(!((Vec_Seq *)vin->data)->unplacedarray, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "VecViennaCLPlaceArray()/VecPlaceArray() was already called on this vector, without a call to VecViennaCLResetArray()/VecResetArray()");
1071:   ((Vec_Seq *)vin->data)->unplacedarray  = (PetscScalar *)((Vec_ViennaCL *)vin->spptr)->GPUarray; /* save previous GPU array so reset can bring it back */
1072:   ((Vec_ViennaCL *)vin->spptr)->GPUarray = (ViennaCLVector *)a;
1073:   vin->offloadmask                       = PETSC_OFFLOAD_GPU;
1074:   PetscCall(PetscObjectStateIncrease((PetscObject)vin));
1075:   PetscFunctionReturn(PETSC_SUCCESS);
1076: }

1078: /*@
1079:   VecViennaCLResetArray - Resets a vector to use its default memory. Call this
1080:   after the use of VecViennaCLPlaceArray().

1082:   Not Collective

1084:   Input Parameters:
1085: . vin - the vector

1087:   Level: developer

1089: .seealso: `VecViennaCLPlaceArray()`, `VecResetArray()`, `VecCUDAResetArray()`, `VecPlaceArray()`
1090: @*/
1091: PETSC_EXTERN PetscErrorCode VecViennaCLResetArray(Vec vin)
1092: {
1093:   PetscFunctionBegin;
1094:   PetscCheckTypeNames(vin, VECSEQVIENNACL, VECMPIVIENNACL);
1095:   PetscCall(VecViennaCLCopyToGPU(vin));
1096:   ((Vec_ViennaCL *)vin->spptr)->GPUarray = (ViennaCLVector *)((Vec_Seq *)vin->data)->unplacedarray;
1097:   ((Vec_Seq *)vin->data)->unplacedarray  = 0;
1098:   vin->offloadmask                       = PETSC_OFFLOAD_GPU;
1099:   PetscCall(PetscObjectStateIncrease((PetscObject)vin));
1100:   PetscFunctionReturn(PETSC_SUCCESS);
1101: }

1103: /*  VecDotNorm2 - computes the inner product of two vectors and the 2-norm squared of the second vector
1104:  *
1105:  *  Simply reuses VecDot() and VecNorm(). Performance improvement through custom kernel (kernel generator) possible.
1106:  */
1107: PetscErrorCode VecDotNorm2_SeqViennaCL(Vec s, Vec t, PetscScalar *dp, PetscScalar *nm)
1108: {
1109:   PetscFunctionBegin;
1110:   PetscCall(VecDot_SeqViennaCL(s, t, dp));
1111:   PetscCall(VecNorm_SeqViennaCL(t, NORM_2, nm));
1112:   *nm *= *nm; //squared norm required
1113:   PetscFunctionReturn(PETSC_SUCCESS);
1114: }

1116: PetscErrorCode VecDuplicate_SeqViennaCL(Vec win, Vec *V)
1117: {
1118:   PetscFunctionBegin;
1119:   PetscCall(VecCreateSeqViennaCL(PetscObjectComm((PetscObject)win), win->map->n, V));
1120:   PetscCall(PetscLayoutReference(win->map, &(*V)->map));
1121:   PetscCall(PetscObjectListDuplicate(((PetscObject)win)->olist, &((PetscObject)*V)->olist));
1122:   PetscCall(PetscFunctionListDuplicate(((PetscObject)win)->qlist, &((PetscObject)*V)->qlist));
1123:   (*V)->stash.ignorenegidx = win->stash.ignorenegidx;
1124:   PetscFunctionReturn(PETSC_SUCCESS);
1125: }

1127: PetscErrorCode VecDestroy_SeqViennaCL(Vec v)
1128: {
1129:   PetscFunctionBegin;
1130:   try {
1131:     if (v->spptr) {
1132:       delete ((Vec_ViennaCL *)v->spptr)->GPUarray_allocated;
1133:       delete (Vec_ViennaCL *)v->spptr;
1134:     }
1135:   } catch (char *ex) {
1136:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex);
1137:   }
1138:   PetscCall(VecDestroy_SeqViennaCL_Private(v));
1139:   PetscFunctionReturn(PETSC_SUCCESS);
1140: }

1142: PetscErrorCode VecGetArray_SeqViennaCL(Vec v, PetscScalar **a)
1143: {
1144:   PetscFunctionBegin;
1145:   if (v->offloadmask == PETSC_OFFLOAD_GPU) {
1146:     PetscCall(VecViennaCLCopyFromGPU(v));
1147:   } else {
1148:     PetscCall(VecViennaCLAllocateCheckHost(v));
1149:   }
1150:   *a = *((PetscScalar **)v->data);
1151:   PetscFunctionReturn(PETSC_SUCCESS);
1152: }

1154: PetscErrorCode VecRestoreArray_SeqViennaCL(Vec v, PetscScalar **a)
1155: {
1156:   PetscFunctionBegin;
1157:   v->offloadmask = PETSC_OFFLOAD_CPU;
1158:   PetscFunctionReturn(PETSC_SUCCESS);
1159: }

1161: PetscErrorCode VecGetArrayWrite_SeqViennaCL(Vec v, PetscScalar **a)
1162: {
1163:   PetscFunctionBegin;
1164:   PetscCall(VecViennaCLAllocateCheckHost(v));
1165:   *a = *((PetscScalar **)v->data);
1166:   PetscFunctionReturn(PETSC_SUCCESS);
1167: }

1169: static PetscErrorCode VecBindToCPU_SeqAIJViennaCL(Vec V, PetscBool flg)
1170: {
1171:   PetscFunctionBegin;
1172:   V->boundtocpu = flg;
1173:   if (flg) {
1174:     PetscCall(VecViennaCLCopyFromGPU(V));
1175:     V->offloadmask          = PETSC_OFFLOAD_CPU; /* since the CPU code will likely change values in the vector */
1176:     V->ops->dot             = VecDot_Seq;
1177:     V->ops->norm            = VecNorm_Seq;
1178:     V->ops->tdot            = VecTDot_Seq;
1179:     V->ops->scale           = VecScale_Seq;
1180:     V->ops->copy            = VecCopy_Seq;
1181:     V->ops->set             = VecSet_Seq;
1182:     V->ops->swap            = VecSwap_Seq;
1183:     V->ops->axpy            = VecAXPY_Seq;
1184:     V->ops->axpby           = VecAXPBY_Seq;
1185:     V->ops->axpbypcz        = VecAXPBYPCZ_Seq;
1186:     V->ops->pointwisemult   = VecPointwiseMult_Seq;
1187:     V->ops->pointwisedivide = VecPointwiseDivide_Seq;
1188:     V->ops->setrandom       = VecSetRandom_Seq;
1189:     V->ops->dot_local       = VecDot_Seq;
1190:     V->ops->tdot_local      = VecTDot_Seq;
1191:     V->ops->norm_local      = VecNorm_Seq;
1192:     V->ops->mdot_local      = VecMDot_Seq;
1193:     V->ops->mtdot_local     = VecMTDot_Seq;
1194:     V->ops->maxpy           = VecMAXPY_Seq;
1195:     V->ops->mdot            = VecMDot_Seq;
1196:     V->ops->mtdot           = VecMTDot_Seq;
1197:     V->ops->aypx            = VecAYPX_Seq;
1198:     V->ops->waxpy           = VecWAXPY_Seq;
1199:     V->ops->dotnorm2        = NULL;
1200:     V->ops->placearray      = VecPlaceArray_Seq;
1201:     V->ops->replacearray    = VecReplaceArray_Seq;
1202:     V->ops->resetarray      = VecResetArray_Seq;
1203:     V->ops->duplicate       = VecDuplicate_Seq;
1204:   } else {
1205:     V->ops->dot             = VecDot_SeqViennaCL;
1206:     V->ops->norm            = VecNorm_SeqViennaCL;
1207:     V->ops->tdot            = VecTDot_SeqViennaCL;
1208:     V->ops->scale           = VecScale_SeqViennaCL;
1209:     V->ops->copy            = VecCopy_SeqViennaCL;
1210:     V->ops->set             = VecSet_SeqViennaCL;
1211:     V->ops->swap            = VecSwap_SeqViennaCL;
1212:     V->ops->axpy            = VecAXPY_SeqViennaCL;
1213:     V->ops->axpby           = VecAXPBY_SeqViennaCL;
1214:     V->ops->axpbypcz        = VecAXPBYPCZ_SeqViennaCL;
1215:     V->ops->pointwisemult   = VecPointwiseMult_SeqViennaCL;
1216:     V->ops->pointwisedivide = VecPointwiseDivide_SeqViennaCL;
1217:     V->ops->setrandom       = VecSetRandom_SeqViennaCL;
1218:     V->ops->dot_local       = VecDot_SeqViennaCL;
1219:     V->ops->tdot_local      = VecTDot_SeqViennaCL;
1220:     V->ops->norm_local      = VecNorm_SeqViennaCL;
1221:     V->ops->mdot_local      = VecMDot_SeqViennaCL;
1222:     V->ops->mtdot_local     = VecMTDot_SeqViennaCL;
1223:     V->ops->maxpy           = VecMAXPY_SeqViennaCL;
1224:     V->ops->mdot            = VecMDot_SeqViennaCL;
1225:     V->ops->mtdot           = VecMTDot_SeqViennaCL;
1226:     V->ops->aypx            = VecAYPX_SeqViennaCL;
1227:     V->ops->waxpy           = VecWAXPY_SeqViennaCL;
1228:     V->ops->dotnorm2        = VecDotNorm2_SeqViennaCL;
1229:     V->ops->placearray      = VecPlaceArray_SeqViennaCL;
1230:     V->ops->replacearray    = VecReplaceArray_SeqViennaCL;
1231:     V->ops->resetarray      = VecResetArray_SeqViennaCL;
1232:     V->ops->destroy         = VecDestroy_SeqViennaCL;
1233:     V->ops->duplicate       = VecDuplicate_SeqViennaCL;
1234:     V->ops->getarraywrite   = VecGetArrayWrite_SeqViennaCL;
1235:     V->ops->getarray        = VecGetArray_SeqViennaCL;
1236:     V->ops->restorearray    = VecRestoreArray_SeqViennaCL;
1237:   }
1238:   V->ops->duplicatevecs = VecDuplicateVecs_Default;
1239:   PetscFunctionReturn(PETSC_SUCCESS);
1240: }

1242: PETSC_EXTERN PetscErrorCode VecCreate_SeqViennaCL(Vec V)
1243: {
1244:   PetscMPIInt size;

1246:   PetscFunctionBegin;
1247:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)V), &size));
1248:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQVIENNACL on more than one process");
1249:   PetscCall(VecCreate_Seq_Private(V, 0));
1250:   PetscCall(PetscObjectChangeTypeName((PetscObject)V, VECSEQVIENNACL));

1252:   PetscCall(VecBindToCPU_SeqAIJViennaCL(V, PETSC_FALSE));
1253:   V->ops->bindtocpu = VecBindToCPU_SeqAIJViennaCL;

1255:   PetscCall(VecViennaCLAllocateCheck(V));
1256:   PetscCall(VecSet_SeqViennaCL(V, 0.0));
1257:   PetscFunctionReturn(PETSC_SUCCESS);
1258: }

1260: /*@
1261:   VecViennaCLGetCLContext - Get the OpenCL context in which the Vec resides.

1263:   Caller should cast (*ctx) to (const cl_context). Caller is responsible for
1264:   invoking clReleaseContext().

1266:   Input Parameter:
1267: . v - the vector

1269:   Output Parameter:
1270: . ctx - pointer to the underlying CL context

1272:   Level: intermediate

1274: .seealso: `VecViennaCLGetCLQueue()`, `VecViennaCLGetCLMemRead()`
1275: @*/
1276: PETSC_EXTERN PetscErrorCode VecViennaCLGetCLContext(Vec v, PETSC_UINTPTR_T *ctx)
1277: {
1278: #if !PetscDefined(HAVE_OPENCL)
1279:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to get the associated cl_context.");
1280: #else

1282:   PetscFunctionBegin;
1283:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1284:   const ViennaCLVector *v_vcl;
1285:   PetscCall(VecViennaCLGetArrayRead(v, &v_vcl));
1286:   try {
1287:     viennacl::ocl::context vcl_ctx = v_vcl->handle().opencl_handle().context();
1288:     const cl_context       ocl_ctx = vcl_ctx.handle().get();
1289:     clRetainContext(ocl_ctx);
1290:     *ctx = (PETSC_UINTPTR_T)ocl_ctx;
1291:   } catch (std::exception const &ex) {
1292:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
1293:   }
1294:   PetscFunctionReturn(PETSC_SUCCESS);
1295: #endif
1296: }

1298: /*@
1299:   VecViennaCLGetCLQueue - Get the OpenCL command queue to which all
1300:   operations of the Vec are enqueued.

1302:   Caller should cast (*queue) to (const cl_command_queue). Caller is
1303:   responsible for invoking clReleaseCommandQueue().

1305:   Input Parameter:
1306: . v - the vector

1308:   Output Parameter:
1309: . queue - pointer to the CL command queue

1311:   Level: intermediate

1313: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLGetCLMemRead()`
1314: @*/
1315: PETSC_EXTERN PetscErrorCode VecViennaCLGetCLQueue(Vec v, PETSC_UINTPTR_T *queue)
1316: {
1317: #if !PetscDefined(HAVE_OPENCL)
1318:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to get the associated cl_command_queue.");
1319: #else
1320:   PetscFunctionBegin;
1321:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1322:   const ViennaCLVector *v_vcl;
1323:   PetscCall(VecViennaCLGetArrayRead(v, &v_vcl));
1324:   try {
1325:     viennacl::ocl::context              vcl_ctx   = v_vcl->handle().opencl_handle().context();
1326:     const viennacl::ocl::command_queue &vcl_queue = vcl_ctx.current_queue();
1327:     const cl_command_queue              ocl_queue = vcl_queue.handle().get();
1328:     clRetainCommandQueue(ocl_queue);
1329:     *queue = (PETSC_UINTPTR_T)ocl_queue;
1330:   } catch (std::exception const &ex) {
1331:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
1332:   }
1333:   PetscFunctionReturn(PETSC_SUCCESS);
1334: #endif
1335: }

1337: /*@
1338:   VecViennaCLGetCLMemRead - Provides access to the CL buffer inside a Vec.

1340:   Caller should cast (*mem) to (const cl_mem). Caller is responsible for
1341:   invoking clReleaseMemObject().

1343:   Input Parameter:
1344: . v - the vector

1346:   Output Parameter:
1347: . mem - pointer to the device buffer

1349:   Level: intermediate

1351: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLGetCLMemWrite()`
1352: @*/
1353: PETSC_EXTERN PetscErrorCode VecViennaCLGetCLMemRead(Vec v, PETSC_UINTPTR_T *mem)
1354: {
1355: #if !PetscDefined(HAVE_OPENCL)
1356:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to get a Vec's cl_mem");
1357: #else
1358:   PetscFunctionBegin;
1359:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1360:   const ViennaCLVector *v_vcl;
1361:   PetscCall(VecViennaCLGetArrayRead(v, &v_vcl));
1362:   try {
1363:     const cl_mem ocl_mem = v_vcl->handle().opencl_handle().get();
1364:     clRetainMemObject(ocl_mem);
1365:     *mem = (PETSC_UINTPTR_T)ocl_mem;
1366:   } catch (std::exception const &ex) {
1367:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
1368:   }
1369:   PetscFunctionReturn(PETSC_SUCCESS);
1370: #endif
1371: }

1373: /*@
1374:   VecViennaCLGetCLMemWrite - Provides access to the CL buffer inside a Vec.

1376:   Caller should cast (*mem) to (const cl_mem). Caller is responsible for
1377:   invoking clReleaseMemObject().

1379:   The device pointer has to be released by calling
1380:   VecViennaCLRestoreCLMemWrite().  Upon restoring the vector data the data on
1381:   the host will be marked as out of date.  A subsequent access of the host data
1382:   will thus incur a data transfer from the device to the host.

1384:   Input Parameter:
1385: . v - the vector

1387:   Output Parameter:
1388: . mem - pointer to the device buffer

1390:   Level: intermediate

1392: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLRestoreCLMemWrite()`
1393: @*/
1394: PETSC_EXTERN PetscErrorCode VecViennaCLGetCLMemWrite(Vec v, PETSC_UINTPTR_T *mem)
1395: {
1396: #if !PetscDefined(HAVE_OPENCL)
1397:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to get a Vec's cl_mem");
1398: #else
1399:   PetscFunctionBegin;
1400:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1401:   ViennaCLVector *v_vcl;
1402:   PetscCall(VecViennaCLGetArrayWrite(v, &v_vcl));
1403:   try {
1404:     const cl_mem ocl_mem = v_vcl->handle().opencl_handle().get();
1405:     clRetainMemObject(ocl_mem);
1406:     *mem = (PETSC_UINTPTR_T)ocl_mem;
1407:   } catch (std::exception const &ex) {
1408:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
1409:   }
1410:   PetscFunctionReturn(PETSC_SUCCESS);
1411: #endif
1412: }

1414: /*@
1415:   VecViennaCLRestoreCLMemWrite - Restores a CL buffer pointer previously
1416:   acquired with VecViennaCLGetCLMemWrite().

1418:   This marks the host data as out of date.  Subsequent access to the
1419:   vector data on the host side with for instance VecGetArray() incurs a
1420:   data transfer.

1422:   Input Parameter:
1423: . v - the vector

1425:   Level: intermediate

1427: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLGetCLMemWrite()`
1428: @*/
1429: PETSC_EXTERN PetscErrorCode VecViennaCLRestoreCLMemWrite(Vec v)
1430: {
1431: #if !PetscDefined(HAVE_OPENCL)
1432:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to restore a Vec's cl_mem");
1433: #else
1434:   PetscFunctionBegin;
1435:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1436:   PetscCall(VecViennaCLRestoreArrayWrite(v, NULL));
1437:   PetscFunctionReturn(PETSC_SUCCESS);
1438: #endif
1439: }

1441: /*@
1442:   VecViennaCLGetCLMem - Provides access to the CL buffer inside a Vec.

1444:   Caller should cast (*mem) to (const cl_mem). Caller is responsible for
1445:   invoking clReleaseMemObject().

1447:   The device pointer has to be released by calling VecViennaCLRestoreCLMem().
1448:   Upon restoring the vector data the data on the host will be marked as out of
1449:   date.  A subsequent access of the host data will thus incur a data transfer
1450:   from the device to the host.

1452:   Input Parameter:
1453: . v - the vector

1455:   Output Parameter:
1456: . mem - pointer to the device buffer

1458:   Level: intermediate

1460: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLRestoreCLMem()`
1461: @*/
1462: PETSC_EXTERN PetscErrorCode VecViennaCLGetCLMem(Vec v, PETSC_UINTPTR_T *mem)
1463: {
1464: #if !PetscDefined(HAVE_OPENCL)
1465:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to get a Vec's cl_mem");
1466: #else
1467:   PetscFunctionBegin;
1468:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1469:   ViennaCLVector *v_vcl;
1470:   PetscCall(VecViennaCLGetArray(v, &v_vcl));
1471:   try {
1472:     const cl_mem ocl_mem = v_vcl->handle().opencl_handle().get();
1473:     clRetainMemObject(ocl_mem);
1474:     *mem = (PETSC_UINTPTR_T)ocl_mem;
1475:   } catch (std::exception const &ex) {
1476:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex.what());
1477:   }
1478:   PetscFunctionReturn(PETSC_SUCCESS);
1479: #endif
1480: }

1482: /*@
1483:   VecViennaCLRestoreCLMem - Restores a CL buffer pointer previously
1484:   acquired with VecViennaCLGetCLMem().

1486:   This marks the host data as out of date. Subsequent access to the vector
1487:   data on the host side with for instance VecGetArray() incurs a data
1488:   transfer.

1490:   Input Parameter:
1491: . v - the vector

1493:   Level: intermediate

1495: .seealso: `VecViennaCLGetCLContext()`, `VecViennaCLGetCLMem()`
1496: @*/
1497: PETSC_EXTERN PetscErrorCode VecViennaCLRestoreCLMem(Vec v)
1498: {
1499: #if !PetscDefined(HAVE_OPENCL)
1500:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "PETSc must be configured with --with-opencl to restore a Vec's cl_mem");
1501: #else
1502:   PetscFunctionBegin;
1503:   PetscCheckTypeNames(v, VECSEQVIENNACL, VECMPIVIENNACL);
1504:   PetscCall(VecViennaCLRestoreArray(v, NULL));
1505:   PetscFunctionReturn(PETSC_SUCCESS);
1506: #endif
1507: }

1509: PetscErrorCode VecCreate_SeqViennaCL_Private(Vec V, const ViennaCLVector *array)
1510: {
1511:   Vec_ViennaCL *vecviennacl;
1512:   PetscMPIInt   size;

1514:   PetscFunctionBegin;
1515:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)V), &size));
1516:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create VECSEQVIENNACL on more than one process");
1517:   PetscCall(VecCreate_Seq_Private(V, 0));
1518:   PetscCall(PetscObjectChangeTypeName((PetscObject)V, VECSEQVIENNACL));
1519:   PetscCall(VecBindToCPU_SeqAIJViennaCL(V, PETSC_FALSE));
1520:   V->ops->bindtocpu = VecBindToCPU_SeqAIJViennaCL;

1522:   if (array) {
1523:     if (!V->spptr) V->spptr = new Vec_ViennaCL;
1524:     vecviennacl                     = (Vec_ViennaCL *)V->spptr;
1525:     vecviennacl->GPUarray_allocated = 0;
1526:     vecviennacl->GPUarray           = (ViennaCLVector *)array;
1527:     V->offloadmask                  = PETSC_OFFLOAD_UNALLOCATED;
1528:   }
1529:   PetscFunctionReturn(PETSC_SUCCESS);
1530: }