gpt4 book ai didi

c - 计算 pi 的线程程序

转载 作者:行者123 更新时间:2023-11-30 21:10:14 25 4
gpt4 key购买 nike

为类编写一个计算 pi 的程序。完成所有编码并运行程序 - 但对于某些值它不起作用。该程序应该接受 2 个参数,第一个是计算 pi 的元素数量,第二个是要使用的线程数量。它对于许多数字都可以完美地工作,但对于 1、2、5、6、7、8 和其他一些数字,它的计算效果不佳。这可能是循环中的限制,但我无法弄清楚。任何帮助表示赞赏。代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

//global variables
int N, T;
double *vsum;

//pie function
void* pie_runner(void* arg)
{
long j = (long)arg;
//double *limit_ptr = (double*) arg;
//double j = *limit_ptr;

//for(int i = (j-1)*N/T; i < N*(j) /T; i++)
for(int i = (N/T)*(j-1); i < ((N/T)*(j)); i++)
{

if(i % 2 == 0){
vsum[j] += 4.0/((2*j)*(2*j+1)*(2*j+2));
//printf("vsum %lu = %f\n", j, vsum[j]);
}
else{
vsum[j] -= 4.0/((2*j)*(2*j+1)*(2*j+2));
//printf("vsum %lu = %f\n", j, vsum[j]);
}


}

pthread_exit(0);
}

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

if(argc != 3) {
printf("Error: Must send it 2 parameters, you sent %d\n", argc-1);
exit(1);
}
N = atoi(argv[1]);
T = atoi(argv[2]);

vsum = malloc((T+1) * sizeof(*vsum));
if(vsum == NULL) {
fprintf(stderr, "Memory allocation problem\n");
exit(1);
}

if(N <= T) {
printf("Error: Number of terms must be greater then number of threads.\n");
exit(1);
}

for(int p=1; p<=T; p++) //initialize array to 0
{
vsum[p] = 0;
}

double pie = 3.0;
//launch threads
pthread_t tids[T+1];

for(long i = 1; i<=T; i++)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, pie_runner, (void*)i);
}

//wait for threads...
for(int k = 1; k<=T; k++)
{
pthread_join(tids[k], NULL);
}

for(int x=1; x<=T; x++)
{
pie += vsum[x];
}

printf("pi computed with %d terms in %d threads is %.20f\n", N, T, pie);

//printf("pi computed with %d terms in %d threads is %20f\n", N, T, pie);

free(vsum);
}

值不起作用:

./pie1 2 1pi computed with 2 terms in 1 threads is 3.00000000000000000000./pie1 3 1pi computed with 3 terms in 1 threads is 3.16666666666666651864 ./pie1 3 2pi computed with 3 terms in 2 threads is 3.13333333333333330373 ./pie1 4 2pi computed with 4 terms in 2 threads is 3.00000000000000000000 ./pie1 4 1pi computed with 4 terms in 1 threads is 3.00000000000000000000 ./pie1 4 3pi computed with 4 terms in 3 threads is 3.14523809523809516620 ./pie1 10 1pi computed with 10 terms in 1 threads is 3.00000000000000000000 ./pie1 10 2pi computed with 10 terms in 2 threads is 3.13333333333333330373 ./pie1 10 3pi computed with 10 terms in 3 threads is 3.14523809523809516620 ./pie1 10 4pi computed with 10 terms in 4 threads is 3.00000000000000000000./pie1 10 5pi computed with 10 terms in 5 threads is 3.00000000000000000000./pie1 10 6pi computed with 10 terms in 6 threads is 3.14088134088134074418 ./pie1 10 7pi computed with 10 terms in 7 threads is 3.14207181707181693042./pie1 10 8pi computed with 10 terms in 8 threads is 3.14125482360776464574 ./pie1 10 9pi computed with 10 terms in 9 threads is 3.14183961892940200045./pie1 11 2pi computed with 11 terms in 2 threads is 3.13333333333333330373 ./pie1 11 4pi computed with 11 terms in 4 threads is 3.00000000000000000000

最佳答案

我认为问题在于您在代码中使用了类似 N/T 的表达式。这里NT是整数。 C 语言(以及许多其他语言)处理整数除法的方式与我们手工处理整数除法的方式不同。

例如,5/3 = 1,而不是 1.66667。要获得 1.66667,您可以将其中一个整数转换为 double。

关于c - 计算 pi 的线程程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30835814/

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