SCM Repository
View of /branches/vis12/src/lib/parallel-target/cpu.c
Parent Directory
|
Revision Log
Revision 3291 -
(download)
(as text)
(annotate)
Wed Oct 14 21:25:00 2015 UTC (6 years, 8 months ago) by jhr
File size: 3580 byte(s)
Wed Oct 14 21:25:00 2015 UTC (6 years, 8 months ago) by jhr
File size: 3580 byte(s)
code is copyright University of Chicago
/*! \file cpu.c * * \author John Reppy */ /* * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) * * COPYRIGHT (c) 2015 The University of Chicago * All rights reserved. */ #include "Diderot/diderot.h" #if defined (__APPLE__) # include <sys/sysctl.h> #endif #ifdef HAVE_LIBNUMA # include <numa.h> #endif #include <errno.h> /*! \brief determine the number of CPUs * \return true if there is an error, false otherwise. */ bool Diderot_GetNumCPUs (CPUInfo_t *info) { #if defined(HAVE__PROC_CPUINFO) /* Get information from /proc/cpuinfo. The interesting * fields are: * * processor : <id> # logical processor id * physical id : <id> # node id * core id : <id> # core id (per node) * cpu cores : <n> # number of cores per node */ FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); char buf[1024]; if (cpuinfo != NULL) { int maxProcId = 0, maxNodeId = 0, maxCoreId = 0, nCores = 0; while (fgets(buf, sizeof(buf), cpuinfo) != 0) { int tmp; if (sscanf(buf, "processor : %d", &tmp) == 1) { maxProcId = (tmp > maxProcId) ? tmp : maxProcId; } else if (sscanf(buf, "physical id : %d", &tmp) == 1) { maxNodeId = (tmp > maxNodeId) ? tmp : maxNodeId; } else if (sscanf(buf, "core id : %d", &tmp) == 1) { maxCoreId = (tmp > maxCoreId) ? tmp : maxCoreId; } else if (sscanf(buf, "cpu cores : %d", &tmp) == 1) { nCores = (tmp > nCores) ? tmp : nCores; } } fclose (cpuinfo); #ifdef HAVE_LIBNUMA info->numHWThreads = info->numHWCores = maxProcId + 1; info->numHWNodes = numa_max_node()+1; info->numThdsPerCore = 1; info->numCoresPerNode = info->numHWThreads / info->numHWNodes; #else info->numHWNodes = maxNodeId + 1; info->numHWCores = info->numHWNodes * nCores; info->numHWThreads = maxProcId + 1; info->numCoresPerNode = nCores; info->numThdsPerCore = info->numHWThreads / info->numHWCores; #endif return false; } else { fprintf(stderr, "unable to determine the number of processors\n"); return true; } #elif defined(__APPLE__) size_t len = sizeof(int); /* the number of nodes */ if (sysctlbyname("hw.packages", &(info->numHWNodes), &len, 0, 0) < 0) { if (errno == ENOENT) { // "hw.packages" is not known info->numHWNodes = 1; } else { fprintf(stderr, "unable to determine the number of nodes\n"); return true; } } /* the number of cores */ if (sysctlbyname("hw.physicalcpu", &(info->numHWCores), &len, 0, 0) < 0) { fprintf(stderr, "unable to determine the number of physical CPUs\n"); return true; } /* the number of hardware threads */ if (sysctlbyname("hw.logicalcpu", &(info->numHWThreads), &len, 0, 0) < 0) { if (errno == ENOENT) { // "hw.packages" is not known info->numHWThreads = info->numHWCores; } else { fprintf(stderr, "unable to determine the number of logical CPUs\n"); return true; } } info->numCoresPerNode = info->numHWCores / info->numHWNodes; info->numThdsPerCore = info->numHWThreads / info->numHWCores; return false; #else return true; #endif } /* end of Diderot_GetNumCPUs */
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |