/*使用梯形规则和Pthreads计算一个定积分梯形规则
*
* 输入:a,b,n
* 输出:估计f(x)从a到b的积分
* 使用梯形规则和n个梯形
*
* 使用方法
* 编译:gcc -g -Wall -o <生成的可执行文件> <该文件名> -lpthread
* 使用方法: ./pth_trap <线程的数量> <方法>
*
* 算法。
* 1.每个线程计算 "它的 "间隔的整合
* 2.每个线程估计f(x)的积分
* 2.每个线程使用梯形规则估计f(x)在其区间内的积分
* 3.每个线程将其计算结果加入到全局总量
* 4.主函数main打印出结果
*
* 注意
* 1. f(x)是硬性规定的,可自行修改
* 2. 假设线程的数量均匀地划分为梯形。
* 3. 方法1(默认):使用互斥量mutex
* 方法2:使用信号量semaphore
* 方法3:使用忙等待
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
/* 全局变量在所有线程之间共享 */
int thread_count;
double a, b, h;
int n, local_n;
int method;
/* 关键部分的锁定变量 */
int flag;
sem_t sem;
pthread_mutex_t mutex;
double total;
long start_time, end_time;
void *Thread_work(void* rank);
double Trap(double local_a, double local_b, int local_n,
double h); /* 计算局部积分 */
double f(double x); /* 整合函数 */
/*--------------------------------------------------------------*/
int main(int argc, char** argv) {
long i;
pthread_t* thread_handles;
total = 0.0;
if (argc != 3) {
fprintf(stderr, "usage: %s <number of threads> <method>\n",
argv[0]);
exit(0);
}
thread_count = strtol(argv[1], NULL, 10);
method = strtol(argv[2], NULL, 10);
printf("Enter a, b, n\n");
scanf("%lf %lf %d", &a, &b, &n);
h = (b-a)/n;
local_n = n/thread_count;
/* 为线程句柄分配存储空间 */
thread_handles = (pthread_t*)malloc(thread_count*sizeof(pthread_t));
/* 初始化mutex、semaphore、busy-wait */
flag = 0;
pthread_mutex_init(&mutex, NULL);
sem_init(&sem, 0, 1);
start_time = clock(); /* 启动时间 */
/* 启动线程 */
for (i = 0; i < thread_count; i++) {
pthread_create(&thread_handles[i], NULL, Thread_work,
(void*)(long long) i);
}
/* 等待线程结束 */
for (i = 0; i < thread_count; i++) {
pthread_join(thread_handles[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&sem);
free(thread_handles);
end_time = clock();/* 结束时间 */
printf("With n = %d trapezoids, our estimate\n", n);
printf("of the integral from %lf to %lf = %lf\n", a, b, total);
printf("Time consuming: %lf", (double)(end_time - start_time) / CLOCKS_PER_SEC);
return 0;
} /* main */
/*--------------------------------------------------------------*/
void *Thread_work(void* rank) {
double local_a; /* 线程左端点 */
double local_b; /* 线程右端点 */
double my_int; /* 区间上的积分 */
long my_rank = (long long) rank;
/* 每个进程的整合区间的长度 = local_n*h. 间隔区间:*/
local_a = a + my_rank*local_n*h;
local_b = local_a + local_n*h;
my_int = Trap(local_a, local_b, local_n, h);
switch (method) {
case 2:
/* 信号量semaphore */
sem_wait(&sem);
total += my_int;
sem_post(&sem);
break;
case 3:
/* 忙等待 */
while(flag != my_rank);
total += my_int;
flag = (flag+1) % thread_count;
break;
default:
/* 互斥量mutex */
pthread_mutex_lock(&mutex);
total += my_int;
pthread_mutex_unlock(&mutex);
break;
}
return NULL;
} /* Thread_work */
/*--------------------------------------------------------------*/
double Trap(
double local_a /* in */,
double local_b /* in */,
int local_n /* in */,
double h /* in */) {
double integral; /* 储存积分结果 */
double x;
int i;
integral = (f(local_a) + f(local_b))/2.0;
x = local_a;
for (i = 1; i <= local_n-1; i++) {
x = local_a + i*h;
integral += f(x);
}
integral = integral*h;
return integral;
} /* Trap */
/*--------------------------------------------------------------*/
/* 设置f(x) */
double f(double x) {
double return_val;
return_val = x*x;
return return_val;
} /* f */
不知道为啥start_time跟end_time是相等的,运行时间算出来就是0