假设我有:
class A {
public:
A(int x_) : x(x_) {}
int x;
};
class B: public A { };
class C: public A { };
Run Code Online (Sandbox Code Playgroud)
使用此代码,B和C将不具有任何构造函数(复制构造函数除外).我想改变的东西类A(不是在B或C),这样既B和C将继承的构造A.这有可能吗?
我写了以下惊天动地的应用程序:
class SomeA { }; class SomeB { }; class SomeC { };
template <typename A, typename B, typename... Cs>
class Foo {
public:
template <typename U> static void bar();
};
template <typename U>
void Foo<SomeA, SomeB, SomeC>::bar() { };
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
当我编译这个(gcc 4.9.3 with -std=c++11)时,出现以下错误:
a.cpp:10:36: error: ambiguating new declaration of ‘static void Foo<SomeA, SomeB, SomeC>::bar()’
void Foo<SomeA, SomeB, SomeC>::bar() { };
^
a.cpp:6:36: note: old declaration ‘static void Foo<A, B, Cs>::bar() [with U …Run Code Online (Sandbox Code Playgroud) 我希望能够写出类似的东西
template <typename T> void foo() {
// ...
if (is_nice<T>::value) {
bar_which_is_defined_only_for_nice_types<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译它(g ++ 4.9.3,没有优化)时,我得到了一个投诉bar_which_is_defined_only_for_nice_types.如何在不诉诸2定义的情况下达到预期效果foo()?
它的执行成本有多高
const std::string s(my_const_char_ptr, my_length);
Run Code Online (Sandbox Code Playgroud)
?是否涉及复制?如果没有,我可以从典型的标准库实现中获得多少指令?在性能关键代码中几乎没有这个?
...或者我必须获得GSL实施和使用string_view吗?
假设我想在我编写的某些源代码上使用 CUDA 的较低级别驱动程序 API。我知道cuLaunchKernel,但我似乎无法在文档中找到如何cuFunction从函数传递给它的确切解释__global__。
在 CUDA PTX 中,有一个特殊寄存器,用于保存线程的扭曲索引:%warpid。现在,规范说:
请注意,它
%warpid是易失性的,并返回读取时线程的位置,但其值可能在执行期间发生变化,例如,由于抢占后线程的重新调度。
嗯,那是什么位置?它不应该是块内的位置,例如一维网格吗%tid.x / warpSize?它是 SM 中的一些扭曲槽(例如扭曲调度程序或一些内部队列)吗?我很困惑。
动机:我想%tid.x / warpSize通过使用这个特殊寄存器来省去计算的麻烦并释放寄存器。然而,回想起来,这是一个错误的动机,因为读取特殊寄存器的成本很高;请参阅:计算一维网格中的扭曲 id / 通道 id 的最有效方法是什么?
我正在尝试将uint8_t数组强制转换为uint32_t数组。但是,当我尝试执行此操作时,我似乎无法访问每个连续的4个字节。
让我们说我有一个8字节的uint8_t数组。我想作为一个uint32_t访问字节2-> 6。
这些都得到相同的值*((uint32_t*)&uint8Array[0]),*((uint32_t*)&uint8Array[1]),*((uint32_t*)&uint8Array[2]),*((uint32_t*)&uint8Array[3])
虽然*((uint32_t*)&uint8Array[4])按预期方式获得字节4-> 8。
看来我无法从任何地址访问4个连续字节?
有什么办法可以做到这一点?
Boost <boost/any.hpp>有:
template<typename ValueType>
ValueType any_cast(any & operand);
template<typename ValueType>
inline ValueType any_cast(const any & operand);
Run Code Online (Sandbox Code Playgroud)
(以及其他变体.)这种组合不应该导致诸如boost::any_cast<int>(my_any);?之类的调用模糊不清吗?
我问,因为如果我写这个程序:
#include <boost/any.hpp>
#include <iostream>
template<typename ValueType>
ValueType any_cast(boost::any & operand)
{
return boost::any_cast<ValueType>(operand);
}
int main()
{
int x = 123;
boost::any my_any(x);
std::cout << "my_any = " << any_cast<int>(my_any) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我确实抱怨模糊不清:
g++ -std=c++14 -O3 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In function 'int main()':
main.cpp:14:57: error: call of overloaded 'any_cast(boost::any&)' is …Run Code Online (Sandbox Code Playgroud) 因此,我想在GPU上划分一些32位无符号整数,并且我不关心获得确切的结果。实际上,让我们宽容些,并假设我愿意接受高达2的乘法误差因子,即,如果q = x / y,我愿意接受0.5 * q和2 * q之间的任何值。
我还没有测量任何东西,但是在我看来,这样的东西(CUDA代码)应该有用:
__device__ unsigned cheap_approximate_division(unsigned dividend, unsigned divisor)
{
return 1u << (__clz(dividend) - __clz(divisor));
}
Run Code Online (Sandbox Code Playgroud)
它使用固有的“查找第一个(位)集”整数作为便宜的基2对数函数。
注意:我可以使这个非32位专用的,但是然后我必须使代码与模板复杂化,并包装__clz()使用要使用的模板化函数__clzl(),__clzll()等等。
问题:
在CUDA文档中,我发现cudaDeviceGetAttribute是一个__host__ __device__函数.所以我想我可以在我的__global__函数中调用它来获取我的设备的一些属性.可悲的是,它似乎意味着不同的东西,因为如果我把它放入一个__device__函数并从我的全局调用它,我会得到一个编译错误事件.
是否可以在我的GPU上调用cudaDeviceGetAttribute?或者其他什么__host__ __device__意思?
这是我的源代码:
__device__ void GetAttributes(int* unique)
{
cudaDeviceAttr attr = cudaDevAttrMaxThreadsPerBlock;
cudaDeviceGetAttribute(unique, attr, 0);
}
__global__ void ClockTest(int* a, int* b, long* return_time, int* unique)
{
clock_t start = clock();
//some complex calculations
*a = *a + *b;
*b = *a + *a;
GetAttributes(unique);
*a = *a + *b - *a;
clock_t end = clock();
*return_time = end - start;
}
int main()
{
int a = 2;
int …Run Code Online (Sandbox Code Playgroud) c++ ×5
cuda ×5
templates ×3
c++11 ×2
alignment ×1
ambiguity ×1
approximate ×1
boost-any ×1
c++14 ×1
construct ×1
gpgpu ×1
inheritance ×1
nvcc ×1
overloading ×1
performance ×1
ptx ×1
string ×1