- MATLAB®
- MATLAB Coder™
- GPU Coder™
- Parallel Computing Toolbox™
- Deep Learning Toolbox™
- Image Processing Toolbox™
- An Amazon Web Services™ (AWS) account
- An SSH Key Pair for your AWS account in the US East (N. Virginia) region. For more information, see Amazon EC2 Key Pairs.
You are responsible for the cost of the AWS services used when implementing this demo. Resource settings, such as instance type, will affect the cost of deployment. The AMI used for this guide, with the recommended instance type costs $1/hour. For cost estimates, see the pricing pages for each AWS service you will be using. Prices are subject to change.
The following guide demonstrates how to generate CUDA code using GPU Coder, build and run an executable on an EC2 instance.
- If you don't have an AWS account, create one at https://aws.amazon.com by following the on-screen instructions.
- Use the regions selector in the navigation bar to choose the US-EAST (N. Virginia) region where you want to deploy MATLAB.
- Create a key pair in that region. The key pair is necessary as it is the only way to connect to the instance as an administrator.
- If necessary, request a service limit increase for the Amazon EC2 instance type or VPCs. You might need to do this if you already have existing deployments that use that instance type or you think you might exceed the default limit with this deployment.
- Write an entry point function in MATLAB
- Generate a static library using GPU Coder
- Setup AWS environment
- Deploy generated code to an S3 bucket
- Deploy code in S3 bucket to an EC2 instance(s) or AutoScaling Group
- Build executable on EC2 instance
- Create a simple web app to interact with the executable
Please see the MATLAB script alexnet_predict.m
for more information
Please see the MATLAB script test_codegen.m
for more information
AWS component | steps |
---|---|
IAM Roles |
|
EC2 Instance(s) | |
S3 Bucket |
|
CodeDeploy | You can also use scp to upload the code generated files. However, AWS CodeDeploy is a free service that allows you to deploy applications from your development machine to one or more EC2 instances at once.
|
- You need to do this from the command line using the AWS CLI
- Please follow steps here to set up the AWS CLI on your development machine
- Place your codegen directory and the appspec.yml file in one directory and navigate to the parent directory from the command line
- The directory structure would look like:
- temp
- codegen
- appspec.yml
- temp
- Execute the following command in the command line:
aws deploy push --application-name <name of your CodeDeploy App> --s3-location s3://codegens3/codegen.zip --ignore-hidden-files
- Create a YAML file named ‘appspec.yml’ in the same directory that contains the codegen directory
- This file tells AWS CodeDeploy the source and destination of your files
- An example appspec.yml file is shown here
- The deployment will fail if:
- YAML file has incorrect syntax
- You don’t have path permissions at the destination
- Click on ‘Create deployment’
- Select S3 under Revision type
- The revision location would be:
s3://your-bucket-name/codegen.zip
- Complete by clicking on ‘Create deployment’
- SSH into the EC2 instance using the instructions here
- Navigate to the directory containing all the code generated files
- Find and edit the make file(.mk extension) to update the paths to the source files
- Execute the following commands to create the static library using the updated paths:
make -f <path to make file> clean
followed bymake -f <path to make file>
- Execute the following command from the command line:
nvcc -arch sm_35 -o classifier relative path/to/main.cu relative path/to/inputFile.a -I<relative path to codegen directory><br/> -L"./<relative path to codegen directory>" -lmwjpegreader -lcudart -lcudnn -lcudart -lcublas
lmwjpegreader
is needed if you are using theimread
MATLAB function in your MATLAB source code. Please see this documentation page for more information on how to use image processing functions in code generation. Alternatively, you can also use the OpenCV library to read images.- Please see this documentation page for more information on
nvcc
.
- Install Apache web server on EC2 instance:
sudo apt-get install apache2
- We are using PHP, to install PHP:
sudo apt-get install php libapache2-mod-php
- Change server root to
/var/www/
by editing file/etc/apache2/sites-enabled/ 000-default.conf
- Add server root folder permissions for the user:
sudo chmod -R g+rw /var/www
- Restart server:
sudo service apache2 restart
- Create a dummy php file in the server folder and check if the server is running by going to
<EC2 public DNS>/filename.php
- Place the files in
var/www
from the repository in your server root - The single page PHP webapp will let the user upload a JPEG file
- Clicking on the predict button will call the CUDA executable
- The webapp will parse and display the classification result