I have allocated an array in C as follows:
void *mem = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
Imagine this array is initialized and now I need to migrate (pin) it to a NUMA node. I know I can use numa_alloc_on_node()
function to directly allocate on a node. However, please note that the concern here is to migrate the already allocated memory to a specified node. Is there any way to do this in C?
Thanks
CodePudding user response:
I don't think there is, as such.
It's worth considering what would be involved in doing so; it would involve copying data from where it currently is in memory, to another place in memory. One could achieve what you want by using numa_alloc_on_node() for where you want the data to got to, and then memcpy() to put the data there.
If there were an all-in-one solution (library function, or op-code, or whatever), it'd have to do basically those operations anyway; there's no magic short cut that could be taken by the CPU's electronics to achieve the same end result.
So I'm fairly sure that you won't be missing out if you do do this with a call to numa_alloc_on_node() followed by a memcpy(). It's worth noting that with today's CPUs (some support internal DMAs) a good memcpy implementation will be moving data as fast as the electronics can achieve anyway.
The pointer to the data changes, of course.
CodePudding user response:
It seems numa_move_pages
from numalib
does the job. Here one can find its usage: https://github.com/numactl/numactl/blob/master/test/migrate_pages.c
These examples are also useful: https://cpp.hotexamples.com/examples/-/-/numa_move_pages/cpp-numa_move_pages-function-examples.html
There is also another function numa_migrate_page
which migrates the entire pages that belongs to the PID.
Hope this helps someone in the future.