EmptyKernelTest.cpp 6.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#if defined(__NVCC__)

#include <cuda_runtime.h>

// Datatypes
#define hipError_t                                         cudaError_t
#define hipEvent_t                                         cudaEvent_t
#define hipStream_t                                        cudaStream_t

// Enumerations
#define hipSuccess                                         cudaSuccess

// Functions
#define hipEventCreate                                     cudaEventCreate
#define hipEventDestroy                                    cudaEventDestroy
#define hipEventElapsedTime                                cudaEventElapsedTime
#define hipGetErrorString                                  cudaGetErrorString
#define hipEventRecord                                     cudaEventRecord
#define hipStreamCreate                                    cudaStreamCreate
#define hipStreamDestroy                                   cudaStreamDestroy
#define hipStreamSynchronize                               cudaStreamSynchronize

#else

#include <hip/hip_ext.h>
#include <hip/hip_runtime.h>
#include <hsa/hsa_ext_amd.h>

#endif

#include <iostream>
#include <chrono>
#include <algorithm>
#include <vector>
#include <numeric>

// Helper macro for catching HIP errors
#define HIP_CALL(cmd)                                                                   \
    do {                                                                                \
        hipError_t error = (cmd);                                                       \
        if (error != hipSuccess)                                                        \
        {                                                                               \
            std::cerr << "Encountered HIP error (" << hipGetErrorString(error)          \
                      << ") at line " << __LINE__ << " in file " << __FILE__ << "\n";   \
            exit(-1);                                                                   \
        }                                                                               \
    } while (0)


__global__ void EmptyKernel(){};

float calStdDev(const std::vector<float>& allDeltaMs, float mean)
{
  std::vector<float> diff(allDeltaMs.size());
  std::transform(allDeltaMs.begin(), allDeltaMs.end(), diff.begin(), [mean](double x) { return x - mean; });
  double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
  double stdev = std::sqrt(sq_sum / allDeltaMs.size());
  return stdev;
}

int main(int argc, char **argv)
{
  int numIterations = (argc > 1 ? atoi(argv[1]) : 10);
  int gridSize      = (argc > 2 ? atoi(argv[2]) : 1);
  int blockSize     = (argc > 3 ? atoi(argv[3]) : 1);
  int numWarmups    = 3;
  printf("Running %d iterations <<<%d,%d>>>\n", numIterations, gridSize, blockSize);

  // Create events and stream
  hipEvent_t startEvent, stopEvent;
  HIP_CALL(hipEventCreate(&startEvent));
  HIP_CALL(hipEventCreate(&stopEvent));
  hipStream_t stream;
  HIP_CALL(hipStreamCreate(&stream));

  // Run untimed warmup iterations (to cache kernel code)
  for (int iteration = 0; iteration < numWarmups; iteration++)
  {
    EmptyKernel<<<gridSize, blockSize, 0, stream>>>();
  }
  HIP_CALL(hipStreamSynchronize(stream));
  std::vector<float> allGpuDeltaMsec(numIterations);
  std::vector<float> allCpuDeltaMsec(numIterations);

  // Launch empty kernel
  // NOTE: Timing is done per-iteration, instead of batching multiple iterations
  double cpuSum = 0.0;
  double gpuSum = 0.0;
  for (int iteration = 0; iteration < numIterations; iteration++)
  {
    // Start timing
    auto cpuStart = std::chrono::high_resolution_clock::now();
    HIP_CALL(hipEventRecord(startEvent, stream));

    // Launch kernel and wait for completion
    EmptyKernel<<<gridSize, blockSize, 0, stream>>>();
    HIP_CALL(hipEventRecord(stopEvent, stream));
    HIP_CALL(hipStreamSynchronize(stream));

    // Collect timing info
    auto cpuDelta = std::chrono::high_resolution_clock::now() - cpuStart;
    double cpuDeltaMsec = std::chrono::duration_cast<std::chrono::duration<double>>(cpuDelta).count() * 1000.0;
    float gpuDeltaMsec;
    HIP_CALL(hipEventElapsedTime(&gpuDeltaMsec, startEvent, stopEvent));

    // Report timing
    printf("Iteration %03d Kernel Launch Time (usec) %10.5f (CPU) %10.5f (GPU)\n", iteration, cpuDeltaMsec *1000.0, gpuDeltaMsec * 1000.0);
    allGpuDeltaMsec[iteration] = gpuDeltaMsec * 1000.0;
    allCpuDeltaMsec[iteration] = cpuDeltaMsec * 1000.0;
    cpuSum += cpuDeltaMsec * 1000.0;
    gpuSum += gpuDeltaMsec * 1000.0;
  }
  printf("\n");

  // Report averages
  double avgCpuUsec = cpuSum / numIterations;
  double avgGpuUsec = gpuSum / numIterations;
  auto   minCpuUsec = std::min_element(std::begin(allCpuDeltaMsec), std::end(allCpuDeltaMsec));
  auto   minGpuUsec = std::min_element(std::begin(allGpuDeltaMsec), std::end(allGpuDeltaMsec));
  auto   maxCpuUsec = std::max_element(std::begin(allCpuDeltaMsec), std::end(allCpuDeltaMsec));
  auto   maxGpuUsec = std::max_element(std::begin(allGpuDeltaMsec), std::end(allGpuDeltaMsec));
  auto   varCpuUsec = calStdDev(allCpuDeltaMsec, avgCpuUsec);
  auto   varGpuUsec = calStdDev(allGpuDeltaMsec, avgGpuUsec);

  printf("Average       Kernel Launch time (usec) %10.5f (CPU) %10.5f (GPU)\n", avgCpuUsec, avgGpuUsec);
  printf("Minimum       Kernel Launch time (usec) %10.5f (CPU) %10.5f (GPU)\n", *minCpuUsec, *minGpuUsec);
  printf("Maximum       Kernel Launch time (usec) %10.5f (CPU) %10.5f (GPU)\n", *maxCpuUsec, *maxGpuUsec);
  printf("Stddev        Kernel Launch time (usec) %10.5f (CPU) %10.5f (GPU)\n", varCpuUsec, varGpuUsec);
  // Cleanup events and stream
  HIP_CALL(hipStreamDestroy(stream));
  HIP_CALL(hipEventDestroy(startEvent));
  HIP_CALL(hipEventDestroy(stopEvent));

  return 0;
}