Crafter.Build/tests/CudaCompile/fixture/kernel.cu
Jorijn van der Graaf ad021ee652
test(cuda): build a .cu with the toolkit CUDA_PATH names, and with none
CudaCompile puts an nvcc on PATH that fails, names a toolkit through
CUDA_PATH whose nvcc writes down its arguments and runs the real one,
and builds an executable from a C++ unit and a .cu: with the default
architecture, then with another one, which compiles the unchanged .cu
again in a directory of its own, then with nothing changed, which
compiles nothing. With the bare "nvcc" of before, the first build runs
the nvcc on PATH and fails.

It needs a real toolkit, which it looks for as crafter-build does, with
ResolveToolchain and the inputs GetToolchain() gives it, and it skips
without one. Nothing in CI installs one, so AGENTS.template.md says to
run it in the native loop.

CudaMissing needs none, so CI runs it: with CUDA_PATH naming an empty
directory, a build with a .cu fails with "cannot compile kernel.cu: no
nvcc at <directory>/bin/nvcc (CUDA_PATH)", and a build without CUDA
sources in the same process still succeeds. With the bare "nvcc" of
before, that build ran whatever nvcc PATH held, and where PATH held
none, as in the Flatpak, failed with "sh: line 1: nvcc: command not
found". AGENTS.template.md names it beside CudaCompile.
2026-11-11 19:34:52 +01:00

20 lines
751 B
Text

// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#include <cstdint>
#include <cuda_runtime.h>
__global__ void Double(float* values, std::int32_t count) {
const std::int32_t i = static_cast<std::int32_t>(blockIdx.x * blockDim.x + threadIdx.x);
if (i < count) values[i] = 2.0f * static_cast<float>(i);
}
extern "C" std::int32_t LaunchDouble(std::int32_t count, float* values) {
float* device = nullptr;
cudaError_t status = cudaMalloc(&device, sizeof(float) * count);
if (status != cudaSuccess) return status;
Double<<<1, 32>>>(device, count);
status = cudaMemcpy(values, device, sizeof(float) * count, cudaMemcpyDeviceToHost);
cudaFree(device);
return status;
}