xref: /libCEED/backends/ref/ceed-ref-vector.c (revision 5ea7526be93ee9305780167d3eadf6104744a15b)
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 //------------------------------------------------------------------------------
20 // Vector Set Array
21 //------------------------------------------------------------------------------
22 static int CeedVectorSetArray_Ref(CeedVector vec, CeedMemType mtype,
23                                   CeedCopyMode cmode, CeedScalar *array) {
24   int ierr;
25   CeedVector_Ref *impl;
26   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
27   CeedInt length;
28   ierr = CeedVectorGetLength(vec, &length); CeedChk(ierr);
29   Ceed ceed;
30   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
31 
32   if (mtype != CEED_MEM_HOST)
33     // LCOV_EXCL_START
34     return CeedError(ceed, 1, "Only MemType = HOST supported");
35   // LCOV_EXCL_STOP
36   ierr = CeedFree(&impl->array_allocated); CeedChk(ierr);
37   switch (cmode) {
38   case CEED_COPY_VALUES:
39     ierr = CeedMalloc(length, &impl->array_allocated); CeedChk(ierr);
40     impl->array = impl->array_allocated;
41     if (array) memcpy(impl->array, array, length * sizeof(array[0]));
42     break;
43   case CEED_OWN_POINTER:
44     impl->array_allocated = array;
45     impl->array = array;
46     break;
47   case CEED_USE_POINTER:
48     impl->array = array;
49   }
50   return 0;
51 }
52 
53 //------------------------------------------------------------------------------
54 // Vector Get Array
55 //------------------------------------------------------------------------------
56 static int CeedVectorGetArray_Ref(CeedVector vec, CeedMemType mtype,
57                                   CeedScalar **array) {
58   int ierr;
59   CeedVector_Ref *impl;
60   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
61   Ceed ceed;
62   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
63 
64   if (mtype != CEED_MEM_HOST)
65     // LCOV_EXCL_START
66     return CeedError(ceed, 1, "Can only provide to HOST memory");
67   // LCOV_EXCL_STOP
68   if (!impl->array) { // Allocate if array is not yet allocated
69     ierr = CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL);
70     CeedChk(ierr);
71   }
72   *array = impl->array;
73   return 0;
74 }
75 
76 //------------------------------------------------------------------------------
77 // Vector Get Array Read
78 //------------------------------------------------------------------------------
79 static int CeedVectorGetArrayRead_Ref(CeedVector vec, CeedMemType mtype,
80                                       const CeedScalar **array) {
81   int ierr;
82   CeedVector_Ref *impl;
83   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
84   Ceed ceed;
85   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
86 
87   if (mtype != CEED_MEM_HOST)
88     // LCOV_EXCL_START
89     return CeedError(ceed, 1, "Can only provide to HOST memory");
90   // LCOV_EXCL_STOP
91   if (!impl->array) { // Allocate if array is not yet allocated
92     ierr = CeedVectorSetArray(vec, CEED_MEM_HOST, CEED_COPY_VALUES, NULL);
93     CeedChk(ierr);
94   }
95   *array = impl->array;
96   return 0;
97 }
98 
99 //------------------------------------------------------------------------------
100 // Vector Restore Array
101 //------------------------------------------------------------------------------
102 static int CeedVectorRestoreArray_Ref(CeedVector vec) {
103   return 0;
104 }
105 
106 static int CeedVectorRestoreArrayRead_Ref(CeedVector vec) {
107   return 0;
108 }
109 
110 //------------------------------------------------------------------------------
111 // Vector Destroy
112 //------------------------------------------------------------------------------
113 static int CeedVectorDestroy_Ref(CeedVector vec) {
114   int ierr;
115   CeedVector_Ref *impl;
116   ierr = CeedVectorGetData(vec, (void *)&impl); CeedChk(ierr);
117 
118   ierr = CeedFree(&impl->array_allocated); CeedChk(ierr);
119   ierr = CeedFree(&impl); CeedChk(ierr);
120   return 0;
121 }
122 
123 //------------------------------------------------------------------------------
124 // Vector Create
125 //------------------------------------------------------------------------------
126 int CeedVectorCreate_Ref(CeedInt n, CeedVector vec) {
127   int ierr;
128   CeedVector_Ref *impl;
129   Ceed ceed;
130   ierr = CeedVectorGetCeed(vec, &ceed); CeedChk(ierr);
131 
132   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetArray",
133                                 CeedVectorSetArray_Ref); CeedChk(ierr);
134   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArray",
135                                 CeedVectorGetArray_Ref); CeedChk(ierr);
136   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead",
137                                 CeedVectorGetArrayRead_Ref); CeedChk(ierr);
138   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray",
139                                 CeedVectorRestoreArray_Ref); CeedChk(ierr);
140   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead",
141                                 CeedVectorRestoreArrayRead_Ref); CeedChk(ierr);
142   ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Destroy",
143                                 CeedVectorDestroy_Ref); CeedChk(ierr);
144   ierr = CeedCalloc(1,&impl); CeedChk(ierr);
145   ierr = CeedVectorSetData(vec, (void *)&impl); CeedChk(ierr);
146   return 0;
147 }
148 //------------------------------------------------------------------------------
149