how to rework this code to use unified memory " cudaStreamt stream; //resize input image…
- User
how to rework this code to use unified memory " cudaStream_t stream; //resize input image to the size required by neural net cv::Mat image_resized(h_, w_, CV_8UC3, inputCpu_); if (up_crop_ != 0) { std::int16_t cropRows = image.rows - up_crop_; cv::Rect crop(0, up_crop_, image.cols, cropRows); cv::Mat img_crop = image(crop); cv::resize(img_crop, image_resized, cv::Size(w_, h_)); } else { cv::resize(image, image_resized, cv::Size(w_, h_)); }
NV_CUDA_CHECK(cudaStreamCreate(&stream));
//transfer the image to GPU NV_CUDA_CHECK(cudaMemcpyAsync(inputGpu_, inputCpu_, h_ * w_ * 3 * sizeof(unsigned char), cudaMemcpyHostToDevice));
//normalize image and reshape it such that it has channels-first format (required by neural net) NV_CUDA_CHECK(preprocessOnGpuWrapper(inputGpu_, inputCfGpu_, h_, w_, coef_, mean_[0], mean_[1], mean_[2], std_[0], std_[1], std_[2]));
//inference buffers_[0] = inputCfGpu_; buffers_[1] = outputRawGpu_; context_->getBindingDimensions(0);
context_->enqueueV2((void **)buffers_, stream, nullptr);
//compute segmentation masks from raw neural net ouput NV_CUDA_CHECK(computeOutputOnGpuWrapper(outputRawGpu_, outputMaskGpu_, h_, w_, numClasses_, threshold_));
//transfer result from GPU to the host NV_CUDA_CHECK(cudaMemcpyAsync(outputMaskCpu_, outputMaskGpu_, h_ * w_ * sizeof(unsigned char), cudaMemcpyDeviceToHost)); // V1-2, 5
NV_CUDA_CHECK(cudaStreamSynchronize(stream));
// Wait for the work in the stream to complete. // Release stream. NV_CUDA_CHECK(cudaStreamDestroy(stream));
void Segmentator::memoryAlloc() { //pinned memory allocation for input and output NV_CUDA_CHECK(cudaMallocHost((void **)&inputCpu_, h_ * w_ * 3 * sizeof(unsigned char))); NV_CUDA_CHECK(cudaMallocHost((void **)&outputMaskCpu_, h_ * w_ * sizeof(unsigned char)));
//GPU memory allocation NV_CUDA_CHECK(cudaMalloc((void **)&inputGpu_, h_ * w_ * 3 * sizeof(unsigned char))); NV_CUDA_CHECK(cudaMalloc((void **)&inputCfGpu_, h_ * w_ * 3 * sizeof(float))); NV_CUDA_CHECK(cudaMalloc((void **)&outputRawGpu_, h_ * w_ * numClasses_ * sizeof(float))); NV_CUDA_CHECK(cudaMalloc((void **)&outputMaskGpu_, h_ * w_ * sizeof(unsigned char)));}
void Segmentator::memoryFree() { //free pinned host memory NV_CUDA_CHECK(cudaFreeHost(inputCpu_)); NV_CUDA_CHECK(cudaFreeHost(outputMaskCpu_));
//free gpu memory NV_CUDA_CHECK(cudaFree(inputGpu_)); NV_CUDA_CHECK(cudaFree(inputCfGpu_)); NV_CUDA_CHECK(cudaFree(outputRawGpu_)); NV_CUDA_CHECK(cudaFree(outputMaskGpu_));} "
- Assistant