xref: /petsc/src/sys/objects/device/tests/ex2cu.cu (revision 3307d110e72ee4e6d2468971620073eb5ff93529)
1 static char help[] = "Benchmarking cudaPointerGetAttributes() time\n";
2 /*
3   Running example on Summit at OLCF:
4   # run with total 1 resource set (RS) (-n1), 1 RS per node (-r1), 1 MPI rank (-a1), 7 cores (-c7) and 1 GPU (-g1) per RS
5   $ jsrun -n1 -a1 -c7 -g1 -r1  ./ex2cu
6     Average cudaPointerGetAttributes() time = 0.29 microseconds
7 */
8 #include <petscsys.h>
9 #include <petscdevice.h>
10 
11 int main(int argc,char **argv)
12 {
13   PetscInt                     i,n=2000;
14   cudaError_t                  cerr;
15   PetscScalar                  **ptrs;
16   PetscLogDouble               tstart,tend,time;
17   struct cudaPointerAttributes attr;
18 
19   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
20   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
21 
22   PetscCall(PetscMalloc1(n,&ptrs));
23   for (i=0; i<n; i++) {
24     if (i%2) PetscCall(PetscMalloc1(i+16,&ptrs[i]));
25     else PetscCallCUDA(cudaMalloc((void**)&ptrs[i],(i+16)*sizeof(PetscScalar)));
26   }
27 
28   PetscCall(PetscTime(&tstart));
29   for (i=0; i<n; i++) {
30     cerr = cudaPointerGetAttributes(&attr,ptrs[i]);
31     if (cerr) cudaGetLastError();
32   }
33   PetscCall(PetscTime(&tend));
34   time = (tend-tstart)*1e6/n;
35 
36   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Average cudaPointerGetAttributes() time = %.2f microseconds\n",time));
37 
38   for (i=0; i<n; i++) {
39     if (i%2) PetscCall(PetscFree(ptrs[i]));
40     else PetscCallCUDA(cudaFree(ptrs[i]));
41   }
42   PetscCall(PetscFree(ptrs));
43 
44   PetscCall(PetscFinalize());
45   return 0;
46 }
47 
48 /*TEST
49   build:
50     requires: cuda
51 
52   test:
53     requires: cuda
54     args: -n 2
55     output_file: output/empty.out
56     filter: grep "DOES_NOT_EXIST"
57 
58 TEST*/
59