Cuda10.1总结2-RuntimeAPI

内存模型

CUdeviceptr:cuda中的指针,用于指向一为显存。注意为Drive接口指针。

CUarray:用来标出cuda中的二维显存。

具体使用方式如下。

/**
 *@brief 展示显存使用的代码,使用的编译工具是visual studio 2013。
 */
#include <fstream>
#include <string>
#include <iostream>
#include <cstdint>
using namespace std;

#include <cuda.h>
#include <cuda_runtime_api.h>

/**
 *@brief 二进制文件的读写操作。
 */
int32_t main()
{
	try {
		string inFileName = "F:\\Middleware\\In.bgra";
		string outFileName = "F:\\Middleware\\Out.bgra";
		int32_t width = 1920;
		int32_t height = 1080;
		ifstream inFile;
		ofstream outFile;
		uint8_t *RGBA = new uint8_t[width * height * 4];
		uint8_t *outRGBA = new uint8_t[width * height * 4];

		inFile.open(inFileName.c_str(), ios::binary);
		if (!inFile.is_open())
			throw inFileName;
		outFile.open(outFileName.c_str(), ios::binary);
		if (!outFile.is_open())
			throw outFileName;

		//为cudaArray分配内存。4个分量,每个分量为8bit无符号的整数。
		cudaArray *cuInputArray;
		cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 8, 8, 8, cudaChannelFormatKindUnsigned);
		cudaMallocArray(&cuInputArray, &channelDesc, width, height, cudaArrayDefault);

		//为CUdeviceptr分配内存。注意CUdeviceptr是Drive内存,不是Runtime内存,但
		CUdeviceptr outCuMemory;
		void *cudaOutMemory = nullptr;
		cudaMalloc(&cudaOutMemory, width * height * 4);
		outCuMemory = (CUdeviceptr)cudaOutMemory;

		int32_t i = 0;
		for (i = 0; i < 20; i++) {
			inFile.read((char *)RGBA, width * height * 4);
			//cudaMemcpy2DToArray()经测试,所有的宽度、高度、pitch的单位都是字节,而不是像素。
			//拷贝数据,从主机内存到显卡内存Array。
			cudaMemcpy2DToArray(cuInputArray, 0, 0, RGBA, width * 4, width * 4, height, cudaMemcpyHostToDevice);
		    //图像处理部分
			//拷贝数据,从显卡内存Array到显卡线性内存。
			cudaMemcpy2DFromArray(cudaOutMemory, width * 4, cuInputArray, 0, 0, width * 4, height, cudaMemcpyDeviceToDevice);
			//拷贝数据,从显卡线性内存到主机内存。
			cudaMemcpy(outRGBA, cudaOutMemory, width * height * 4, cudaMemcpyDeviceToHost);
			outFile.write((char *)outRGBA, width * height * 4);
		}

		cudaFreeArray(cuInputArray);
		cuMemFree(outCuMemory);
		cudaFree(cudaOutMemory);

		if (RGBA != nullptr) 
		{
			delete[] RGBA;
			RGBA = nullptr;
		}

		if (outRGBA != nullptr)
		{
			delete[] outRGBA;
			outRGBA = nullptr;
		}

		if (inFile.is_open()) {
			inFile.clear();
			inFile.close();
		}

		if (outFile.is_open()) {
			outFile.clear();
			outFile.close();
		}

		return 0;
	}
	catch (const bad_alloc &e) {//内存分配异常
		cerr << __FILE__ << "(" << __LINE__ << "):Unable to allocate memory!" << endl;
		cerr << e.what() << endl;
		return -1;
	}
	catch (string s) {//文件打开异常
		cerr << __FILE__ << "(" << __LINE__ << ")" << s.c_str() << endl;
		return -1;
	}
	catch (...) {//其他异常
		cerr << "发生了没有定义的异常" << endl;
		return -1;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加菲猫0320

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值