Untitled
unknown
c_cpp
2 years ago
1.6 kB
16
Indexable
int main() {
// Load an RGB image using OpenCV
cv::Mat inputImage = cv::imread("/home/hesam/Desktop/playground/cuda/DSC_0325.JPG", cv::IMREAD_COLOR);
cv::resize(inputImage, inputImage, cv::Size(600, 400));
if (inputImage.empty()) {
std::cerr << "Error: Could not load the image." << std::endl;
return -1;
}
int width = inputImage.cols;
int height = inputImage.rows;
size_t imageSize = width * height * sizeof(uchar3);
// Allocate Unified Memory for image data
uchar3* unifiedInputImage;
uchar3* unifiedOutputImage;
cv::Mat* unifiedResultImage; // Pointer to the final result in Unified Memory
cudaMallocManaged(&unifiedInputImage, imageSize);
cudaMallocManaged(&unifiedOutputImage, imageSize);
cudaMallocManaged(&unifiedResultImage, sizeof(cv::Mat));
// Load the input image directly into Unified Memory
memcpy(unifiedInputImage, inputImage.ptr<uchar3>(), imageSize);
// Define block and grid dimensions
dim3 blockSize(16, 16);
dim3 gridSize((width + blockSize.x - 1) / blockSize.x, (height + blockSize.y - 1) / blockSize.y);
// Launch the convolution kernel on the GPU
convolutionKernelGPU<<<gridSize, blockSize>>>(unifiedInputImage, unifiedOutputImage, width, height);
cudaDeviceSynchronize();
*unifiedResultImage = cv::Mat(height, width, CV_8UC3, unifiedOutputImage);
// Save the resulting image
cv::imwrite("blurred.jpeg", *unifiedResultImage);
// Free Unified Memory
cudaFree(unifiedInputImage);
cudaFree(unifiedOutputImage);
cudaFree(unifiedResultImage);
return 0;
}Editor is loading...