|
1 | 1 | package tests
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "os/exec"
|
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "syscall" |
5 | 10 | "testing"
|
6 | 11 |
|
7 |
| - "github.com/ingonyama-zk/icicle/v3/wrappers/golang/runtime" |
| 12 | + icicle_runtime "github.com/ingonyama-zk/icicle/v3/wrappers/golang/runtime" |
8 | 13 |
|
9 | 14 | "github.com/stretchr/testify/assert"
|
10 | 15 | )
|
11 | 16 |
|
12 | 17 | func TestGetDeviceType(t *testing.T) {
|
13 | 18 | expectedDeviceName := "test"
|
14 |
| - config := runtime.CreateDevice(expectedDeviceName, 0) |
| 19 | + config := icicle_runtime.CreateDevice(expectedDeviceName, 0) |
15 | 20 | assert.Equal(t, expectedDeviceName, config.GetDeviceType())
|
16 | 21 |
|
17 | 22 | expectedDeviceNameLong := "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttest"
|
18 |
| - configLargeName := runtime.CreateDevice(expectedDeviceNameLong, 1) |
| 23 | + configLargeName := icicle_runtime.CreateDevice(expectedDeviceNameLong, 1) |
19 | 24 | assert.NotEqual(t, expectedDeviceNameLong, configLargeName.GetDeviceType())
|
20 | 25 | }
|
21 | 26 |
|
22 | 27 | func TestIsDeviceAvailable(t *testing.T) {
|
23 |
| - runtime.LoadBackendFromEnvOrDefault() |
24 |
| - dev := runtime.CreateDevice("CUDA", 0) |
25 |
| - _ = runtime.SetDevice(&dev) |
26 |
| - res, err := runtime.GetDeviceCount() |
27 |
| - |
28 |
| - expectedNumDevices, error := exec.Command("nvidia-smi", "-L", "|", "wc", "-l").Output() |
29 |
| - if error != nil { |
30 |
| - t.Skip("Failed to get number of devices") |
| 28 | + dev := icicle_runtime.CreateDevice("CUDA", 0) |
| 29 | + _ = icicle_runtime.SetDevice(&dev) |
| 30 | + res, err := icicle_runtime.GetDeviceCount() |
| 31 | + |
| 32 | + smiCommand := exec.Command("nvidia-smi", "-L") |
| 33 | + smiCommandStdout, _ := smiCommand.StdoutPipe() |
| 34 | + wcCommand := exec.Command("wc", "-l") |
| 35 | + wcCommand.Stdin = smiCommandStdout |
| 36 | + |
| 37 | + smiCommand.Start() |
| 38 | + |
| 39 | + expectedNumDevicesRaw, wcErr := wcCommand.Output() |
| 40 | + smiCommand.Wait() |
| 41 | + |
| 42 | + expectedNumDevicesAsString := strings.TrimRight(string(expectedNumDevicesRaw), " \n\r\t") |
| 43 | + expectedNumDevices, _ := strconv.Atoi(expectedNumDevicesAsString) |
| 44 | + if wcErr != nil { |
| 45 | + t.Skip("Failed to get number of devices:", wcErr) |
31 | 46 | }
|
32 | 47 |
|
33 |
| - assert.Equal(t, runtime.Success, err) |
| 48 | + assert.Equal(t, icicle_runtime.Success, err) |
34 | 49 | assert.Equal(t, expectedNumDevices, res)
|
35 | 50 |
|
36 |
| - err = runtime.LoadBackendFromEnvOrDefault() |
37 |
| - assert.Equal(t, runtime.Success, err) |
38 |
| - devCuda := runtime.CreateDevice("CUDA", 0) |
39 |
| - assert.True(t, runtime.IsDeviceAvailable(&devCuda)) |
40 |
| - devCpu := runtime.CreateDevice("CPU", 0) |
41 |
| - assert.True(t, runtime.IsDeviceAvailable(&devCpu)) |
42 |
| - devInvalid := runtime.CreateDevice("invalid", 0) |
43 |
| - assert.False(t, runtime.IsDeviceAvailable(&devInvalid)) |
| 51 | + assert.Equal(t, icicle_runtime.Success, err) |
| 52 | + devCuda := icicle_runtime.CreateDevice("CUDA", 0) |
| 53 | + assert.True(t, icicle_runtime.IsDeviceAvailable(&devCuda)) |
| 54 | + devCpu := icicle_runtime.CreateDevice("CPU", 0) |
| 55 | + assert.True(t, icicle_runtime.IsDeviceAvailable(&devCpu)) |
| 56 | + devInvalid := icicle_runtime.CreateDevice("invalid", 0) |
| 57 | + assert.False(t, icicle_runtime.IsDeviceAvailable(&devInvalid)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestSetDefaultDevice(t *testing.T) { |
| 61 | + runtime.LockOSThread() |
| 62 | + defer runtime.UnlockOSThread() |
| 63 | + tidOuter := syscall.Gettid() |
| 64 | + |
| 65 | + gpuDevice := icicle_runtime.CreateDevice("CUDA", 0) |
| 66 | + icicle_runtime.SetDefaultDevice(&gpuDevice) |
| 67 | + |
| 68 | + activeDevice, err := icicle_runtime.GetActiveDevice() |
| 69 | + assert.Equal(t, icicle_runtime.Success, err) |
| 70 | + assert.Equal(t, gpuDevice, *activeDevice) |
| 71 | + |
| 72 | + done := make(chan struct{}, 1) |
| 73 | + go func() { |
| 74 | + runtime.LockOSThread() |
| 75 | + defer runtime.UnlockOSThread() |
| 76 | + |
| 77 | + // Ensure we are operating on an OS thread other than the original one |
| 78 | + tidInner := syscall.Gettid() |
| 79 | + for tidInner == tidOuter { |
| 80 | + fmt.Println("Locked thread is the same as original, getting new locked thread") |
| 81 | + runtime.UnlockOSThread() |
| 82 | + runtime.LockOSThread() |
| 83 | + tidInner = syscall.Gettid() |
| 84 | + } |
| 85 | + |
| 86 | + activeDevice, err := icicle_runtime.GetActiveDevice() |
| 87 | + assert.Equal(t, icicle_runtime.Success, err) |
| 88 | + assert.Equal(t, gpuDevice, *activeDevice) |
| 89 | + |
| 90 | + close(done) |
| 91 | + }() |
| 92 | + |
| 93 | + <-done |
| 94 | + |
| 95 | + cpuDevice := icicle_runtime.CreateDevice("CPU", 0) |
| 96 | + icicle_runtime.SetDefaultDevice(&cpuDevice) |
44 | 97 | }
|
45 | 98 |
|
46 | 99 | func TestRegisteredDevices(t *testing.T) {
|
47 |
| - err := runtime.LoadBackendFromEnvOrDefault() |
48 |
| - assert.Equal(t, runtime.Success, err) |
49 |
| - devices, _ := runtime.GetRegisteredDevices() |
| 100 | + devices, _ := icicle_runtime.GetRegisteredDevices() |
50 | 101 | assert.Equal(t, []string{"CUDA", "CPU"}, devices)
|
51 | 102 | }
|
52 | 103 |
|
53 | 104 | func TestDeviceProperties(t *testing.T) {
|
54 |
| - _, err := runtime.GetDeviceProperties() |
55 |
| - assert.Equal(t, runtime.Success, err) |
| 105 | + _, err := icicle_runtime.GetDeviceProperties() |
| 106 | + assert.Equal(t, icicle_runtime.Success, err) |
56 | 107 | }
|
57 | 108 |
|
58 | 109 | func TestActiveDevice(t *testing.T) {
|
59 |
| - runtime.SetDevice(&DEVICE) |
60 |
| - activeDevice, err := runtime.GetActiveDevice() |
61 |
| - assert.Equal(t, runtime.Success, err) |
62 |
| - assert.Equal(t, DEVICE, *activeDevice) |
63 |
| - memory1, err := runtime.GetAvailableMemory() |
64 |
| - if err == runtime.ApiNotImplemented { |
65 |
| - t.Skipf("GetAvailableMemory() function is not implemented on %s device", DEVICE.GetDeviceType()) |
| 110 | + devCpu := icicle_runtime.CreateDevice("CUDA", 0) |
| 111 | + icicle_runtime.SetDevice(&devCpu) |
| 112 | + activeDevice, err := icicle_runtime.GetActiveDevice() |
| 113 | + assert.Equal(t, icicle_runtime.Success, err) |
| 114 | + assert.Equal(t, devCpu, *activeDevice) |
| 115 | + memory1, err := icicle_runtime.GetAvailableMemory() |
| 116 | + if err == icicle_runtime.ApiNotImplemented { |
| 117 | + t.Skipf("GetAvailableMemory() function is not implemented on %s device", devCpu.GetDeviceType()) |
66 | 118 | }
|
67 |
| - assert.Equal(t, runtime.Success, err) |
| 119 | + assert.Equal(t, icicle_runtime.Success, err) |
68 | 120 | assert.Greater(t, memory1.Total, uint(0))
|
69 | 121 | assert.Greater(t, memory1.Free, uint(0))
|
70 | 122 | }
|
0 commit comments