gpu_info_oneapi.c 7.85 KB
Newer Older
mashun1's avatar
v1  
mashun1 committed
1
2
3
4
5
6
#ifndef __APPLE__

#include "gpu_info_oneapi.h"

#include <string.h>

xuxzh1's avatar
init  
xuxzh1 committed
7
void oneapi_init(char *oneapi_lib_path, oneapi_init_resp_t *resp) {
mashun1's avatar
v1  
mashun1 committed
8
9
  ze_result_t ret;
  resp->err = NULL;
xuxzh1's avatar
init  
xuxzh1 committed
10
11
12
13
  resp->oh.devices = NULL;
  resp->oh.num_devices = NULL;
  resp->oh.drivers = NULL;
  resp->oh.num_drivers = 0;
mashun1's avatar
v1  
mashun1 committed
14
15
  const int buflen = 256;
  char buf[buflen + 1];
xuxzh1's avatar
init  
xuxzh1 committed
16
17
  int i, d;
  struct lookup {
mashun1's avatar
v1  
mashun1 committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    char *s;
    void **p;
  } l[] = {
      {"zesInit", (void *)&resp->oh.zesInit},
      {"zesDriverGet", (void *)&resp->oh.zesDriverGet},
      {"zesDeviceGet", (void *)&resp->oh.zesDeviceGet},
      {"zesDeviceGetProperties", (void *)&resp->oh.zesDeviceGetProperties},
      {"zesDeviceEnumMemoryModules",
       (void *)&resp->oh.zesDeviceEnumMemoryModules},
      {"zesMemoryGetProperties", (void *)&resp->oh.zesMemoryGetProperties},
      {"zesMemoryGetState", (void *)&resp->oh.zesMemoryGetState},
      {NULL, NULL},
  };

  resp->oh.handle = LOAD_LIBRARY(oneapi_lib_path, RTLD_LAZY);
xuxzh1's avatar
init  
xuxzh1 committed
33
  if (!resp->oh.handle) {
mashun1's avatar
v1  
mashun1 committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    char *msg = LOAD_ERR();
    snprintf(buf, buflen,
             "Unable to load %s library to query for Intel GPUs: %s\n",
             oneapi_lib_path, msg);
    free(msg);
    resp->err = strdup(buf);
    return;
  }

  // TODO once we've squashed the remaining corner cases remove this log
  LOG(resp->oh.verbose,
      "wiring Level-Zero management library functions in %s\n",
      oneapi_lib_path);

xuxzh1's avatar
init  
xuxzh1 committed
48
  for (i = 0; l[i].s != NULL; i++) {
mashun1's avatar
v1  
mashun1 committed
49
50
51
52
    // TODO once we've squashed the remaining corner cases remove this log
    LOG(resp->oh.verbose, "dlsym: %s\n", l[i].s);

    *l[i].p = LOAD_SYMBOL(resp->oh.handle, l[i].s);
xuxzh1's avatar
init  
xuxzh1 committed
53
    if (!*(l[i].p)) {
mashun1's avatar
v1  
mashun1 committed
54
55
56
57
58
59
60
61
62
63
64
      resp->oh.handle = NULL;
      char *msg = LOAD_ERR();
      LOG(resp->oh.verbose, "dlerr: %s\n", msg);
      UNLOAD_LIBRARY(resp->oh.handle);
      snprintf(buf, buflen, "symbol lookup for %s failed: %s", l[i].s, msg);
      free(msg);
      resp->err = strdup(buf);
      return;
    }
  }

xuxzh1's avatar
init  
xuxzh1 committed
65
66
  LOG(resp->oh.verbose, "calling zesInit\n");

mashun1's avatar
v1  
mashun1 committed
67
  ret = (*resp->oh.zesInit)(0);
xuxzh1's avatar
init  
xuxzh1 committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  if (ret != ZE_RESULT_SUCCESS) {
    LOG(resp->oh.verbose, "zesInit err: %x\n", ret);
    snprintf(buf, buflen, "oneapi vram init failure: %x", ret);
    resp->err = strdup(buf);
    oneapi_release(resp->oh);
    return;
  }

  LOG(resp->oh.verbose, "calling zesDriverGet\n");
  ret = (*resp->oh.zesDriverGet)(&resp->oh.num_drivers, NULL);
  if (ret != ZE_RESULT_SUCCESS) {
    LOG(resp->oh.verbose, "zesDriverGet err: %x\n", ret);
    snprintf(buf, buflen, "unable to get driver count: %x", ret);
    resp->err = strdup(buf);
    oneapi_release(resp->oh);
    return;
  }
  LOG(resp->oh.verbose, "oneapi driver count: %d\n", resp->oh.num_drivers);
  resp->oh.drivers = malloc(resp->oh.num_drivers * sizeof(zes_driver_handle_t));
  resp->oh.num_devices = malloc(resp->oh.num_drivers * sizeof(uint32_t));
  memset(&resp->oh.num_devices[0], 0, resp->oh.num_drivers * sizeof(uint32_t));
  resp->oh.devices =
      malloc(resp->oh.num_drivers * sizeof(zes_device_handle_t *));
  ret = (*resp->oh.zesDriverGet)(&resp->oh.num_drivers, &resp->oh.drivers[0]);
  if (ret != ZE_RESULT_SUCCESS) {
    LOG(resp->oh.verbose, "zesDriverGet err: %x\n", ret);
    snprintf(buf, buflen, "unable to get driver count: %x", ret);
mashun1's avatar
v1  
mashun1 committed
95
    resp->err = strdup(buf);
xuxzh1's avatar
init  
xuxzh1 committed
96
97
    oneapi_release(resp->oh);
    return;
mashun1's avatar
v1  
mashun1 committed
98
99
  }

xuxzh1's avatar
init  
xuxzh1 committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  for (d = 0; d < resp->oh.num_drivers; d++) {
    LOG(resp->oh.verbose, "calling zesDeviceGet count %d: %p\n", d, resp->oh.drivers[d]);
    ret = (*resp->oh.zesDeviceGet)(resp->oh.drivers[d],
                                   &resp->oh.num_devices[d], NULL);
    if (ret != ZE_RESULT_SUCCESS) {
      LOG(resp->oh.verbose, "zesDeviceGet err: %x\n", ret);
      snprintf(buf, buflen, "unable to get device count: %x", ret);
      resp->err = strdup(buf);
      oneapi_release(resp->oh);
      return;
    }
    resp->oh.devices[d] =
        malloc(resp->oh.num_devices[d] * sizeof(zes_device_handle_t));
    ret = (*resp->oh.zesDeviceGet)(
        resp->oh.drivers[d], &resp->oh.num_devices[d], resp->oh.devices[d]);
    if (ret != ZE_RESULT_SUCCESS) {
      LOG(resp->oh.verbose, "zesDeviceGet err: %x\n", ret);
      snprintf(buf, buflen, "unable to get device count: %x", ret);
      resp->err = strdup(buf);
      oneapi_release(resp->oh);
      return;
    }
  }
mashun1's avatar
v1  
mashun1 committed
123
124
125
126

  return;
}

