How to allocate memory in a specific place in memory (C programming)?

c, memory, memory-management, parallel-processing, ram

Solution

Assuming you are using Linux, the default NUMA policy prefers to allocate memory locally, so what you are asking for should work out-of-the-box. This can be changed through configuration though.

Whatever the current policy you can use `libnuma` to allocate memory on the local NUMA node (that's what the call the combination of RAM + socket / core) or on a specific node, with `numa_alloc_local`, `numa_alloc_onnode`, and so on. To free the memory use `numa_free`. See the man pages of numa(7) and numa_alloc(3) for details on these functions and the NUMA system in general.

Problem

I have a server with 2 CPUs, each with 6 cores. Each of the CPUs are connected to 4 GB of RAM. I have a parallel program that runs the same code (with minor changes) in both CPUs in parallel, using 4 threads in each core. For efficiency reasons, it would be best if there was a way to ensure that the code running on CPU 1 would only allocate memory on its corresponding ram and not on the ram of CPU 2, and vice versa, as the communication between CPUs would create an overhead. Is there any way to do this?

Original source