If you want your code to be portable, you shouldn't definitely not assume that PetscScalar is the same as double, nor should you assume that PetscInt is the same as int. The reason is that depending on how you configured PETSc, these types can be mapped to different different types.
For example, PetscScalar could be mapped to PetscScalar ==> PetscReal == double (default) PetscScalar ==> PetscReal ==> float (--with-precision=single) PetscScalar ==> double precision complex number (--with-sclar-type=complex) whilst PetscInt ==> 32 bit int (default) PetscInt ==> 64 bit int (--with-64bit-indices) Cheers, Dave On 23 November 2013 07:40, Justin Dong <[email protected]> wrote: > I’m still trying to get the hang of PETSc but am adapting pretty well. I > was cleaning up some code I wrote today and realized that my function > prototype expects a pointer to a double but I accidentally gave it a > pointer to a PetscScalar instead. I didn't get any errors during compiling > or execution so I was just wondering if the two types are compatible. The > same goes for PetscInt and ints. Here's a minimal example below. Is this > considered a major error? I'm correcting it anyway but just wanted to > satisfy my curiosity. > > > #define A(i,j) A[ j + 5*i ] > #define b(i,j) b[ j + 5*i ] > > void myFunction(double* A, int* b) > { > > int i, j; > > for (i=0; i<5, ++i) > { > for (j=0; j<5; ++j) > { > A(i,j) = sin(i+j); > b(i,j) = i+j; > } > } > > } > > int main(int argc, char **argv) > { > > PetscErrorCode ierr; > > ierr = PetscInitialize(&argc,&argv,(char*)0,help); CHKERRQ(ierr); > PetscScalar* A; > PetscInt* b; > > int n = 5; > > PetscMalloc(sizeof(PetscScalar)*n*n, &A); > PetscMalloc(sizeof(PetscInt)*n*n, &b); > > myFunction(A, b); > > PetscFree(A); > PetscFree(b); > > ierr = PetscFinalize(); > > return 0; > } > > Thanks, > Justin >
