Linux下,cuda编写的计算pi的程序 的问题
在Linux下用写了个程序计算pi:
#include "stdio.h"
#define N 100000
__global__ void compute(float *res,int num){
int id=threadIdx.x;
if(id<num){
float count=0;
for(int s=id;s<N;s=s+num){
float temp=1.0+((s+0.5)/(float)N)*((s+0.5)/(float)N);
float temp2=4.0/(float)N/temp;
count=count+temp2;
}
res[id]=count;//保存每个线程计算的结果
}
}//这是计算pi的kernel函数
int main(){
const int threadnum=10;
float res[threadnum]; float host_res[threadnum];
cudaMalloc((void**)&res,sizeof(float)*threadnum);
compute<<<1,threadnum>>>(res,threadnum);
cudaMemcpy(host_res,res,sizeof(float)*threadnum,cudaMemcpyDeviceToHost);
float result=0;
for(int i=0;i<threadnum;i++)
result=result+host_res[i];
printf("the result is %f\n",result);
return 0;
}
1、这样总不对,每次跑出来的结果都乱七八糟的,不一定是啥。可是如果把红色的部分改成:float *res;就对了。想请教大家一下,这是为什么呀?
2、nvcc编译的时候报Warning:ptxas /tmp/tmpxft_00006f31_00000000-2_pi.ptx, line 73; warning : Double is not supported. Demoting to float可是程序里确实用的都是float,没用double,这个warning又是咋回事呀?
小弟初学,很多都不懂,先谢谢大家啦!