▶ 在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的更多相关文章

随机推荐

  1. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ----初始化一个线程

    使用线程的一个常见问题就是如何能够在一个线程开始运行之前,适当地将它初始化.初始化最常见的理由就是为了调整优先权.另一个理由是为了在SMP 系统中设定线程比较喜欢的 CPU.第10 章谈到 MFC 时 ...

  2. 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头

    目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...

  3. You Are the One DP

    You Are the One Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  4. uva12519

    The Farnsworth Parabox Professor Farnsworth, a renowned scientist that lives in year 3000 working at ...

  5. Docker入门之六端口映射与容器互联

    一.端口映射 在之前的博客搭建私有仓库时用到这样一句:docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry来r ...

  6. Lucene介绍与入门使用

    Lucene简介 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整 ...

  7. c# 【MVC】WebApi通过HttpClient来调用Web Api接口

    /// <summary> /// HttpClient实现Post请求(异步) /// </summary> static async void dooPost() { st ...

  8. JAVA几种常见的编码格式(转)

    简介 编码问题一直困扰着开发人员,尤其在 Java 中更加明显,因为 Java 是跨平台语言,不同平台之间编码之间的切换较多.本文将向你详细介绍 Java 中编码问题出现的根本原因,你将了解到:Jav ...

  9. caffe源码 池化层 反向传播

    图示池化层(前向传播) 池化层其实和卷积层有点相似,有个类似卷积核的窗口按照固定的步长在移动,每个窗口做一定的操作,按照这个操作的类型可以分为两种池化层: 输入参数如下: 输入: 1 * 3 * 4 ...

  10. 关于使用连接器arm-linux-ld时指定链接地址的作用

    首先,记住一句话:程序的链接地址必须等于运行地址! 在学习exynos 4412的启动过程时,发现自己对链接地址的作用不是很了解,于是上网查找了资料做了基本了解,在此做个总结. 上图是exynos 4 ...