我根据黎曼求和的思想编写了一个程序来求积分值。它使用多个线程,但与我后来编写的顺序程序相比,它的性能(算法)较差。从算法角度来说,除了线程之外,它们是相同的,所以问题是它出了什么问题?我认为 pthread_join 情况并非如此,因为如果一个线程比另一个线程完成得早,那么该 join 就会等待,将来它会简单地跳过它。那是对的吗? free
调用可能是错误的,并且在创建线程时没有错误检查,我知道这一点,我在测试各种东西的过程中删除了它。抱歉英语不好,提前致谢。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <time.h>
int counter = 0;
float sum = 0;
pthread_mutex_t mutx;
float function_res(float);
struct range {
float left_border;
int steps;
float step_range;
};
void *calcRespectiveRange(void *ranges) {
struct range *rangs = ranges;
float left_border = rangs->left_border;
int steps = rangs->steps;
float step_range = rangs->step_range;
free(rangs);
//printf("left: %f steps: %d step range: %f\n", left_border, steps, step_range);
int i;
float temp_sum = 0;
for(i = 0; i < steps; i++) {
temp_sum += step_range * function_res(left_border);
left_border += step_range;
}
sum += temp_sum;
pthread_exit(NULL);
}
int main() {
clock_t begin, end;
if(pthread_mutex_init(&mutx, NULL) != 0) {
printf("mutex error\n");
}
printf("enter range, amount of steps and threads: \n");
float left_border, right_border;
int steps_count;
int threads_amnt;
scanf("%f %f %d %d", &left_border, &right_border, &steps_count, &threads_amnt);
float step_range = (right_border - left_border) / steps_count;
int i;
pthread_t tid[threads_amnt];
float chunk = (right_border - left_border) / threads_amnt;
int steps_per_thread = steps_count / threads_amnt;
begin = clock();
for(i = 0; i < threads_amnt; i++) {
struct range *ranges;
ranges = malloc(sizeof(ranges));
ranges->left_border = i * chunk + left_border;
ranges->steps = steps_per_thread;
ranges->step_range = step_range;
pthread_create(&tid[i], NULL, calcRespectiveRange, (void*) ranges);
}
for(i = 0; i < threads_amnt; i++) {
pthread_join(tid[i], NULL);
}
end = clock();
pthread_mutex_destroy(&mutx);
printf("\n%f\n", sum);
double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %lf\n", time_spent);
return(0);
}
float function_res(float lb) {
return(lb * lb + 4 * lb + 3);
}
编辑:简而言之 - 可以改进以减少执行时间(例如使用互斥体)吗?
最佳答案
如果您有多个可用的硬件线程,执行时间将会缩短。
问题在于如何测量时间:clock
返回程序使用的处理器时间。这意味着,它汇总了所有线程所花费的时间。如果您的程序使用 2 个线程,并且其线性执行时间为 1 秒,则意味着每个线程使用了 1 秒的 CPU 时间,clock
将返回相当于 2 秒的时间。
要获取实际使用时间(在 Linux 上),请使用 gettimeofday
。我通过添加修改了您的代码
#include <sys/time.h>
并捕获循环之前的开始时间:
struct timeval tv_start;
gettimeofday( &tv_start, NULL );
及之后:
struct timeval tv_end;
gettimeofday( &tv_end, NULL );
并计算以秒为单位的差异:
printf("CPU Time: %lf\nTime passed: %lf\n",
time_spent,
((tv_end.tv_sec * 1000*1000.0 + tv_end.tv_usec) -
(tv_start.tv_sec * 1000*1000.0 + tv_start.tv_usec)) / 1000/1000
);
(我还修复了 malloc(sizeof(ranges))
中的 malloc,它将指针的大小(32/64 位 CPU 为 4 或 8 字节)分配给 malloc(sizeof(struct range))
(12 字节))。
当使用输入参数0 1000000000 1000000000 1
运行时,即1个线程中10亿次迭代,我机器上的输出是:
CPU Time: 4.352000
Time passed: 4.400006
当使用 0 1000000000 1000000000 2
运行时,即 10 亿次迭代分布在 2 个线程上(每个线程 5 亿次迭代),输出为:
CPU Time: 4.976000
Time passed: 2.500003
为了完整起见,我使用输入0 1000000000 1000000000 4
进行了测试:
CPU Time: 8.236000
Time passed: 2.180114
它稍微快一点,但不是 2 个线程的两倍,而且它使用的 CPU 时间是双倍的。这是因为我的 CPU 是 Core i3,是带有超线程的双核,这不是真正的硬件线程。
关于C 线程程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33972498/