xuxzh1's avatar
init  
xuxzh1 committed
127
128
void oneapi_check_vram(oneapi_handle_t h, int driver, int device,
                       mem_info_t *resp) {
mashun1's avatar
v1  
mashun1 committed
129
130
131
132
133
134
135
136
  ze_result_t ret;
  resp->err = NULL;
  uint64_t totalMem = 0;
  uint64_t usedMem = 0;
  const int buflen = 256;
  char buf[buflen + 1];
  int i, d, m;

xuxzh1's avatar
init  
xuxzh1 committed
137
  if (h.handle == NULL) {
mashun1's avatar
v1  
mashun1 committed
138
139
140
141
    resp->err = strdup("Level-Zero handle not initialized");
    return;
  }

xuxzh1's avatar
init  
xuxzh1 committed
142
143
  if (driver > h.num_drivers || device > h.num_devices[driver]) {
    resp->err = strdup("driver of device index out of bounds");
mashun1's avatar
v1  
mashun1 committed
144
145
146
147
148
149
    return;
  }

  resp->total = 0;
  resp->free = 0;

xuxzh1's avatar
init  
xuxzh1 committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  zes_device_ext_properties_t ext_props;
  ext_props.stype = ZES_STRUCTURE_TYPE_DEVICE_EXT_PROPERTIES;
  ext_props.pNext = NULL;

  zes_device_properties_t props;
  props.stype = ZES_STRUCTURE_TYPE_DEVICE_PROPERTIES;
  props.pNext = &ext_props;

  ret = (*h.zesDeviceGetProperties)(h.devices[driver][device], &props);
  if (ret != ZE_RESULT_SUCCESS) {
    snprintf(buf, buflen, "unable to get device properties: %d", ret);
    resp->err = strdup(buf);
    return;
  }

  snprintf(&resp->gpu_name[0], GPU_NAME_LEN, "%s", props.modelName);

  // TODO this needs to map to ONEAPI_DEVICE_SELECTOR syntax
  // (this is probably wrong...)
  // TODO - the driver isn't included - what if there are multiple drivers?
  snprintf(&resp->gpu_id[0], GPU_ID_LEN, "%d", device);

  if (h.verbose) {
    // When in verbose mode, report more information about
    // the card we discover.
    LOG(h.verbose, "[%d:%d] oneAPI device name: %s\n", driver, device,
        props.modelName);
    LOG(h.verbose, "[%d:%d] oneAPI brand: %s\n", driver, device,
        props.brandName);
    LOG(h.verbose, "[%d:%d] oneAPI vendor: %s\n", driver, device,
        props.vendorName);
    LOG(h.verbose, "[%d:%d] oneAPI S/N: %s\n", driver, device,
        props.serialNumber);
    LOG(h.verbose, "[%d:%d] oneAPI board number: %s\n", driver, device,
        props.boardNumber);
  }

  // TODO
  // Compute Capability equivalent in resp->major, resp->minor, resp->patch

  uint32_t memCount = 0;
  ret = (*h.zesDeviceEnumMemoryModules)(h.devices[driver][device], &memCount,
                                        NULL);
  if (ret != ZE_RESULT_SUCCESS) {
    snprintf(buf, buflen, "unable to enumerate Level-Zero memory modules: %x",
             ret);
    resp->err = strdup(buf);
    return;
  }

  LOG(h.verbose, "discovered %d Level-Zero memory modules\n", memCount);

  zes_mem_handle_t *mems = malloc(memCount * sizeof(zes_mem_handle_t));
  (*h.zesDeviceEnumMemoryModules)(h.devices[driver][device], &memCount, mems);

  for (m = 0; m < memCount; m++) {
    zes_mem_state_t state;
    state.stype = ZES_STRUCTURE_TYPE_MEM_STATE;
    state.pNext = NULL;
    ret = (*h.zesMemoryGetState)(mems[m], &state);
    if (ret != ZE_RESULT_SUCCESS) {
      snprintf(buf, buflen, "unable to get memory state: %x", ret);
mashun1's avatar
v1  
mashun1 committed
212
      resp->err = strdup(buf);
xuxzh1's avatar
init  
xuxzh1 committed
213
      free(mems);
mashun1's avatar
v1  
mashun1 committed
214
215
216
      return;
    }

xuxzh1's avatar
init  
xuxzh1 committed
217
218
219
    resp->total += state.size;
    resp->free += state.free;
  }
mashun1's avatar
v1  
mashun1 committed
220

xuxzh1's avatar
init  
xuxzh1 committed
221
222
  free(mems);
}
mashun1's avatar
v1  
mashun1 committed
223

