4.3 Reduction代码(Heterogeneous Parallel Programming class lab)
首先添加上Heterogeneous Parallel Programming class 中 lab: Reduction的代码:
myReduction.c
// MP Reduction
// Given a list (lst) of length n
// Output its sum = lst[0] + lst[1] + ... + lst[n-1]; #include <wb.h> #define BLOCK_SIZE 512 //@@ You can change this #define wbCheck(stmt) do { \
cudaError_t err = stmt; \
if (err != cudaSuccess) { \
wbLog(ERROR, "Failed to run stmt ", #stmt); \
wbLog(ERROR, "Got CUDA error ... ", cudaGetErrorString(err)); \
return -; \
} \
} while() __global__ void reduction(float *g_idata, float *g_odata, unsigned int n){ __shared__ float sdata[BLOCK_SIZE]; // load shared mem
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*blockDim.x + threadIdx.x; sdata[tid] = (i < n) ? g_idata[i] : ; __syncthreads(); // do reduction in shared mem, stride is divided by 2,
for (unsigned int s=blockDim.x/; s>; s>>=)
{
//__syncthreads();
if (tid < s)
{
sdata[tid] += sdata[tid + s];
} __syncthreads();
} // write result for this block to global mem
if (tid == ) g_odata[blockIdx.x] = sdata[]; } __global__ void total(float * input, float * output, int len) {
//@@ Load a segment of the input vector into shared memory
__shared__ float partialSum[ * BLOCK_SIZE]; //blockDim.x is not okay, compile fail
unsigned int t = threadIdx.x;
unsigned int start = * blockIdx.x * blockDim.x;
if (start + t < len)
partialSum[t] = input[start + t];
else
partialSum[t] = ; if (start + blockDim.x + t < len)
partialSum[blockDim.x + t] = input[start + blockDim.x + t];
else
partialSum[blockDim.x + t] = ; //@@ Traverse the reduction tree
for (unsigned int stride = blockDim.x; stride >= ; stride >>= ) {
__syncthreads();
if (t < stride)
partialSum[t] += partialSum[t+stride];
}
//@@ Write the computed sum of the block to the output vector at the
//@@ correct index
if (t == )
output[blockIdx.x] = partialSum[];
} int main(int argc, char ** argv) {
int ii;
wbArg_t args;
float * hostInput; // The input 1D list
float * hostOutput; // The output list
float * deviceInput;
float * deviceOutput;
int numInputElements; // number of elements in the input list
int numOutputElements; // number of elements in the output list args = wbArg_read(argc, argv); wbTime_start(Generic, "Importing data and creating memory on host");
hostInput = (float *) wbImport(wbArg_getInputFile(args, ), &numInputElements); numOutputElements = numInputElements / (BLOCK_SIZE);
if (numInputElements % (BLOCK_SIZE)) {
numOutputElements++;
} //This for kernel total
/*numOutputElements = numInputElements / (BLOCK_SIZE <<1);
if (numInputElements % (BLOCK_SIZE)<<1) {
numOutputElements++;
} */
hostOutput = (float*) malloc(numOutputElements * sizeof(float)); wbTime_stop(Generic, "Importing data and creating memory on host"); wbLog(TRACE, "The number of input elements in the input is ", numInputElements);
wbLog(TRACE, "The number of output elements in the input is ", numOutputElements); wbTime_start(GPU, "Allocating GPU memory.");
//@@ Allocate GPU memory here
cudaMalloc((void **) &deviceInput, numInputElements * sizeof(float));
cudaMalloc((void **) &deviceOutput, numOutputElements * sizeof(float)); wbTime_stop(GPU, "Allocating GPU memory."); wbTime_start(GPU, "Copying input memory to the GPU.");
//@@ Copy memory to the GPU here
cudaMemcpy(deviceInput,
hostInput,
numInputElements * sizeof(float),
cudaMemcpyHostToDevice); wbTime_stop(GPU, "Copying input memory to the GPU.");
//@@ Initialize the grid and block dimensions here
dim3 dimGrid(numOutputElements, , );
dim3 dimBlock(BLOCK_SIZE, , ); wbTime_start(Compute, "Performing CUDA computation");
//@@ Launch the GPU Kernel here
reduction<<<dimGrid,dimBlock>>>(deviceInput, deviceOutput, numInputElements);
//total<<<dimGrid, dimBlock>>>(deviceInput, deviceOutput, numInputElements);
cudaDeviceSynchronize();
wbTime_stop(Compute, "Performing CUDA computation"); wbTime_start(Copy, "Copying output memory to the CPU");
//@@ Copy the GPU memory back to the CPU here
cudaMemcpy(hostOutput, deviceOutput, sizeof(float) * numOutputElements, cudaMemcpyDeviceToHost);
wbTime_stop(Copy, "Copying output memory to the CPU"); /********************************************************************
* Reduce output vector on the host
* NOTE: One could also perform the reduction of the output vector
* recursively and support any size input. For simplicity, we do not
* require that for this lab.
********************************************************************/
for (ii = ; ii < numOutputElements; ii++) {
hostOutput[] += hostOutput[ii];
} wbTime_start(GPU, "Freeing GPU Memory");
//@@ Free the GPU memory here
cudaFree(deviceInput);
cudaFree(deviceOutput); wbTime_stop(GPU, "Freeing GPU Memory"); wbSolution(args, hostOutput, ); free(hostInput);
free(hostOutput); return ;
}
4.3 Reduction代码(Heterogeneous Parallel Programming class lab)的更多相关文章
- PatentTips - Heterogeneous Parallel Primitives Programming Model
BACKGROUND 1. Field of the Invention The present invention relates generally to a programming model ...
- Notes of Principles of Parallel Programming - TODO
0.1 TopicNotes of Lin C., Snyder L.. Principles of Parallel Programming. Beijing: China Machine Pres ...
- Task Cancellation: Parallel Programming
http://beyondrelational.com/modules/2/blogs/79/posts/11524/task-cancellation-parallel-programming-ii ...
- Samples for Parallel Programming with the .NET Framework
The .NET Framework 4 includes significant advancements for developers writing parallel and concurren ...
- 2018-12-09 疑似bug_中文代码示例之Programming in Scala笔记第九十章
续前文: 中文代码示例之Programming in Scala笔记第七八章 源文档库: program-in-chinese/Programming_in_Scala_study_notes_zh ...
- 2018-11-27 中文代码示例之Programming in Scala笔记第七八章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...
- 2018-11-16 中文代码示例之Programming in Scala笔记第四五六章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章. 同样仅节选有意思的例程部分作演示之用. 源文档仍在: program-in-chinese/Programming_ ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
随机推荐
- unity 3d 获取鼠标当前坐标
获取当前鼠标position:Input.mousePosition;
- uva 825
这个......小学生就会的 坑在输入输出了 两个数之间可能不止一个空格....wa了好几遍啊 #include <cstdio> #include <cstring> # ...
- POJ1265Area
http://poj.org/problem?id=1265 题意 : 给你一个点阵,上边有很多点连成的多边形,让你求多边形内部的点和边界上的点以及多边形的面积,要注意他每次给出的点并不是点的横纵坐标 ...
- 在线学习SQL语句?没问题~~
以前弄得少,没注意.. http://sqlfiddle.com/ CREATE TABLE Presidents ( Id INT UNSIGNED NOT NULL AUTO_INCREMENT, ...
- hdu 4649 Professor Tian 反状态压缩+概率DP
思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...
- BZOJ 3747 POI2015 Kinoman
因为上午没有准备够题目,结果发现写完这道题没题可写了QAQ 又因为这道题范围是100w,我写了发线段树,以为要T,上午就花了一个小时拼命卡常数 结果下午一交居然过了QAQ 我们考虑枚举L,求最大R使得 ...
- [itint5]区间相交
http://www.itint5.com/oj/#14 要记录原来的索引,所以用了额外的空间,新生成一个结构.如果要省空间,可以用指针来排序,最后拿指针减去索引0的位置就是index,见:http: ...
- clone函数
http://blog.csdn.net/caianye/article/details/5947282 http://wenku.baidu.com/link?url=qnq7laYDYm1V8tl ...
- python numpy笔记:给matlab使用者
利用Numpy,python可以进行有效的科学计算.本文给过去常用matlab,现在正学习Numpy的人. 在进行矩阵运算等操作时,使用array还是matrix?? 简短的回答,更多的时候使用arr ...
- 【剑指offer】找出数组中出现一次的两个数
2013-09-08 10:50:46 一个整型数组中,除了两个数字之外,其他数字都出现了2次,找出这两个只出现一次的数字,要求时间复杂度是O(N),空间复杂度是O(1). 小结: 任何数与0异或,结 ...