hillpig的个人博客分享 http://blog.sciencenet.cn/u/hillpig 畅想ing,思考ing,前行ing Email:bluevaley@gmail.com

博文

获取cpu逻辑核的数量

已有 6021 次阅读 2011-1-18 20:23 |个人分类:postgresql|系统分类:科研笔记| The, machine, Number, find, cores

在many-core程序的设计中,往往需要获取cpu核的数量,觉得这段代码很好,摘录下来:
摘自:http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
#ifdef _WIN32
#include <windows.h>
#elif MACOS
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#include <unistd.h>
#endif

int getNumCores() {
#ifdef WIN32
   SYSTEM_INFO sysinfo
;
   
GetSystemInfo(&sysinfo);
   
return sysinfo.dwNumberOfProcessors;
#elif MACOS
   
int nm[2];
   size_t len
= 4;
   uint32_t count
;

   nm
[0] = CTL_HW; nm[1] = HW_AVAILCPU;
   sysctl
(nm, 2, &count, &len, NULL, 0);

   
if(count < 1) {
       nm
[1] = HW_NCPU;
       sysctl
(nm, 2, &count, &len, NULL, 0);
       
if(count < 1) { count = 1; }
   
}
   
return count;
#else
   
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}

加我私人微信,交流技术。




https://wap.sciencenet.cn/blog-419883-405622.html

上一篇:只有情永在
下一篇:你被云计算“晕”了吗?
收藏 IP: 223.72.72.*| 热度|

1 唐常杰

该博文允许注册用户评论 请点击登录 评论 (2 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-28 23:26

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部