xref: /petsc/src/sys/objects/device/tests/ex2cu.cu (revision 2e956fe4fc852fabc23b437482e1fb7b77fddb0d)
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.31 microseconds
7 */
8 #include <petscsys.h>
9 #include <petscdevice.h>
10 
11 int main(int argc,char **argv)
12 {
13   PetscInt                     i,n=4000;
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   PetscCallCUDA(cudaStreamSynchronize(NULL)); /* Initialize CUDA runtime to get more accurate timing below */
22 
23   PetscCall(PetscMalloc1(n,&ptrs));
24   for (i=0; i<n; i++) {
25     if (i%2) PetscCall(PetscMalloc1(i+16,&ptrs[i]));
26     else PetscCallCUDA(cudaMalloc((void**)&ptrs[i],(i+16)*sizeof(PetscScalar)));
27   }
28 
29   PetscCall(PetscTime(&tstart));
30   for (i=0; i<n; i++) {
31     cerr = cudaPointerGetAttributes(&attr,ptrs[i]);
32     if (cerr) cerr = cudaGetLastError();
33   }
34   PetscCall(PetscTime(&tend));
35   time = (tend-tstart)*1e6/n;
36 
37   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Average cudaPointerGetAttributes() time = %.2f microseconds\n",time));
38 
39   for (i=0; i<n; i++) {
40     if (i%2) PetscCall(PetscFree(ptrs[i]));
41     else PetscCallCUDA(cudaFree(ptrs[i]));
42   }
43   PetscCall(PetscFree(ptrs));
44 
45   PetscCall(PetscFinalize());
46   return 0;
47 }
48 
49 /*TEST
50   build:
51     requires: cuda
52 
53   test:
54     requires: cuda
55     args: -n 2
56     output_file: output/empty.out
57     filter: grep "DOES_NOT_EXIST"
58 
59 TEST*/
60