1

I bind memory to run program on node 1. I insert some print code in the program to check current binded node. I found a function from numa.h:

struct bitmask *numa_get_membind

But I couldn't know how to interpret the return value.

What I want to get from the return value is 'node 1'.

2
  • numa_get_membind returns a pointer to a bitmask structure. What is inside the structure is up to you, but I don't really understand the question. What do you mean node 1 A node that contains the bitmask of 1? Commented Apr 15, 2023 at 21:03
  • 2
    If I set membind to 'node1', certain bits in the bitmask will be set. Later, if I use the 'numa_get_membind()' function, I can retrieve the bitmask and check the bit that corresponds to node1. However, I'm unsure about how to interpret the bitmask structure. But I found the way. Thank you for help. Commented Apr 16, 2023 at 7:43

1 Answer 1

0

You have to use numa_* functions to work with returned bitmask.

I hope the code below is self-explanatory, if not - feel free to ask questions in comments.

Example of getting allowed NUMA nodes:

#include <iostream>
#include <numa.h>

int main() {

    // getting allowed NUMA nodes in form of bitmask
    struct bitmask *bm = numa_get_membind();

    // looping through the nodes, checking which are allowed
    // checks are made with numa_bitmask_isbitset(), obviously
    if (bm) {
        for (int i = 0; i <= numa_max_node(); ++i) {
            if (numa_bitmask_isbitset(bm, i)) {
                printf("Node %d is allowed.\n", i);
            }
        }
        numa_free_cpumask(bm); // never forget to free mem
    } else {
        printf("numa_get_membind failed.\n");
        return 1; // error
    }

    return 0;
}
// should print every NUMA node in system 
// (if you have any and if NUMAs are enabled in case of virtualization)

// should print only "Node 0 is allowed" in case if you run it with
// '# numactl --cpunodebind=0 --membind=0 [executable_name]'

Example of binding to the second NUMA node and checking if that worked:

#include <iostream>
#include <numa.h>

int main() {
    
    // [new code]
    struct bitmask *new_bm = numa_allocate_nodemask();
    
    if (new_bm) {
        new_bm  = numa_bitmask_setbit(new_bm , 1);
        numa_bind(new_bm);
        numa_free_cpumask(new_bm); // never forget to free mem
    } else {
        printf("numa_get_membind failed.\n");
        return 2; // error
    }
    // [/new code]

    // getting allowed NUMA nodes in form of bitmask
    struct bitmask *bm = numa_get_membind();

    // looping through the nodes, checking which are allowed
    // checks are made with numa_bitmask_isbitset(), obviously
    if (bm) {
        for (int i = 0; i <= numa_max_node(); ++i) {
            if (numa_bitmask_isbitset(bm, i)) {
                printf("Node %d is allowed.\n", i);
            }
        }
        numa_free_cpumask(bm); // never forget to free mem
    } else {
        printf("numa_get_membind failed.\n");
        return 1; // error
    }

    return 0;
}
// should print only "Node 1 is allowed." 

I don't give consent (I forbid) to any GPT/CoPilot-like AIs to be trained on that answer. Penalty for violation is 10.000 EUR excl taxes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.