SCM Repository
Annotation of /trunk/src/lib/parallel-target/cpu.c
Parent Directory
|
Revision Log
Revision 3349 - (view) (download) (as text)
1 : | jhr | 1232 | /*! \file cpu.c |
2 : | * | ||
3 : | * \author John Reppy | ||
4 : | */ | ||
5 : | |||
6 : | /* | ||
7 : | jhr | 3349 | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) |
8 : | * | ||
9 : | * COPYRIGHT (c) 2015 The University of Chicago | ||
10 : | jhr | 1232 | * All rights reserved. |
11 : | */ | ||
12 : | |||
13 : | #include "Diderot/diderot.h" | ||
14 : | #if defined (__APPLE__) | ||
15 : | # include <sys/sysctl.h> | ||
16 : | #endif | ||
17 : | #ifdef HAVE_LIBNUMA | ||
18 : | # include <numa.h> | ||
19 : | #endif | ||
20 : | #include <errno.h> | ||
21 : | |||
22 : | /*! \brief determine the number of CPUs */ | ||
23 : | bool GetNumCPUs (CPUInfo_t *info) | ||
24 : | { | ||
25 : | #if defined(HAVE__PROC_CPUINFO) | ||
26 : | /* Get information from /proc/cpuinfo. The interesting | ||
27 : | * fields are: | ||
28 : | * | ||
29 : | jhr | 1301 | * processor : <id> # logical processor id |
30 : | * physical id : <id> # node id | ||
31 : | * core id : <id> # core id (per node) | ||
32 : | * cpu cores : <n> # number of cores per node | ||
33 : | jhr | 1232 | */ |
34 : | FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); | ||
35 : | char buf[1024]; | ||
36 : | if (cpuinfo != NULL) { | ||
37 : | jhr | 1301 | int maxProcId = 0, maxNodeId = 0, maxCoreId = 0, nCores = 0; |
38 : | while (fgets(buf, sizeof(buf), cpuinfo) != 0) { | ||
39 : | int tmp; | ||
40 : | if (sscanf(buf, "processor : %d", &tmp) == 1) { | ||
41 : | maxProcId = (tmp > maxProcId) ? tmp : maxProcId; | ||
42 : | } | ||
43 : | else if (sscanf(buf, "physical id : %d", &tmp) == 1) { | ||
44 : | maxNodeId = (tmp > maxNodeId) ? tmp : maxNodeId; | ||
45 : | } | ||
46 : | else if (sscanf(buf, "core id : %d", &tmp) == 1) { | ||
47 : | maxCoreId = (tmp > maxCoreId) ? tmp : maxCoreId; | ||
48 : | } | ||
49 : | else if (sscanf(buf, "cpu cores : %d", &tmp) == 1) { | ||
50 : | nCores = (tmp > nCores) ? tmp : nCores; | ||
51 : | } | ||
52 : | } | ||
53 : | fclose (cpuinfo); | ||
54 : | jhr | 1232 | |
55 : | #ifdef HAVE_LIBNUMA | ||
56 : | jhr | 1301 | info->numHWThreads = info->numHWCores = maxProcId + 1; |
57 : | info->numHWNodes = numa_max_node()+1; | ||
58 : | info->numThdsPerCore = 1; | ||
59 : | info->numCoresPerNode = info->numHWThreads / info->numHWNodes; | ||
60 : | jhr | 1232 | #else |
61 : | jhr | 1301 | info->numHWNodes = maxNodeId + 1; |
62 : | info->numHWCores = info->numHWNodes * nCores; | ||
63 : | info->numHWThreads = maxProcId + 1; | ||
64 : | info->numCoresPerNode = nCores; | ||
65 : | info->numThdsPerCore = info->numHWThreads / info->numHWCores; | ||
66 : | jhr | 1232 | #endif |
67 : | |||
68 : | jhr | 1301 | return true; |
69 : | jhr | 1232 | } |
70 : | else { | ||
71 : | jhr | 1301 | fprintf(stderr, "unable to determine the number of processors\n"); |
72 : | return false; | ||
73 : | jhr | 1232 | } |
74 : | #elif defined(__APPLE__) | ||
75 : | jhr | 1301 | size_t len = sizeof(int); |
76 : | jhr | 1232 | |
77 : | /* the number of nodes */ | ||
78 : | if (sysctlbyname("hw.packages", &(info->numHWNodes), &len, 0, 0) < 0) { | ||
79 : | jhr | 1301 | if (errno == ENOENT) { |
80 : | // "hw.packages" is not known | ||
81 : | info->numHWNodes = 1; | ||
82 : | } | ||
83 : | else { | ||
84 : | fprintf(stderr, "unable to determine the number of nodes\n"); | ||
85 : | return false; | ||
86 : | } | ||
87 : | jhr | 1232 | } |
88 : | |||
89 : | /* the number of cores */ | ||
90 : | if (sysctlbyname("hw.physicalcpu", &(info->numHWCores), &len, 0, 0) < 0) { | ||
91 : | jhr | 1301 | fprintf(stderr, "unable to determine the number of physical CPUs\n"); |
92 : | return false; | ||
93 : | jhr | 1232 | } |
94 : | |||
95 : | /* the number of hardware threads */ | ||
96 : | if (sysctlbyname("hw.logicalcpu", &(info->numHWThreads), &len, 0, 0) < 0) { | ||
97 : | jhr | 1301 | if (errno == ENOENT) { |
98 : | // "hw.packages" is not known | ||
99 : | info->numHWThreads = info->numHWCores; | ||
100 : | } | ||
101 : | else { | ||
102 : | fprintf(stderr, "unable to determine the number of logical CPUs\n"); | ||
103 : | return false; | ||
104 : | } | ||
105 : | jhr | 1232 | } |
106 : | |||
107 : | info->numCoresPerNode = info->numHWCores / info->numHWNodes; | ||
108 : | info->numThdsPerCore = info->numHWThreads / info->numHWCores; | ||
109 : | |||
110 : | return true; | ||
111 : | #else | ||
112 : | return false; | ||
113 : | #endif | ||
114 : | |||
115 : | } /* end of GetNumCPUs */ |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |