对于在CUDA中使用原子操作,是否需要包含一些CUDA头文件?CUDA编程指南似乎对此非常紧张.
下面给出的代码glmax.cu给出了以下编译错误.
gaurish108 MyPractice: nvcc glmax.cu -o glmax
glmax.cu(11): error: identifier "atomicMax" is undefined
1 error detected in the compilation of "/tmp/tmpxft_000010fa_00000000-4_glmax.cpp1.ii".
Run Code Online (Sandbox Code Playgroud)
这是代码.它基本上是使用原子操作计算GPU上阵列的最大值atomicMax.由于我是CUDA的新手,因此我确信这是一个非常简单的代码,但我写这篇文章是为了帮助自己理解原子操作.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
__global__ void global_max(int* values, int* gl_max)
{
int i=threadIdx.x + blockDim.x * blockIdx.x;
int val=values[i];
atomicMax(gl_max,val);
}
int main(void)
{
int array_size=5;
int num_bytes=array_size*sizeof(int);
int *device_array=0;
int *host_array=0;
int *device_max=0;
int *host_max=0;
//Allocate memory on the host
host_array=(int*)malloc(num_bytes);
//Allocate memory on the device
cudaMalloc((void**)&device_array,num_bytes);
cudaMalloc((void**)&device_max,sizeof(int));
//If either memory allocation failed, report an …Run Code Online (Sandbox Code Playgroud) Java是否具有C++的默认复制构造函数?如果它有一个 - 如果我明确地声明另一个构造函数(不是复制构造函数),它是否仍然可用?
c++ java language-comparisons copy-constructor object-construction
我需要在Fedora 24机器上构建Boost 1.62和1.63,但是使用GCC 4.9.3或GCC 5.4.0(取决于版本CUDA,这就是我需要旧编译器的原因).但是,如果我按照本答案中的描述设置自定义GCC版本并运行
/b2 --toolset=gcc-5.4.0 stage
Run Code Online (Sandbox Code Playgroud)
令我懊恼的是,我现在看到:
- 32-bit : no
- 64-bit : yes
- arm : no
- mips1 : no
- power : no
- sparc : no
- x86 : yes
- symlinks supported : yes
- C++11 mutex : no
- lockfree boost::atomic_flag : yes
- Boost.Config Feature Check: cxx11_auto_declarations : no
- Boost.Config Feature Check: cxx11_constexpr : no
- Boost.Config Feature Check: cxx11_defaulted_functions : no
- Boost.Config Feature Check: cxx11_final …Run Code Online (Sandbox Code Playgroud) 对于作为ptr指针的迭代器,std::fill_n(ptr, n, 0)应该做同样的事情memset(ptr, 0, n * sizeof(*ptr))(但请参阅@ KeithThompson对此答案的评论).
对于C++ 11/C++ 14/C++ 17模式下的C++编译器,我可以期望将这些条件编译为相同的代码吗?当/如果它们没有编译成相同的代码,是否与-O0有显着的性能差异?-O3?
注意:当然,一些/大多数答案可能是特定于编译器的.我只对一两个特定的编译器感兴趣,但请写下你知道答案的编译器.
假设foo_t具有命名构造函数的类型,make_foo().现在,我想要真正拥有123个foo - 不多也不少.所以,我在考虑一个std::array<foo_t, 123>.现在,如果foo_t是默认构造的,我会写:
std::array<foo_t, 123> pity_the_foos;
std::generate(
std::begin(pity_the_foos), std::end(pity_the_foos),
[]() { return make_foo(); }
);
Run Code Online (Sandbox Code Playgroud)
而鲍勃是我的叔叔,对吗?不幸的是...... foo_t没有默认的ctor.
那么我应该如何初始化我的数组呢?我是否需要使用一些可变模板扩展伏都教?
注意:如果有帮助,答案可能会使用C++ 11,C++ 14或C++ 17中的任何内容.
请考虑以下代码:
#include <string.h>
void bar(char c);
void foo(const char* __restrict__ ss)
{
for (int i = 0; i < strlen(ss); ++i)
{
bar(*ss);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望strlen(ss)在这些基本理想的条件下将其提升出来; 然而 - 它不是,既不是由clang 5.0也不是由gcc 7.3和最大优化(-O3).
为什么会这样?
注意:灵感来自(我的回答)这个问题.
考虑以下两个带有CRTP模式的代码:
template <typename Derived>
struct Base1 {
int baz(typename Derived::value_type) {
return 42;
}
};
struct Foo1 : Base1<Foo1> {
using value_type = int;
};
Run Code Online (Sandbox Code Playgroud)
template <typename Derived>
struct Base2 {
auto baz() {
return typename Derived::value_type {};
}
};
struct Foo2 : Base2<Foo2> {
using value_type = int;
};
Run Code Online (Sandbox Code Playgroud)
第一个编译失败,而第二个编译。我的直觉说,它们应该要么编译要么都不编译。现在,如果我们将autoin 替换Base2为显式类型:
template <typename Derived>
struct Base3 {
typename Derived::value_type baz() {
return typename Derived::value_type {};
}
};
struct Foo3 …Run Code Online (Sandbox Code Playgroud) AVX-512 指令集扩展之一是AVX-512 + GFNI,“伽罗华域新指令”。
伽罗瓦理论是关于域扩展的。这与处理矢量化整数或浮点值有什么关系?指令应该执行“Galois 域仿射变换”,它的逆,以及“Galois 域乘以字节”。
那些是什么领域?这些说明实际上做了什么,它有什么用?
在这个问题中:
我们有一些关于如何让典型的 C++ 编译器在编译时打印类型名称的建议。但是,它们依赖于触发编译错误。
我的问题:我可以让 C++ 编译器在不停止编译的情况下打印类型的名称吗?
一般来说,答案是“可能不是”,因为一个有效的程序可以编译成它的目标对象,而无需在任何地方打印任何内容,所以我特别询问 GCC 和 clang,可能使用预处理器指令、编译器内置程序或任何编译器- 特定的技巧。
笔记:
using/typedef语句、模板参数值、可变参数模板等后面打印类型。如果类型明确可用,您可以使用类似的东西#message "my type is unsigned long long"(如@NutCracker 建议的那样)。但这不是问题的内容。c++ ×6
c++11 ×3
c ×2
gcc ×2
auto ×1
avx512 ×1
b2 ×1
boost ×1
clang ×1
clang++ ×1
coding-style ×1
compile-time ×1
crtp ×1
cuda ×1
debug-print ×1
g++ ×1
galois-field ×1
glib ×1
gpu-atomics ×1
hoisting ×1
java ×1
memory ×1
memset ×1
stdarray ×1