Files
project_6/cccl_upstream/thrust/examples/include/timer.h
EngineX CI 56fd68e7dd [INFRA] Import NVIDIA/CCCL upstream as optimization reference library
CCCL (CUDA C++ Core Libraries) provides:
- CUB: device/block/warp-level GPU primitives (reduce, scan, sort, topk)
- Thrust: high-level parallel algorithms (transform_reduce, sort, scan)
- libcudacxx: CUDA C++ standard library (atomics, barriers, memory)
- cudax: experimental features (memory resources, allocators)
- Tuning policies: per-SM hardware-specific algorithm parameters

Competition optimization vectors mapped to CCCL:
- Output TPS (83% weight): warp_reduce, block_reduce, device_topk
- Input TPS (14% weight): device_scan, block_load, prefetch
- Cache TPS (3% weight): prefix caching strategy patterns
- Memory (0.9 util): pooled/cached/buddy allocators

Source: https://github.com/NVIDIA/cccl (shallow clone, HEAD only)
License: Apache-2.0
2026-07-30 09:35:51 +00:00

101 lines
1.6 KiB
C++

// SPDX-FileCopyrightText: Copyright (c) 2008-2009, NVIDIA Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#pragma once
// A simple timer class
#ifdef __CUDACC__
// use CUDA's high-resolution timers when possible
# include <thrust/system/cuda/error.h>
# include <thrust/system_error.h>
# include <string>
# include <cuda_runtime_api.h>
void cuda_safe_call(cudaError_t error, const std::string& message = "")
{
if (error)
{
throw thrust::system_error(error, thrust::cuda_category(), message);
}
}
struct timer
{
cudaEvent_t start;
cudaEvent_t end;
timer()
{
cuda_safe_call(cudaEventCreate(&start));
cuda_safe_call(cudaEventCreate(&end));
restart();
}
~timer()
{
static_cast<void>(cudaEventDestroy(start));
static_cast<void>(cudaEventDestroy(end));
}
void restart()
{
cuda_safe_call(cudaEventRecord(start, nullptr));
}
double elapsed()
{
cuda_safe_call(cudaEventRecord(end, nullptr));
cuda_safe_call(cudaEventSynchronize(end));
float ms_elapsed;
cuda_safe_call(cudaEventElapsedTime(&ms_elapsed, start, end));
return ms_elapsed / 1e3;
}
double epsilon()
{
return 0.5e-6;
}
};
#else
// fallback to clock()
# include <ctime>
struct timer
{
clock_t start;
clock_t end;
timer()
{
restart();
}
~timer() = default;
void restart()
{
start = clock();
}
double elapsed()
{
end = clock();
return static_cast<double>(end - start) / static_cast<double>(CLOCKS_PER_SEC);
}
double epsilon()
{
return 1.0 / static_cast<double>(CLOCKS_PER_SEC);
}
};
#endif