gpt4 book ai didi

c - 问题在 C 中为 pthreads 的线程函数计时

转载 作者:行者123 更新时间:2023-12-04 05:25:51 25 4
gpt4 key购买 nike

所以我在计算每个线程的线程函数的运行时间时遇到问题,我需要能够找到所有线程的总运行时间,但它没有正确执行。 (见代码下面的输出)

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <time.h>

int numthread;
double x1;
double x2;
double h;
double totalintegral;
int n; //number of trapezoids
int localn;

double gnolock;
double gmute;
double gbusy;
double gsema;

double doTrapRule(double localx1, double localx2, double h, int localn);
double doFunction(double x);
void *threadCalc(void* threadid);

int main(int argc, char * argv[])
{
int i;
x1 = 0.0;
x2 = 20.0;
n = 200000;

numthread = 10;

pthread_t* threads = malloc(numthread*sizeof(pthread_t));

h = (x2 - x1)/n;
localn = n/numthread;

for(i = 0; i < numthread; i++)
{
pthread_create(&threads[i], NULL, (void *) &threadCalc, (void*) i);
}

for(i = 0; i < numthread; i++)
{
pthread_join(threads[i], NULL);
}

printf("Trap rule result with %d trap(s) is %f\n", n, totalintegral);
fflush(stdout);
printf("no lock completed in %f\n", gnolock);
exit(0);
}

void *threadCalc(void* threadid)
{
clock_t start = clock();
double localx1;
double localx2;
double localintegral;
int cur_thread = (int)threadid;

localx1 = x1 + cur_thread * localn * h;
localx2 = localx1 + localn * h;

localintegral = doTrapRule(localx1, localx2, h, localn);

totalintegral = totalintegral + localintegral;
//printf("Trap rule result with %d trap(s) is %f", n, totalintegral);
clock_t stop = clock();
double time_elapsed = (long double)(stop - start)/CLOCKS_PER_SEC;
printf("time elapsed of each thread %f\n",time_elapsed);
gnolock = gnolock + time_elapsed;
return NULL;
}


double doTrapRule(double localx1, double localx2, double h, int localn)
{
//time start here
double localtrapintegral;
double tempx1;
int i;

localtrapintegral = (doFunction(localx1) + doFunction(localx2)) / 2.0;

for(i = 1; i <= (localn - 1); i++)
{
tempx1 = localx1 + i * h;
localtrapintegral = localtrapintegral + doFunction(tempx1);
}

localtrapintegral = localtrapintegral * h;
//time end here, add elapsed to global
return localtrapintegral;
}

double doFunction(double x)
{
double result;
result = x*x*x;

return result;
}

输出:
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
time elapsed of each thread 0.010000
time elapsed of each thread 0.010000
time elapsed of each thread 0.000000
time elapsed of each thread 0.000000
Trap rule result with 200000 trap(s) is 40000.000001
no lock completed in 0.020000

正如您所看到的,无论出于何种原因,只有线程中的某个人实际上正在返回时间。我运行了多次,每次只有几个线程返回结果。正如 FYI gnolock 是我的变量,它存储经过的总时间。我猜为什么这不起作用是因为小数点超出范围,但它不应该吗?

最佳答案

如果您调用clock()在您的系统上,它的分辨率为 10 毫秒。因此,如果一个进程需要 2 毫秒,那么它通常会报告 0.00 秒或 0.01 秒的时间,这取决于你无法控制的一堆事情。

请改用其中一个高分辨率时钟。您可以使用 clock_gettimeCLOCK_THREAD_CPUTIME_IDCLOCK_PROCESS_CPUTIME_ID ,我相信这个时钟的分辨率比clock()好几个数量级.

man 2 clock_gettime了解更多信息。

关于c - 问题在 C 中为 pthreads 的线程函数计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com