问题
我使用常量内存准备了一个示例CUDA代码.我可以在cuda 4.2中成功运行它,但是当我使用CUDA 5编译时,我得到 "无效的设备符号".我在这里附加了示例代码.
代码
#include <iostream>
#include <stdio.h>
#include <cuda_runtime.h>
#include <cuda.h>
struct CParameter
{
int A;
float B;
float C;
float D;
};
__constant__ CParameter * CONSTANT_PARAMETER;
#define PARAMETER "CONSTANT_PARAMETER"
bool ERROR_CHECK(cudaError_t Status)
{
if(Status != cudaSuccess)
{
printf(cudaGetErrorString(Status));
return false;
}
return true;
}
// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N)
{
a[idx] = CONSTANT_PARAMETER->A * a[idx]; …Run Code Online (Sandbox Code Playgroud) 我注意到在精美打印元组的上下文中提到了"索引技巧".这听起来很有趣,所以我按照链接.
嗯,那不顺利.我理解了这个问题,但实际上并不能跟踪发生的事情.为什么我们甚至需要任何指数?那里定义的不同功能如何帮助我们?什么是'裸露'?等等
有人可以为参数包和可变元组的专家提供那种东西的游戏吗?
我的目标是写std::variant,可能没有完全吹,但至少与完全工作的构造函数/析构函数对和std::get<>()函数.
我试图使用char数组保留一个内存.它的大小由最大类型决定,通过使用find_biggest_size<>()函数找到.构造函数使用静态断言,因为它执行检查类型是否在指定类型的列表中.现在,构造函数和就地构造函数工作.
template <typename ... alternatives>
class variant
{
char object[find_biggest_size<alternatives...>::value];
public:
template <typename T>
variant(T&& other)
{
static_assert(is_present<T, alternatives...>::value, "type is not in range");
new ((T*)&object[0]) T(std::forward<T>(other));
}
template <typename T, typename ... ArgTypes>
variant(in_place_t<T>, ArgTypes&& ... args)
{
static_assert(is_present<T, alternatives...>::value, "type is not in range");
new ((T*)&object[0]) T(std::forward<ArgTypes>(args)...);
}
~variant()
{
// what to do here?
}
};
Run Code Online (Sandbox Code Playgroud)
然后我偶然发现了一个问题.我不知道当对象死亡时要执行什么析构函数.最重要的是,不可能访问底层对象,因为我无法专门std::get<>()获得正确的类型.
我的问题是:如何在创建对象后存储类型?这是正确的方法吗?如果没有,我应该使用什么?
编辑:
我试图应用评论.问题是当前存活的类型的索引不能constexpr,因此我无法从类型列表中提取所需的类型并调用适当的析构函数.
~variant()
{
using T = typename …Run Code Online (Sandbox Code Playgroud) 我正在尝试用介子配置一个项目。具体来说,我正在尝试设置一些选项。
meson config告诉我,除其他外:
Core options:
Option Current Value Possible Values Description
------ ------------- --------------- -----------
buildtype debug [plain, debug, debugoptimized, release, minsize, custom] Build type to use
Base options:
Option Current Value Possible Values Description
------ ------------- --------------- -----------
b_lto false [true, false] Use link time optimization
Run Code Online (Sandbox Code Playgroud)
(当然,其他选项已从此打印输出中删除。)
所以,我写:
meson build . --buildtype=release
Run Code Online (Sandbox Code Playgroud)
在我的构建目录中,一切顺利 - 没有警告或错误(我仔细检查了选项值是否已更改)。然后我写:
meson build . --b_lto=true
Run Code Online (Sandbox Code Playgroud)
但这让我感到:
meson: error: unrecognized arguments: --b_lto=true
Run Code Online (Sandbox Code Playgroud)
我也尝试过-b_lto=true、、、、。而所有这些都没有价值。没有运气。--b_lto trueb_lto=trueb_lto truetrue
那么我该如何设置这些“基本选项”呢?
command-line command-line-arguments buildconfiguration meson-build
C++23 添加了一些关于可选值的“monadic-style”功能,作为以下方法optional<T>:
optional<T>::and_then()(并忽略 的限定符this):
Run Code Online (Sandbox Code Playgroud)template<class F> constexpr auto and_then(F&& f);返回对包含的值(如果存在)调用 f 的结果。否则,返回返回类型的空值。
optional<T>::transform()(并忽略 的限定符this):
Run Code Online (Sandbox Code Playgroud)template<class F> constexpr auto transform(F&& f);如果包含一个值,则返回一个包含对所包含值
std::optional调用的结果的。否则,返回此类类型的空值。f*thisstd::optional
那么,这两个函数不是在做同样的事情吗?
我正在寻找C风格联盟的替代品.boost :: variant就是这样一个选择.std C++中有什么东西吗?
union {
int i;
double d;
}
Run Code Online (Sandbox Code Playgroud) 如下面的错误所示,在内核中不允许调用主机函数('rand'),我想知道是否有解决方案,如果我确实需要这样做.
error: calling a host function("rand") from a __device__/__global__ function("xS_v1_cuda") is not allowed
Run Code Online (Sandbox Code Playgroud) 根据"CUDA C编程指南",只有在多处理器常量高速缓存被命中时,常量内存访问才会受益(见第5.3.2.4节)1.否则,对于半翘曲,可能存在比合并的全局存储器读取更多的存储器请求.那么为什么常量内存大小限制为64 KB?
还有一个问题是为了不要问两次.据我所知,在Fermi架构中,纹理缓存与L2缓存相结合.纹理使用是否仍然有意义或全局内存读取是否以相同的方式缓存?
1 恒定存储器(第5.3.2.4节)
常量存储空间驻留在设备存储器中,并缓存在F.3.1和F.4.1节中提到的常量高速缓存中.
对于计算能力为1.x的设备,对于warp的常量内存请求首先被分成两个请求,每个半warp一个,独立发出.
然后,请求被分成多个单独的请求,因为初始请求中存在不同的内存地址,吞吐量减少的因子等于单独请求的数量.
然后,在高速缓存命中的情况下,或者在设备存储器的吞吐量下,以恒定高速缓存的吞吐量来服务所得到的请求.
我发现它binary_function已从C++ 11中删除.我想知道为什么.
C++ 98:
template <class T> struct less : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x<y;}
};
Run Code Online (Sandbox Code Playgroud)
C++ 11:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
Run Code Online (Sandbox Code Playgroud)
MODIFIED ------------------------------------------------- ---------------------------
template<class arg,class result>
struct unary_function
{
typedef arg argument_type;
typedef result result_type;
};
Run Code Online (Sandbox Code Playgroud)
例如,如果我们想在C++ 98中为函数编写适配器,
template <class T> struct even : …Run Code Online (Sandbox Code Playgroud) 许多开发人员和库作者已经在编译时字符串方面苦苦挣扎了好几年了,因为标准(库)字符串std::string需要动态分配内存,而不是constexpr。
因此,关于如何正确获取编译时字符串,我们有很多问题和博客文章:
现在我们已经了解到,不仅代码new可用constexpr,可以在编译时进行动态分配,而且实际上,std::string它将在C ++ 20(Herb Sutter的C ++标准工作组会议报告)中成为constexpr。
这是否意味着对于C ++ 20及以上版本的代码,我们应该放弃所有这些漂亮的编译时字符串实现,并且始终使用std::string?
如果不是,我们什么时候会这样做,什么时候我们会坚持今天的可能(当然不是向后兼容的代码)?
注意:我不是在谈论内容是其类型一部分的字符串,即不是在谈论std::integral_constant; 的等价形式。绝对不会std::string。