C API Example (Thread Pinning) - C API Example (Thread Pinning) - 5.3 English - 57404

AOCL User Guide (57404)

Document_ID
57404
Release_Date
2026-05-13
Version
5.3 English
#include "Capi/au/threadpinning.h"

#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define NUM_THREADS 5

void* threadFunction(void* arg)
{
   int id = *((int*)arg);
   printf("Thread started with ID: %d\n", id);
   sleep(3);
   return NULL;
}

void checkThreadAffinity(pthread_t thread)
{
   cpu_set_t cpuset;
   CPU_ZERO(&cpuset);
   if (pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset) != 0) {
      printf("Error reading thread affinity on Linux\n");
   } else {
      printf("Affinity mask: %lu\n", cpuset.__bits[0]);
   }
}

int main()
{
   int       thread_ids[NUM_THREADS];
   pthread_t threads[NUM_THREADS];

   for (int i = 0; i < NUM_THREADS; ++i) {
      thread_ids[i] = i;
      if (pthread_create(&threads[i], NULL, threadFunction, &thread_ids[i])
            != 0) {
            printf("Error creating thread %d\n", i);
            return 1;
      }
   }

   // Pin threads to the processor group using different strategies
   // CORE, LOGICAL, SPREAD, CUSTOM
   printf(
      "Pinning threads to the processor group using different strategies\n");
   printf("CORE\n");
   au_pin_threads_core(threads, NUM_THREADS);
   for (int i = 0; i < NUM_THREADS; ++i) {
      checkThreadAffinity(threads[i]);
   }
   printf("LOGICAL\n");
   au_pin_threads_logical(threads, NUM_THREADS);
   for (int i = 0; i < NUM_THREADS; ++i) {
      checkThreadAffinity(threads[i]);
   }
   printf("SPREAD\n");
   au_pin_threads_spread(threads, NUM_THREADS);
   for (int i = 0; i < NUM_THREADS; ++i) {
      checkThreadAffinity(threads[i]);
   }
   printf("CUSTOM\n");
   int affinityVector[NUM_THREADS] = { 0, 1, 4, 6, 7 };
   au_pin_threads_custom(threads, NUM_THREADS, affinityVector, NUM_THREADS);
   for (int i = 0; i < NUM_THREADS; ++i) {
      checkThreadAffinity(threads[i]);
   }

   for (int i = 0; i < NUM_THREADS; ++i) {
      pthread_join(threads[i], NULL);
   }

   return 0;
}