Affinity refers to the core selection APIs in the Heterogeneous Compute SDK.
Most Snapdragon mobile platforms include a big-LITTLE architecture, as shown in orange in the image. The big cores are performance-efficient and the LITTLE cores are power-efficient.
Having the choice of core for executing a task on the CPU has a significant impact on the power-performance characteristics of the application. The Affinity APIs allow developers to select the CPU core on which to run the program construct.
- Location — Selects the CPU (i.e., big or LITTLE core) on which the program construct will run
- Pinning — Determines whether a thread can migrate between cores (has a significant impact on performance because of cache coherency)
- Mode — Determines whether to adhere to or override local settings that come with the task or kernel abstraction
Affinity APIs support standalone functions as well as tasks, patterns and kernel abstractions in the Heterogeneous Compute SDK.
For example, to encapsulate an existing, standalone function (such as a bubble sort), developers can use the execute(settings, fn, fn_args) API in the Heterogeneous Compute SDK, then pass in the settings for location, pinning and mode. The Heterogeneous Compute runtime then tries to adhere to the affinity settings specified in the execute API.
To set the affinity for a particular task or kernel abstraction in the Heterogeneous Compute SDK, the set_big and set_little APIs can be used.
The Heterogeneous Compute runtime provides two variants for affinity settings:
- Global settings, which affect the entire application
- Local settings, for a specific function, task or kernel
The code in the image below shows how to set affinity.
The code sample for kernels created a CPU kernel abstraction using a vector_double function. Here, the highlighted set_big API sets the affinity so that the CPU kernel will run on the big cluster. From a developer’s perspective, set_big tells the Heterogeneous Compute runtime to ensure the vector_double API doesn’t get scheduled in the LITTLE cluster.