0_Simple__cudaOpenMP
▶ 在OpenMP的多线程程序中,各线程分别调用CUDA进行计算。OpenMP的简单示例。
▶ 源代码,OpenMP 出了点问题,没有正确输出结果
#include <stdio.h>
#include <omp.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_cuda.h> __global__ void kernelAddConstant(int *g_a, const int b)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
g_a[idx] += b;
} int main(int argc, char *argv[])
{
const int num_gpus = ;
unsigned int n = num_gpus * , nbytes = sizeof(int) * n;
omp_set_num_threads(num_gpus); // 使用CPU线程数量等于GPU设备数量。可以使用更多,如 2*num_gpus int b = ;
int *a = (int *)malloc(nbytes);
if (a == NULL)
{
printf("couldn't allocate CPU memory\n");
return ;
}
for (unsigned int i = ; i < n; i++)
a[i] = i; #pragma omp parallel num_threads(8) // 强制使用 8 个线程
{
unsigned int thread_size = omp_get_num_threads(), thread_rank = omp_get_thread_num(); int gpu_id = -;
cudaSetDevice(thread_rank % num_gpus); // 使用 % 使得一个 GPU 能接受更多 CPU 线程
cudaGetDevice(&gpu_id);
printf("CPU thread %d (of %d) uses CUDA device %d\n", thread_rank, thread_size, gpu_id); int *d_a = NULL;
int *sub_a = a + thread_rank * n / thread_size; // 主机内存分段,每个线程计算不同的分段
unsigned int byte_per_kernel = nbytes / thread_size;
cudaMalloc((void **)&d_a, byte_per_kernel);
cudaMemset(d_a, , byte_per_kernel);
cudaMemcpy(d_a, sub_a, byte_per_kernel, cudaMemcpyHostToDevice); dim3 gpu_threads();
dim3 gpu_blocks(n / (gpu_threads.x * thread_size));
kernelAddConstant << <gpu_blocks, gpu_threads >> >(d_a, b);
cudaMemcpy(sub_a, d_a, byte_per_kernel, cudaMemcpyDeviceToHost);
cudaFree(d_a);
} if (cudaGetLastError() != cudaSuccess) // 检查结果
printf("%s\n", cudaGetErrorString(cudaGetLastError()));
for (int i = ; i < n; i++)
{
if (a[i] != i + b)
{
printf("Error at i == %d, a[i] == %d", i, a[i]);
break;
}
}
printf("finish!\n"); free(a);
getchar();
return ;
}
0_Simple__cudaOpenMP的更多相关文章
随机推荐
- iOS9.3越狱
转载:http://bbs.feng.com/read-htm-tid-10680439.html 首先是Windows英文版越狱的的教程 下载 Cydia Impactor 工具(用来安装越狱A ...
- OpenJudge_1321:棋盘问题
题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆 ...
- 在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装
在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装 1.PXEServer:OS:CentOS6.9IP:172.16.25.69: (1)apache:# mo ...
- 苹果iPhone X上搭载的那颗A11仿生芯片,到底牛在哪?
苹果iPhone X上搭载的那颗A11仿生芯片,到底牛在哪? 上周,苹果公司在刚刚落成投入使用的“飞船”新总部(Apple Park)举行2017年秋季新品发布会,整场发布会基本被iPhone X抢尽 ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- 自签名的https证书是不安全的
一.项目内的需求 我们做的app都是企业级的应用,而企业级的应用的下载需要遵循itms协议,itms协议下需要https链接,这就需要你的服务器支持https的协议,该协议需要申请SSL证书,我们测试 ...
- Python之scrapy安装
1.按照网上教程一步步实验,运行时报错: 'HtmlResponse' object has no attribute 'xpath' in scrapy 个人使用的是scrapy0.14.4,搜索得 ...
- wxPython中菜单、按钮学习
---恢复内容开始--- wx.Window 是一个基类,许多构件从它继承.包括 wx.Frame 构件.技术上这意味着,我们可以在所有的 子类中使用 wx.Window 的方法.我们这里介绍它的几种 ...
- Win CE 6.0 获取手持机GPS定位1----基础知识 (C#)
一.GPS全球定位系统的组成 (1)GPS卫星(空间部分) 由沿接近环形的地球轨道运行的24颗卫星组成,位于距地表20200千米的高空,均匀分布在6个轨道面上(每个轨道面4颗),轨道倾角55度.此外, ...
- js 浏览器上调试方法多样
1.alert(111) 直接打印出 111 2.debugger 写在代码要调试的地方 3.直接在控制台 source 里找到要调试的代码打断点 4.console 常用的 ...