多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 ...
随机推荐
- spring使用redisTemplate
连接工厂:spring data redis 2.0中提供了两种redis客户端实现 LettuceConnectionFactory JedisConnectionFactory 区别: Lettu ...
- 73th LeetCode Weekly Contest Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- 73th LeetCode Weekly Contest Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- linux查看硬盘空间,删除大文件
df -Phdu -h --max-depth=1du -sh /u02/weblogic/user_projects/domains/logsdu -sh /u02/mysqlfind / -siz ...
- sql For update
for update 的作用和目的:select for update 是为了在查询时,对这条数据进行加锁,避免其他用户以该表进行插入,修改或删除等操作,造成表的不一致性. 几个类似的场景: sele ...
- python3 下载 以及 练习1 以及 pycharm 专业版 安装
下载python: https://www.python.org/downloads/release/python-365/ ########sample 1 下载pycharm 社区版本,但是web ...
- (转)认识 Linux 文件系统
7.1 认识 Linux 文件系统 原文:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/59.html Linux 最传统 ...
- datagrid 里面的formatter方法
A.{field:'station_staus',title:'工位状态',width:250,align:'center',formatter: function(value,row,index){ ...
- 开源的SSH框架优缺点分析
开源是3个框架共有的优点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: 2) 丰富的标签库,大大提高了开发的效率: 3) Str ...
- asp.net 子域跨域 带cookie
先来一个老外的解决方案: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api ...