17 process_core_string_item(
const char* str,
int* sub_list,
int* sub_ncores)
26 if(str == NULL || sub_list == NULL || sub_ncores == NULL ||
32 if(strstr(str,
"-") != NULL){
34 int low_core, high_core;
35 if (sscanf(str,
"%d-%d",&low_core, &high_core) != 2){
39 if(*sub_ncores < high_core-low_core +1){
44 for(i = 0; i < high_core-low_core +1; i++){
45 sub_list[i] = i + low_core;
47 *sub_ncores = high_core - low_core +1;
52 if (sscanf(str,
"%d", &core) != 1){
63 process_core_string_list(
const char* _str,
int* list,
int* ncores)
71 if(_str == NULL || list == NULL || ncores == NULL
78 strncpy(str, _str,
sizeof(str));
80 int left_space = *ncores;
83 char* item = strtok(str,
",");
90 int sub_ncores = left_space;
91 int* sub_list = list + tot_cores;
93 int rc = process_core_string_item(item, sub_list, &sub_ncores);
99 tot_cores += sub_ncores;
100 left_space -= sub_ncores;
102 item = strtok(NULL,
",");
103 }
while( item != NULL);
111 getNumaAffinity(
int my_gpu,
int *cpu_cores,
int* ncores)
113 FILE *nvidia_info, *pci_bus_info;
116 char nvidia_info_path[255], pci_bus_info_path[255];
120 sprintf(nvidia_info_path,
"/proc/driver/nvidia/gpus/%d/information", my_gpu);
121 nvidia_info= fopen(nvidia_info_path,
"r");
122 if (nvidia_info == NULL){
128 while (!feof(nvidia_info)){
129 if ( -1 == getline(&my_line, &nbytes, nvidia_info)){
135 if ( 1 == sscanf(my_line,
"Bus Location: %s", bus_info )){
136 sprintf(pci_bus_info_path,
"/sys/class/pci_bus/%.7s/cpulistaffinity",
142 pci_bus_info= fopen(pci_bus_info_path,
"r");
143 if (pci_bus_info == NULL){
150 while (!feof(pci_bus_info)){
151 if ( -1 == getline(&my_line, &nbytes, pci_bus_info)){
154 int rc = process_core_string_list(my_line, cpu_cores, ncores);
156 warningQuda(
"Failed to process the line \"%s\"", my_line);
173 int rc = getNumaAffinity(devid, cpu_cores, &ncores);
175 warningQuda(
"Failed to determine NUMA affinity for device %d (possibly not applicable)", devid);
178 int which = devid % ncores;
179 printfQuda(
"Setting NUMA affinity for device %d to CPU core %d\n", devid, cpu_cores[which]);
194 for(
int i=0;i < ncores;i++){
195 if( i != which)
continue;
196 CPU_SET(cpu_cores[i], &cpu_set);
199 rc = sched_setaffinity(0,
sizeof(cpu_set_t), &cpu_set);
201 warningQuda(
"Failed to enforce NUMA affinity (probably due to lack of kernel support)");
#define safe_malloc(size)
int setNumaAffinity(int devid)