1

Simple C code can use libnuma library to allocate memory in specific memory node in a NUMA system. For example, it can be done using following function:

void *numa_alloc_onnode(size_t size, int node)

How one instantiate a class in some memory node? One way, I can think of is:

numa_set_membind(<nodemask>);
o = new Object();
numa_set_localalloc();

Will this work?

shoban
  • 650
  • 1
  • 9
  • 14

1 Answers1

4

Placement new is what you are looking for. Example:

void *blob = numa_alloc_onnode(sizeof(Object), ...);
Object *object = new(blob) Object;
JustSid
  • 25,168
  • 7
  • 79
  • 97
  • Although do be careful about alignment requirements. – Ben Voigt Apr 17 '14 at 20:28
  • 2
    How do you deallocate? – David Heffernan Apr 17 '14 at 20:31
  • 3
    Also, apparently one has to free using `numa_free()`, so to be complete: `object->~Object(); numa_free(blob);` – shoban Apr 17 '14 at 20:34
  • @BenVoigt: How does one align properly? – shoban Apr 17 '14 at 20:49
  • 2
    @shoban Here is a [stackoverflow](http://stackoverflow.com/questions/8154162/numa-aware-cache-aligned-memory-allocation) question about how to do aligned allocations with numa – JustSid Apr 17 '14 at 21:05
  • @shoban, `numa_alloc_onnode` returns page-aligned memory, therefore you don't have to do anything special in addition. – Hristo Iliev Apr 24 '14 at 15:27
  • @HristoIliev The man page is not explicit about it. I guess the following excerpt from man page makes what you are saying implicit: "The size argument will be rounded up to a multiple of the system page size." – shoban Apr 25 '14 at 15:51