1
1
#include " common.h"
2
2
3
3
#include < cassert>
4
+ #include < iostream>
4
5
#include < cstring>
5
6
#include < fstream>
6
7
#include < string>
7
8
#include < iterator>
8
9
#include < algorithm>
9
10
#include < sstream>
10
- #include < iostream>
11
+
12
+ #if defined(__APPLE__) && defined(__MACH__)
13
+ #include < sys/types.h>
14
+ #include < sys/sysctl.h>
15
+ #endif
11
16
12
17
#if defined (_WIN32)
13
18
#include < fcntl.h>
@@ -25,19 +30,43 @@ extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int
25
30
#define CP_UTF8 65001
26
31
#endif
27
32
28
- bool gpt_params_parse (int argc, char ** argv, gpt_params & params) {
29
- // determine sensible default number of threads.
30
- // std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
33
+ int32_t get_num_physical_cores () {
31
34
#ifdef __linux__
32
35
std::ifstream cpuinfo (" /proc/cpuinfo" );
33
- params.n_threads = std::count (std::istream_iterator<std::string>(cpuinfo),
34
- std::istream_iterator<std::string>(),
35
- std::string (" processor" ));
36
- #endif
37
- if (params.n_threads == 0 ) {
38
- params.n_threads = std::max (1 , (int32_t ) std::thread::hardware_concurrency ());
36
+ std::string line;
37
+ while (std::getline (cpuinfo, line)) {
38
+ std::size_t pos = line.find (" cpu cores" );
39
+ if (pos != std::string::npos) {
40
+ pos = line.find (" : " , pos);
41
+ if (pos != std::string::npos) {
42
+ try {
43
+ // Extract the number and return it
44
+ return static_cast <int32_t >(std::stoul (line.substr (pos + 2 )));
45
+ } catch (const std::invalid_argument &) {
46
+ // Ignore if we could not parse
47
+ }
48
+ }
49
+ }
50
+ }
51
+ #elif defined(__APPLE__) && defined(__MACH__)
52
+ int32_t num_physical_cores;
53
+ size_t len = sizeof (num_physical_cores);
54
+ int result = sysctlbyname (" hw.perflevel0.physicalcpu" , &num_physical_cores, &len, NULL , 0 );
55
+ if (result == 0 ) {
56
+ return num_physical_cores;
57
+ }
58
+ result = sysctlbyname (" hw.physicalcpu" , &num_physical_cores, &len, NULL , 0 );
59
+ if (result == 0 ) {
60
+ return num_physical_cores;
39
61
}
62
+ #elif defined(_WIN32)
63
+ // TODO: Implement
64
+ #endif
65
+ unsigned int n_threads = std::thread::hardware_concurrency ();
66
+ return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2 ) : 4 ;
67
+ }
40
68
69
+ bool gpt_params_parse (int argc, char ** argv, gpt_params & params) {
41
70
bool invalid_param = false ;
42
71
std::string arg;
43
72
gpt_params default_params;
0 commit comments