How to measure cache misses in Julia?

#1

Hi there!
Do we have a tool or the means for measuring cache misses in Julia? If we had a macro for this would really set Julia apart in the HPC world. I did search around here but did not find anything, thank you.
Pedro

2 Likes

#2

How does one measure cache misses in other systems? Presumably this requires hardware support.

0 Likes

#3

https://www.quora.com/How-do-you-measure-cache-misses-in-C-C++

0 Likes

#4

I’m not sure this is usable for serious usecase, but this works for me in some toy examples. Requires perf CLI.

"""
    perf(f, args)

Run `perf` program with `args` while executing `f`.

# Examples
```
julia> A = randn(100, 100); B = randn(100, 100);

julia> perf(`stat`) do
           @benchmark \$A * \$B
       end
```
"""
function perf(f, args)
    pid = getpid()
    cmd = `perf $args --pid=$pid`
    proc = run(pipeline(cmd, stdout=stdout, stderr=stderr); wait=false)
    try
        return f()
    finally
        flush(stdout)
        flush(stderr)
        kill(proc, Base.SIGINT)
        wait(proc)
    end
end

perf_stat_l1(f, args = ``) =
    perf(f, `stat -e L1-dcache-load-misses,L1-dcache-loads,L1-dcache-stores,L1-icache-load-misses $args`)
12 Likes

#5

perf is definitely the best option on Linux for measuring cache misses (or cache/CPU data in general). It has the benefit of being zero-overhead due to using the CPU’s in-built hardware performance counters. +1 to your solution!

2 Likes

#6

Thank you!

0 Likes

#7

https://github.com/carnaval/LinuxPerf.jl could perhaps give some inspiration. Package most likely needs updating though.

0 Likes

#8

This would be a great GSoC project. Phase 1: resurrect the Linux version. Phase 2: implement similar functionality on Windows and/or macOS and/or FreeBSD. Phase 3: wrap them in a common interface, et voilà, cross-platform performance counters in Julia! Even if not completely finished in a summer any initial portion of this would be worthwhile.

9 Likes

#9

Thanks for sharing the package, and yes, that would be an outstanding HPC feature. In fact, just looking around Julia’s website, we have a reference tag for GPU, but why don’t have a more general pitch to the public for HPC?

0 Likes

#10

@phrmoy On the Julia Slack there is a specicific #HPC channel.
On here the discussion on HPC would be in Parallel/Distributed. I agree with you - perhaps there is a case for renaming this section.

0 Likes

#11

Thanks for the info John, how do I get an invite to the slack channel?

0 Likes

#12

Slack invite requests are here: https://slackinvite.julialang.org/

1 Like