|
1 | 1 | // Note: porting this file to C++ is a work in progress |
2 | 2 |
|
| 3 | +#ifdef _WIN32 |
| 4 | +#define WIN32_LEAN_AND_MEAN |
| 5 | +#ifndef NOMINMAX |
| 6 | +# define NOMINMAX |
| 7 | +#endif |
| 8 | +#include <windows.h> |
| 9 | +#endif |
| 10 | + |
3 | 11 | #include "ggml-backend-impl.h" |
4 | 12 | #include "ggml-alloc.h" |
5 | 13 | #include "ggml-impl.h" |
|
10 | 18 | #include <stdio.h> |
11 | 19 | #include <stdlib.h> |
12 | 20 | #include <string.h> |
13 | | - |
| 21 | +#include <string> |
14 | 22 | #include <vector> |
15 | 23 |
|
| 24 | +#ifdef __APPLE__ |
| 25 | +#include <sys/types.h> |
| 26 | +#include <sys/sysctl.h> |
| 27 | +#endif |
| 28 | + |
| 29 | + |
16 | 30 | // backend buffer type |
17 | 31 |
|
18 | 32 | const char * ggml_backend_buft_name(ggml_backend_buffer_type_t buft) { |
@@ -1021,17 +1035,80 @@ ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size) |
1021 | 1035 |
|
1022 | 1036 | //////////////////////// |
1023 | 1037 |
|
| 1038 | +struct ggml_backend_cpu_device_context { |
| 1039 | + std::string description = "CPU"; |
| 1040 | + |
| 1041 | + ggml_backend_cpu_device_context() { |
| 1042 | +#ifdef __APPLE__ |
| 1043 | + size_t len = 0; |
| 1044 | + if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) { |
| 1045 | + description.resize(len); |
| 1046 | + sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT |
| 1047 | + } |
| 1048 | +#elif defined(__linux__) |
| 1049 | + FILE * f = fopen("/proc/cpuinfo", "r"); |
| 1050 | + if (f) { |
| 1051 | + char buf[1024]; |
| 1052 | + while (fgets(buf, sizeof(buf), f)) { |
| 1053 | + if (strncmp(buf, "model name", 10) == 0) { |
| 1054 | + char * p = strchr(buf, ':'); |
| 1055 | + if (p) { |
| 1056 | + p++; |
| 1057 | + while (std::isspace(*p)) { |
| 1058 | + p++; |
| 1059 | + } |
| 1060 | + while (std::isspace(p[strlen(p) - 1])) { |
| 1061 | + p[strlen(p) - 1] = '\0'; |
| 1062 | + } |
| 1063 | + description = p; |
| 1064 | + break; |
| 1065 | + } |
| 1066 | + } |
| 1067 | + } |
| 1068 | + fclose(f); |
| 1069 | + } |
| 1070 | +#elif defined(_WIN32) |
| 1071 | + HKEY hKey; |
| 1072 | + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
| 1073 | + TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), |
| 1074 | + 0, |
| 1075 | + KEY_READ, |
| 1076 | + &hKey) == ERROR_SUCCESS) { |
| 1077 | + DWORD cpu_brand_size = 0; |
| 1078 | + if (RegQueryValueExA(hKey, |
| 1079 | + TEXT("ProcessorNameString"), |
| 1080 | + NULL, |
| 1081 | + NULL, |
| 1082 | + NULL, |
| 1083 | + &cpu_brand_size) == ERROR_SUCCESS) { |
| 1084 | + description.resize(cpu_brand_size); |
| 1085 | + if (RegQueryValueExA(hKey, |
| 1086 | + TEXT("ProcessorNameString"), |
| 1087 | + NULL, |
| 1088 | + NULL, |
| 1089 | + (LPBYTE)&description[0], // NOLINT |
| 1090 | + &cpu_brand_size) == ERROR_SUCCESS) { |
| 1091 | + if (description.find('\0') != std::string::npos) { |
| 1092 | + description.resize(description.find('\0')); |
| 1093 | + } |
| 1094 | + } |
| 1095 | + } |
| 1096 | + RegCloseKey(hKey); |
| 1097 | + } |
| 1098 | +#endif |
| 1099 | + } |
| 1100 | +}; |
| 1101 | + |
1024 | 1102 | static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) { |
1025 | 1103 | return "CPU"; |
1026 | 1104 |
|
1027 | 1105 | GGML_UNUSED(dev); |
1028 | 1106 | } |
1029 | 1107 |
|
1030 | 1108 | static const char * ggml_backend_cpu_device_get_description(ggml_backend_dev_t dev) { |
1031 | | - // TODO |
1032 | | - return "CPU"; |
| 1109 | + struct ggml_backend_cpu_device_context * ctx = (struct ggml_backend_cpu_device_context *)dev->context; |
1033 | 1110 |
|
1034 | | - GGML_UNUSED(dev); |
| 1111 | + return ctx->description.c_str(); |
1035 | 1112 | } |
1036 | 1113 |
|
1037 | 1114 | static void ggml_backend_cpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) { |
@@ -1144,10 +1221,11 @@ static size_t ggml_backend_cpu_reg_get_device_count(ggml_backend_reg_t reg) { |
1144 | 1221 | static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) { |
1145 | 1222 | GGML_ASSERT(index == 0); |
1146 | 1223 |
|
| 1224 | + static ggml_backend_cpu_device_context ctx; |
1147 | 1225 | static ggml_backend_device ggml_backend_cpu_device = { |
1148 | 1226 | /* .iface = */ ggml_backend_cpu_device_i, |
1149 | 1227 | /* .reg = */ reg, |
1150 | | - /* .context = */ NULL, |
| 1228 | + /* .context = */ &ctx, |
1151 | 1229 | }; |
1152 | 1230 |
|
1153 | 1231 | return &ggml_backend_cpu_device; |
|
0 commit comments