Skip to content

Commit b1d2d2b

Browse files
committed
experiment with CPU identification logic
1 parent 5e5c217 commit b1d2d2b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ authors = ["Johannes Blaschke <[email protected]>"]
44
version = "0.1.0"
55

66
[deps]
7+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
78
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
9+
CpuId = "adafc99b-e345-5852-983c-f28acb93d879"
810
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
11+
ThreadPools = "b189fb0b-2eb5-4ed4-bc0c-d34c51242431"
912

1013
[compat]
11-
CEnum = "0.4"
14+
CEnum = "0.4"

tmp/threads_cpuids.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# threads_cpuids.jl
2+
using CpuId
3+
function cpuid_coreid()
4+
eax, ebx, ecx, edx = CpuId.cpuid(1, 0)
5+
if ( (edx & (0x00000001 << 9)) == 0x00000000)
6+
CPU = -1; # no APIC on chip
7+
else
8+
CPU = (ebx%Int) >> 24;
9+
end
10+
CPU < 0 ? 0 : CPU
11+
end
12+
13+
glibc_coreid() = @ccall sched_getcpu()::Cint
14+
15+
const cpucycle_mask = (
16+
(1 << (64 - leading_zeros(CpuId.cputhreads()))) - 1
17+
) % UInt32
18+
# const cpucycle_mask = 0x00000fff
19+
println(cpucycle_mask)
20+
cpucycle_coreid() = Int(cpucycle_id()[2] & cpucycle_mask)
21+
22+
using ThreadPools
23+
using Base.Threads: nthreads
24+
25+
tglibc_coreid(i::Integer) = fetch(@tspawnat i glibc_coreid());
26+
tcpuid_coreid(i::Integer) = fetch(@tspawnat i cpuid_coreid());
27+
tcpucycle_coreid(i::Integer) = fetch(@tspawnat i cpucycle_coreid());
28+
29+
for i in 1:nthreads()
30+
println("Running on thread $i (glibc_coreid: $(tglibc_coreid(i)), cpuid_coreid: $(tcpuid_coreid(i)), cpucycle_coreid: $(tcpucycle_coreid(i)))")
31+
# @sync @tspawnat i sum(abs2, rand()^2 + rand()^2 for i in 1:500_000_000)
32+
end
33+

0 commit comments

Comments
 (0)