OpenCL and CUDA 10.0 on AWS With Ubuntu 18.04

Introduction

If you choose to follow these instructions you will have to initiate a P3 instance of Amazon Web Services for which you will be charged the usage fees. These fees are less than dollar for an hour. However, you may face a substantial several thousand dollars bill if you let the instance run continuously for a year without termination.

If you are creating P3 instance for the first time, you will very likely have to submit a "Limit Increase Request" to Amazon. It may take 3 days for Amazon to grant you the permission to run a P3 instance.

This document contains the explanation on how to create an Amazon p3.2xlarge instance that runs Ubuntu 18.04 on which it is possible to practice parallel programming in OpenCL and C++. It will take about 30 minutes to complete the steps outlined below. The instructions presented are adaption of instructions from CUDA 7.5 on AWS GPU Instance Running Ubuntu 14.04.

Creating a P3 instance with Ubuntu 18.04

Selection of the instance

Go to console.aws.amazon.com and click on the link "Launch virtual machine with EC2".

Select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type and make sure that the selection is made for 64-bit (x86). As of February 2019, the instance type is ami-0bbe6b35405ecebdb and the default offering by Amazon may change. The tutorial is tested on ami-0bbe6b35405ecebdb but will probably continue working for newer versions.

Choose GPU instance p3.2xlarge and click on Preview and Launch.

Setting up permissions

You will notice a warning that tells you that this instance is not free. If you are reading this tutorial, you are probably aware of the fact already.

Click on the link Edit security groups. In the column Source select My IP. If you intend to access the instance from a different IP address then you should

  • Click on Add Rule for each additional IP addresses;

  • Make sure that the first column is SSH, the selection in the last column is Custom, and that each IP address is followed by \(/32\).

After you are done, click on Preview and Launch.

Configuring the storage

You may want to add storage space to your instance. Click on Edit storage. The column Size (GiB) contains the number 8. Change it to at least \(20\) and click on Preview and Launch.

Launching the instance

Click on Launch.

In the case that you have not created a public/private keypair, you need to follow the instructions below

Limit Increase Request

It is very likely that you will need to submit a request for the increase in the instance limit. If the following message appears:

 
You have requested more instances (1) than your current instance limit of 0 allows for the specified instance type. Please visit http://aws.amazon.com/contact-us/ec2-request to request an adjustment to this limit.
then you will need to contact amazon customer support and submit a request to launch one P3.2xlarge instance. It may take 2-3 days for the request to be approved.

Logging into the instance and installation of g++ compiler

Click on Instances in EC2 console. You should see that you have 1 running instance. Click on this link. In order to log in to the instance we need to find out the SSH command. The proper SSH command must contain the public DNS and the entire command is obtained by clicking on the link Connect. The pop-up window named Connect To Your Instance will provide an example of the correct SSH command. It should look something like:

ssh -i "key_pair_for_aws_pe.pem" ubuntu@ec2-54-158-21-6.compute-1.amazonaws.com

Copy the appropriate command from the pop-up window and paste it to your terminal session. After this you may close the pop-up window. When asked about whether you are sure that you want to continue, you should write yes and press enter.

Execute the following two commands from the terminal:

sudo apt update
sudo apt install -y g++

Upgrade of the packages and DRM installation

Execute the following commands to upgrade the packages:

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo apt update
sudo apt upgrade -y
sudo apt install -y opencl-headers build-essential protobuf-compiler
sudo apt install -y libprotoc-dev libboost-all-dev libleveldb-dev hdf5-tools
sudo apt install -y libhdf5-serial-dev libopencv-core-dev libopencv-highgui-dev libsnappy-dev
sudo apt install -y libatlas-base-dev cmake libstdc++6-4.8-dbg libgoogle-glog0v5
sudo apt install -y libgoogle-glog-dev libgflags-dev liblmdb-dev git python-pip gfortran

Execute the clean utility.

sudo apt clean

Since the operating system we are running is the server version of Ubuntu 18.04 it comes without graphical user interface, and specifically without Direct Rendering Manager (DRM). Since DRM module is needed by NVIDIA drivers, we have to install it with the command

sudo apt install -y linux-image-extra-`uname -r` linux-headers-`uname -r` linux-image-`uname -r`

If you end up upgrading the kernel, you may need to re-run this command. Refer to Caffe on EC2 Ubuntu 14.04 Cuda 7 for further explanation.

Installation of CUDA

If you want to have OpenCL and/or CUDA, you must install CUDA first. The newest version is installed with the command

sudo apt install -y cuda

Testing CUDA

Run the following command

nvidia-smi

You will receive the following output:

Installation of OpenCL

The following commands install OpenCL

sudo apt install nvidia-opencl-dev
sudo apt clean
sudo apt install clinfo
clinfo

Testing OpenCL

In order to verify that the OpenCL installation was successful we will compile and run one openCL program. You first need to download the archive that contains the source code.

wget https://www.imomath.com/bmath/pcpdf/programming/MTH4300_OpenCL.tar.gz

After the download is complete, you need to unpack the archive using

tar -zxvf MTH4300_OpenCL.tar.gz

To compile and run the sample program, type the following

cd MTH4300_OpenCL/
c++ -o gcard_ex01 gcard_example01.cpp -lOpenCL -std=c++11
./gcard_ex01

Your openCL installation is successful if the file output01.txt is created according to the requirements of our Example 1 from the document Graphic cards and OpenCL