2013-03-23 59 views
0

此代码是为赋值给出的。但是,蝙蝠在计算pi蒙特卡洛函数中存在错误。从int到unsigned int的转换错误统计无效。林不知道这是否是有意的,但我一直无法纠正这个问题。如果你想知道这个函数是线程示例的一部分。任何建议是非常感谢将int无效转换为无符号整型

void *compute_pi(void *s) 
{ 
int seed; 
int ii; 
int *hit_pointer; 
int local_hits; 
double rand_no_x; 
double rand_no_y; 

hit_pointer = (int *) s; 
seed = *hit_pointer; 
local_hits = 0; 

for(ii=0; ii < sample_points_per_thread; ii++) 
{ 
    rand_no_x = (double) (rand_r(&seed))/(double)RAND_MAX;//*error is these lines 
    rand_no_y = (double) (rand_r(&seed))/(double)RAND_MAX;//*error is these lines 
    if(((rand_no_x - 0.5) * (rand_no_x - 0.5) + 
    (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)//*error 
local_hits++; 
    seed *= ii; 
} 

*hit_pointer = local_hits; 
pthread_exit(0); 
} 

我做了一些调试,现在看看错误可能位于哪里。现在生病发布计算pi被调用的主要函数。我相信这是参数被分析为采样点和线程数量的地方。当我运行该程序后,它会要求输入,它会失败并出现segementation故障。有什么建议么?

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

    #define MAX_THREADS 512 

    void *compute_pi(void *); 


    int sample_points; 
    int total_hits; 
    int total_misses; 
    int hits[ MAX_THREADS ]; 
    int sample_points_per_thread; 
    int num_threads; 



    int main(int argc, char *argv[]) 
    { 
    /* local variables */ 
    int ii; 
    int retval; 
    pthread_t p_threads[MAX_THREADS]; 
    pthread_attr_t attr; 
    double computed_pi; 

    /* initialize local variables */ 
    retval = 0; 


    pthread_attr_init(&attr); 
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 

    printf("Enter number of sample points: "); 
    scanf("%d", &sample_points); 
    printf("Enter number of threads: "); 
    scanf("%d%", &num_threads); 

    /* parse command line arguments into sample points and number of threads */ 
    /* there is no error checking here!!!!! */ 
    sample_points = atoi(argv[1]); 
    num_threads = atoi(argv[2]); 


    total_hits = 0; 
    sample_points_per_thread = sample_points/num_threads; 

    for(ii=0; ii<num_threads; ii++) 
    { 
    hits[ii] = ii; 
    pthread_create(&p_threads[ ii ], &attr, compute_pi, (void *) &hits[ii]); 
    } 

    for(ii=0; ii<num_threads; ii++) 
    { 
    pthread_join(p_threads[ ii ], NULL); 
    total_hits += hits[ ii ]; 
    } 

    computed_pi = 4.0 * (double) total_hits/((double) (sample_points)); 


    printf("Computed PI = %lf\n", computed_pi); 


    /* return to calling environment */ 
    return(retval); 
    } 
+0

预期与否,原始的“修复”似乎源于声明'seed'匹配所提供的'void *'参数'hit_pointer'的强制类型。这个“比赛”结束。你是正确的,['rand_r()'](http://linux.die.net/man/3/rand_r)确实规定使用'unsigned int *'作为其单个参数。 – WhozCraig 2013-03-23 03:06:36

+1

仔细阅读错误消息,不要将其解释。错误是**不是无效**将'int'转换为'unsigned';没关系。该错误是无效的'int *'转换为'unsigned *',这是非常不同的。 – 2013-03-23 11:13:16

回答

1

rand_r(3) man page

int 
rand_r(unsigned *ctx); 

正如你所看到的,它需要一个unsigned指针 - 如果你传递一个int,你有适当的警告/错误的编译器中打开(和它似乎你),你会得到这个错误。更改seed的类型,即可设置。

+0

并改变'hit_pointer'的类型。哦,和'local_hits'。 – scones 2013-03-23 03:07:36

+0

是的,那些也是。 – 2013-03-23 03:09:13

+0

但它们应该是int数据类型吧?因为他们正在处理整数。 hit_pointer通过为采样点和种子输入的数字分配一个int。 – user2125805 2013-03-23 03:55:52

相关问题