Actual source code: ex1.c

petsc-3.5.1 2014-08-06
Report Typos and Errors
  1: static char help[] = "Test PetscThreadComm Interface.\n\n";

  3: /*T
  4:    Concepts: PetscThreadComm^basic example
  5: T*/

  7: /*
  8:   Include "petscthreadcomm.h" so that we can use the PetscThreadComm interface.
  9: */
 10: #include <petscthreadcomm.h>

 12: PetscErrorCode kernel_func1(PetscInt myrank,PetscInt *ranks,PetscScalar *values)
 13: {
 14:   values[myrank] *= 1;
 15:   printf("First Kernel:My rank is %d, x = %f\n",ranks[myrank],PetscRealPart(values[myrank]));
 16:   return(0);
 17: }

 19: PetscErrorCode kernel_func2(PetscInt myrank,PetscInt *ranks,PetscScalar *values)
 20: {
 21:   values[myrank] *= 2;
 22:   printf("Second Kernel:My rank is %d, x = %f\n",ranks[myrank],PetscRealPart(values[myrank]));
 23:   return(0);

 25: }

 29: int main(int argc,char **argv)
 30: {
 32:   PetscInt       nthreads,i;
 33:   PetscInt       *ranks;
 34:   PetscScalar    *values;

 36:   PetscInitialize(&argc,&argv,(char*)0,help);

 38:   PetscThreadCommView(PETSC_COMM_WORLD,0);
 39:   PetscThreadCommGetNThreads(PETSC_COMM_WORLD,&nthreads);

 41:   PetscMalloc1(nthreads,&ranks);
 42:   PetscMalloc1(nthreads,&values);

 44:   for (i=0; i < nthreads; i++) {
 45:     ranks[i] = i; values[i] = i;
 46:   }

 48:   PetscThreadCommRunKernel(PETSC_COMM_WORLD,(PetscThreadKernel)kernel_func1,2,ranks,values);
 49:   PetscThreadCommRunKernel(PETSC_COMM_WORLD,(PetscThreadKernel)kernel_func2,2,ranks,values);

 51:   PetscThreadCommBarrier(PETSC_COMM_WORLD);
 52:   PetscFree(ranks);
 53:   PetscFree(values);
 54:   PetscFinalize();
 55:   return 0;
 56: }