当前位置:文档之家› 并行计算第一次实验报告

并行计算第一次实验报告

并行计算上机实验报告题目:多线程计算Pi值
学生姓名
学院名称计算机学院
专业计算机科学与技术时间
一. 实验目的
1、掌握集群任务提交方式;
2、掌握多线程编程。

二.实验内容
1、通过下图中的近似公式,使用多线程编程实现pi的计算;
2、通过控制变量N的数值以及线程的数量,观察程序的执行效率。

三.实现方法
1. 下载配置SSH客户端
2. 用多线程编写pi代码
3. 通过文件传输界面,将文件上传到集群上
4.将命令行目录切换至data,对.c文件进行编译
5.编写PBS脚本,提交作业
6.实验代码如下:
#include <math.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
static double PI=0;
static int N=0;
static int numOfThread=0;
static int length=0;
static int timeUsed=0;
static int numOfThreadArray[]={1,2,4,6,8,10,12,14,16,20,24,30};
static int threadArraySize=12;
static int nTime=4;
static int repeatTime=30;
static double totalTime=0;
struct timeval tvpre, tvafter;
pthread_mutex_t mut;
clockid_t startTime,endTime;
void cmpu(int i)
{
double p=0;
int k;
for(k=i*length;k<(i+1)*length;k++)
{
p+=4.0/((double)(1+(pow((k+0.5)/N,2 )))); }
p=p/N;
pthread_mutex_lock(&mut);
PI+=p;
pthread_mutex_unlock(&mut);
}
void mu()
{
gettimeofday(&tvpre, NULL);
startTime=clock();
pthread_t *myThread;
myThread=(pthread_t*)malloc((sizeof(pthread_t)*numOfThread) );
int i;
int res;
for(i=0;i<numOfThread;i++)
{
res=pthread_create(&myThread[i],NULL,cmpu,i);
if(res!=0)
{
perror("Thread join failed");
}
}
for(i=0;i<numOfThread;i++)
{
pthread_join(myThread[i],NULL);
}
int left;
for(left=(length*numOfThread);left<N;left++){
PI+=4.0/((double)(1+(pow((left+0.5)/N,2 ))))/N;
}
endTime=clock();
timeUsed=endTime-startTime;
gettimeofday(&tvafter, NULL);
}
void printResult()
{
printf("N is %d , num of threads is %d\n",N,numOfThread); printf("Pi is %0.15f \n Used time %d ms\n",PI,(timeUsed)); printf("cost time: %ld msecond\n", (__sec)*1000+(_usec-tvpre.t v_usec)/1000);
}
void mainPro(int ns,int n_thread,FILE *fp)
{
N=ns;
numOfThread=n_thread;
PI=0;
length=N/numOfThread;
mu();
}
void main(int arg,char **args)
{
N=100000;
FILE *fp=fopen("myLog.xls","a");
int i=0;
for(i=0;i<nTime;i++)
{
N=N*10;
int j=0;
for(j=0;j<threadArraySize;j++)
{
numOfThread=numOfThreadArray[j];
totalTime=0;
int q=0;
for(q=0;q<repeatTime;q++)
{
mainPro(N,numOfThread,fp);
totalTime+=(__sec)*1000+(tvafter.t v__usec)/1000;
}
fprintf(fp,"%d,%d,%f\n",N,numOfThread,totalTime/repeatTi me);
printf("%d,%d,%f\n",N,numOfThread,totalTime/repeatTime);
}
}
fclose(fp);
}
四.程序流程图
五.加速比曲线
1.下图为线程数与运行时间的关系,横坐标代表线程数,纵坐标代
表运行时间,黄色的曲线代表N=100000000,粉色的曲线代表N=1000000,黑色的线代表N=100000.
2.加速比曲线
100000000
10000000
1000000
六.总结感悟
随着运算线程增加,运算量一定情况下,运算时间减少。

这符合加速比定律。

此程序不需要线程间通信,而内存占用等因素集群又可满足,所以基本上加速比和线程数是成正比的。

通过此次实验,我学会了如何在集群上提交任务,如何查看任务情况等,了解并行程序和串行程序的差别,在课堂上学到的理论知识通过实践可以更深刻更透彻的理解,通过实验我收获很多。

相关主题