二维显存的使用
来源:互联网 发布:linux 启动oracle实例 编辑:程序博客网 时间:2024/05/17 09:28
维数组动态申请内存空间,及其与显存数据相互拷贝的两种方式如下:
-
-
-
-
-
-
- #include <stdio.h>
- #include <cutil_inline.h>
- #include <iostream>
- using namespace std;
-
- int main(int argc, char **argv)
- {
- CUT_DEVICE_INIT(argc, argv);
- #if 1
-
- float **CPU_ORIGN, **CPU_RET;
- float **GPU;
- int width = 5, height = 3;
- size_t size = sizeof(float)*width;
- int row, col;
-
-
- CPU_ORIGN = new float*[height];
- CPU_RET = new float*[height];
- for(row = 0; row < height; ++row)
- {
- CPU_ORIGN[row] = new float[width];
- CPU_RET[row] = new float[width];
-
- for(col = 0; col < width; ++col)
- {
- CPU_ORIGN[row][col] = (float)(row + col);
- CPU_RET[row][col] = 0.0f;
- }
- }
-
-
- GPU = new float*[height];
- for(row = 0; row < height; ++row)
- {
- cutilSafeCall( cudaMalloc((void**)&GPU[row], size));
- cutilSafeCall( cudaMemset(GPU[row], 0, size));
- }
-
-
- for(row = 0; row < height; ++row)
- cutilSafeCall(cudaMemcpy(GPU[row], CPU_ORIGN[row], size, cudaMemcpyHostToDevice));
-
-
- for(row = 0; row < height; ++row)
- cutilSafeCall(cudaMemcpy(CPU_RET[row], GPU[row], size, cudaMemcpyDeviceToHost));
-
-
- for(row = 0; row < height; ++row)
- {
- for(col = 0; col < width; ++col)
- cout << CPU_RET[row][col] << " ";
- cout << endl;
- }
-
- free(CPU_ORIGN);
- free(CPU_RET);
- for(row = 0; row < height; ++row)
- cutilSafeCall(cudaFree(GPU[row]));
- #endif
- #if 0
-
- float **CPU_ORIGN, **CPU_RET;
- float **GPU;
- int width = 5, height = 3;
- size_t size = sizeof(float)*width;
- size_t pitch;
- int row, col;
-
-
- CPU_ORIGN = new float*[height];
- CPU_RET = new float*[height];
- for(row = 0; row < height; ++row)
- {
- CPU_ORIGN[row] = new float[width];
- CPU_RET[row] = new float[width];
-
- for(col = 0; col < width; ++col)
- {
- CPU_ORIGN[row][col] = (float)(row + col);
- CPU_RET[row][col] = 0.0f;
- }
- }
-
-
- cutilSafeCall(cudaMallocPitch((void**)&GPU, &pitch, size, height));
- cutilSafeCall(cudaMemset2D(GPU, pitch, 0, size, height));
-
-
- cutilSafeCall(cudaMemcpy2D(GPU, pitch, CPU_ORIGN, size, size, height, cudaMemcpyHostToDevice));
-
-
- cutilSafeCall(cudaMemcpy2D(CPU_RET, size, GPU, pitch, size, height, cudaMemcpyDeviceToHost));
-
-
- for(row = 0; row < height; ++row)
- {
- for(col = 0; col < width; ++col)
- cout << CPU_RET[row][col] << " ";
- cout << endl;
- }
-
- free(CPU_ORIGN);
- free(CPU_RET);
- cutilSafeCall(cudaFree(GPU));
- #endif
- CUT_EXIT(argc, argv);
- }