On Aug 4, 2013, at 7:54 PM, Roc Wang <[email protected]> wrote: > Thanks, Barry, > > I meant in ComputeRHS(KSP,Vec,void* ctx), I can only has one ponter > array like double* arr1d which can onle be casted from void *ctxto 1d array > directly. If it is a 3d array outside, I have to use 3 for-iterations to > convert the 1d array ( arr1d ) to a 3d array (arr3d) inside, like > > int i,j,k,cnt; > cnt=0; > double* arr1d=static_cast<double *>(ctx); > for (i=0; i<3; i++) > { for(j=0; j<3; j++) > { for(k=0; k<3;k++) > arr3d[i][j][k]=arr1d[cnt]; > cnt++; > } > } > > but cannot use the arr3d directly inside the function. Is there anyway to > cast *ctx to a 3d array directly? Thanks again.
Sure, you can case any pointer to void and then back to whatever point typer it is. Barry > > > > > int size_in_func > > > double* arr1d=static_cast<double *>(ctx); > > > Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, void*) > > in the interface of KSPSetComputeRHS > > From: [email protected] > > Date: Sun, 4 Aug 2013 17:17:59 -0500 > > CC: [email protected]; [email protected] > > To: [email protected] > > > > > > Sure, just caste it to a void* and then caste it back in the subroutine. > > Note that in parallel you will only want to store on each process the > > "local" values for the field, not all the values. > > > > Barry > > > > On Aug 4, 2013, at 3:35 PM, Roc Wang <[email protected]> wrote: > > > > > Thanks a lot, Dave, > > > > > > I can pass an 1-D array into ComputeRHS via *void ctx. > > > > > > I am using the following procedure: > > > > > > in main() > > > {//------- > > > ... > > > double array_1d[array_size]; > > > > > > ComputeRHS(array_1d); > > > ierr = KSPSetComputeRHS(ksp,ComputeRHS, array_1d); > > > ... > > > //-------- > > > } > > > > > > in ComputeRHS(KSP,Vec,void* ctx ) > > > { > > > int size_in_func > > > double* arr1d=static_cast<double *>(ctx); > > > for (i=0;i<size_in_func; i++) > > > cout<<" ComputeRHS="<<arr1d[i] <<endl; > > > } > > > > > > Definitely, a 3d array can be passed by converting it to 1d in main() > > > before calling KSPSetComputeRHS and back to 3d in ComputeRHS(). However, > > > is it possible to pass 3d array directly? Thanks. > > > > > > Date: Sun, 4 Aug 2013 18:18:54 +0200 > > > Subject: Re: [petsc-users] passing variables into (*func)(KSP, Vec, > > > void*) in the interface of KSPSetComputeRHS > > > From: [email protected] > > > To: [email protected] > > > CC: [email protected] > > > > > > The last argument you give to KSPSetComputeRHS() will be passed into your > > > user defined function ComputeRHS(). All data needed to evaluate the rhs > > > should be made available through that pointer (void *ctx) > > > > > > > > > On Sunday, 4 August 2013, Roc Wang wrote: > > > Hello, I am developing a poisson solver based on the example code > > > petsc-3.4.0/src/ksp/ksp/examples/tutorials/ex45.c. > > > > > > I have a question on utilizing functions > > > KSPSetComputeRHS (KSP ksp,PetscErrorCode (*func)(KSP,Vec,void*),void > > > *ctx) and ComputeRHS(KSP,Vec,void*) which is the (*func)(KSP,Vec,void*) > > > in the interface. > > > > > > > > > Since I have source term generated on the right hand side of Poisson > > > equation and they are computed outside KSPSetComputeRHS() and > > > ComputeRHS(), an array of source term variables must be passed into > > > ComuteRHS() function. From the manual online, it seems the interface of > > > ComputeRHS is only for passing variables from inside of ComputeRHS to > > > KSPSetComputeRHS. There is not a dummy for passing into ComputeRHS. Can > > > anybody give me suggestions on it? Thanks in advance. > > > > > > > > > > >
