Skip to content

Commit 83a9a98

Browse files
committed
ggml-backend : add device description to CPU backend
1 parent c83ad6d commit 83a9a98

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Note: porting this file to C++ is a work in progress
22

3+
#ifdef _WIN32
4+
#define WIN32_LEAN_AND_MEAN
5+
#ifndef NOMINMAX
6+
# define NOMINMAX
7+
#endif
8+
#include <windows.h>
9+
#endif
10+
311
#include "ggml-backend-impl.h"
412
#include "ggml-alloc.h"
513
#include "ggml-impl.h"
@@ -10,9 +18,15 @@
1018
#include <stdio.h>
1119
#include <stdlib.h>
1220
#include <string.h>
13-
21+
#include <string>
1422
#include <vector>
1523

24+
#ifdef __APPLE__
25+
#include <sys/types.h>
26+
#include <sys/sysctl.h>
27+
#endif
28+
29+
1630
// backend buffer type
1731

1832
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)
10211035

10221036
////////////////////////
10231037

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+
10241102
static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) {
10251103
return "CPU";
10261104

10271105
GGML_UNUSED(dev);
10281106
}
10291107

10301108
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;
10331110

1034-
GGML_UNUSED(dev);
1111+
return ctx->description.c_str();
10351112
}
10361113

10371114
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) {
11441221
static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
11451222
GGML_ASSERT(index == 0);
11461223

1224+
static ggml_backend_cpu_device_context ctx;
11471225
static ggml_backend_device ggml_backend_cpu_device = {
11481226
/* .iface = */ ggml_backend_cpu_device_i,
11491227
/* .reg = */ reg,
1150-
/* .context = */ NULL,
1228+
/* .context = */ &ctx,
11511229
};
11521230

11531231
return &ggml_backend_cpu_device;

0 commit comments

Comments
 (0)