初次使用Windows下的OpenMP
来源:互联网 发布:linux 查看scala版本 编辑:程序博客网 时间:2024/04/30 03:59
一、开发环境配置
1)Windows下面比较容易,安装VS2008 or VS2010之后,新建一个工程并右键点击“属性”,依次进入“配置属性”--》“C/C++”--》“语言”,设置右侧的第五个选项,“OpenMP支持”设定为“是(/openmp)”,这样就完成了开发环境的设定;
2)设置系统环境变量
在环境变量中增加“OMP_NUM_THREADS”变量,数值自己根据你的CPU的性能来设置,一般2、4、8等。
二、OpenMP的helloworld
经过以上环境变量设置之后,可以再VS界面下编写OpenMP程序了,比如建立 一个控制台程序:
#include "stdafx.h"
#include "stdlib.h"
#include <omp.h>
int _tmain(int argc, _TCHAR* argv[])
{
omp_set_num_threads(4);
#pragma omp parallel
printf("hello world!No.%d\n",omp_get_thread_num());
return 0;
}
输出结果如下:
另一个稍微复杂一点的:
// firstMP.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include <omp.h>
#define TRUE 1
#define FALSE 0
int _tmain(int argc, _TCHAR* argv[])
{
int n = 4;
int *a, **b;
#ifdef _OPENMP
(void) omp_set_dynamic(FALSE);
if (omp_get_dynamic()) {printf("Warning: dynamic adjustment of threads has been set\n");}
(void) omp_set_num_threads(4);
#endif
if ( (a=(int *)malloc(n*sizeof(int))) == NULL ) {
perror("array a"); exit(-1);
}
if ( (b=(int **)malloc(n*sizeof(int *))) == NULL ) {
perror("array b"); exit(-1);
}
else {
for (int i=0; i<n; i++)
if ( (b[i]=(int *)malloc(n*sizeof(int))) == NULL )
{perror("array b"); exit(-1);}
}
#pragma omp parallel shared(n,a,b)
{
#pragma omp for
for (int i=0; i<n; i++)
{
a[i] = i + 1;
#pragma omp parallel for
for (int j=0; j<n; j++)
b[i][j] = a[i];
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("b[%d][%d] = %d ",i,j,b[i][j]);
printf("\n");
}
free(a);
free(b);
return 0;
}
输出结果为:
- 初次使用Windows下的OpenMP
- OpenMP: OpenMP在VC6下的使用
- Python在windows下的安装及初次使用
- windows下ipython notebook初次使用记录
- OpenMP多线程linux下的使用
- 初次使用crywin从安装到使用windows下的python
- Mac下使用OpenMP
- windows下OpenMP学习【for_wind】
- linux下gsoap的初次使用
- 初次使用Eclipse下的Python编程
- linux下gsoap的初次使用
- linux下gsoap的初次使用 (c)
- centos7下的rpmbuild初次使用~~~
- 初次使用Eclipse下的Python编程
- OpenMP: OpenMP的一点使用经验
- openMP的使用
- OpenMP的使用
- cmake openmp的使用
- 驾驭你的“职场布朗运动”
- 【JavaWeb】common-long包简介
- 尚观第15天nagios安装配置
- 新手 xcode4.5 真机调试与部署
- MySQL索引背后的数据结构及算法原理
- 初次使用Windows下的OpenMP
- 黑马程序员----面向对象总结二
- ffmpeg简介
- 第三届信息技术大赛Java组A卷答案
- Android 仿微信之(二)--主页面实现篇
- NSURLConnection 下载数据 -- IOS(实例)
- 雪·北漂
- 黑马程序员-----集合框架总结笔记一
- 与Socket通讯(Echo)