文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > pThread_nbody1
侵权投诉

pThread_nbody1

多核计算机MPI

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include <pthread.h>
#define MAXThreadsNum 32 //允许启动的最大线程数量

int BodyNum=0;
int TimeSteps=0;

double *pBody;//存储粒子的基本信息,每个粒子占用4个连续的浮点数:mass、x、y、z
double *pForce;//存储粒子的受力,每个粒子占用3个连续的浮点数:Fx、Fy、Fz


int thread_num=0, progress=0;
void *calcForce(void *);
pthread_mutex_t mutex1, mutex2;
pthread_cond_t cond;

int main(int argc, char** argv )
{

int n, t, i, threadID[MAXThreadsNum];
pthread_t threads[MAXThreadsNum];
double run_time;
char *pStr;
FILE *fResult;

timeval start, end;

gettimeofday(&start, NULL);

for ( i=1; i<argc; i++ ) {
pStr=strstr(argv[i], "-s=");
if ( pStr!=NULL) sscanf(pStr, "-s=%d", &BodyNum);
pStr=strstr(argv[i], "-t=");
if ( pStr!=NULL) sscanf(pStr, "-t=%d", &TimeSteps);
pStr=strstr(argv[i], "-p=");
if ( pStr!=NULL ) sscanf(pStr, "-p=%d", &thread_num);


}

if ( BodyNum*TimeSteps*thread_num==0) {
printf("usage: ser_nbody -s=number-of-bodies -t=number-of-steps -p=number-of-threads\n");
return 0;
}

if(thread_num<2) {
printf("Please input the number of threads to be created by specifying -p=num_of_threads, where num_of_threads is a integer greater than 1\n");
return 0;
}
if(thread_num>MAXThreadsNum) thread_num = MAXThreadsNum;

pForce = new double[3*BodyNum];
pBody = new double[4*BodyNum];

/* Initialize mass and positions in array p to make a test case
Initialize force to 0
*/
for ( i=0; i<BodyNum; i++)
{
*(pBody+4*i) = 10.05 + i;
*(pBody+4*i+1) = 30.0*i;
*(pBody+4*i+2) = 20.0*i;
*(pBody+4*i+3) = 10.0*i;
*(pForce+3*i) = 0;
*(pForce+3*i+1) = 0;
*(pForce+3*i+2) = 0;
}

//并行计算
if (pthread_mutex_init(&mutex1,NULL)>0) printf("error in initializing mutex1\n");
if (pthread_mutex_init(&mutex2,NULL)>0) printf("error in initializing mutex2\n");
pthread_cond_init(&cond, NULL);

//创建线程
for(i=0; i<thread_num; i++) {
threadID[i] = i;
pthread_create(&threads[i],NULL, calcForce, &threadID[i]);
}

//等待全部线程结束
for(i=0; i<thread_num; i++)
pthread_join(threads[i],NULL);

pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_cond_destroy(&cond);


fResult=fopen("result_pthread_nbody1.txt", "w");

char result[50];
for (i=0; i<BodyNum; i++) {
sprintf(result, "(%10.4f %10.4f %10.4f %10.4f)\n", *(pBody+4*i), *(pBody+4*i+1), *(pBody+4*i+2), *(pBody+4*i+3));
fwrite(result, sizeof(char), strlen(result), fResult);
}
fclose(fResult);

delete[] pForce;
delete[] pBody;

gettimeofday(&end, NULL);
run_time = end.tv_sec - start.tv_sec + (double)(end.tv_usec-start

第1页

你可能喜欢

  • 广州知名企业hot
  • 多线程pthread
  • 知名儿童英语绘本wintervacation
  • 知名企业erp
  • 知名房地产offer

热门文档

相关文档

  • pThread_prime1

    pThread_nbody2 pThread_prime3 1/2 相关文档推荐 PThread 31页 1下载券 ...(pthread_mutex_init(&mutex1,NULL)>0) printf("error in initializing mutex...

  • 多线程编程实例---pthread_join函数详解1

    多线程编程实例---pthread_join函数详解1_计算机软件及应用_IT/计算机_专业资料。多线程编程实例---pthread_join函数,验证过,对自己学习很有帮助...

  • pthread_create用法

    pthread_create用法 linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程... pThread_nbody2 2页 免费 pThread_prime3 2页 免费 pThread_prime1 2页 ...

  • pThread

    pThread_nbody11/2 相关文档推荐 pThread_prime3 2页 免费 pThread_prime1 2页 免费 PThread 31页 1财富值 Pthread 2页 2财富值 pthread 1页 免费 pthread...

  • Linux程式设计入门-fork,pthread,and signals1

    Linux程式设计入门-fork,pthread,and signals1Linux程式设计入门-fork,pthread,and signals1隐藏>> Linux 程式设计入门 程式设计入门-fork,pthread,and signals ...

  • 多线程编程(pthread)

    多线程编程(pthread)_计算机软件及应用_IT/计算机_专业资料。今日推荐 ... pThread_nbody2 2页 免费 pThread_prime3 2页 免费喜欢此文档的还喜欢...

  • barnes-hut 算法

    64页 免费 pThread_nbody2 2页 免费 pThread_prime 2页 免费barnes-hut 算法...

  • 教 育部 - 英特尔 精 品课 程 ”建设 项目 申 请报 告

    基于多核Linux操作系统的PThread多线程编程、基于Intel Thread Checker和Profiler...(Nbody)案例,掌握 N 体问题的并行算法,实现此 算法,深化基于汇集操作的 MPI ...

  • 教 育部 - 英特尔 精 品课 程 ”建设 项目 申 请报 告

    基于多核Linux操作系统的PThread多线程编程、基于Intel Thread Checker和Profiler...(Nbody)案例,掌握 N 体问题的并行算法,实现此 算法,深化基于汇集操作的 MPI ...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email