%% GPU Code Generation for Lane Detection in Simulink
% This example shows how to generate CUDA(R) code for a Simulink(R) model
% that can detect and output lane marker boundaries on an image. This
% example takes RGB image as an input and uses the
% <docid:images_ref#buxswkh-1 imresize>, <docid:matlab_ref#buiz8mj-1
% rgb2gray>, <docid:images_ref#bupba70-1 ordfilt2>,
% <docid:images_ref#buwew0l-1 hough>, <docid:images_ref#buw9d0p-1
% houghpeaks>, and <docid:images_ref#buwgo_f-1 houghlines> functions that
% are part of Image Processing Toolbox(TM) to detect lane markings.
% This example closely follows
% <docid:gpucoder_ug#example-gpucoderdemo_lane_detection_houghlines Lane 
% Detection on the GPU by Using the houghlines Function>.
%
% This example illustrates the following concepts:
%
% * Model a lane detection application in Simulink by using image processing
% functions.
% * Configure the model for GPU code generation.
% * Generate a CUDA executable for the Simulink model.
%

% Copyright 2020 The MathWorks, Inc.

%% Prerequisites
%
% * CUDA enabled NVIDIA(R) GPU with compute capability 3.2 or higher.
% * NVIDIA CUDA toolkit and driver.
% * Image Processing Toolbox(TM).
% * Environment variables for the compilers and libraries. For more
% information, see
% <docid:gpucoder_gs#mw_aa8b0a39-45ea-4295-b244-52d6e6907bff Third-party
% Products> and <docid:gpucoder_gs#mw_453fbbd7-fd08-44a8-9113-a132ed383275
% Environment Variables>.
%
%% Verify GPU Environment
% To verify that the compilers and libraries necessary for running this
% example are set up correctly, use the
% <docid:gpucoder_ref#mw_0957d820-192f-400a-8045-0bb746a75278
% coder.checkGpuInstall> function.
envCfg = coder.gpuEnvConfig('host');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

%% Lane Detection using |houghlines| Simulink Model
% The Simulink model for lane detection is shown. 
open_system('lane_detection');
%%
% The |Lane Detection| subsystem contains a |MATLAB Function| block that 
% takes an intensity image as input and provides detected lanes as output. 
% This function is based on the lane detection algorithm implementation 
% using |houghlines| as described in 
% <docid:gpucoder_ug#example-gpucoderdemo_lane_detection_houghlines Lane 
% Detection on the GPU by Using the houghlines Function> example.
% When the model
% runs, the |Visualization| block displays the lane detected output
% image.

%% Run the Simulation
% Open Configuration Parameters dialog box.
%
% In *Simulation Target* pane, select *GPU acceleration*.
%
% <<../SimulationTargetSettings.JPG>>
%
% Run the simulation in Normal mode.
set_param('lane_detection', 'SimulationMode', 'Normal');
sim('lane_detection');
%% Generate and Build the Simulink Model
% In *Code Generation* pane, select the *Language* as *C++* and enable
% *Generate GPU code*.
%
% <<../CodeGenerationSettings.JPG>>
%
% Open *Simulation Target* pane. In the *Advanced parameters*, 
% enable *Dynamic memory allocation threshold in MATLAB functions*. 
% For more information, see
% <docid:simulink_gui#bvk1wgc Dynamic memory allocation in MATLAB functions>
%
% <<../SimulationTargetSettingsDynamicMemoryAllocation.JPG>>
%
% Open *Code Generation > GPU Code* pane. In the subcategory *Libraries*,
% enable *cuBLAS*, *cuSOLVER* and *cuFFT*.
%
% <<../GPUCodeSettings.JPG>>
%
% Generate and build the Simulink model on the host GPU by using the
% |rtwbuild| command. The code generator places the files in a _build
% folder_, a subfolder named |lane_detection_ert_rtw| under your current
% working folder.
status = evalc("rtwbuild('lane_detection')");
%% Cleanup
% Close the Simulink model.
close_system('lane_detection');