以后再改吧= =
// MP 4 Reduction
// Given a list (lst) of length n
// Output its sum = lst[0] + lst[1] + ... + lst[n-1];
// Due Tuesday, January 15, 2013 at 11:59 p.m. PST
#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); \
return -1; \
} \
} while(0)
__global__ void total(float * input, float * output, int len) {
//@@ Load a segment of the input vector into shared memory
__shared__ float partialSum[2*BLOCK_SIZE];
unsigned int t=threadIdx.x;
unsigned int start=2*blockIdx.x*BLOCK_SIZE;
if((start+t)<len){
partialSum[t]=input[start+t];
}else partialSum[t]=0.0;
if((start+BLOCK_SIZE+t)<len){partialSum[BLOCK_SIZE+t]=input[start+BLOCK_SIZE+t];}
else partialSum[BLOCK_SIZE+t]=0.0;
//@@ Traverse the reduction tree
for(unsigned int stride=BLOCK_SIZE;stride>=1;stride>>=1){
__syncthreads();
if(t<stride)
partialSum[t]+=partialSum[t+stride];
}
//@@ Write the computed sum of the block to the output vector at the
//@@ correct index
output[blockIdx.x]=partialSum[0];
}
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, 0), &numInputElements);
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
wbCheck(cudaMalloc((void**)&deviceInput,numInputElements*sizeof(float)));
wbCheck(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 dimBlock(numOutputElements,1,1);
dim3 dimGrid(BLOCK_SIZE,1,1);
wbTime_start(Compute, "Performing CUDA computation");
//@@ Launch the GPU Kernel here
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,numOutputElements*sizeof(float),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 = 1; ii < numOutputElements; ii++) {
hostOutput[0] += 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, 1);
free(hostInput);
free(hostOutput);
return 0;
}
转载于:https://my.oschina.net/xinyi/blog/102344
分享到:
相关推荐
“Coursera 课程下载利器Windows版本”指的是专为Windows操作系统设计的一款软件,用于方便用户下载Coursera平台上的在线课程。这个工具简化了下载过程,使得用户能够更便捷地获取和保存Coursera上的学习资源。 **...
《Coursera机器学习课程答案》是一份宝贵的教育资源,它涵盖了Andrew Ng教授在Coursera平台上讲授的机器学习课程的全部作业解答。这门课程是机器学习领域的经典入门课程,由斯坦福大学的著名教授授课,深受全球学习...
Coursera机器学习课程作业源码
吴恩达Coursera深度学习课程的笔记。在Coursera上吴恩达老师的DeepLearning.ai课程项目中,第一部分《神经网络和深度学习》第四周课程“深层神经网络”部分关键点的笔记。笔记并不包含全部小视频课程的记录,如需...
Coursera machine learning-答案
Coursera是一个国际知名的在线教育平台,它与世界各地的著名大学合作,提供了大量免费和付费的课程资源。该平台以其高质量的教学内容和灵活性吸引了世界各地的学习者。随着平台的不断发展,Coursera增加了中文界面的...
Coursera机器学习的8个练习所有答案,自己写的
标题 "Coursera 吴恩达作业及答案" 指的是在Coursera平台上的著名课程"吴恩达的机器学习"(Machine Learning by Andrew Ng)或"深度学习专项课程"(Deep Learning Specialization)中的课后练习与解答资源。...
Coursera data science课程 第二次大作业
Coursera Industry Skills Report 2021
coursera机器学习编程答案
coursera-dl, 下载 Coursera.org 视频和命名它们的脚本 Coursera下载器 简介特性免责声明安装指令推荐所有操作系统的安装方法。安装缺少的依赖项的其他方法Unix系统的可选安装方法( )安装依赖你自己的产品。Windows...
【标题】中的“北京大学Coursera课程C++程序设计答案”指的是在Coursera这个在线教育平台上,北京大学开设的一门关于C++编程的课程。这门课程可能涵盖了C++的基础语法、面向对象编程、模板、异常处理等核心概念。...
Coursera 在线全部课程,包含讲解视频和资料 .全套资料
这里是coursera课程Hadoop Platform and Application Framework的所有项目源代码,都通过测试考核,所以应该准确无误。原课程讲解Hadoop和Spark,有兴趣的小伙伴们可以参考,不过建议自己编程,有助于能力的提高。原...
在本资源中,“Coursera机器学习课后习题答案全套”涵盖了Andrew NG教授在斯坦福大学开设的著名在线课程“机器学习”的全部习题解答。这个课程是全球范围内最受欢迎的机器学习入门课程之一,其内容广泛且深入,旨在...
本资源是coursera机器学习每周每课时(2--11周)的测验题目,包括每节课都有暂停之后的小练习,都是自己截图下来的,是纯英文版的,部分图片里面我个人用FastStone添加了个人理解解题思路,如有问题可以一起交流。...
这门课程由斯坦福大学的教授Andrew Ng教授主讲,并通过在线教育平台Coursera向全球开放。该课程的内容非常全面,涵盖了机器学习的各个方面,从基础理论到实际应用,无所不包。为了方便大家学习,我将会对这门课程的...
机器学习coursera课程答案1
《数据科学的统计推断:Coursera统计推断课程的同伴》这本书为实践中的数据科学家提供了一种简短而严谨的方法来理解统计推断的核心概念和技术。接下来,我们将深入探讨书中的关键知识点。 #### 二、统计推断的基本...