skip to content
==> tests/driver.c <==
1/* The table: names, ck messages, an injected fake, and loading on a machine
2 * without the driver (this one: the error names libcuda.so.1). */
3#include "mesh/check.h"
4#include "mesh/cuda_driver.h"
5
6#include <stdlib.h>
7#include <string.h>
8
9static CUresult fake_name(CUresult r, const char **name) {
10    *name = r == 100 ? "CUDA_ERROR_NO_DEVICE" : NULL;
11    return r == 100 ? 0 : 1;
12}
13
14int main(void) {
15    CHECK_S(mesh_cu_result_name(0), "CUDA_SUCCESS");
16    CHECK_S(mesh_cu_result_name(CUDA_ERROR_NOT_READY), "CUDA_ERROR_NOT_READY");
17    CHECK_S(mesh_cu_result_name(2), "CUDA_ERROR_OUT_OF_MEMORY");
18    CHECK(mesh_cu_result_name(12345) == NULL);
19    char *d = mesh_cu_result_debug(12345);
20    CHECK_S(d, "cudaError_enum(12345)");
21    free(d);
22    char *err = NULL;
23    CHECK(mesh_cu_ck(0, "cuInit", &err) && err == NULL);
24    CHECK(!mesh_cu_ck_debug(2, "cuMemAlloc", &err));
25    CHECK_S(err, "cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY");
26    mesh_cuda_fns fake = {0};
27    fake.cuGetErrorName = fake_name;
28    mesh_cuda_set(&fake);
29    CHECK(mesh_cuda() == &fake);
30    CHECK(!mesh_cu_ck(100, "cuInit", &err));
31    CHECK_S(err, "cuInit: CUDA_ERROR_NO_DEVICE");
32    CHECK(!mesh_cu_ck(2, "x", &err));
33    CHECK_S(err, "x: CUDA_ERROR_OUT_OF_MEMORY");
34    mesh_cuda_set(NULL);
35    /* No GPU here: loading fails cleanly (on a GPU machine it succeeds). */
36    if (!mesh_cuda_load(&err)) CHECK(strstr(err, "libcuda.so.1") != NULL);
37    else CHECK(mesh_cuda() != NULL && mesh_cuda()->cuInit != NULL);
38    free(err);
39    return mesh_check_done();
40}