%% GPU Code Generation for a Fog Rectification Simulink Model
% This example demonstrates how to generate CUDA(R) code from the
% Simulink(R) model that takes a foggy image as input and produces 
% a defogged image as output.  This example is a typical implementation of 
% fog rectification algorithm. The example uses <docid:matlab_ref#bvgtez6 conv2>, 
% <docid:matlab_ref#buiz8mj-1 rgb2gray>, and <docid:images_ref#buo3qek-1 imhist>
% functions. This example closely follows 
% <docid:gpucoder_ug#example-gpucoderdemo_fog_rectification Fog
% Rectification> example.
% This example illustrates the following concepts:
%
% * Verification of GPU Environment.
% * Model fog rectification 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);

%% Fog Rectification Simulink Model
% The Simulink model for fog rectification consists of |Fog Rectification| 
% subsystem that contains a |MATLAB Function| block which
% takes a foggy image as input and returns a defogged image as output.
% It uses |fog_rectification| algorithm described in 
% <docid:gpucoder_ug#example-gpucoderdemo_fog_rectification Fog
% Rectification> example. When the model runs, 
% the |Visualization| block displays the foggy input image and defogged output image.
open_system('fog_rectification_model');

%% Run model on the Simulation Target
% Open Configuration Parameters dialog box.
%
% In *Simulation Target* pane, select *GPU acceleration*.
%
% <<../SimulationTargetSettings.JPG>>
%
% Run the Simulation in Normal mode.
set_param('fog_rectification_model', 'SimulationMode', 'Normal');
sim('fog_rectification_model');
%% Generate and Build the Simulink Model
% In *Code Generation* pane, select the *Language* as *C++* and enable
% *Generate GPU code*.
%
% <<../CodeGenerationSettings.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 |fog_rectification_model_ert_rtw| under your current
% working folder.
status = evalc("rtwbuild('fog_rectification_model')");
%% Cleanup
% Close the Simulink model.
close_system('fog_rectification_model');