0_Simple__simpleAssert + 0_Simple__simpleAssert_nvrtc
在核函数中使用强制终止函数 assert()。并且在静态代码和运行时编译两种条件下使用。
▶ 源代码:静态使用
#include <windows.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_functions.h>
#include <helper_cuda.h> #define WINDOWS_LEAN_AND_MEAN
#define NOMINMAX __global__ void testKernel(int N)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x ; // 检查条件为“线程总编号小于 N”,即阻塞不满足该条件的线程
// 阻塞的同时向屏幕输出阻塞线程的信息,包括核函数所在文件绝对路径、行号、线程块号,线程号,没有通过的检查条件
assert(tid < N) ;
} bool runTest()
{
// 使用2个线程块各32条线程,使用 assert() 阻塞最后 4 条(即第 1 线程块的第 29、30、31、32 号线程)
int Nblocks = ;
int Nthreads = ;
cudaError_t error ; dim3 dimGrid(Nblocks);
dim3 dimBlock(Nthreads);
testKernel<<<dimGrid, dimBlock>>>(); printf("\n-- Begin assert output\n\n");
error = cudaDeviceSynchronize(); // 使用设备同步来获取错误信息
printf("\n-- End assert output\n\n"); if (error == cudaErrorAssert) // 输出错误信息种类
printf("CUDA error message is: %s\n",cudaGetErrorString(error)); return error == cudaErrorAssert;
} int main()
{
bool testResult; printf("\n\tStarted!\n"); testResult = runTest(); printf("\n\tCompleted! main function returned %s\n", testResult ? "OK!" : "ERROR!");
getchar(); return ;
}
即时编译版:
/*simpleAssert_kernel.cu*/
extern "C" __global__ void testKernel(int N)
{
int tid = blockIdx.x*blockDim.x + threadIdx.x ;
assert(tid < N) ;
}
/*simpleAssert.cpp*/
#include <windows.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include <helper_functions.h>
#include "nvrtc_helper.h" #define WINDOWS_LEAN_AND_MEAN
#define NOMINMAX bool runTest()
{
int Nblocks = ;
int Nthreads = ; // 紧张的 .cu 即时编译过程
char *kernel_file = sdkFindFilePath("simpleAssert_kernel.cu", NULL); char *ptx;
size_t ptxSize;
compileFileToPTX(kernel_file, , NULL, &ptx, &ptxSize); CUmodule module = loadPTX(ptx, , NULL); CUfunction kernel_addr;
cuModuleGetFunction(&kernel_addr, module, "testKernel"); dim3 dimGrid(Nblocks);
dim3 dimBlock(Nthreads);
int count = ;
void *args[] = { (void *)&count };
cuLaunchKernel(kernel_addr,dimGrid.x, dimGrid.y, dimGrid.z,dimBlock.x, dimBlock.y, dimBlock.z,,,&args[],); printf("\n-- Begin assert output\n\n");
CUresult res = cuCtxSynchronize(); // 用的是上下文同步?
printf("\n-- End assert output\n\n"); if (res == CUDA_ERROR_ASSERT)
printf("Device assert failed as expected\n"); return res == CUDA_ERROR_ASSERT ;
} int main()
{
bool testResult; printf("\n\tStarted!\n"); testResult = runTest(); printf("\n\tCompleted! main function returned %s\n", testResult ? "OK!" : "ERROR!");
getchar(); return ;
}
▶ 输出结果:
Started! -- Begin assert output D:/Program/CUDA/Samples/0_Simple/simpleAssert/simpleAssert.cu:: block: [,,], thread: [,,] Assertion `tid < N` failed.
D:/Program/CUDA/Samples/0_Simple/simpleAssert/simpleAssert.cu:: block: [,,], thread: [,,] Assertion `tid < N` failed.
D:/Program/CUDA/Samples/0_Simple/simpleAssert/simpleAssert.cu:: block: [,,], thread: [,,] Assertion `tid < N` failed.
D:/Program/CUDA/Samples/0_Simple/simpleAssert/simpleAssert.cu:: block: [,,], thread: [,,] Assertion `tid < N` failed. -- End assert output CUDA error message is: device-side assert triggered Completed! main function returned OK!
▶ 涨姿势:
● 在核函数中使用 assert( condition ) 来检查各线程中是否满足某条件。
若不满足条件 condition,则强制终止该线程,并输出核函数所在文件绝对路径、行号、线程块号,线程号,没有通过的检查条件
返回错误种类: cudaErrorAssert,错误代码 59,信息为 device-side assert triggered
cudaErrorAssert 为定义在 driver_type.h 中的枚举类型 enum __device_builtin__ cudaError{...}; 中,记录了各种错误信息。
● 调用核函数的另一种方法。使用定义在 cuda.h 中的函数 cuLaunchKernel。使用的参数与 <<< >>> 方式基本相同。
CUresult CUDAAPI cuLaunchKernel
(
CUfunction f,
unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ,
unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ,
unsigned int sharedMemBytes,
CUstream hStream,
void **kernelParams,
void **extra
);
● 两种方法使用的同步函数
静态方法时使用的是设备同步 extern __host__ __cudart_builtin__ cudaError_t CUDARTAPI cudaDeviceSynchronize(void);,定义在 cuda_runtime_api.h 中
即时编译时用的是上下文同步 CUresult CUDAAPI cuCtxSynchronize(void); ,定义在 cuda.h 中
尚不清楚两者的差别,等待填坑。
0_Simple__simpleAssert + 0_Simple__simpleAssert_nvrtc的更多相关文章
随机推荐
- [bzoj1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏--矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选 ...
- python GUI实战项目——tkinter库的简单实例
一.项目说明: 本次通过实现一个小的功能模块对Python GUI进行实践学习.项目来源于软件制造工程的作业.记录在这里以复习下思路和总结编码过程.所有的源代码和文件放在这里: 链接: https:/ ...
- Theano学习-梯度计算
1. 计算梯度 创建一个函数 \(y\) ,并且计算关于其参数 \(x\) 的微分. 为了实现这一功能,将使用函数 \(T.grad\) . 例如:计算 \(x^2\) 关于参数 \(x\) 的梯度. ...
- idea自我使用简单使用方式和出现的一些简单问题以及常用快捷键
首先配置完Idea的简单使用步骤后,今天在使用Idea时,一直持续提示web项目404的错误提示,因为之前使用idea时,部署的是springBoot的项目,使用的是SpringBoot自带的Tomc ...
- VirtualBox 磁盘容量调整
起因 此前用VBox,初始时动态或固定分配的磁盘容量用完了就再重新建一个磁盘,但是有个虚拟机的系统分区容量不够了,把整个磁盘都分成系统盘仍然不够,于是研究了下磁盘容量调整问题. 正文 1,从当前虚拟机 ...
- HDU5661 Claris and XOR
我们求二进制是怎么求的呢:先看看二进制的每一位代表多大:.......32 16 8 4 2 1 假如n=10, ..... 32>n ,不要. 16>n,不要. 8<=n,要,然后 ...
- Jquery qTip2实现多种提示效果,支持ajax,以及多种样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Sql语句构造类,多字段新增或修改时,拼装sql语句比较方便
using System; using System.Collections.Generic; using System.Text; namespace MSCL { #region 使用示例 /* ...
- Github Page+Bmob实现简单动态功能
Github Page基于jekyll能够实现简单的静态网站,但是没有提供后端服务.目前国内外也有很多提供后台服务,特别是云服务.譬如国外有AWS,记得好像是注册免费使用一年:再如Heroku,支持N ...
- OpenGL ES2.0贴图
1.定义传入着色器的顶点数据及索引 //传入结构体 typedef struct { ]; ]; } Vertex; //顶点数据 const Vertex Vertices[] = { {{, -, ...