xref: /libCEED/backends/ref/ceed-ref-vector.c (revision 0436c2ad4dd95c9ccd89f5346568014600de10a7)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include "ceed-ref.h"
18 
19 static int CeedVectorSetArray_Ref(CeedVector vec, CeedMemType mtype,
20                                   CeedCopyMode cmode, CeedScalar *array) {
21   int ierr;
22   CeedVector_Ref *impl;
23   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
24   CeedInt length;
25   ierr = CeedVectorGetLength(vec, &length); CeedChk(ierr);
26   Ceed ceed;
27   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
28 
29   if (mtype != CEED_MEM_HOST)
30     // LCOV_EXCL_START
31     return CeedError(ceed, 1, "Only MemType = HOST supported");
32   // LCOV_EXCL_STOP
33   ierr = CeedFree(&impl->array_allocated); CeedChk(ierr);
34   switch (cmode) {
35   case CEED_COPY_VALUES:
36     ierr = CeedMalloc(length, &impl->array_allocated); CeedChk(ierr);
37     impl->array = impl->array_allocated;
38     if (array) memcpy(impl->array, array, length * sizeof(array[0]));
39     break;
40   case CEED_OWN_POINTER:
41     impl->array_allocated = array;
42     impl->array = array;
43     break;
44   case CEED_USE_POINTER:
45     impl->array = array;
46   }
47   return 0;
48 }
49 
50 static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mtype,
51                                   CeedScalar **array) {
52   int ierr;
53   CeedVector_Ref *impl;
54   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
55   Ceed ceed;
56   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
57 
58   if (mtype != CEED_MEM_HOST)
59     // LCOV_EXCL_START
60     return CeedError(ceed, 1, "Can only provide to HOST memory");
61   // LCOV_EXCL_STOP
62   if (!impl->array) { // Allocate if array is not yet allocated
63     ierr = CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL);
64     CeedChk(ierr);
65   }
66   *array = impl->array;
67   return 0;
68 }
69 
70 static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mtype,
71                                       const CeedScalar **array) {
72   int ierr;
73   CeedVector_Ref *impl;
74   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
75   Ceed ceed;
76   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
77 
78   if (mtype != CEED_MEM_HOST)
79     // LCOV_EXCL_START
80     return CeedError(ceed, 1, "Can only provide to HOST memory");
81   // LCOV_EXCL_STOP
82   if (!impl->array) { // Allocate if array is not yet allocated
83     ierr = CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL);
84     CeedChk(ierr);
85   }
86   *array = impl->array;
87   return 0;
88 }
89 
90 static int CeedVectorRestoreArray_Ref(CeedVector vec) {
91   return 0;
92 }
93 
94 static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) {
95   return 0;
96 }
97 
98 static int CeedVectorDestroy_Ref(CeedVector vec) {
99   int ierr;
100   CeedVector_Ref *impl;
101   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
102 
103   ierr = CeedFree(&impl->array_allocated); CeedChk(ierr);
104   ierr = CeedFree(&impl); CeedChk(ierr);
105   return 0;
106 }
107 
108 int CeedVectorCreate_Ref(CeedInt n, CeedVector vec) {
109   int ierr;
110   CeedVector_Ref *impl;
111   Ceed ceed;
112   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
113 
114   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetArray",
115                                 CeedVectorSetArray_Ref); CeedChk(ierr);
116   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArray",
117                                 CeedVectorGetArray_Ref); CeedChk(ierr);
118   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead",
119                                 CeedVectorGetArrayRead_Ref); CeedChk(ierr);
120   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray",
121                                 CeedVectorRestoreArray_Ref); CeedChk(ierr);
122   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead",
123                                 CeedVectorRestoreArrayRead_Ref); CeedChk(ierr);
124   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Destroy",
125                                 CeedVectorDestroy_Ref); CeedChk(ierr);
126   ierr = CeedCalloc(1,&impl); CeedChk(ierr);
127   ierr = CeedVectorSetData(vec, (void *)&impl); CeedChk(ierr);
128   return 0;
129 }
130