-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
90 lines (83 loc) · 2.1 KB
/
utilities.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Print outs to which core MPI rank and OpenMP thread is bind to.
* Performs also dummy calculation, increase in execution time indicates
* that cores are oversubscribed. Comparing times with single MPI task,
* single thread vs. full node gives also hints about effects of CPU
* frequency scaling.
* Copyright (C) 2023 CSC - IT Center for Science
* This file may be redistributed under the terms of the
* GNU General Public License. See accompanying LICENSE for details
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <numaif.h>
char* get_affinity_str(char *str)
{
cpu_set_t mask;
sched_getaffinity(0, sizeof(mask), &mask);
// Code based on https://github.com/util-linux/util-linux (lib/cpuset.c)
char *ptr = str;
int i, j, entry_made = 0;
for (i = 0; i < CPU_SETSIZE; i++) {
if (CPU_ISSET(i, &mask)) {
int run = 0;
entry_made = 1;
for (j = i + 1; j < CPU_SETSIZE; j++) {
if (CPU_ISSET(j, &mask)) run++;
else break;
}
if (!run)
sprintf(ptr, "%d,", i);
else if (run == 1) {
sprintf(ptr, "%d,%d,", i, i + 1);
i++;
} else {
sprintf(ptr, "%d-%d,", i, i + run);
i += run;
}
while (*ptr != 0) ptr++;
}
}
ptr -= entry_made;
*ptr = '\0';
return(str);
}
char* get_mempolicy_str(char *str)
{
unsigned long nodemask;
const unsigned long maxnode = 8;
int mode;
get_mempolicy(&mode, &nodemask, maxnode, NULL, 0);
if ( MPOL_DEFAULT == mode) {
sprintf(str, "MPOL_DEFAULT");
return str;
}
int entry_made = 0;
for (int i=0; i < maxnode; i++) {
if (nodemask & (1 << i)) {
int run = 0;
entry_made = 1;
for (int j = i + 1; j < maxnode; j++) {
if (nodemask & (1 << j)) run++;
else break;
}
if (!run)
sprintf(str, "%d,", i);
else if (run == 1) {
sprintf(str, "%d,%d,", i, i + 1);
i++;
} else {
sprintf(str, "%d-%d,", i, i + run);
i += run;
}
while (*str != 0) str++;
}
}
str -= entry_made;
*str = '\0';
return(str);
}