我在这里介绍一些代码
__constant__ int array[1024];
__global__ void kernel1(int *d_dst) {
int tId = threadIdx.x + blockIdx.x * blockDim.x;
d_dst[tId] = array[tId];
}
__global__ void kernel2(int *d_dst, int *d_src) {
int tId = threadIdx.x + blockIdx.x * blockDim.x;
d_dst[tId] = d_src[tId];
}
int main(int argc, char **argv) {
int *d_array;
int *d_src;
cudaMalloc((void**)&d_array, sizeof(int) * 1024);
cudaMalloc((void**)&d_src, sizeof(int) * 1024);
int *test = new int[1024];
memset(test, 0, sizeof(int) * 1024);
for (int i = 0; i < 1024; i++) {
test[i] = …
Run Code Online (Sandbox Code Playgroud) 从以下链接阅读问题及其答案后
我脑子里还有一个问题.从我在C/C++中的背景; 我知道使用volatile
它有它的缺点.并且在答案中指出,在CUDA的情况下,优化可以用寄存器替换共享数组,以volatile
在不使用关键字时保留数据.
我想知道在计算(总和)减少时可能遇到的性能问题.例如
__device__ void sum(volatile int *s_data, int tid)
{
if (tid < 16)
{
s_data[tid] += s_data[tid + 16];
s_data[tid] += s_data[tid + 8];
s_data[tid] += s_data[tid + 4];
s_data[tid] += s_data[tid + 2];
s_data[tid] += s_data[tid + 1];
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用减少经线.由于warp中的所有线程都是同步的,因此我认为不需要使用syncthreads()
构造.
我想知道将删除关键字volatile
弄乱我的总和(由于cuda优化)?没有volatile
关键字可以使用这样的减少.
由于我多次使用此缩减功能,volatile
关键字会导致性能下降吗?
I am trying to create a map using STL C++. But I am getting into problems and am not able to figure out whats exactly is wrong.
For demonstration purposes I put down my code here.
#include <map>
#include <iostream>
using namespace::std;
struct COORD{
int X;
int Y;
bool operator<(const COORD &other) const { return X == other.X && Y == other.Y;}
COORD(int x, int y) {
X = x;
Y = y;
}
};
struct CAR{
double speed;
double …
Run Code Online (Sandbox Code Playgroud)