xuxzh1's avatar
init  
xuxzh1 committed
224
225
226
227
228
229
230
231
232
233
234
void oneapi_release(oneapi_handle_t h) {
  int d;
  LOG(h.verbose, "releasing oneapi library\n");
  for (d = 0; d < h.num_drivers; d++) {
    if (h.devices != NULL && h.devices[d] != NULL) {
      free(h.devices[d]);
    }
  }
  if (h.devices != NULL) {
    free(h.devices);
    h.devices = NULL;
mashun1's avatar
v1  
mashun1 committed
235
  }
xuxzh1's avatar
init  
xuxzh1 committed
236
237
238
239
240
241
242
243
244
245
246
247
  if (h.num_devices != NULL) {
    free(h.num_devices);
    h.num_devices = NULL;
  }
  if (h.drivers != NULL) {
    free(h.drivers);
    h.drivers = NULL;
  }
  h.num_drivers = 0;
  UNLOAD_LIBRARY(h.handle);
  h.handle = NULL;
}
mashun1's avatar
v1  
mashun1 committed
248

xuxzh1's avatar
init  
xuxzh1 committed
249
250
251
252
253
254
255
256
int oneapi_get_device_count(oneapi_handle_t h, int driver) {
  if (h.handle == NULL || h.num_devices == NULL) {
    return 0;
  }
  if (driver > h.num_drivers) {
    return 0;
  }
  return (int)h.num_devices[driver];
mashun1's avatar
v1  
mashun1 committed
257
258
259
}

#endif // __APPLE__