当前位置: 首页 > news >正文

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

自定义设备函数
  • 核函数:__global__修饰;在设备中执行;
  • 设备函数:__device__修饰;在设备中执行;只能被核函数或其他设备函数调用;
  • 主机函数:__host__修饰(可省略);在主机中执行;
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include<math.h>
#include <malloc.h> 
#include <opencv2/opencv.hpp>#include <stdlib.h>#define BLOCK_SIZE 1void __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;
}

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

http://www.lryc.cn/news/196932.html

相关文章:

  • 微前端四:qiankun在开发中遇到的问题
  • Android DisplayPolicy增加一些动作,打开后台接口
  • 基于Linux安装Hive
  • FPGA 图像缩放 1G/2.5G Ethernet PCS/PMA or SGMII实现 UDP 网络视频传输,提供工程和QT上位机源码加技术支持
  • 重复控制逆变器的仿真分析研究
  • WuThreat身份安全云-TVD每日漏洞情报-2023-10-18
  • 开启机器人学新时代,《机器人学建模、规划与控制》完美诠释未来
  • C#根据ip获取地理位置信息的方法,史上最全
  • Git问题汇总
  • 【linux 0.11 学习记录】一、环境配置,用Bochs输出hello world
  • 【LeetCode75】第七十三题 用最少数量的箭引爆气球
  • 航天科技×辰安科技 打造智慧化工园区安全保障平台
  • 6-2 分治法求解金块问题
  • A062-防火墙安全配置-配置Iptables防火墙策略
  • Java包装类
  • 常用字符字符串处理函数
  • 【汇编语言特别篇】DOSBox及常用汇编工具的详细安装教程
  • 【牛客网刷题(数据结构)】:环形链表的约瑟夫问题
  • 虾皮印尼买家号如何注册
  • SpringBoot WebService服务端客户端使用教程
  • 【Python 千题 —— 基础篇】字符串长度
  • AIGC - 入门向量空间模型
  • python中使用xml.dom.minidom模块读取解析xml文件
  • 计算机网络第一章补充整理(计算机网络体系结构)
  • 2023_Spark_实验十七:导入招聘大数据(项目)
  • 小程序无感刷新
  • Unity C#随笔:简述String和StringBuilder的区别
  • 图论相关算法
  • Python人工智能需要学什么
  • Java 获取请求真实IP