Determine the socket / processor of the worker thread

I am writing a NUMA-aaware cache for large objects (doubling matrices) for 4 socket servers. I noticed that cross-secret communication is a bottleneck for my application. Hence, I want the threads on different sockets to have separate matrix caches. I have limited threads for specific physical processors and now I need the threads to select the correct cache.

Let's assume the cache is defined like this:

matrix_cache_t *cache[SOCKETS_LIMIT];

      

I need each thread to find out its socket id and select the correct cache for example. cache[0]

, cache[1]

, cache[2]

Or cache[3]

.

I am writing an application in C using OpenMP and it should work on both Windows and Linux.

+3


source to share


1 answer


On Windows, you can use the GetLogicalProcessorInformationEx()

API using relationship RelationNumaNode

or RelationProcessorPackage

. It gives you the bits of all processors in the appropriate ratio, which correspond to the affinity bits used to bind the thread to the processor.

On Linux, you can use sched_getcpu



#include <stdio.h>
#include <sched.h>

int sched_getcpu();

int main()
{
    (void) printf("cpu %d\n", sched_getcpu());
}

      

The socket id can be found in /proc/cpuinfo

or/sys/devices/system/cpu/cpu0/topology/physical_package_id

0


source







All Articles