资源简介
并行计算三种方式计算π:概率法,积分法,级数法,代码中包括计算量和线程个数设置。
使用时进行编译后输入相应提示的数值即可,例如N=100000 t=8
使用时进行编译后输入相应提示的数值即可,例如N=100000 t=8
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
double pi = 0.0;
long long m = 0;
long long N; //投的点的个数
int t; //线程数量
pthread_mutex_t lock;
void *thread(void *ID){
long id = (long) ID;
long long i;
long long length = N/t;
long long mi = 0;
for(i = 0;i < length;i++){
float x = rand()/(RAND_MAX + 1.0);
float y = rand()/(RAND_MAX + 1.0);
if ((x*x + y*y) <= 1.0)
mi++;
}
pthread_mutex_lock(&lock);
m += mi;
pthread_mutex_unlock(&lock);
}
int main(){
int i; //用于遍历
pthread_mutex_init(&lockNULL);
pthread_t *threads;
threads=(pthread_t *)malloc(sizeof(pthread_t)*t);
//获取输入
printf(“Please enter the number of N: “);
scanf(“%ld“&N);
printf(“Please enter the number of threads (t): “);
scanf(“%d“&t);
printf(“\n“);
for(i=0;i pthread_create(&threads[i]NULLthread(void*)i); // 创建t个线程,并将线程标记为i
}
for(i=0;i {
pthread_join(threads[i]NULL);
}
pi = 4.0 * m/N;
printf(pi);
pthread_mutex_destroy(&lock);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1090 2020-03-22 22:20 积分法.c
文件 1348 2020-03-22 22:18 级数法.c
文件 1250 2020-03-22 22:18 概率法.c
相关资源
- 基于MPI并行计算的高斯消元法程序
- 并行计算大作业:矩阵乘法报告
- 天津大学计算机学院并行计算mpi矩阵
- 天津大学《并行计算》实验指南
- 如何在超算中心使用fluent做并行计算
- NUMECA并行计算教程
- MPI时间函数的测试
- 实现快速排序MPI并行计算
- 并行计算_mpi编程手册完整版.pdf
- 并行计算八皇后,N皇后
- CPU GPU协同并行计算
- pthread多线程计算pi
- 并行计算——结构·算法·编程 习题答
- 基于fortran95,openMP的并行计算 中文版
- 通过减少问题规模形式,做并行计算
- MPI并行计算教程与
- OpenMP程序
- 并行计算——结构·算法·编程习题答
- CUDA by example 中文:GPU高性能编程CUD
- 知识结构梳理_总复习_并行计算架构与
- 往年试题和重点_并行计算架构与模式
- An Introduction to Parallel Programming by Pet
- 并行粒子群优化算法的设计与实现
- 并行计算基础实验报告
- SOC 采用开路电压法和安时积分法相结
- 并行计算导论.pdf
- 并行计算体系结构(陈国良版)课后
- mpi实现块棋盘法矩阵转置
- 并行pthread求π算法
- 地震三维正演模拟并行计算程序
评论
共有 条评论