unsigned int gem::thread::getCPUCount(void) { // http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine #ifdef _OPENMP /* OpenMP */ return omp_get_num_procs(); #endif #ifdef _WIN32 SYSTEM_INFO sysinfo; GetSystemInfo( &sysinfo ); return (sysinfo.dwNumberOfProcessors); #endif #ifdef __linux__ /* Linux, Solaris, & AIX (per comments): */ return sysconf( _SC_NPROCESSORS_ONLN ); #endif #if defined (__APPLE__) || defined (__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* FreeBSD, MacOS X, NetBSD, OpenBSD, */ uint32_t count; int mib[4]; size_t len = sizeof(count); /* set the mib for hw.ncpu */ mib[0] = CTL_HW; mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU; /* get the number of CPUs from the system */ sysctl(mib, 2, &count, &len, NULL, 0); if( count < 1 ) { mib[1] = HW_NCPU; sysctl( mib, 2, &count, &len, NULL, 0 ); if(count < 1 ) { count = 1; } } return count; #endif #ifdef __hpux /* HPUX */ return mpctl(MPC_GETNUMSPUS, NULL, NULL); #endif #ifdef __irix__ /* IRIX */ return sysconf( _SC_NPROC_ONLN ); #endif return 1; // safe default }
int vmd_thread_numphysprocessors(void) { int a=1; #ifdef VMDTHREADS #if defined(__APPLE__) a = MPProcessorsScheduled(); /* Number of active/running CPUs */ #endif #ifdef _MSC_VER struct _SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); a = sysinfo.dwNumberOfProcessors; /* total number of CPUs */ #endif /* _MSC_VER */ #if defined(_CRAY) a = sysconf(_SC_CRAY_NCPU); #endif #if defined(__sun) || defined(__linux) || defined(__osf__) || defined(_AIX) a = sysconf(_SC_NPROCESSORS_ONLN); /* number of active/running CPUs */ #endif /* SunOS */ #if defined(__irix) a = sysconf(_SC_NPROC_ONLN); /* number of active/running CPUs */ #endif /* IRIX */ #if defined(__hpux) a = mpctl(MPC_GETNUMSPUS, 0, 0); /* total number of CPUs */ #endif /* HPUX */ #endif /* VMDTHREADS */ return a; }
/* Note: get_cpubind not available on HP-UX */ static int hwloc_hpux_set_proc_cpubind(hwloc_topology_t topology, hwloc_pid_t pid, hwloc_const_bitmap_t hwloc_set, int flags) { ldom_t ldom; spu_t cpu; /* Drop previous binding */ mpctl(MPC_SETLDOM, MPC_LDOMFLOAT, pid); mpctl(MPC_SETPROCESS, MPC_SPUFLOAT, pid); if (hwloc_bitmap_isequal(hwloc_set, hwloc_topology_get_complete_cpuset(topology))) return 0; ldom = hwloc_hpux_find_ldom(topology, hwloc_set); if (ldom != -1) return mpctl(MPC_SETLDOM, ldom, pid); cpu = hwloc_hpux_find_spu(topology, hwloc_set); if (cpu != -1) return mpctl(flags & HWLOC_CPUBIND_STRICT ? MPC_SETPROCESS_FORCE : MPC_SETPROCESS, cpu, pid); errno = EXDEV; return -1; }
static int cpu_count(void) { int n; #if defined (_SC_NPROCESSORS_ONLN) n = (int) sysconf(_SC_NPROCESSORS_ONLN); #elif defined (_SC_NPROC_ONLN) n = (int) sysconf(_SC_NPROC_ONLN); #elif defined (HPUX) #include <sys/mpctl.h> n = mpctl(MPC_GETNUMSPUS, 0, 0); #else n = -1; errno = ENOSYS; #endif return n; }
/* ** PR_GetNumberOfProcessors() ** ** Implementation notes: ** Every platform does it a bit different. ** numCpus is the returned value. ** for each platform's "if defined" section ** declare your local variable ** do your thing, assign to numCpus ** order of the if defined()s may be important, ** especially for unix variants. Do platform ** specific implementations before XP_UNIX. ** */ PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) { PRInt32 numCpus; #if defined(WIN32) SYSTEM_INFO info; GetSystemInfo( &info ); numCpus = info.dwNumberOfProcessors; #elif defined(BEOS) system_info sysInfo; get_system_info(&sysInfo); numCpus = sysInfo.cpu_count; #elif defined(OS2) DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); #elif defined(_PR_HAVE_SYSCTL) int mib[2]; int rc; size_t len = sizeof(numCpus); mib[0] = CTL_HW; mib[1] = HW_NCPU; rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); if ( -1 == rc ) { numCpus = -1; /* set to -1 for return value on error */ _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); } #elif defined(HPUX) numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); if ( numCpus < 1 ) { numCpus = -1; /* set to -1 for return value on error */ _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); } #elif defined(IRIX) numCpus = sysconf( _SC_NPROC_ONLN ); #elif defined(RISCOS) || defined(SYMBIAN) numCpus = 1; #elif defined(XP_UNIX) numCpus = sysconf( _SC_NPROCESSORS_ONLN ); #else #error "An implementation is required" #endif return(numCpus); } /* end PR_GetNumberOfProcessors() */
/////////////////////////////////////////////////////////////////////// // Function : osAvailableCPUs // Description : Get number of CPUs available // Return Value : Number of available CPUs or -1 if unknown // Comments : See <http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine> int osAvailableCPUs() { #if defined(WIN32) SYSTEM_INFO sys_info; GetSystemInfo(&sys_info); return sys_info.dwNumberOfProcessors; #elif defined(CTL_HW) // BSD, Mac OS X int ncpu = 0; int mib[2]; size_t sz = sizeof(ncpu); mib[0] = CTL_HW; #ifdef HW_AVAILCPU mib[1] = HW_AVAILCPU; #else mib[1] = HW_NCPU; #endif if(sysctl(mib, 2, &ncpu, &sz, NULL, 0) == -1) return ncpu; #elif defined(_SC_NPROCESSORS_ONLN) // Linux, AIX, Solaris return sysconf( _SC_NPROCESSORS_ONLN ); #elif defined(MPC_GETNUMSPUS) // HP-UX return mpctl(MPC_GETNUMSPUS, NULL, NULL); #elif defined(_SC_NPROC_ONLN) // IRIX return sysconf( _SC_NPROC_ONLN ); #endif return -1; }
/* ** PR_GetNumberOfProcessors() ** ** Implementation notes: ** Every platform does it a bit different. ** numCpus is the returned value. ** for each platform's "if defined" section ** declare your local variable ** do your thing, assign to numCpus ** order of the if defined()s may be important, ** especially for unix variants. Do platform ** specific implementations before XP_UNIX. ** */ PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) { PRInt32 numCpus; #if defined(WIN32) SYSTEM_INFO info; GetSystemInfo( &info ); numCpus = info.dwNumberOfProcessors; #elif defined(BEOS) system_info sysInfo; get_system_info(&sysInfo); numCpus = sysInfo.cpu_count; #elif defined(OS2) DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); #elif defined(_PR_HAVE_SYSCTL) int mib[2]; int rc; size_t len = sizeof(numCpus); mib[0] = CTL_HW; mib[1] = HW_NCPU; rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); if ( -1 == rc ) { numCpus = -1; /* set to -1 for return value on error */ _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); } #elif defined(HPUX) numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); if ( numCpus < 1 ) { numCpus = -1; /* set to -1 for return value on error */ _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); } #elif defined(IRIX) numCpus = sysconf( _SC_NPROC_ONLN ); #elif defined(RISCOS) || defined(SYMBIAN) numCpus = 1; #elif defined(LINUX) /* for the benefit of devices with advanced power-saving, that actually hotplug their cpus in heavy load, try to figure out the real number of CPUs */ char buf[MAX_LINE]; FILE *fin; const char *cpu_present = "/sys/devices/system/cpu/present"; size_t strsize; numCpus = 0; fin = fopen(cpu_present, "r"); if (fin != NULL) { if (fgets(buf, MAX_LINE, fin) != NULL) { /* check that the format is what we expect */ if (buf[0] == '0') { strsize = strlen(buf); if (strsize == 1) { /* single core */ numCpus = 1; } else if (strsize >= 3 && strsize <= 5) { /* should be of the form 0-999 */ /* parse the part after the 0-, note count is 0-based */ if (buf[1] == '-' && isdigit(buf[2])) { numCpus = 1 + atoi(buf + 2); } } } } fclose(fin); } /* if that fails, fall back to more standard methods */ if (!numCpus) { numCpus = sysconf( _SC_NPROCESSORS_CONF ); } #elif defined(XP_UNIX) numCpus = sysconf( _SC_NPROCESSORS_CONF ); #else #error "An implementation is required" #endif return(numCpus); } /* end PR_GetNumberOfProcessors() */
int getProcessorCount() { return mpctl(MPC_GETNUMSPUS, NULL, NULL); }
void hwloc_look_hpux(struct hwloc_topology *topology) { int has_numa = sysconf(_SC_CCNUMA_SUPPORT) == 1; hwloc_obj_t *nodes = NULL, obj; spu_t currentcpu; ldom_t currentnode; int i, nbnodes = 0; #ifdef HAVE__SC_LARGE_PAGESIZE topology->levels[0][0]->attr->machine.huge_page_size_kB = sysconf(_SC_LARGE_PAGESIZE); #endif if (has_numa) { nbnodes = mpctl(topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM ? MPC_GETNUMLDOMS_SYS : MPC_GETNUMLDOMS, 0, 0); hwloc_debug("%d nodes\n", nbnodes); nodes = malloc(nbnodes * sizeof(*nodes)); i = 0; currentnode = mpctl(topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM ? MPC_GETFIRSTLDOM_SYS : MPC_GETFIRSTLDOM, 0, 0); while (currentnode != -1 && i < nbnodes) { hwloc_debug("node %d is %d\n", i, currentnode); nodes[i] = obj = hwloc_alloc_setup_object(HWLOC_OBJ_NODE, currentnode); obj->cpuset = hwloc_bitmap_alloc(); obj->nodeset = hwloc_bitmap_alloc(); hwloc_bitmap_set(obj->nodeset, currentnode); /* TODO: obj->attr->node.memory_kB */ /* TODO: obj->attr->node.huge_page_free */ currentnode = mpctl(topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM ? MPC_GETNEXTLDOM_SYS : MPC_GETNEXTLDOM, currentnode, 0); i++; } } i = 0; currentcpu = mpctl(topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM ? MPC_GETFIRSTSPU_SYS : MPC_GETFIRSTSPU, 0,0); while (currentcpu != -1) { obj = hwloc_alloc_setup_object(HWLOC_OBJ_PU, currentcpu); obj->cpuset = hwloc_bitmap_alloc(); hwloc_bitmap_set(obj->cpuset, currentcpu); hwloc_debug("cpu %d\n", currentcpu); if (nodes) { /* Add this cpu to its node */ currentnode = mpctl(MPC_SPUTOLDOM, currentcpu, 0); if ((ldom_t) nodes[i]->os_index != currentnode) for (i = 0; i < nbnodes; i++) if ((ldom_t) nodes[i]->os_index == currentnode) break; if (i < nbnodes) { hwloc_bitmap_set(nodes[i]->cpuset, currentcpu); hwloc_debug("is in node %d\n", i); } else { hwloc_debug("%s", "is in no node?!\n"); } } /* Add cpu */ hwloc_insert_object_by_cpuset(topology, obj); currentcpu = mpctl(topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM ? MPC_GETNEXTSPU_SYS : MPC_GETNEXTSPU, currentcpu, 0); } if (nodes) { /* Add nodes */ for (i = 0 ; i < nbnodes ; i++) hwloc_insert_object_by_cpuset(topology, nodes[i]); free(nodes); } topology->support.discovery->pu = 1; hwloc_obj_add_info(topology->levels[0][0], "Backend", "HP-UX"); }
static int hwloc_look_hpux(struct hwloc_backend *backend) { struct hwloc_topology *topology = backend->topology; int has_numa = sysconf(_SC_CCNUMA_SUPPORT) == 1; hwloc_obj_t *nodes = NULL, obj; spu_t currentcpu; ldom_t currentnode; int i, nbnodes = 0; if (topology->levels[0][0]->cpuset) /* somebody discovered things */ return -1; hwloc_alloc_obj_cpusets(topology->levels[0][0]); if (has_numa) { nbnodes = mpctl((topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) ? MPC_GETNUMLDOMS_SYS : MPC_GETNUMLDOMS, 0, 0); hwloc_debug("%d nodes\n", nbnodes); nodes = malloc(nbnodes * sizeof(*nodes)); i = 0; currentnode = mpctl((topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) ? MPC_GETFIRSTLDOM_SYS : MPC_GETFIRSTLDOM, 0, 0); while (currentnode != -1 && i < nbnodes) { hwloc_debug("node %d is %d\n", i, currentnode); nodes[i] = obj = hwloc_alloc_setup_object(topology, HWLOC_OBJ_NUMANODE, currentnode); obj->cpuset = hwloc_bitmap_alloc(); obj->nodeset = hwloc_bitmap_alloc(); hwloc_bitmap_set(obj->nodeset, currentnode); /* TODO: obj->attr->node.memory_kB */ /* TODO: obj->attr->node.huge_page_free */ currentnode = mpctl((topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) ? MPC_GETNEXTLDOM_SYS : MPC_GETNEXTLDOM, currentnode, 0); i++; } } i = 0; currentcpu = mpctl((topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) ? MPC_GETFIRSTSPU_SYS : MPC_GETFIRSTSPU, 0,0); while (currentcpu != -1) { obj = hwloc_alloc_setup_object(topology, HWLOC_OBJ_PU, currentcpu); obj->cpuset = hwloc_bitmap_alloc(); hwloc_bitmap_set(obj->cpuset, currentcpu); hwloc_debug("cpu %d\n", currentcpu); if (nodes) { /* Add this cpu to its node */ currentnode = mpctl(MPC_SPUTOLDOM, currentcpu, 0); /* Hopefully it's just the same as previous cpu */ if (i >= nbnodes || (ldom_t) nodes[i]->os_index != currentnode) for (i = 0; i < nbnodes; i++) if ((ldom_t) nodes[i]->os_index == currentnode) break; if (i < nbnodes) { hwloc_bitmap_set(nodes[i]->cpuset, currentcpu); hwloc_debug("is in node %d\n", i); } else { hwloc_debug("%s", "is in no node?!\n"); } } /* Add cpu */ hwloc_insert_object_by_cpuset(topology, obj); currentcpu = mpctl((topology->flags & HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) ? MPC_GETNEXTSPU_SYS : MPC_GETNEXTSPU, currentcpu, 0); } if (nodes) { /* Add nodes */ for (i = 0 ; i < nbnodes ; i++) hwloc_insert_object_by_cpuset(topology, nodes[i]); free(nodes); } topology->support.discovery->pu = 1; hwloc_obj_add_info(topology->levels[0][0], "Backend", "HP-UX"); hwloc_add_uname_info(topology, NULL); return 0; }
int Thread_GetNumCPUS (void) { int numcpus = mpctl(MPC_GETNUMSPUS, NULL, NULL); return numcpus; }
int wkf_thread_numphysprocessors(void) { int a=1; #ifdef WKFTHREADS #if defined(__APPLE__) #if 1 int rc; int mib[2]; u_int miblen; size_t alen = sizeof(a); mib[0] = CTL_HW; mib[1] = HW_AVAILCPU; miblen = 2; rc = sysctl(mib, miblen, &a, &alen, NULL, 0); /**< Number of active CPUs */ if (rc < 0) { perror("Error during sysctl() query for CPU count"); a = 1; } #else a = MPProcessorsScheduled(); /**< Number of active/running CPUs */ #endif #endif #ifdef _MSC_VER struct _SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); a = sysinfo.dwNumberOfProcessors; /**< total number of CPUs */ #endif /* _MSC_VER */ #if defined(_CRAY) a = sysconf(_SC_CRAY_NCPU); /**< total number of CPUs */ #endif #if defined(ANDROID) /* Android toggles cores on/off according to system activity, */ /* thermal management, and battery state. For now, we will */ /* use as many threads as the number of physical cores since */ /* the number that are online may vary even over a 2 second */ /* time window. We will likely have this issue on other */ /* platforms as power management becomes more important... */ /* use sysconf() for initial guess, although it produces incorrect */ /* results on the older android releases due to a bug in the platform */ a = sysconf(_SC_NPROCESSORS_CONF); /**< Number of physical CPU cores */ /* check CPU count by parsing /sys/devices/system/cpu/present and use */ /* whichever result gives the larger CPU count... */ { int rc=0, b=1, i=-1, j=-1; FILE *ifp; ifp = fopen("/sys/devices/system/cpu/present", "r"); if (ifp != NULL) { rc = fscanf(ifp, "%d-%d", &i, &j); /* read and interpret line */ fclose(ifp); if (rc == 2 && i == 0) { b = j+1; /* 2 or more cores exist */ } } /* return the greater CPU count result... */ a = (a > b) ? a : b; } #else #if defined(__sun) || defined(__linux) || defined(__osf__) || defined(_AIX) a = sysconf(_SC_NPROCESSORS_ONLN); /**< Number of active/running CPUs */ #endif /* SunOS, and similar... */ #endif /* Android */ #if defined(__irix) a = sysconf(_SC_NPROC_ONLN); /**< Number of active/running CPUs */ #endif /* IRIX */ #if defined(__hpux) a = mpctl(MPC_GETNUMSPUS, 0, 0); /**< total number of CPUs */ #endif /* HPUX */ #endif /* WKFTHREADS */ return a; }