Skip to content

Instantly share code, notes, and snippets.

@brycelelbach
Created September 10, 2019 04:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycelelbach/5a3bc0f71c9db92ffd438628094839da to your computer and use it in GitHub Desktop.
Save brycelelbach/5a3bc0f71c9db92ffd438628094839da to your computer and use it in GitHub Desktop.
// This is how we run libc++ tests on the GPU without modification.
// We force include this header into each test with `-include`.
__host__ __device__
int fake_main(int, char**);
__global__
void fake_main_kernel(int * ret)
{
*ret = fake_main(0, NULL);
}
#define CUDA_CALL(err, ...) \
do { \
err = __VA_ARGS__; \
if (err != cudaSuccess) \
{ \
printf("CUDA ERROR: %s: %s\n", \
cudaGetErrorName(err), cudaGetErrorString(err)); \
return err; \
} \
} while (false)
int main(int argc, char** argv)
{
// Check if the CUDA driver/runtime are installed and working for sanity.
cudaError_t err;
CUDA_CALL(err, cudaDeviceSynchronize());
list_devices();
int ret = fake_main(argc, argv);
if (ret != 0)
{
return ret;
}
int * cuda_ret = nullptr;
CUDA_CALL(err, cudaMalloc(&cuda_ret, sizeof(int)));
fake_main_kernel<<<1, 1>>>(cuda_ret);
CUDA_CALL(err, cudaGetLastError());
CUDA_CALL(err, cudaDeviceSynchronize());
CUDA_CALL(err, cudaMemcpy(&ret, cuda_ret, sizeof(int), cudaMemcpyDeviceToHost));
CUDA_CALL(err, cudaFree(cuda_ret));
return ret;
}
#define main fake_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment