Skip to content

Instantly share code, notes, and snippets.

@malfet
Last active November 8, 2024 20:32
Show Gist options
  • Save malfet/b9488d51110f431eac1af2c0159eafd5 to your computer and use it in GitHub Desktop.
Save malfet/b9488d51110f431eac1af2c0159eafd5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cuda_runtime.h>
__host__ __device__ int return_two() {
#if defined(__CUDA_ARCH__)
return 3;
#else
return 2;
#endif
}
__global__ void kernel() {
printf("CUDA return_two()=%d\n", return_two());
}
template<typename T>
void host_function() {
#if defined(__CUDA_ARCH__)
static_assert(false && sizeof(T), " Don't call me from device code");
#endif
printf("host return_two()=%d\n", return_two());
}
int main() {
#if 0
// This will fail to compile, even though function is not use from CUDA code
host_function<int>();
#else
printf("host return_two()=%d\n", return_two());
#endif
kernel<<<1, 1>>>();
cudaError_t rc = cudaGetLastError();
if (rc != cudaSuccess)
printf("cudaGetLastError()=%s(%d)\n", cudaGetErrorString(rc), rc);
rc = cudaDeviceSynchronize();
if (rc != cudaSuccess)
printf("cudaDeviceSynchronize()=%s(%d)\n", cudaGetErrorString(rc), rc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment