高性能计算实验报告 本文关键词:高性能,实验,计算,报告
高性能计算实验报告 本文简介:高性能计算实验报告学生姓名:XX学号:XXXXXXXXXX班号:116122指导教师:郭明强中国地质大学(武汉)信息工程学院第一题1.编写console程序2.由下图看出,电脑是双核CPU3.多线程程序,利用windowsAPI函数创建线程代码#include“stdafx.h“#include#i
高性能计算实验报告 本文内容:
高性能计算实验报告
学生姓名:
X
X
学
号:
XXXXXXXXXX
班
号:
116122
指导教师:
郭明强
中国地质大学(武汉)信息工程学院
第一题
1.编写console程序
2.由下图看出,电脑是双核CPU
3.多线程程序,利用windowsAPI函数创建线程
代码
#include
“stdafx.h“#include
#include
“windows.h“using
namespace
std;
DWORD
WINAPI
first(PVOID
pParam)
{
for
(int
i
=
0;i
using
namespace
std;
//计算e
DWORD
WINAPI
ThreadCalc_E(PVOID
pParam)//计算e子函数
{
double
factorial
=
1;
int
i
=
1;
double
e
=
1;
for
(;i
using
namespace
std;
void
main()
{
#pragma
omp
parallel
num_threads(8)
{
printf(“Hello,World!,ThreadId=%d/n“,omp_get_thread_num());
}
}
2.For语句
#include
“stdafx.h“#include
“windows.h“#include
“omp.h“#include
using
namespace
std;
void
main()
{
int
j
=
0;
#pragma
omp
parallel
{
#pragma
omp
for
for
(j
=
0;j
using
namespace
std;
void
main(int
argc,char
argv)
{
#pragma
omp
parallel
sections
{
#pragma
omp
section
printf(“section
1
threadid
=
%d
/n“,omp_get_thread_num());
#pragma
omp
section
printf(“section
2
threadid
=
%d
/n“,omp_get_thread_num());
#pragma
omp
section
printf(“section
3
threadid
=
%d
/n“,omp_get_thread_num());
#pragma
omp
section
printf(“section
4
threadid
=
%d
/n“,omp_get_thread_num());
}
}
4.Threadprivate语句的用法
#include
“stdafx.h“#include
“windows.h“#include
“omp.h“#include
using
namespace
std;
int
a,b,i,tid;
float
x;
#pragma
omp
threadprivate(a,x)
void
main(){
//关闭动态线程分配
omp_set_dynamic(0);
printf(“1st
Parallel
Region:/n“);
#pragma
omp
parallel
private(b,tid)
{
tid
=
omp_get_thread_num();
a
=
tid;
b
=
tid;
x
=
1.1*tid
+
1.0;
printf(“Threading
%d:
a,b,x
=
%d
%d
%f/n“,tid,a,b,x);
}//end
of
parallel
section
printf(“********************************************/n“);
printf(“主线程中串行线程/n“);
printf(“********************************************/n“);
printf(“2nd
Parallel
Region:/n“);
#pragma
omp
parallel
private(tid)
{
tid
=
omp_get_thread_num();
printf(“Threading
%d:
a,b,x
=
%d
%d
%f/n“,tid,a,b,x);
}//end
of
parallel
section
}
5.reduction语句的用法
#include
“stdafx.h“#include
“windows.h“#include
“omp.h“#include
using
namespace
std;
#include
void
main()
{
int
i,n,chunk;
float
a[100],b[100],result;
//变量的初始化
n
=
100;
chunk
=
10;
result
=
0.0;
for
(i
=
0;i
#define
num_steps
20000000
int
main(int
argc,charargv[])
{
double
start,stop;
double
e,pi,factorial,product;
int
i;
//启动定时器
start
=
clock();
//首先运用taylor展开运算e
printf(“e
started/n“);
e
=
1;
factorial
=
1;
for
(i
=
1;i
#define
num_steps
20000000
int
main(int
argc,char
argv[])
{
double
start,stop;//任务开始
double
e,pi,factorial,product;
int
i;
//启动定时器
start
=
clock();
//启动两个进程
分别计算e
pi
#pragma
omp
parallel
sections
shared(e,pi)
{
#pragma
omp
section
{
printf(“e
started/n“);
e
=
1;
factorial
=
1;
for
(i
=
1;i
<
num_steps;i++)
{
factorial=
i;
e
+=
1.0
/
factorial;
}
printf(“e
done/n“);
}
#pragma
omp
section
{
printf(“pi
started/n“);
pi
=
0;
for
(i
=
1;i
<
num_steps
10;i++)
{
pi
+=
1.0/(i*4.0
+
1.0);
pi
-=
1.0/(i*4.0
+
3.0);
}
pi
=
pi*4.0;
printf(“pi
done/n“);
}
}//omp
sections
//两个线程合并为主线程
product
=
e*pi;
stop
=
clock();
printf(“reached
result
%f
in
%.3f
second/n“,product,(stop
-
start)/1000);
return
0;
}
由下面两个图可知,并行比串行运行速度快,CPU使用效率高
1.串行
2.并行