@@ -49,15 +49,21 @@ static int CeedVectorSetArray_Memcheck(CeedVector vec, CeedMemType mem_type, Cee
4949
5050 CeedCheck (mem_type == CEED_MEM_HOST , CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND , "Can only set HOST memory for this backend" );
5151
52+ if (impl -> array_allocated ) {
53+ for (CeedSize i = 0 ; i < length ; i ++ ) impl -> array_allocated [i ] = NAN ;
54+ }
5255 CeedCallBackend (CeedFree (& impl -> array_allocated ));
56+ if (impl -> array_owned ) {
57+ for (CeedSize i = 0 ; i < length ; i ++ ) impl -> array_owned [i ] = NAN ;
58+ }
5359 CeedCallBackend (CeedFree (& impl -> array_owned ));
5460 switch (copy_mode ) {
5561 case CEED_COPY_VALUES :
5662 CeedCallBackend (CeedCalloc (length , & impl -> array_owned ));
5763 impl -> array_borrowed = NULL ;
5864 impl -> array = impl -> array_owned ;
5965 if (array ) {
60- memcpy (impl -> array , array , length * sizeof (array [ 0 ] ));
66+ memcpy (impl -> array , array , length * sizeof (CeedScalar ));
6167 } else {
6268 for (CeedInt i = 0 ; i < length ; i ++ ) impl -> array [i ] = NAN ;
6369 }
@@ -73,27 +79,32 @@ static int CeedVectorSetArray_Memcheck(CeedVector vec, CeedMemType mem_type, Cee
7379 }
7480 // Copy data to check access
7581 CeedCallBackend (CeedCalloc (length , & impl -> array_allocated ));
76- memcpy (impl -> array_allocated , impl -> array , length * sizeof (array [ 0 ] ));
82+ memcpy (impl -> array_allocated , impl -> array , length * sizeof (CeedScalar ));
7783 impl -> array = impl -> array_allocated ;
7884 VALGRIND_DISCARD (impl -> mem_block_id );
79- impl -> mem_block_id = VALGRIND_CREATE_BLOCK (impl -> array , length * sizeof (array [ 0 ] ), "'Vector backend array data copy'" );
85+ impl -> mem_block_id = VALGRIND_CREATE_BLOCK (impl -> array , length * sizeof (CeedScalar ), "'Vector backend array data copy'" );
8086 return CEED_ERROR_SUCCESS ;
8187}
8288
8389//------------------------------------------------------------------------------
8490// Vector Take Array
8591//------------------------------------------------------------------------------
8692static int CeedVectorTakeArray_Memcheck (CeedVector vec , CeedMemType mem_type , CeedScalar * * array ) {
93+ CeedSize length ;
8794 CeedVector_Memcheck * impl ;
8895
8996 CeedCallBackend (CeedVectorGetData (vec , & impl ));
97+ CeedCallBackend (CeedVectorGetLength (vec , & length ));
9098
9199 CeedCheck (mem_type == CEED_MEM_HOST , CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND , "Can only provide HOST memory for this backend" );
92100
93101 (* array ) = impl -> array_borrowed ;
94102 impl -> array_borrowed = NULL ;
95103 impl -> array = NULL ;
96104 VALGRIND_DISCARD (impl -> mem_block_id );
105+ if (impl -> array_allocated ) {
106+ for (CeedSize i = 0 ; i < length ; i ++ ) impl -> array_allocated [i ] = NAN ;
107+ }
97108 CeedCallBackend (CeedFree (& impl -> array_allocated ));
98109 return CEED_ERROR_SUCCESS ;
99110}
@@ -111,7 +122,7 @@ static int CeedVectorGetArray_Memcheck(CeedVector vec, CeedMemType mem_type, Cee
111122 CeedCheck (mem_type == CEED_MEM_HOST , CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND , "Can only provide HOST memory for this backend" );
112123
113124 CeedCallBackend (CeedCalloc (length , & impl -> array_writable_copy ));
114- memcpy (impl -> array_writable_copy , impl -> array , length * sizeof (( impl -> array )[ 0 ] ));
125+ memcpy (impl -> array_writable_copy , impl -> array , length * sizeof (CeedScalar ));
115126 * array = impl -> array_writable_copy ;
116127 return CEED_ERROR_SUCCESS ;
117128}
@@ -129,11 +140,11 @@ static int CeedVectorGetArrayRead_Memcheck(CeedVector vec, CeedMemType mem_type,
129140 CeedCheck (mem_type == CEED_MEM_HOST , CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND , "Can only provide HOST memory for this backend" );
130141
131142 // Make copy to verify no write occurred
132- * array = impl -> array ;
133143 if (!impl -> array_read_only_copy ) {
134144 CeedCallBackend (CeedCalloc (length , & impl -> array_read_only_copy ));
135- memcpy (impl -> array_read_only_copy , * array , length * sizeof (( * array )[ 0 ] ));
145+ memcpy (impl -> array_read_only_copy , impl -> array , length * sizeof (CeedScalar ));
136146 }
147+ * array = impl -> array_read_only_copy ;
137148 return CEED_ERROR_SUCCESS ;
138149}
139150
@@ -167,7 +178,8 @@ static int CeedVectorRestoreArray_Memcheck(CeedVector vec) {
167178 CeedCallBackend (CeedVectorGetLength (vec , & length ));
168179 CeedCallBackend (CeedVectorGetCeed (vec , & ceed ));
169180
170- memcpy (impl -> array , impl -> array_writable_copy , length * sizeof ((impl -> array )[0 ]));
181+ memcpy (impl -> array , impl -> array_writable_copy , length * sizeof (CeedScalar ));
182+ for (CeedSize i = 0 ; i < length ; i ++ ) impl -> array_writable_copy [i ] = NAN ;
171183 CeedCallBackend (CeedFree (& impl -> array_writable_copy ));
172184 if (impl -> is_write_only_access ) {
173185 for (CeedSize i = 0 ; i < length ; i ++ ) {
@@ -177,10 +189,10 @@ static int CeedVectorRestoreArray_Memcheck(CeedVector vec) {
177189 impl -> is_write_only_access = false;
178190 }
179191 if (impl -> array_borrowed ) {
180- memcpy (impl -> array_borrowed , impl -> array , length * sizeof (impl -> array [ 0 ] ));
192+ memcpy (impl -> array_borrowed , impl -> array , length * sizeof (CeedScalar ));
181193 }
182194 if (impl -> array_owned ) {
183- memcpy (impl -> array_owned , impl -> array , length * sizeof (impl -> array [ 0 ] ));
195+ memcpy (impl -> array_owned , impl -> array , length * sizeof (CeedScalar ));
184196 }
185197 return CEED_ERROR_SUCCESS ;
186198}
@@ -195,9 +207,10 @@ static int CeedVectorRestoreArrayRead_Memcheck(CeedVector vec) {
195207 CeedCallBackend (CeedVectorGetData (vec , & impl ));
196208 CeedCallBackend (CeedVectorGetLength (vec , & length ));
197209
198- CeedCheck (!memcmp (impl -> array , impl -> array_read_only_copy , length * sizeof (impl -> array [ 0 ] )), CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND ,
210+ CeedCheck (!memcmp (impl -> array , impl -> array_read_only_copy , length * sizeof (CeedScalar )), CeedVectorReturnCeed (vec ), CEED_ERROR_BACKEND ,
199211 "Array data changed while accessed in read-only mode" );
200212
213+ for (CeedSize i = 0 ; i < length ; i ++ ) impl -> array_read_only_copy [i ] = NAN ;
201214 CeedCallBackend (CeedFree (& impl -> array_read_only_copy ));
202215 return CEED_ERROR_SUCCESS ;
203216}
0 commit comments