| 189 | } |
| 190 | |
| 191 | static int DeviceScope_init(DeviceScope *self, PyObject *args, PyObject *kwargs) { |
| 192 | sz_size_t cpu_cores = 0; |
| 193 | sz_size_t gpu_device = 0; |
| 194 | PyObject *cpu_cores_obj = NULL; |
| 195 | PyObject *gpu_device_obj = NULL; |
| 196 | |
| 197 | static char *kwlist[] = {"cpu_cores", "gpu_device", NULL}; |
| 198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist, &cpu_cores_obj, &gpu_device_obj)) return -1; |
| 199 | |
| 200 | sz_status_t status; |
| 201 | char const *error_detail = NULL; |
| 202 | |
| 203 | if (cpu_cores_obj != NULL && gpu_device_obj != NULL) { |
| 204 | PyErr_SetString(PyExc_ValueError, "Cannot specify both cpu_cores and gpu_device"); |
| 205 | return -1; |
| 206 | } |
| 207 | else if (cpu_cores_obj != NULL) { |
| 208 | if (!PyLong_Check(cpu_cores_obj)) { |
| 209 | PyErr_SetString(PyExc_TypeError, "cpu_cores must be an integer"); |
| 210 | return -1; |
| 211 | } |
| 212 | cpu_cores = PyLong_AsSize_t(cpu_cores_obj); |
| 213 | if (cpu_cores == (sz_size_t)-1 && PyErr_Occurred()) { return -1; } |
| 214 | status = szs_device_scope_init_cpu_cores(cpu_cores, &self->handle, &error_detail); |
| 215 | if (cpu_cores == 1) { snprintf(self->description, sizeof(self->description), "default"); } |
| 216 | else if (cpu_cores == 0) { snprintf(self->description, sizeof(self->description), "CPUs:all"); } |
| 217 | else { snprintf(self->description, sizeof(self->description), "CPUs:%zu", cpu_cores); } |
| 218 | } |
| 219 | else if (gpu_device_obj != NULL) { |
| 220 | if (!PyLong_Check(gpu_device_obj)) { |
| 221 | PyErr_SetString(PyExc_TypeError, "gpu_device must be an integer"); |
| 222 | return -1; |
| 223 | } |
| 224 | gpu_device = PyLong_AsSize_t(gpu_device_obj); |
| 225 | if (gpu_device == (sz_size_t)-1 && PyErr_Occurred()) { return -1; } |
| 226 | status = szs_device_scope_init_gpu_device(gpu_device, &self->handle, &error_detail); |
| 227 | snprintf(self->description, sizeof(self->description), "GPU:%zu", gpu_device); |
| 228 | } |
| 229 | else { |
| 230 | status = szs_device_scope_init_default(&self->handle, &error_detail); |
| 231 | snprintf(self->description, sizeof(self->description), "default"); |
| 232 | } |
| 233 | |
| 234 | if (status != sz_success_k) { |
| 235 | set_stringzilla_error(status, error_detail, "DeviceScope initialization"); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static PyObject *DeviceScope_repr(DeviceScope *self) { |
| 243 | return PyUnicode_FromFormat("DeviceScope(%s)", self->description); |
nothing calls this directly
no test coverage detected
searching dependent graphs…