我有两个文件。
- 树搜索.cpp
- TreeSearchCUDA.cu
在 TreeSearch.cpp 中我有:
int* searchTree(vector<TreeNode> &tree, vector<ImageFeature> featureList)
{
float** features = makeMatrix(featureList, CHILDREN);
float* featuresArray = makeArray(features, featureList.size());
float* centroidNodes = convertTree(tree);
int numFeatures = featureList.size();
for(int j = 0; j < 10; j++)
{
cout << "C++ " << centroidNodes[j] << endl;
}
cout << "" << endl;
int* votes = startSearch(centroidNodes, tree.size(), featuresArray, numFeatures);
return votes;
}
startSearch 存在于 TreeSearchCUDA.cu 中,如下所示:
int* startSearch(float* centroids, int nodesCount, float* features, int featuresCount)
{
for(int j = 0; j < 10; j++)
{
printf("CUDA %f \n", centroids[j]);
}
...
}
现在,如果我们查看输出,它看起来像这样:
C++ 0
C++ 2.52435e-29
C++ 0
C++ 2.52435e-29
C++ 6.72623e-44
C++ 1.26117e-44
C++ 2.03982e+12
C++ 4.58477e-41
C++ 0
C++ 1.26117e-44
CUDA 0.000000
CUDA 0.000000
CUDA 0.000000
CUDA 0.000000
CUDA 0.000000
CUDA 0.000000
CUDA 2039820058624.000000
CUDA 0.000000
CUDA 0.000000
CUDA 0.000000
结果不一样。有人有什么想法吗? :) 我有一个想法,这是因为代码的某些部分是用 -m64 编译的,而某些部分不是。然而,这是不可能改变的。链接对象时,我使用 -m64。
我希望有人有解决方案或解释:)
最佳答案
从您的输出看来,CUDA 正在将非常小的 float 近似为 0
。除了 2.03982e+12
之外,您的所有输入都是非常小的 float 或 0
。 2.03982e+12
在输出中保持不变。您的质心应该很小吗?
关于c++ - 浮点值变化。不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499965/