Skip to content

Commit 484a1b2

Browse files
committed
Refactor cpu_get_num_math() with cores.h header
1 parent ce85577 commit 484a1b2

File tree

17 files changed

+40
-25
lines changed

17 files changed

+40
-25
lines changed

llama.cpp/common.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ using json = nlohmann::ordered_json;
9090
//
9191

9292
int32_t cpu_get_num_physical_cores();
93-
int32_t cpu_get_num_math();
9493

9594
//
9695
// CLI argument parsing

llama.cpp/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include "llamafile/log.h"
9+
#include "llama.cpp/cores.h"
910
#include "llama.h"
1011

1112
#include "sampling.h"
@@ -61,7 +62,6 @@ struct llama_control_vector_load_info;
6162
//
6263

6364
int32_t cpu_get_num_physical_cores();
64-
int32_t cpu_get_num_math();
6565

6666
//
6767
// CLI argument parsing

llama.cpp/cores.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "cores.h"
2+
13
#include <cosmo.h>
24
#include <fstream>
35
#include <thread>
@@ -87,10 +89,7 @@ static int count_math_cpus(int cpu_count) {
8789

8890
#endif // __x86_64__ && __linux__
8991

90-
/**
91-
* Returns number of CPUs on system that are useful for math.
92-
*/
93-
int cpu_get_num_math() {
92+
static int cpu_get_num_math_impl() {
9493
#if defined(__x86_64__) && (defined(__linux__) || defined(__COSMOPOLITAN__)) && !defined(__ANDROID__)
9594
int cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
9695
if (cpu_count < 1) {
@@ -109,3 +108,16 @@ int cpu_get_num_math() {
109108
#endif
110109
return get_num_physical_cores();
111110
}
111+
112+
/**
113+
* Returns number of CPUs on system that are useful for math.
114+
*/
115+
int cpu_get_num_math() {
116+
static bool once;
117+
static int result;
118+
if (!once) {
119+
result = cpu_get_num_math_impl();
120+
once = true;
121+
}
122+
return result;
123+
}

llama.cpp/cores.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
#ifdef __cplusplus
3+
extern "C" {
4+
#endif
5+
6+
int cpu_get_num_math(void);
7+
8+
#ifdef __cplusplus
9+
}
10+
#endif

llama.cpp/llama-bench/llama-bench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sys/stat.h>
2525
#include <sys/auxv.h>
2626
#include <libc/intrin/x86.h>
27+
#include "llama.cpp/cores.h"
2728
#include <libc/sysv/consts/hwcap.h>
2829

2930
#include "llama.cpp/ggml.h"

llamafile/ansiblas.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <cmath>
2121
#include <unistd.h>
2222

23-
int cpu_get_num_math();
23+
#include "llama.cpp/cores.h"
2424

2525
namespace {
2626
namespace ansiBLAS {

llamafile/core_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <assert.h>
2121

22-
int cpu_get_num_math();
22+
#include "llama.cpp/cores.h"
2323

2424
CoreManager g_core_manager;
2525

llamafile/flags.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// limitations under the License.
1717

1818
#include "debug.h"
19+
#include "llama.cpp/cores.h"
1920
#include "llamafile.h"
2021
#include "trust.h"
2122

@@ -67,8 +68,6 @@ int FLAG_verbose = 0;
6768
int FLAG_warmup = true;
6869
int FLAG_workers;
6970

70-
int cpu_get_num_math();
71-
7271
static wontreturn void usage(int rc, int fd) {
7372
tinyprint(fd, "usage: ", program_invocation_name, " -m MODEL -l [HOST:]PORT\n", NULL);
7473
exit(rc);

llamafile/sgemm_matmul_test.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#define ITERATIONS 30
3030
#define ALLOC(n) (float *)memalign(4096, sizeof(float) * (n))
3131

32-
int cpu_get_num_math();
33-
3432
void llamafile_sgemm_openmp(long m, long n, long k, const void *A, long lda, const void *B,
3533
long ldb, void *C, long ldc, int Atype, int Btype, int Ctype) {
3634
static int nth = cpu_get_num_math();

llamafile/sgemm_sss_test.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#define ITERATIONS 5
2929
#define ALLOC(n) (float *)memalign(4096, sizeof(float) * (n))
3030

31-
int cpu_get_num_math();
32-
3331
void llamafile_sgemm_openmp(long m, long n, long k, const void *A, long lda, const void *B,
3432
long ldb, void *C, long ldc, int Atype, int Btype, int Ctype) {
3533
static int nth = cpu_get_num_math();

0 commit comments

Comments
 (0)