测试其他东西我偶然发现了一些我还没有想到的东西.
我们来看看这个片段:
#include <iostream>
#include <chrono>
int main () {
int i = 0;
using namespace std::chrono_literals;
auto const end = std::chrono::system_clock::now() + 5s;
while (std::chrono::system_clock::now() < end) {
++i;
}
std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)
我注意到计数在很大程度上取决于我执行它的机器.
我用gcc 7.3,8.2和clang 6.0编译了std=c++17 -O3.
在i7-4790(4.17.14-arch1-1-ARCH内核):〜3e8
但在Xeon E5-2630 v4(3.10.0-514.el7.x86_64)上:〜8e6
现在这是我想要理解的差异,所以我已经检查过了 perf stat -d
在i7上:
4999.419546 task-clock:u (msec) # 0.999 CPUs utilized
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
120 page-faults:u # 0.024 K/sec
19,605,598,394 cycles:u # 3.922 GHz …Run Code Online (Sandbox Code Playgroud) 我观察到一些我无法解释的奇怪行为.代码如下所示:
#include <memory>
#include <vector>
#include <algorithm>
int main(){
std::vector<double> t1(10, 5.0);
std::vector<double*> t2(10);
std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
//std::transform(t1.begin(), t1.end(), t2.begin(), [](double& a){return &a;});
}
Run Code Online (Sandbox Code Playgroud)
这里有一个版本来玩https://godbolt.org/g/YcNdbf 问题是这个代码使用gcc4.9-6.3编译好但在gcc 7.1下失败.Clang也不喜欢它.
(编辑)来自gcc 7.1的错误消息:
<source>: In function 'int main()':
8 : <source>:8:76: error: no matching function for call to 'transform(std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double*>::iterator, <unresolved overloaded function type>)'
std::transform(t1.begin(), t1.end(), t2.begin(), std::addressof<double>);
^
In file included from /opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/algorithm:62:0,
from <source>:3:
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/stl_algo.h:4281:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
transform(_InputIterator …Run Code Online (Sandbox Code Playgroud) 我对CUDA/Thrust很新,并且在代码片段方面存在问题.为了使它更容易,我把它修剪到最低限度.代码如下:
struct functor{
functor(float (*g)(const float&)) : _g{g} {}
__host__ __device__ float operator()(const float& x) const {
return _g(x);
}
private:
float (*_g)(const float&);
};
__host__ __device__ float g(const float& x){return 3*x;}
int main(void){
thrust::device_vector<float> X(4,1);
thrust::transform(X.begin(), X.end(), X.begin(), functor(&g));
}
Run Code Online (Sandbox Code Playgroud)
我的想法是我可以将任何函数传递给仿函数,因此我可以将该函数应用于Vector中的每个元素.不幸的是,我不确定为什么我会得到描述的错误.我编译-w -O3 -shared -arch=sm_20 -std=c++11 -DTHRUST_DEBUG
我很感谢你能给我的任何帮助:)