- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 C++ 和 CUDA 编写的 TensorFlow r1.5 操作的一部分涉及对张量的缩减。我已经实现了简单的交错缩减算法,如所述here .但是,似乎并没有减少整个缓冲区。
block 减少的实现如下
template<typename T>
__global__
void blockReduceDevice(const T *buffer, T *out, size_t len) {
const size_t tIdx = threadIdx.x;
const size_t bIdx = blockIdx.x;
const size_t bDim = blockDim.x;
const size_t idx = bIdx * bDim + tIdx;
//To allow templated, dynamic shared memory, we set the
//smem to be uchar and reinterpret as templated type.
extern __shared__ __align__(sizeof(T)) unsigned char buffReduce[];
__syncthreads();
//Set contribution of this thread. 0 if out of bounds.
T *reduce = reinterpret_cast<T*>(buffReduce);
reduce[tIdx] = (idx >= len) ? 0.0 : buffer[idx];
__syncthreads();
//Block reduce.
#pragma unroll
for (int i = bDim >> 1; i >= 1; i >>= 1) {
if(tIdx < i) {
reduce[tIdx] += reduce[tIdx + i];
}
__syncthreads();
}
if(tIdx == 0) {
out[bIdx] = reduce[tIdx];
}
}
上面的内核调用如下
template<typename T>
void testReduce(const T *buffer, T *blockVals, const GPUDevice &dev, size_t len) {
//Get CUDA stream.
const cudaStream_t &stream = dev.stream();
//Get launch configuration for reduction operation.
const auto reduceConfig = tensorflow::GetCudaLaunchConfig(len, dev);
const size_t blocks = reduceConfig.block_count;
const size_t threads = reduceConfig.thread_per_block;
const size_t shared = threads * sizeof(T);
//Reset buffer to known value.
std::vector<T> knownValsHost(len, 1.0);
cudaMemcpyAsync(buffer, &knownValsHost[0], len * sizeof(T), cudaMemcpyHostToDevice, stream);
CUSAFE(cudaStreamSynchronize(stream));
//Reset output to nought.
std::vector<T> tmp(blocks, 0.0);
cudaMemcpyAsync(blockVals, &tmp[0], blocks * sizeof(T), cudaMemcpyHostToDevice, stream);
CUSAFE(cudaStreamSynchronize(stream));
//Reduce on the GPU.
blockReduceDevice<T><<<blocks, threads, shared, stream>>>(buffer, blockVals, len);
CUSAFE(cudaPeekAtLastError());
CUSAFE(cudaStreamSynchronize(stream));
//Further reduce on the CPU.
std::vector<T> blockValsHost(blocks, 0.0);
cudaMemcpyAsync(&blockValsHost[0], blockVals, blocks * sizeof(T), cudaMemcpyDeviceToHost, stream);
CUSAFE(cudaStreamSynchronize(stream));
const T resGPU = std::accumulate(blockValsHost.begin(), blockValsHost.end(), static_cast<T>(0));
//Get result when copying buffer to CPU memory and reducing.
std::vector<T> bufferHost(len, 0.0);
cudaMemcpyAsync(&bufferHost[0], buffer, len * sizeof(T), cudaMemcpyDeviceToHost, stream);
CUSAFE(cudaStreamSynchronize(stream));
const T resCPU = std::accumulate(bufferHost.begin(), bufferHost.end(), static_cast<T>(0));
//Print some output for diagnostics.
std::cout << "Length: " << len << std::endl;
std::cout << "Num CUDA Blocks: " << blocks << std::endl;
std::cout << "Num CUDA Threads Per Block: " << threads << std::endl;
std::cout << "GPU Result: " << resGPU << std::endl;
std::cout << "CPU Result: " << resCPU << std::endl;
}
在上面的测试用例中,给出了以下输出,其中所有缓冲区条目都设置为 1.0
Length: 32768
Num CUDA Blocks: 10
Num CUDA Threads Per Block: 1024
GPU Result: 10240
CPU Result: 32768
可以看出,使用 std::accumulate
的 CPU 减少按预期工作(如 len == resCPU
)。这让我相信 CUDA 内核没有完全执行为 blocks * threads != len
。
TensorFlow 文档指出 here CUDA 内核启动配置应该使用 tensorflow/core/util/cuda_kernel_helper.h
header 获取,可以在 here 中找到.
出于什么原因,TensorFlow 会为我提供未执行适当线程数的启动配置?
我在手动设置启动配置参数时也收到了类似的结果。
最佳答案
For what reason would TensorFlow provide me with a launch configuration that does not execute the appropriate number of threads?
我猜是因为 Tensorflow 期望它运行的内核符合您的内核不符合的设计原则。 Tensorflow 返回的执行参数会将线程数限制为理论上可以在给定设备上运行的最大并发线程数。参见 here了解全部详情。
您的工作是编写一个符合该设计模式的内核,基本上是通过每个线程能够处理多个输入数据点。实际上,这意味着将您的内核修改成这样:
template<typename T>
__global__
void blockReduceDevice(const T *buffer, T *out, size_t len) {
const size_t tIdx = threadIdx.x;
const size_t bIdx = blockIdx.x;
const size_t bDim = blockDim.x;
const size_t idx = bIdx * bDim + tIdx;
const size_t stride = gridDim.x * blockDim.x
//To allow templated, dynamic shared memory, we set the
//smem to be uchar and reinterpret as templated type.
extern __shared__ __align__(sizeof(T)) unsigned char buffReduce[];
// cargo cult : __syncthreads();
//Set contribution of this thread. 0 if out of bounds.
T *reduce = reinterpret_cast<T*>(buffReduce);
T threadsum = T(0);
for(; idx < len; idx += stride)
threadsum += buffer[idx];
// store thread local partial reduction to shared memory
reduce[tIdx] = threadsum;
__syncthreads();
// etc
[警告:显然从未编译或运行,使用风险自负]
基本上,此设计将使每个线程尝试遍历尽可能多的输入数据点,以确保内存合并的方式处理所有输入数据。
关于c++ - Tensorflow CUDA Reduction Op 没有完全减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041821/
我对流动的两种情况下变量 acc 的数据共享范围感到困惑。在情况 1 中,我收到以下编译错误:error: reduction variable ‘acc’ is private in outer c
我正在研究这个 Verilog 文件: `default_nettype none module stroboscope(i_clk, o_led); input wire i_clk
我正在为即将到来的 Haskell 考试复习,但我不明白过去论文中的一个问题。谷歌出现 nothing useful fst(x, y) = x square i = i * i i) Source
LEt x_t = F(x_{t-1}) 是 chaotic regime. 中的一个时间离散动力系统 从初始条件x_0开始,我们可以生成一个时间序列=x_t,其中t =1,2,...,T 表示时间索
我正在尝试使用 OpenMP 并行化 vector 点积程序。下面的代码显示了我所做的。 #define N 1000000 float dotProduct = 0; float vector1Ho
我有一个需要以下内容的项目。 代码中将声明四个数组,如下所示: var ROW1 = ['module1']; var ROW2 = ['module2', 'module3']; var ROW3
我是 opencl 的新手。我试过“获取数组中每个元素的所有立方体的总和”。这是我的内核代码: kernel void cubeSum(global float *input,
在 C99 规范中它说 remquo: The remquo functions are intended for implementing argument reductions which can
我正在关注'Learn Haskell Fast and Hard'我能够理解其中的大部分内容,但我对以下代码示例有两个问题。 在第一个函数中,为什么我不需要 l 但在第二个版本中我确实需要 l? 在
我需要更新数据框中的一些数据,就像 SQL 中的更新查询一样。我当前的代码如下: import pandas df = pandas.read_csv('filee.csv') # load trad
我有函数的当前版本: void* function(const Input_st *Data, Output_st *Image) { int i,j,r,Of
目前正在尝试使用 CUDA pdf 中的 Reduction #3 outline here . 这是我的 Reduction 函数的样子 template __device__ void offs
我正在尝试使用官方 CUDA 缩减 PDF 中讨论的缩减内核之一 here .但是,我不明白它是如何工作的,除非我遗漏了一些似乎没有多大意义的东西。 这是我的内核: __global__ void e
Please click this to see my problem 嗨。 关于这个问题,我只是看不懂它提供的解决方案。 我们知道 Atm 的补码 = { : M是TM,M不接受W}和照片中描述的
我已经看到各种讨论和代码尝试来解决 "String reduction"来自 interviewstreet.com 的问题,但没有一个是通过动态规划来解决的。 在 Dynamic Programmi
我正在尝试对 zip 迭代器进行最小缩减,但使用自定义运算符仅考虑元组中的第二个字段(第一个字段是键,而第二个字段是值)实际上与减少有关) 但是,我无法让它工作,目前正在计算 vector 中存在的结
这个问题在这里已经有了答案: OpenMP in C array reduction / parallelize the code (1 个回答) 关闭去年。 我正在尝试使用 #pragma omp
我有一种用 PLT-Redex 定义的语言,它具有(动态)mixin 类型。表达式如下所示: ; terms / expressions (e ::= x (lkp e f) (c
我正在研究代码 war 中的方向减少问题,但我无法弄清楚它给我带来的错误。我知道也有类似的情况,但是当我在 Visual Studio Code 上测试我的代码时,它工作得完美无缺,所以我不确定为什么
我用 C++ 和 CUDA 编写的 TensorFlow r1.5 操作的一部分涉及对张量的缩减。我已经实现了简单的交错缩减算法,如所述here .但是,似乎并没有减少整个缓冲区。 block 减少的
我是一名优秀的程序员,十分优秀!