Skip to content

Commit 202f414

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

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 82 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,14 @@
1018
#include <stdio.h>
1119
#include <stdlib.h>
1220
#include <string.h>
13-
1421
#include <vector>
1522

23+
#ifdef __APPLE__
24+
#include <sys/types.h>
25+
#include <sys/sysctl.h>
26+
#endif
27+
28+
1629
// backend buffer type
1730

1831
const char * ggml_backend_buft_name(ggml_backend_buffer_type_t buft) {
@@ -1021,17 +1034,80 @@ ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size)
10211034

10221035
////////////////////////
10231036

1037+
struct ggml_backend_cpu_device_context {
1038+
ggml_backend_cpu_device_context() {
1039+
#ifdef __APPLE__
1040+
size_t len = 0;
1041+
if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
1042+
description.resize(len);
1043+
sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
1044+
}
1045+
#elif defined(__linux__)
1046+
FILE * f = fopen("/proc/cpuinfo", "r");
1047+
if (f) {
1048+
char buf[1024];
1049+
while (fgets(buf, sizeof(buf), f)) {
1050+
if (strncmp(buf, "model name", 10) == 0) {
1051+
char * p = strchr(buf, ':');
1052+
if (p) {
1053+
p++;
1054+
while (std::isspace(*p)) {
1055+
p++;
1056+
}
1057+
while (std::isspace(p[strlen(p) - 1])) {
1058+
p[strlen(p) - 1] = '\0';
1059+
}
1060+
description = p;
1061+
break;
1062+
}
1063+
}
1064+
}
1065+
fclose(f);
1066+
}
1067+
#elif defined(_WIN32)
1068+
HKEY hKey;
1069+
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
1070+
TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
1071+
0,
1072+
KEY_READ,
1073+
&hKey) == ERROR_SUCCESS) {
1074+
DWORD cpu_brand_size = 0;
1075+
if (RegQueryValueExA(hKey,
1076+
TEXT("ProcessorNameString"),
1077+
NULL,
1078+
NULL,
1079+
NULL,
1080+
&cpu_brand_size) == ERROR_SUCCESS) {
1081+
description.resize(cpu_brand_size);
1082+
if (RegQueryValueExA(hKey,
1083+
TEXT("ProcessorNameString"),
1084+
NULL,
1085+
NULL,
1086+
(LPBYTE)&description[0], // NOLINT
1087+
&cpu_brand_size) == ERROR_SUCCESS) {
1088+
if (description.find('\0') != std::string::npos) {
1089+
description.resize(description.find('\0'));
1090+
}
1091+
}
1092+
}
1093+
RegCloseKey(hKey);
1094+
}
1095+
#endif
1096+
}
1097+
1098+
std::string description = "CPU";
1099+
};
1100+
10241101
static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) {
10251102
return "CPU";
10261103

10271104
GGML_UNUSED(dev);
10281105
}
10291106

10301107
static const char * ggml_backend_cpu_device_get_description(ggml_backend_dev_t dev) {
1031-
// TODO
1032-
return "CPU";
1108+
struct ggml_backend_cpu_device_context * ctx = (struct ggml_backend_cpu_device_context *)dev->context;
10331109

1034-
GGML_UNUSED(dev);
1110+
return ctx->description.c_str();
10351111
}
10361112

10371113
static void ggml_backend_cpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
@@ -1144,10 +1220,11 @@ static size_t ggml_backend_cpu_reg_get_device_count(ggml_backend_reg_t reg) {
11441220
static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
11451221
GGML_ASSERT(index == 0);
11461222

1223+
static ggml_backend_cpu_device_context ctx;
11471224
static ggml_backend_device ggml_backend_cpu_device = {
11481225
/* .iface = */ ggml_backend_cpu_device_i,
11491226
/* .reg = */ reg,
1150-
/* .context = */ NULL,
1227+
/* .context = */ &ctx,
11511228
};
11521229

11531230
return &ggml_backend_cpu_device;

0 commit comments

Comments
 (0)