• CUDA学习笔记4——自定义设备函数


    限定符
    函数类型限定符
    • 核函数:__global__修饰;① 在设备中执行;② 仅可从设备调用;
    • 设备函数:__device__修饰;① 在设备中执行;② 仅可从设备调用;
    • 主机函数:__host__修饰(可省略);① 在主机中执行;② 尽可从主机调用。

    _global_ 和 _device_ 函数不支持递归;不能声明静态变量在它们体内;不能有自有变量的一个变量数字。

    变量类型限定符
    • _device_
    • _constant_
    • _shared_
    自定义设备函数
    • 设备函数:__device__修饰;在设备中执行;只能被核函数或其他设备函数调用;
    #include 
    #include "cuda_runtime.h"
    #include "device_launch_parameters.h"
    #include
    #include  
    #include 
    
    #include 
    
    #define BLOCK_SIZE 1
    
    void __device__ thread_gpu(unsigned char in, unsigned char* out, int thread)
    {
    	in > thread ? *out = 255 : *out = 0;	
    }
    
    //图像卷积 GPU
    __global__ void sobel_gpu(unsigned char* in, unsigned char* out, const int Height, const int Width)
    {
    	int x = blockDim.x * blockIdx.x + threadIdx.x;
    	int y = blockDim.y + blockIdx.y + threadIdx.y;
    	int index = y * Width + x;
    
    	int Gx = 0;
    	int Gy = 0;
    
    	unsigned char x0, x1, x2, x3, x4, x5, x6, x7, x8;
    
    	if (x>0 && x<(Width-1) && y>0 && y<(Height-1))
    	{
    		x0 = in[(y - 1)*Width + (x - 1)];
    		x1 = in[(y - 1)*Width + (x)];
    		x2 = in[(y - 1)*Width + (x + 1)];
    		x3 = in[(y)*Width + (x - 1)];
    
    		x5 = in[(y)*Width + (x + 1)];
    		x6 = in[(y + 1)*Width + (x - 1)];
    		x7 = in[(y + 1)*Width + (x)];
    		x8 = in[(y + 1)*Width + (x + 1)];
    
    		Gx = (x0 + 2 * x3 + x6) - (x2 + 2 * x5 + x8);
    		Gy = (x0 + 2 * x1 + x2) - (x6 + 2 * x7 + x8);
    
    		out[index] = (abs(Gx) + abs(Gy)) / 2;
    
    		thread_gpu(out[index], &out[index], 80);
    	}
    }
    
    
    
    int main()
    {
    	cv::Mat src;
    	src = cv::imread("photo16.jpg");
    
    	cv::Mat grayImg,gaussImg;
    	cv::cvtColor(src, grayImg, cv::COLOR_BGR2GRAY);
    	cv::GaussianBlur(grayImg, gaussImg, cv::Size(3,3), 0, 0, cv::BORDER_DEFAULT);
    
    	int height = src.rows;
    	int width = src.cols;
    	//输出图像
    	cv::Mat dst_gpu(height, width, CV_8UC1, cv::Scalar(0));
    	//GPU存储空间
    	int memsize = height * width * sizeof(unsigned char);
    	//输入 输出
    	unsigned char* in_gpu;
    	unsigned char* out_gpu;
    
    	cudaMalloc((void**)&in_gpu, memsize);
    	cudaMalloc((void**)&out_gpu, memsize);
    
    	dim3 threadsPreBlock(BLOCK_SIZE, BLOCK_SIZE);
    	dim3 blocksPreGrid((width + threadsPreBlock.x - 1)/threadsPreBlock.x, (height + threadsPreBlock.y - 1)/threadsPreBlock.y);
    	
    	cudaMemcpy(in_gpu, gaussImg.data, memsize, cudaMemcpyHostToDevice);
    
    	sobel_gpu <<<blocksPreGrid, threadsPreBlock>>> (in_gpu, out_gpu, height, width);
    	
    	cudaMemcpy(dst_gpu.data, out_gpu, memsize, cudaMemcpyDeviceToHost);
    
    	cv::imwrite("dst_gpu_save.png", dst_gpu);
    
    	//cv::namedWindow("src", cv::WINDOW_NORMAL);
    	cv::imshow("src", src);
    	cv::imshow("dst_gpu", dst_gpu);
    	cv::waitKey();
    
    	cudaFree(in_gpu);
    	cudaFree(out_gpu);
    
    	return 0;
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    MySQL分区、主从复制,数据库优化
    suricata 流管理
    PyQt GUI 编程-01
    Postman —— postman实现参数化
    学习Python可以做什么工作?选什么方向?
    详谈操作系统中的内核态和用户态
    业务代码到底需不需要用多线程???
    在Elasticsearch IK分词器中更新、停用某些专有名词
    Algorithms practice:Basic Calculator 224
    YoLo V3 SPP u模型的讲解与总结
  • 原文地址:https://blog.csdn.net/akadiao/article/details/133885204