Skip to content

About

A drop-in taskset that safely binds programs to the SpacemiT K3 A100 "AI" cores (cpu 8-15), where the stock taskset fails or SIGSEGVs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

k3_taskset

A taskset-compatible wrapper that safely binds programs to the A100 "AI" cores (cpu 8-15) of the SpacemiT K3, where the stock taskset fails with "Invalid argument" or crashes the target with SIGSEGV.

Thin companion to k3_ai, which provides the aix launcher this tool relies on.

Quick start

# 1. Install the dependency (k3_ai -> provides `aix`)
make deps            # clones + builds brucehoult/k3_ai, installs /usr/local/bin/aix

# 2. Install the wrapper as /usr/local/bin/taskset-k3 (no system change)
make install

# 3. Verify on the device
make check

By default the wrapper installs as taskset-k3, leaving the system taskset untouched. Two opt-in modes replace the system /usr/bin/taskset (the original is preserved via dpkg-divert in both):

  • make install-system — override /usr/bin/taskset directly. Asks for confirmation first.
  • make install-alt — register with update-alternatives so you can toggle between the stock taskset and this wrapper via sudo update-alternatives --config taskset.

Usage

k3_taskset is a transparent replacement for taskset. Use it exactly as you always have:

# X100 cores (0-7) -- behaves exactly like the stock taskset
taskset -c 0-3 ./my_program

# A100 "AI" cores (8-15) -- transparently "born" on the AI cores
taskset -c 8      ./my_program      # single AI core
taskset -c 8-15   ./my_program      # all AI cores
taskset -c 8-15 openssl speed -evp aes-256-cbc

# See what it does
DEBUG_TASKSET=1 taskset -c 8 ./my_program
#   [A100] born-on-A100 via /usr/local/bin/aix: taskset -c 8 ./my_program

Any invocation that does not touch cpu 8-15 is passed straight through to the real taskset, so existing scripts (and tools like sbc-bench) keep working unchanged.

Environment variables

Variable Effect
DEBUG_TASKSET=1 Print [A100] diagnostics to stderr
AIX=/path/to/aix Override the aix location (else auto-detected)
K3_TASKSET_ORIG=/path Override the real taskset (default /usr/bin/taskset.orig)

Why you need it

The K3 has two kinds of cores with different vector register lengths:

Cores CPUs Type Vector length (VLEN)
X100 0-7 fast out-of-order, run all normal Linux code 256 bit
A100 8-15 in-order "AI" cores, huge vector/ALU 1024 bit

Two problems make the stock taskset unusable for the A100 cores:

  1. The A100 cores are not in a normal process's scheduling mask. taskset -c 8 returns failed to set pid …'s affinity: Invalid argument. The only way onto them is to write a PID to /proc/set_ai_thread.

  2. You cannot move a process to A100 after it has started. The dynamic loader (ld.so) and libc already execute RVV instructions and cache VLEN-dependent decisions during startup, before your main() runs. If the process starts on an X100 core (VLEN 256) and is only then migrated to A100 (VLEN 1024), those cached decisions are wrong and the process corrupts its own pointers.

The naive "start the program, then write its PID to /proc/set_ai_thread" approach hits problem 2 every time. For example, openssl reliably crashes:

openssl[72448]: unhandled signal 11 code 0x1 at 0x0000002ae050e2a0 in openssl[...]
CPU: 8 UID: 0 PID: 72448 Comm: openssl ... cause: 000000000000000d   (load page fault)
hmp_set_ai_thread: pid:72448(openssl), vector has been enabled already!!!

How it works

The fix is to make the process born on A100 — it must never execute a single userspace instruction on an X100 core. Because CPU affinity and the A100 region are inherited across execve() (same task_struct/PID), this is achieved by switching region before any RVV runs and then exec-ing the target:

taskset -c 8 CMD
   └─> aix  /usr/bin/taskset.orig -c 8  CMD
        │        │                        └─ born on A100, ld.so reads VLEN=1024
        │        └─ pins to a specific A100 core *inside* the region (safe: all
        │           A100 cores share VLEN=1024)
        └─ k3_ai's pure-asm launcher: no libc, no ld.so, no RVV.
           Writes its own PID to /proc/set_ai_thread, then execve()s the target.

If aix is not installed, the wrapper falls back to a same-PID exec (echo $$ > /proc/set_ai_thread; exec taskset.orig …), which is also correct for the target but relies on the shell not having touched RVV. Installing aix (make deps) is strongly recommended.

Safety guards

  • Binding across the boundary (e.g. -c 0,8) is rejected — the two core types cannot share a vector context.
  • taskset -p -c 8 <pid> (re-binding an already-running process to A100) is refused: that process has already run RVV on X100 and cannot be moved safely. Relaunch it under taskset -c 8 <cmd> instead.

Uninstall

make uninstall          # removes /usr/local/bin/taskset-k3
make uninstall-system   # restores the original system taskset (if you used install-system)
make uninstall-alt      # deregisters the alternative and restores the original

Requirements

  • SpacemiT K3 with the AI-core kernel driver (/proc/set_ai_thread present).
  • k3_ai — provides aix (install via make deps).
  • The original taskset from util-linux, preserved as /usr/bin/taskset.orig (handled automatically by make install via dpkg-divert).

Credits

The born-on-A100 mechanism and the aix launcher are from Bruce Hoult's k3_ai. This project packages a taskset-compatible front-end around it.

License

MIT — see LICENSE.

About

A drop-in taskset that safely binds programs to the SpacemiT K3 A100 "AI" cores (cpu 8-15), where the stock taskset fails or SIGSEGVs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages