File: affinity.cpp

package info (click to toggle)
magma-rocm 2.9.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 83,540 kB
  • sloc: cpp: 709,115; fortran: 121,916; ansic: 32,343; python: 25,603; f90: 15,208; makefile: 945; xml: 253; csh: 232; sh: 203; perl: 104
file content (83 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download | duplicates (4)
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
/*
    -- MAGMA (version 2.9.0) --
       Univ. of Tennessee, Knoxville
       Univ. of California, Berkeley
       Univ. of Colorado, Denver
       @date January 2025

       @author Raffaele Solca

*/
#ifndef MAGMA_NOAFFINITY

#include "affinity.h"

#include <stdio.h>

affinity_set::affinity_set()
{
    CPU_ZERO(&set);
}


affinity_set::affinity_set(int cpu)
{
    CPU_ZERO(&set);
    CPU_SET(cpu, &set);
}


void affinity_set::add(int cpu)
{
    CPU_SET(cpu, &set);
}


int affinity_set::get_affinity()
{
    return sched_getaffinity( 0, sizeof(set), &set);
}


int affinity_set::set_affinity()
{
    return sched_setaffinity( 0, sizeof(set), &set);
}


void affinity_set::print_affinity(int id, const char* s)
{
    if (get_affinity() == 0)
        print_set(id, s);
    else
        printf("Error in sched_getaffinity\n");
}


void affinity_set::print_set(int id, const char* s)
{
// CPU_COUNT() first appeared in glibc 2.6.
#if __GLIBC_PREREQ(2,6)
    char cpustring[1024];

    int cpu_count = CPU_COUNT(&set);
    int charcnt = 0;

    charcnt = snprintf( cpustring, sizeof(cpustring), "thread %d has affinity with %d CPUS: ", id, cpu_count);

    int nrcpu = 0;

    for (int icpu=0; nrcpu < cpu_count && icpu < CPU_SETSIZE; ++icpu) {
        if ( CPU_ISSET( icpu, &set )) {
            charcnt += snprintf( &(cpustring[charcnt]), sizeof(cpustring)-charcnt, "%d,", icpu );
            ++nrcpu;
        }
    }
    // charcnt-1 is used to remove "," after last cpu.
    charcnt += snprintf( &(cpustring[charcnt-1]), sizeof(cpustring)-(charcnt-1), "\n" ) - 1;
    printf("%s: %s", s, cpustring);
    fflush(stdout);
#endif
}

#endif  // MAGMA_NOAFFINITY