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