0_Simple__inlinePTX + 0_Simple__inlinePTX_nvrtc
在核函数代码中加入并行线程执行(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的更多相关文章
随机推荐
- NavigationController的返回按钮自定义
假设需求时这样: NavigationController下有2个视图,从A视图会Push到B视图,默认情况下,当显示视图B时,视图B的导航bar上会出现返回按钮,按钮标题文字默认为A视图的title ...
- 【Linux笔记(001) 】-- centos7 系统目录结构与文件
一.目录结构与用途: /boot:系统引导文件.内核 /bin:用户的基本命令 /dev:设备文件 /etc:配置文件 /home:用户目录 /root:root用户目录 /sbin:管理类的基本命令 ...
- tomcat manager 的用户权限配置,及环境变量CATALINA_HOME的错位问题
因为tomcat的manager是管理其他项目的发布.删除等操作的管理项目,所以需要为其设置登陆用户和密码,以及用户相应的访问权限,配置如下: tomcat-users.xml需要添加如下内容: &l ...
- 记一次坑爹的RSA旅程____快哭了555555555(来自实验吧的warmup的wp和感想)
这么简单的题目搞了我那么久,森森感觉自己菜的不行....哎,努力吧少年,BXS已经全国第二了. 嗯,废话不说,这道题目来自实验吧的"warmup",附上链接 http://www. ...
- 怎样学好哲学(lucas+费马小定理)
怎样学习哲学 时间限制: 1 Sec 内存限制: 128 MB提交: 97 解决: 27[提交][状态][讨论版] 题目描述 OI大师抖儿在夺得银牌之后,顺利保送pku.这一天,抖儿问长者:&qu ...
- centos crontab(定时任务) 使用
一.介绍 crontab命令的功能是在一定的时间间隔调度一些命令的执行.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会 ...
- asp.net mvc 自动化测试工具
好久不写文章了,一直忙在项目中. 前一阵发现公司一个项目,体积巨大.业务很复杂.基于历史原因,项目基于mvc 2迁移过来,视图大多还是aspx 作为视图承载. 控制器中的方法 更是一个比一个多. ...
- ORALCE PL/SQL学习笔记
ORALCE PL/SQL学习笔记 详情见自己电脑的备份数据资料
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...
- win10 UWP 剪贴板 Clipboard
win10 UWP 剪贴板 Clipboard使用Windows.ApplicationModel.DataTransfer.Clipboard 设置文本 DataPackage dataPackag ...