-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtest_backend_device.cpp
More file actions
53 lines (46 loc) · 1.66 KB
/
Copy pathtest_backend_device.cpp
File metadata and controls
53 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Device selection via PARAKEET_DEVICE (issue #17).
//
// Runs on any build, including the CPU-only one used in CI: it exercises the
// env-var parsing and fallback paths that don't need a GPU to be present.
// - PARAKEET_DEVICE=cpu -> CPU backend.
// - PARAKEET_DEVICE=<unknown> -> no such device, falls back to CPU.
// - unset -> CPU on a CPU-only build (auto-pick).
// On a GPU build the unset/auto case may select a GPU/integrated-GPU device, so
// we don't assert a specific name there.
#include "backend.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
static int failures = 0;
static void expect_cpu(const char* label, const std::string& name) {
if (name != "cpu") {
std::fprintf(stderr, "FAIL [%s]: expected device 'cpu', got '%s'\n",
label, name.c_str());
++failures;
} else {
std::printf("ok [%s]: device = %s\n", label, name.c_str());
}
}
int main() {
// Forcing CPU must always yield the CPU backend.
setenv("PARAKEET_DEVICE", "cpu", 1);
{
pk::Backend b(1);
expect_cpu("PARAKEET_DEVICE=cpu", b.device_name());
}
// An unknown device name must not crash; it falls back to CPU.
setenv("PARAKEET_DEVICE", "definitely-not-a-real-device-9000", 1);
{
pk::Backend b(1);
expect_cpu("PARAKEET_DEVICE=<unknown>", b.device_name());
}
// Case-insensitive "CPU" is also honored as a CPU force.
setenv("PARAKEET_DEVICE", "CPU", 1);
{
pk::Backend b(1);
expect_cpu("PARAKEET_DEVICE=CPU", b.device_name());
}
unsetenv("PARAKEET_DEVICE");
return failures == 0 ? 0 : 1;
}