15
15
#endif
16
16
17
17
#if defined (_WIN32)
18
+ #define WIN32_LEAN_AND_MEAN
19
+ #define NOMINMAX
20
+ #include < windows.h>
18
21
#include < fcntl.h>
19
22
#include < io.h>
20
- #pragma comment(lib,"kernel32.lib")
21
- extern " C" __declspec(dllimport) void * __stdcall GetStdHandle (unsigned long nStdHandle);
22
- extern " C" __declspec(dllimport) int __stdcall GetConsoleMode (void * hConsoleHandle, unsigned long * lpMode);
23
- extern " C" __declspec(dllimport) int __stdcall SetConsoleMode (void * hConsoleHandle, unsigned long dwMode);
24
- extern " C" __declspec(dllimport) int __stdcall SetConsoleCP (unsigned int wCodePageID);
25
- extern " C" __declspec(dllimport) int __stdcall SetConsoleOutputCP (unsigned int wCodePageID);
26
- extern " C" __declspec(dllimport) int __stdcall WideCharToMultiByte (unsigned int CodePage, unsigned long dwFlags,
27
- const wchar_t * lpWideCharStr, int cchWideChar,
28
- char * lpMultiByteStr, int cbMultiByte,
29
- const char * lpDefaultChar, bool * lpUsedDefaultChar);
30
23
#define CP_UTF8 65001
31
24
#endif
32
25
@@ -60,7 +53,71 @@ int32_t get_num_physical_cores() {
60
53
return num_physical_cores;
61
54
}
62
55
#elif defined(_WIN32)
63
- // TODO: Implement
56
+ SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer = nullptr ;
57
+ DWORD length = 0 ;
58
+
59
+ // Call GetLogicalProcessorInformationEx with a nullptr buffer to get the required buffer length
60
+ GetLogicalProcessorInformationEx (RelationAll, nullptr , &length);
61
+
62
+ // Allocate memory for the buffer
63
+ buffer = static_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(malloc (length));
64
+
65
+ // Things to count
66
+ unsigned int physical_cores = 0 ;
67
+ unsigned int physical_performance_cores = 0 ;
68
+ unsigned int physical_efficiency_cores = 0 ;
69
+ unsigned int logical_cores = 0 ;
70
+ unsigned int logical_performance_cores = 0 ;
71
+ unsigned int logical_efficiency_cores = 0 ;
72
+
73
+ // Call GetLogicalProcessorInformationEx again with the allocated buffer
74
+ if (GetLogicalProcessorInformationEx (RelationAll, buffer, &length)) {
75
+ DWORD offset = 0 ;
76
+
77
+ while (offset < length) {
78
+ auto info = reinterpret_cast <SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(reinterpret_cast <char *>(buffer) + offset);
79
+
80
+ if (info->Relationship == RelationProcessorCore) {
81
+ physical_cores += info->Processor .GroupCount ;
82
+
83
+ for (WORD i = 0 ; i < info->Processor .GroupCount ; ++i) {
84
+ int core_count = static_cast <int >(__popcnt64 (info->Processor .GroupMask [i].Mask ));
85
+ logical_cores += core_count;
86
+
87
+ // Assuming EfficiencyClass 0 represents performance cores, and others represent efficiency cores
88
+ if (info->Processor .EfficiencyClass == 0 ) {
89
+ physical_performance_cores++;
90
+ logical_performance_cores += core_count;
91
+ } else {
92
+ physical_efficiency_cores++;
93
+ logical_efficiency_cores += core_count;
94
+ }
95
+ }
96
+ }
97
+ offset += info->Size ;
98
+ }
99
+
100
+ // TODO: Remove this once we've verified it's working
101
+ fprintf (stderr,
102
+ " Physical Cores: %u\n "
103
+ " - Performance Cores: %u\n "
104
+ " - Efficiency Cores: %u\n "
105
+ " Logical Cores: %u\n "
106
+ " - Performance Cores: %u\n "
107
+ " - Efficiency Cores: %u\n " ,
108
+ physical_cores, physical_performance_cores, physical_efficiency_cores,
109
+ logical_cores, logical_performance_cores, logical_efficiency_cores);
110
+ } else {
111
+ printf (" Failed to get processor information. Error: %u\n " , GetLastError ());
112
+ }
113
+
114
+ free (buffer);
115
+
116
+ if (physical_performance_cores > 0 ) {
117
+ return static_cast <int32_t >(physical_performance_cores);
118
+ } else if (physical_cores > 0 ) {
119
+ return static_cast <int32_t >(physical_cores);
120
+ }
64
121
#endif
65
122
unsigned int n_threads = std::thread::hardware_concurrency ();
66
123
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2 ) : 4 ;
@@ -69,7 +126,6 @@ int32_t get_num_physical_cores() {
69
126
bool gpt_params_parse (int argc, char ** argv, gpt_params & params) {
70
127
bool invalid_param = false ;
71
128
std::string arg;
72
- gpt_params default_params;
73
129
74
130
for (int i = 1 ; i < argc; i++) {
75
131
arg = argv[i];
@@ -287,7 +343,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
287
343
}
288
344
params.n_parts = std::stoi (argv[i]);
289
345
} else if (arg == " -h" || arg == " --help" ) {
290
- gpt_print_usage (argc, argv, default_params );
346
+ gpt_print_usage (argc, argv);
291
347
exit (0 );
292
348
} else if (arg == " --random-prompt" ) {
293
349
params.random_prompt = true ;
@@ -299,20 +355,22 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
299
355
params.input_prefix = argv[i];
300
356
} else {
301
357
fprintf (stderr, " error: unknown argument: %s\n " , arg.c_str ());
302
- gpt_print_usage (argc, argv, default_params );
358
+ gpt_print_usage (argc, argv);
303
359
exit (1 );
304
360
}
305
361
}
306
362
if (invalid_param) {
307
363
fprintf (stderr, " error: invalid parameter for argument: %s\n " , arg.c_str ());
308
- gpt_print_usage (argc, argv, default_params );
364
+ gpt_print_usage (argc, argv);
309
365
exit (1 );
310
366
}
311
367
312
368
return true ;
313
369
}
314
370
315
- void gpt_print_usage (int /* argc*/ , char ** argv, const gpt_params & params) {
371
+ void gpt_print_usage (int /* argc*/ , char ** argv) {
372
+ gpt_params params;
373
+
316
374
fprintf (stderr, " usage: %s [options]\n " , argv[0 ]);
317
375
fprintf (stderr, " \n " );
318
376
fprintf (stderr, " options:\n " );
0 commit comments