多GPU设备处理点积示例
多GPU设备处理点积示例,项目打包下载
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/ #include "../common/book.h"
#include "cuda.h"
#include "device_launch_parameters.h"
#include "device_functions.h"
#include "cuda_runtime.h" #define imin(a,b) (a<b?a:b) #define N (33*1024*1024)
const int threadsPerBlock = ;
const int blocksPerGrid =
imin(, (N / + threadsPerBlock - ) / threadsPerBlock); __global__ void dot(int size, float *a, float *b, float *c) {
__shared__ float cache[threadsPerBlock];
int tid = threadIdx.x + blockIdx.x * blockDim.x;
int cacheIndex = threadIdx.x; float temp = ;
while (tid < size) {
temp += a[tid] * b[tid];
tid += blockDim.x * gridDim.x;
} // set the cache values
cache[cacheIndex] = temp; // synchronize threads in this block
__syncthreads(); //块内归约
int i = blockDim.x / ;
while (i != ) {
if (cacheIndex < i)
cache[cacheIndex] += cache[cacheIndex + i];
__syncthreads();
i /= ;
} if (cacheIndex == )
c[blockIdx.x] = cache[];
} struct DataStruct {
int deviceID;
int size;
float *a;
float *b;
float returnValue;
}; unsigned WINAPI routine(void *pvoidData)
//void* routine(void *pvoidData)
{
DataStruct *data = (DataStruct*)pvoidData;
HANDLE_ERROR(cudaSetDevice(data->deviceID)); int size = data->size;
float *a, *b, c, *partial_c;
float *dev_a, *dev_b, *dev_partial_c; // allocate memory on the CPU side
a = data->a;
b = data->b;
partial_c = (float*)malloc(blocksPerGrid*sizeof(float)); // allocate the memory on the GPU
HANDLE_ERROR(cudaMalloc((void**)&dev_a,
size*sizeof(float)));
HANDLE_ERROR(cudaMalloc((void**)&dev_b,
size*sizeof(float)));
HANDLE_ERROR(cudaMalloc((void**)&dev_partial_c,
blocksPerGrid*sizeof(float))); // copy the arrays 'a' and 'b' to the GPU
HANDLE_ERROR(cudaMemcpy(dev_a, a, size*sizeof(float),
cudaMemcpyHostToDevice));
HANDLE_ERROR(cudaMemcpy(dev_b, b, size*sizeof(float),
cudaMemcpyHostToDevice)); dot <<<blocksPerGrid, threadsPerBlock >>>(size, dev_a, dev_b,
dev_partial_c);
// copy the array 'c' back from the GPU to the CPU
HANDLE_ERROR(cudaMemcpy(partial_c, dev_partial_c,
blocksPerGrid*sizeof(float),
cudaMemcpyDeviceToHost)); // finish up on the CPU side
c = ;
for (int i = ; i<blocksPerGrid; i++) {
c += partial_c[i];
} HANDLE_ERROR(cudaFree(dev_a));
HANDLE_ERROR(cudaFree(dev_b));
HANDLE_ERROR(cudaFree(dev_partial_c)); // free memory on the CPU side
free(partial_c); data->returnValue = c;
return ;
} int main(void) {
int deviceCount;
HANDLE_ERROR(cudaGetDeviceCount(&deviceCount));
//要求两个设备
if (deviceCount < ) {
printf("We need at least two compute 1.0 or greater "
"devices, but only found %d\n", deviceCount);
return ;
} float *a = (float*)malloc(sizeof(float)* N);
HANDLE_NULL(a);
float *b = (float*)malloc(sizeof(float)* N);
HANDLE_NULL(b); // fill in the host memory with data
for (int i = ; i<N; i++) {
a[i] = i;
b[i] = i * ;
} /*
为多线程做准备
每个DateStruct都为数据集大小的一半
*/
DataStruct data[];
data[].deviceID = ;
data[].size = N / ;
data[].a = a;
data[].b = b; data[].deviceID = ;
data[].size = N / ;
data[].a = a + N / ;
data[].b = b + N / ; CUTThread thread = start_thread(routine, &(data[]));
routine(&(data[]));
end_thread(thread); // free memory on the CPU side
free(a);
free(b); printf("Value calculated: %f\n",
data[].returnValue + data[].returnValue); return ;
}
多GPU设备处理点积示例的更多相关文章
- 利用nvidia-smi 管理和监控NVIDIA GPU设备
NVIDIA系统管理界面介绍 原文来源:https://developer.nvidia.com/nvidia-system-management-interface NVIDIA系统管理界面(nvi ...
- 【VS开发】设备控制台 (DevCon.exe) 示例
设备控制台 (DevCon.exe) 示例 本部分提供以下设备控制台 (DevCon.exe) 命令的示例: DevCon HwIDs 示例 1:查找所有硬件 ID 示例 2:使用模式查找硬件 ID ...
- [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- 使用tf.ConfigProto()配置Session运行参数和GPU设备指定
参考链接:https://blog.csdn.net/dcrmg/article/details/79091941 tf.ConfigProto()函数用在创建session的时候,用来对sessio ...
- tf.Session()函数的参数应用(tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/dcrmg/article/details ...
- OpenStack 企业私有云的若干需求(1):Nova 虚机支持 GPU
本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...
- 《CUDA并行程序设计:GPU编程指南》
<CUDA并行程序设计:GPU编程指南> 基本信息 原书名:CUDA Programming:A Developer’s Guide to Parallel Computing with ...
- GPU编程自学3 —— CUDA程序初探
深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...
随机推荐
- iOS客户端与网页交互文档
很少和客户端打交道,这次由于做会活动,要和客户端配合做个分享的功能 这里总结下基本的流程,就是前端在H5 里调用客户端的方法即可 第一部分 客户端提供需求文档 网页请求设置 客户端发起请求时在HTTP ...
- js 设置 cookie
function setCookie(name,value){ var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Day ...
- docker镜像的分层结构三
docker的镜像分层 docker里的镜像绝大部分都是在别的镜像的基础上去进行创建的,也就是使用镜像的分层结构. 实验 比如说使用dockerfile去创建一个最简单的hello镜像.创建好对应的d ...
- Json 解析Json
1.把LitJson导入到项目里面; 2.建一个下面的脚本,不挂在游戏对象上; 3.新建下面一个脚本,挂在相机上. using System.Collections; using System.Col ...
- Spring Cloud微服务初探
学习初衷 因为加了不少优秀的知识星球,结交了更多的小伙伴,加了更多的群,每每在自我介绍的时候,都说自己是Android & Java攻城狮. 然鹅,有的小伙伴就来问了,你是搞Java的,那对S ...
- InteliJ idea import project 找不到文件结构解决办法
一.按下列步骤操作: 1. 关闭IDEA, 2.然后删除项目文件夹下的.idea文件夹 3.重新用IDEA工具打开项目: 二.import新项目之后,可能需要等1 ...
- 《C#高效编程》读书笔记07-理解GetHashCode()的陷阱
GetHashCode()函数仅会在一个地方用到,即为基于散列(hash)的集合定义的散列键时,此类集合包括HashSet和Dictionary<K,V>容器等. 但object基类提供的 ...
- 从零开始的全栈工程师——js篇2.11(原型)
原型 原型分析 1.每个 函数数据类型(普通函数,类)都有一个prototype属性 并且这个属性是一个对象数据类型2.每个Prototype上都有一个constructor属性 并且这个属性值是当前 ...
- 从零开始的全栈工程师——js篇2.5
数据类型与全局属性 js的本质就是处理数据 数据来自于后台的数据库所以变量就起到一个临时存储数据的这作用ECMAscirpt 制定了js的数据类型 一.数据类型 1.基本数据类型 基本数据类型就是简单 ...
- contenttype组件、Django缓存机制以及跨域请求
1 昨日回顾 版本控制 *** (1)url=127.0.0.1/course/?version=v100000 1 versioning_class=QueryParameterVersioning ...