ubuntu14.04下的openmp支持
2017/8/29 11:32
1、经典源码(helloworld.c):Hello World!
#include
//#include
int main(int argc, char* argv[])
{
#pragma omp parallel
printf( "Hello World!\n");
return 0;
}
(编译时不加并行参数:-fopenmp)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ gcc helloworld.c -o helloworld
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./helloworld
Hello World!
(编译时加并行参数:-fopenmp)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ gcc helloworld.c -o helloworld -fopenmp
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./helloworld
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
2、openmp经典:对for的支持:
#include
//#include
int main(int argc, char* argv[])
{
int i;
#pragma omp parallel for
for (i = 0; i < 10; i++ )
{
printf("i = %d\n", i);
}
return 0;
}
(编译时不加并行参数:-fopenmp)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ gcc for_openmp.c -o for_openmp
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./for_openmp
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
(编译时加并行参数:-fopenmp)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ gcc for_openmp.c -o for_openmp -fopenmp
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./for_openmp
i = 6
i = 4
i = 5
i = 8
i = 0
i = 1
i = 7
i = 9
i = 2
i = 3
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./for_openmp
i = 6
i = 5
i = 8
i = 7
i = 4
i = 2
i = 3
i = 9
i = 0
i = 1
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ ./for_openmp
i = 7
i = 2
i = 3
i = 4
i = 5
i = 6
i = 8
i = 9
i = 0
i = 1
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
(连续执行3次,每次的执行结果都不一样!)
环境变量配置:
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
电脑的硬件配置:神舟(HASEE) K580D-i7 D1 15.6英寸笔记本(i7-4710MQ 4G 1TB GT840M 2G独显 D刻 1080P IPS屏)铁灰
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ grep "model name" /proc/cpuinfo |awk -F ':' '{print $NF}'
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ lscpu |grep "CPU socket" |awk '{print $NF}' 或 lscpu |grep "Socket" |awk '{print $NF}'
awk: 致命错误: 无法以读模式打开文件“或”(没有那个文件或目录)
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ lscpu |grep "CPU socket" |awk '{print $NF}'
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ lscpu |grep "Socket" |awk '{print $NF}'
1
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$ lscpu |grep "Core(s) per socket" |awk '{print $NF}'
4
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
rootroot@rootroot-E400:~/wyb/ubuntu_hex/openmp$
http://blog.csdn.net/suchto/article/details/46330235/
Ubuntu下查看CPU、内存和硬盘详细信息的几个命令
CPU:
型号:grep "model name" /proc/cpuinfo |awk -F ':' '{print $NF}'
数量:lscpu |grep "CPU socket" |awk '{print $NF}' 或 lscpu |grep "Socket" |awk '{print $NF}'
每个CPU的核数:lscpu |grep "Core(s) per socket" |awk '{print $NF}'
参考资料:
https://www.ibm.com/developerworks/cn/aix/library/au-aix-openmp-framework/
通过 GCC 学习 OpenMP 框架
http://blog.csdn.net/qq_20198487/article/details/51626464
Linux环境下的OpenMP多线程编程
http://linux.chinaunix.net/techdoc/beginner/2009/06/10/1117627.shtml
关于linux下的openmp编程基础