在核函数代码中加入并行线程执行(Parallel Thread eXecution,PTX),通过汇编指令获取得有关线程束的信息。并且在静态代码和运行时编译两种条件下使用。

▶ 源代码:静态使用

 #include <stdio.h>
#include <assert.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_functions.h>
#include <helper_cuda.h> __global__ void sequence_gpu(int *d_ptr, int length)
{
int elemID = blockIdx.x * blockDim.x + threadIdx.x; if (elemID < length)
{
unsigned int laneid;
asm("mov.u32 %0, %%laneid;" : "=r"(laneid));// 获取当前线程在线程束中的编号
d_ptr[elemID] = laneid;
}
} void sequence_cpu(int *h_ptr, int length)
{
for (int elemID=; elemID<length; elemID++)
h_ptr[elemID] = elemID % ;
} int main(int argc, char **argv)
{
printf("CUDA inline PTX assembler sample\n"); const int N = ; int dev = findCudaDevice(argc, (const char **) argv);
if (dev == -)
return EXIT_FAILURE; int *d_ptr;
cudaMalloc(&d_ptr, N * sizeof(int));
int *h_ptr;
cudaMallocHost(&h_ptr, N * sizeof(int)); dim3 cudaBlockSize(,,);
dim3 cudaGridSize((N + cudaBlockSize.x - ) / cudaBlockSize.x, , );
sequence_gpu<<<cudaGridSize, cudaBlockSize>>>(d_ptr, N);
cudaGetLastError();
cudaDeviceSynchronize(); sequence_cpu(h_ptr, N); int *h_d_ptr;
cudaMallocHost(&h_d_ptr, N *sizeof(int));
cudaMemcpy(h_d_ptr, d_ptr, N *sizeof(int), cudaMemcpyDeviceToHost); bool bValid = true; for (int i=; i<N && bValid; i++)
{
if (h_ptr[i] != h_d_ptr[i])
bValid = false;
} printf("Test %s.\n", bValid ? "Successful" : "Failed"); cudaFree(d_ptr);
cudaFreeHost(h_ptr);
cudaFreeHost(h_d_ptr); getchar();
return bValid ? EXIT_SUCCESS: EXIT_FAILURE;
}

▶ 源代码:运行时编译

 /*inlinePTX_kernel.cu*/
extern "C" __global__ void sequence_gpu(int *d_ptr, int length)
{
int elemID = blockIdx.x * blockDim.x + threadIdx.x;
if (elemID < length)
{
unsigned int laneid;
asm("mov.u32 %0, %%laneid;" : "=r"(laneid));
d_ptr[elemID] = laneid;
}
}
 /*inlinePTX.cpp*/
#include <stdio.h>
#include <assert.h>
#include <cuda_runtime.h>
#include <nvrtc_helper.h>
#include <helper_functions.h> void sequence_cpu(int *h_ptr, int length)
{
for (int elemID=; elemID<length; elemID++)
h_ptr[elemID] = elemID % ;
} int main(int argc, char **argv)
{
printf("CUDA inline PTX assembler sample\n"); char *ptx, *kernel_file;
size_t ptxSize; kernel_file = sdkFindFilePath("inlinePTX_kernel.cu", argv[]);
compileFileToPTX(kernel_file, , NULL, &ptx, &ptxSize);
CUmodule module = loadPTX(ptx, argc, argv);
CUfunction kernel_addr;
cuModuleGetFunction(&kernel_addr, module, "sequence_gpu"); const int N = ;
int *h_ptr = (int *)malloc(N * sizeof(int)); dim3 cudaBlockSize(,,);
dim3 cudaGridSize((N + cudaBlockSize.x - ) / cudaBlockSize.x, , );
CUdeviceptr d_ptr;
cuMemAlloc(&d_ptr, N * sizeof(int)); void *arr[] = { (void *)&d_ptr, (void *)&N };
cuLaunchKernel(kernel_addr,
cudaGridSize.x, cudaGridSize.y, cudaGridSize.z,
cudaBlockSize.x, cudaBlockSize.y, cudaBlockSize.z,
, , &arr[], ); cuCtxSynchronize();
sequence_cpu(h_ptr, N);
int *h_d_ptr = (int *)malloc(N * sizeof(int));;
cuMemcpyDtoH(h_d_ptr, d_ptr, N *sizeof(int)); bool bValid = true;
for (int i=; i<N && bValid; i++)
{
if (h_ptr[i] != h_d_ptr[i])
bValid = false;
} printf("Test %s.\n", bValid ? "Successful" : "Failed");
cuMemFree(d_ptr); getchar();
return bValid ? EXIT_SUCCESS: EXIT_FAILURE;
}

▶ 输出结果:

CUDA inline PTX assembler sample
GPU Device : "GeForce GTX 1070" with compute capability 6.1 Test Successful.

▶ 涨姿势:

● 获取当前线程在线程束中的编号,即同意先乘数中的线程分别获得值 0 ~ 31

asm("mov.u32 %0, %%laneid;" : "=r"(laneid));

0_Simple__inlinePTX + 0_Simple__inlinePTX_nvrtc的更多相关文章

随机推荐

  1. 【个人笔记】《知了堂》MySQL中的数据类型

    MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) ...

  2. java枚举类(enum) 基础知识讲解

    枚举类是在java 5后新增的,可以用于封装常量,并且还可以为常量的使用提供一些方法. 定义枚举类的语法: public enum EnumName{ 成员1(A,B...),成员2(A,B...), ...

  3. Redis学习——redis.conf 配置文件介绍

    学以致用 学在用前 参看文章: redis.conf 配置详解 Redis配置文件详解(redis.conf)-云栖社区 在Redis的使用过程,除了知道对Redis五种数据类型的操作方法之外,最主要 ...

  4. JSP入门 Listener

    实现HttpSessionListener 编写一个OnlineUserListener类 package anni; import java.util.List; import javax.serv ...

  5. (译)通过 HTML、JS 和 Electron 创建你的第一个桌面应用

    原文:Creating Your First Desktop App With HTML, JS and Electron 作者:Danny Markov 近年来 web 应用变得越来越强大,但是桌面 ...

  6. 自动化双向数据绑定AngularJs---入门

      前  言   AngularJS,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为 ...

  7. PE格式第六讲,导出表

    PE格式第六讲,导出表 请注意,下方字数比较多,其实结构挺简单,但是你如果把博客内容弄明白了,对你受益匪浅,千万不要看到字数多就懵了,其实字数多代表它重要.特别是第五步, 各种表中之间的关系. 作者: ...

  8. UWP 分享用那个图标

    有两个图标,如果让你选,你会用哪个图标做分享图标? 这就算有意义的图标和通用图标的选择. 可以看到 左边的图标比较有意义,但是右边的图标是通用的. 是需要选有意义的?还是通用的 在 UWP ,选的是第 ...

  9. 转:用STL中的vector动态开辟二维数组

    用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...

  10. Centos 7.3 安装配置 PostgreSQL 9.x

    一.安装 PostgresSQL Centos 7 自带的 PostgresSQL 是 9.2 版的.因为,yum 已经做了国内源,速度飞快,所以直接就用 yum 安装了.依次执行以下命令即可,非常简 ...