我有一些例子给我一些奇怪的头痛:我产生一个线程分歧,但我无法弄清楚哪个分支或哪个语句先计算?
第一个例子:
我有以下内核,我从1个块中的2个线程开始.用[0] = 0和1 = 0.
__global__ void branchTest_kernel( float* a){
int tx = threadIdx.x;
if(tx==0){ // or tx==1
a[1] = a[0] + 1; (a)
}else if(tx==1){ // or tx==0
a[0] = a[1] + 1;; (b)
}
}
Run Code Online (Sandbox Code Playgroud)
产量
a[0] = 1
a[1] = 1
Run Code Online (Sandbox Code Playgroud)
我认为因为两个线程在一个warp中,它们以锁步方式执行,而(a)和(b)同时读取[0]和1.
第二个例子:
与第一个完全相同但是,现在删除了else if部分:
__global__ void branchTest_kernel( float* a){
int tx = threadIdx.x;
if(tx==0){
a[1] = a[0] + 1; (a)
}else{
a[0] = a[1] + 1; (b)
}
}
Run Code Online (Sandbox Code Playgroud)
产量
a[0] …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段.有谁知道为什么在主函数中没有为所有情况调用此移动构造函数?为什么要编译呢?赋值运算符是私有的吗?链接:http://ideone.com/bZPnyY
#include <iostream>
#include <vector>
class A{
public:
A(int i){
std::cout << "Constructor "<< i <<std::endl;
for(int l = 0; l<i;l++){
vec.push_back(l);
}
};
A(A && ref): vec(std::move(ref.vec))
{
std::cout << "Move constructor"<<std::endl;
}
A & operator=(A && ref){
if(this != &ref){
vec = std::move(ref.vec);
}
std::cout << "Move assignment"<<std::endl;
return *this;
}
std::vector<int> vec;
private:
A(const A & ref);
A(A & ref);
A & operator=(A & ref);
};
A makeA(){
A a(3);
return a;
}
int …Run Code Online (Sandbox Code Playgroud) 我想测试一个简单的事情,如下所示:
#include <iostream>
#include <boost/variant.hpp>
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1,T2>& dt){
os << dt.first << dt.second;
return os;
}
int main(){
boost::variant<int, std::pair<int,int>, bool> v;
v = std::pair<int,int>(3,3);
std::cout << v << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这实际上应该有效,因为对于普通类型,int, double等等,它会编译.
boost::variant有一个打印机vistor,它在内部使用它来输出内容到流.实际上这无法编译,但我真的不知道这个问题:
代码在这里失败:在variant_io.hpp中
template <typename OStream>
class printer
: public boost::static_visitor<>
{
private: // representation
OStream& out_;
public: // structors
explicit printer(OStream& out)
: out_( out )
{
}
public: // visitor interface
template <typename T>
void operator()(const T& …Run Code Online (Sandbox Code Playgroud) 我有一个uint64_t 整数,并uint32_t通过以下代码将最后 32 位提取为一个数字:
uint32_t getLast(uint64_t v){
uint64_t t= v >> 32;
return reinterpret_cast<uint32_t &>( t ); // type-punned pointer warning
}
int main()
{
uint64_t a = 1l << 33 | 1l << 3;
std::cout << getLast(a) << std::endl; // prints 2
}
Run Code Online (Sandbox Code Playgroud)
此代码给出警告:
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return reinterpret_cast<uint32_t &>( t );
Run Code Online (Sandbox Code Playgroud)
我想解决这个问题,但不是通过使用联合类型。
这 reinterprete_cast给未定义的行为,访问数据(复制重新诠释的东西进入返回值)时,(我想?)
另一个似乎有效的解决方案?
uint32_t getLast(uint64_t v){
return (v >> 32) // conversion from bitshifted 64bit to …Run Code Online (Sandbox Code Playgroud) 我问自己这个问题为什么
#include <random>
struct A{
std::random_device rd; // or any other generator std::mersenne_twister...
void doStuff() const {
auto v = rd();
}
};
const A a;
a.doStuff();
Run Code Online (Sandbox Code Playgroud)
不行,因为random_device::operator()不是const.
我想也许随机设备仍然可以在常量时使用,但它不能再次播种,但事实并非如此(内部状态显然不可变std::random_device)...
我想让A类正确定义(doStuff()是一个const方法),现在我突然需要使用mutable std::random_device rd;的不是那么难看吗?
这个设计有什么理由吗?
怎么可能在这里调用模板类
FrontBackBuffer,模板参数为TBackBufferType,TFrontBufferType
template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
class FrontBackBuffer{
public:
explicit FrontBackBuffer(
TBufferTypeFront const & m_front,
TBufferTypeBack const & m_back):
m_Front(m_front),
m_Back(m_back)
{
};
~FrontBackBuffer()
{};
typename std::remove_reference<
typename std::remove_pointer<TBufferTypeFront>::type
>::type & getFront(){return m_Front;} // error: invalid initialization of reference of type 'A&' from expression of type 'A*'| (here T is A)
typename std::remove_reference<
typename std::remove_pointer<TBufferTypeBack>::type
>::type & getBack(){return m_Back;}
TBufferTypeFront m_Front; ///< The front buffer
TBufferTypeBack m_Back; ///< The back buffer
};
Run Code Online (Sandbox Code Playgroud)
我想实现以下目标:
为了在代码中保持一致,我会优先考虑,无论缓冲区内的Type是什么,都要有一个函数getFront/Back …
我有以下简单的strinToTypeImpl函数将任何类型的字符串转换为模板类型.我担心的问题是编译器告诉我部分特化typename MyMatrix<T>::Vector3 :
模板参数T未在部分特化中使用
我不能在专业化中使用依赖名称吗?
namespace details
{
template<typename T>
struct stringToTypeImpl{
bool operator()(T& t, const std::string& s)
{
std::istringstream iss(s);
return !(iss >> t).fail();
}
};
template<typename T>
struct stringToTypeImpl< typename MyMatrix<T>::Vector3 >{
// Replacing typename MyMatrix<T>::Vector3 by
// Eigen::Matrix<T,3,1> WORKS but why?
bool operator()(typename MyMatrix<PREC>::Vector3 & t, const std::string& s)
{
stringToVector3<PREC>(t,s);
}
};
}
Run Code Online (Sandbox Code Playgroud) 一个合理的合适的编译器可以丢弃这个const静态变量
class A{
const static int a = 3;
}
Run Code Online (Sandbox Code Playgroud)
如果在编译的二进制文件中没有使用它或者它是否在二进制文件中显示?
我有一个类TensorMap(eigen3库),它采用(简化在这里)数字列表:
class TensorMap{
public:
template<typename... T>
TensorMap(T&...i){}
} ;
Run Code Online (Sandbox Code Playgroud)
和使用此类型的结构A.
struct A{
template<unsigned int N>
A( NumberList<N> & idx ): m( /* idx(0),idx(1), ...., idx(N-1) */ ) ) {}
TensorMap m;
};
Run Code Online (Sandbox Code Playgroud)
如何将数字列表注入NumberList<N> & idx到可变参数构造函数中.号码访问idx是由operator()(int i).这有可能通过一些漂亮的模板递归吗?到目前为止,我无法想出一种方法来注入这个,嗯......
当然,人们可以使用一些std::unique_ptr在A做出一个模板递归包装来产生new TensorMap,但是那不是我想要的.
我在用
git fetch origin feature/blabla
Run Code Online (Sandbox Code Playgroud)
真正具体地说明我想要获取什么。
feature/blabla。git fetchonly(假设分支已签出)也获取标签。是否可以使用我的版本并同时获取标签?(我不想用 获取所有标签git fetch --tags)。尤其是在另一个分支上时,这非常有用。