#include "GPU_MLP.cuh"
using namespace std;
__global__ void kernel(int **d_ptr, int width){
d_ptr[threadIdx.y][threadIdx.x] = 3;
}
__global__ void SetDeviceMem(int *src, int **ptr, int width){
ptr[threadIdx.x] = src + width * threadIdx.x;
}
GPU_MLP::GPU_MLP(int width, int height){
nSize = sizeof(int) * width * height;
_width = width;
_height = height;
d_test = NULL;
d_ptr = NULL;
h_test = NULL;
h_ptr = NULL;
HostMemAlloc();
DeviceMemAlloc();
cudaMemcpy(d_test, h_test, nSize, cudaMemcpyHostToDevice);
}
void GPU_MLP::HostMemAlloc(){
h_test = new int[_width * _height];
h_ptr = new int*[_height];
for(int i = 0; i < _height; ++i)
h_ptr[i] = h_test + _width * i;
memset(h_test, 0, nSize);
}
void GPU_MLP::DeviceMemAlloc(){
cudaMalloc<int>(&d_test, nSize);
cudaMalloc<int*>(&d_ptr, sizeof(int*) * _height);
SetDeviceMem<<<1, _height>>>(d_test, d_ptr, _width);
}
void GPU_MLP::Fuction(int value){
dim3 block(_width, _height);
kernel<<<1, block>>>(d_ptr, _width);
cudaMemcpy(h_test, d_test, nSize, cudaMemcpyDeviceToHost);
for(int i = 0; i < _height; ++i){
for(int j = 0; j < _width; ++j){
cout << h_ptr[i][j] << " ";
}
cout << endl;
}
}
GPU_MLP::~GPU_MLP(){
delete[] h_test;
delete[] h_ptr;
cudaFree(d_test);
cudaFree(d_ptr);
cudaDeviceReset();
}
CUDA 파일이름.cuh
#include <iostream>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
__global__ void kernel(int **d_ptr, int width);
__global__ void SetDeviceMem(int *src, int **ptr, int width);
class GPU_MLP{
public:
int *d_test;
int **d_ptr;
int *h_test;
int **h_ptr;
int _width;
int _height;
size_t nSize;
GPU_MLP(int width, int height);
void HostMemAlloc();
void DeviceMemAlloc();
void Fuction(int value);
~GPU_MLP();
};
#include <opencv2\opencv.hpp>
#include "GPU_MLP.cuh"
#include <Windows.h>
using namespace std;
using namespace cv;
int main(int argv, char **argc){
GPU_MLP test(10, 10);
test.Fuction(3);
system("pause");
return 0;
}
'Programing > C++' 카테고리의 다른 글
이중연결리스트.h (0) | 2016.11.30 |
---|---|
스마트 포인터 (0) | 2016.11.30 |
가상함수테이블 (0) | 2016.11.30 |
템플릿(Template) -클래스 템플릿, 템플릿의 특수화 (0) | 2016.11.30 |
템플릿(Template) - 함수 템플릿, 클래스 템플릿, 템플릿의 특수화 (0) | 2016.11.30 |