INLA_DIST
Loading...
Searching...
No Matches
helper_functions.h
1#ifndef helperfunctions_h
2#define helperfunction_h
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <unistd.h>
7#include <sched.h>
8#include <string.h>
9#include <errno.h>
10#include <iostream>
11
12#include "cuda.h"
13#include "cuda_runtime_api.h"
14
15//#define DEBUG
16
17//Mit diesen 3 Werte kannst du dir die NUMA Node ID holen:
18inline int topo_get_numNode(int GPU_rank)
19{
20
21 //printf("in topo get numNode\n");
22 int numaNodeID;
23 int pciBus, pciDomain, pciDevice;
24 cudaDeviceGetAttribute(&pciBus, cudaDevAttrPciBusId, GPU_rank);
25 cudaDeviceGetAttribute(&pciDevice, cudaDevAttrPciDeviceId, GPU_rank);
26 cudaDeviceGetAttribute(&pciDomain, cudaDevAttrPciDomainId, GPU_rank);
27
28 //printf("pciBus: %d, pciDevice %d, pciDomain: %d\n", pciBus, pciDevice, pciDomain);
29
30 char fname[1024];
31 char buff[100];
32 int ret = snprintf(fname, 1023, "/sys/bus/pci/devices/0000:%02x:%02x.%1x/numa_node", pciBus, pciDevice, pciDomain);
33 if (ret > 0)
34 {
35 fname[ret] = '\0';
36 FILE* fp = fopen(fname, "r");
37 if (fp)
38 {
39 ret = fread(buff, sizeof(char), 99, fp);
40 int numaNodeID = atoi(buff);
41 fclose(fp);
42 //std::cout << "In get NUMA ID. GPU rank: " << GPU_rank << ", pciBus: " << pciBus << ", pciDevice: " << pciDevice << ", pciDomain: " << pciDomain << ", numa node ID: " << numaNodeID << std::endl;
43
44 return numaNodeID;
45 }
46 }
47 return -1;
48}
49
50// given a numa domain -> identify all corresponding hwthreads/cores
51// returns # of threads and writes corresponding indices into hwthread list
52inline int read_numa_threads(int numa_node, int** hwthread_list)
53{
54 char path[1024];
55 int total_hwthreads = sysconf(_SC_NPROCESSORS_CONF);
56 int cpuidx = 0;
57 int* cpulist = new int[total_hwthreads];
58 if (!cpulist)
59 {
60 return -1;
61 }
62
63 for (int i = 0; i < total_hwthreads; i++)
64 {
65 int ret = snprintf(path, 1023, "/sys/devices/system/node/node%d/cpu%d", numa_node, i);
66 if (!access(path, F_OK))
67 {
68#ifdef DEBUG
69 printf("HWthread %d located in NUMA domain %d\n", i, numa_node);
70#endif
71 cpulist[cpuidx++] = i;
72 }
73 }
74 *hwthread_list = cpulist;
75#ifdef DEBUG
76 printf("NUMA domain %d has %d HWThreads\n", numa_node, cpuidx);
77#endif
78 return cpuidx;
79}
80
81// pin count-many threads from hwthread list
82inline int pin_hwthreads(int count, int* hwthread_list)
83{
84 cpu_set_t cpuset;
85 CPU_ZERO(&cpuset);
86#ifdef DEBUG
87 printf("Pinning to");
88#endif
89 for (int i = 0; i < count; i++)
90 {
91 CPU_SET(hwthread_list[i], &cpuset);
92#ifdef DEBUG
93 printf(" %d", hwthread_list[i]);
94#endif
95 }
96#ifdef DEBUG
97 printf("\n");
98#endif
99 return sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
100}
101
102
103#endif