xref: /libCEED/tests/t124-vector.c (revision b0976d5a9cd61a1a55d5329148196ea026561ed6)
19c774eddSJeremy L Thompson /// @file
29c774eddSJeremy L Thompson /// Test CeedVectorGetArrayWrite to modify array
39c774eddSJeremy L Thompson /// \test Test CeedVectorGetArrayWrite to modify array
4*b0976d5aSZach Atkins 
5*b0976d5aSZach Atkins //TESTARGS(name="length 10") {ceed_resource} 10
6*b0976d5aSZach Atkins //TESTARGS(name="length 0") {ceed_resource} 0
79c774eddSJeremy L Thompson #include <ceed.h>
849aac155SJeremy L Thompson #include <stdio.h>
9*b0976d5aSZach Atkins #include <stdlib.h>
109c774eddSJeremy L Thompson 
main(int argc,char ** argv)119c774eddSJeremy L Thompson int main(int argc, char **argv) {
129c774eddSJeremy L Thompson   Ceed       ceed;
139c774eddSJeremy L Thompson   CeedVector x;
14*b0976d5aSZach Atkins   CeedInt    len = 10;
159c774eddSJeremy L Thompson 
169c774eddSJeremy L Thompson   CeedInit(argv[1], &ceed);
17*b0976d5aSZach Atkins   len = argc > 2 ? atoi(argv[2]) : len;
189c774eddSJeremy L Thompson 
194fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, len, &x);
204fee36f0SJeremy L Thompson   {
214fee36f0SJeremy L Thompson     CeedScalar *writable_array;
229c774eddSJeremy L Thompson 
234fee36f0SJeremy L Thompson     CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &writable_array);
244fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < len; i++) writable_array[i] = 3 * i;
254fee36f0SJeremy L Thompson     CeedVectorRestoreArray(x, &writable_array);
262b730f8bSJeremy L Thompson   }
274fee36f0SJeremy L Thompson   {
284fee36f0SJeremy L Thompson     const CeedScalar *read_array;
294fee36f0SJeremy L Thompson 
304fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(x, CEED_MEM_HOST, (const CeedScalar **)&read_array);
314fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < len; i++) {
324fee36f0SJeremy L Thompson       if (read_array[i] != (CeedScalar)(3 * i)) printf("Error writing array[%" CeedInt_FMT "] = %f\n", i, read_array[i]);
334fee36f0SJeremy L Thompson     }
344fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(x, (const CeedScalar **)&read_array);
354fee36f0SJeremy L Thompson   }
369c774eddSJeremy L Thompson 
379c774eddSJeremy L Thompson   CeedVectorDestroy(&x);
389c774eddSJeremy L Thompson   CeedDestroy(&ceed);
399c774eddSJeremy L Thompson   return 0;
409c774eddSJeremy L Thompson }
41