Files
leleka/tools/cupti-trace/test/test_app.cu
T

43 lines
1.6 KiB
Plaintext

// Tiny CUDA workload for the tracer self-test: kernels, graph launch, pinned/pageable H2D, memset, syncs. Runs ~SECONDS s.
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>
#include <unistd.h>
__global__ void saxpy(float* y, const float* x, float a, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) y[i] = a * x[i] + y[i];
}
int main(int argc, char** argv) {
double secs = argc > 1 ? atof(argv[1]) : 6.0;
const int n = 1 << 22;
float *dx, *dy, *hp, *hq;
cudaMalloc(&dx, n * 4); cudaMalloc(&dy, n * 4);
cudaMallocHost(&hp, n * 4);
hq = (float*)malloc(n * 4);
for (int i = 0; i < n; i++) hp[i] = hq[i] = 1.0f;
cudaStream_t s1, s2; cudaStreamCreate(&s1); cudaStreamCreate(&s2);
cudaGraph_t g; cudaGraphExec_t ge;
cudaStreamBeginCapture(s1, cudaStreamCaptureModeGlobal);
for (int k = 0; k < 8; k++) saxpy<<<n / 256, 256, 0, s1>>>(dy, dx, 2.0f, n);
cudaStreamEndCapture(s1, &g);
cudaGraphInstantiate(&ge, g, 0);
cudaMemset(dy, 0, n * 4);
double t0 = 0; timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); t0 = ts.tv_sec + ts.tv_nsec * 1e-9;
long it = 0;
for (;;) {
cudaMemcpyAsync(dx, hp, n * 4, cudaMemcpyHostToDevice, s2); // pinned
cudaMemcpyAsync(dx, hq, n * 4 / 4, cudaMemcpyHostToDevice, s2); // pageable
cudaStreamSynchronize(s2);
cudaGraphLaunch(ge, s1);
saxpy<<<n / 256, 256, 0, s2>>>(dy, dx, 1.0f, n);
cudaStreamSynchronize(s1);
cudaDeviceSynchronize();
usleep(2000);
it++;
clock_gettime(CLOCK_MONOTONIC, &ts);
if (ts.tv_sec + ts.tv_nsec * 1e-9 - t0 > secs) break;
}
printf("done iterations=%ld\n", it);
return 0;
}