Saturday 3 October 2015

Installation of CUDA Toolkit on Ubuntu 20.04 LTS

About this tutorial

This tutorial will guide you about installation steps for CUDA parallel programming
architecture.

Introduction

CUDA (Compute Unified Device Architecture) is technology developed by NVIDIA Inc. 
The purpose of CUDA is to execute the programs and instructions in parallel manner on 
the processor. 

Prerequisites

Installation

After completing download and installation of Ubuntu, follow the next steps.
1. Log in as root user
2. Update Ubuntu for latest packages

# apt-get update

3. To install compiler, essential for CUDA toolkit, run following command:

# apt-get install build-essential

Some desktop version of Linux might cause problems such as slow response, 
unresponsive GUI. In order to avoid such problems, we need to switch to text mode.

4. To switch to text mode, use keyboard shortcut:  Alt+Ctrl+F1
5. Login as root.
6. Stop GUI service, use following command:
# service lightdm stop
7. Go to folder location of CUDA toolkit file
# cd /home/user_name/Downloads/

8. Download CUDA toolkit
# wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda_11.4.0_470.42.01_linux.run

9. Change the permission of file and run following command
# sudo sh cuda_11.4.0_470.42.01_linux.run

If file is not executable, try to run it with following command:
# ./cuda_11.4.0_470.42.01_linux.run

10. If prompt of absolute path of CUDA toolkit appear on screen, press Y
11. For /home/<user_name> option, where <user_name> will be your current name, press 'Enter'.

You'll see installation skipped then reboot with
# reboot -h

After restart, Linux will automatically switch to GUI mode. 
Repeat the steps from 4 to 9 to switch back from GUI to text mode. Now reboot system manually.

After system will start, enter two commands as general user:

# export PATH=/usr/local/cuda-11.4/bin:$PATH
# export LD_LIBRARY=/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH

Now we are ready to run our hello world program for Nvidia CUDA.

Copy and paste Hello World example in gedit.
.cu is the extension of our program.
Save to current directory.
To compile:  
# nvcc -cuda hello.cu
To run:      
# ./a.out

If you can see Hello World as output, you've successfully installed CUDA toolkit on Ubuntu.

Installing FreeGLUT in CodeBlock



Install FreeGLUT (Be Careful )

1.       Download : http://www.transmissionzero.co.uk/software/freeglut-devel/
From above location, download freeglut-mingw.zip
Extract the file in any folder.

2.       Now create two copies of GNU GCC Compiler.

a.       First is 32 bits and copy following files as directed:
        
File
From (FreeGLUT\)
To(MinGW32\)
freeglut.dll
bin
bin and C:\Windows\System32\
Freeglut.h
Freeglut_ext.h
Freeglut_std.h
glut.h  (optional as in previous step)
include\GL
include\GL
libfreeglut.a
libfreeglut_static.a
lib
lib

b.      Second one, 64 bits is other copy for it do as follows:

      File
From (FreeGLUT\)
To(MinGW64\)
freeglut.dll
bin\x64
bin and C:\Windows\SysWOW64\
Freeglut.h
Freeglut_ext.h
Freeglut_std.h
glut.h
include\GL
include\GL
libfreeglut.a
libfreeglut_static.a
lib\x64
lib

3.       Now move to settings of Compiler in CB.
a.  For 32 bits Compiler version,

Don’t forget to update Toolchain Executables as per need ,here, C:\TDM-GCC-32

Linker Settings =>Link Libraries
C:\TDM-GCC-32\lib\libfreeglut.a
C:\TDM-GCC-32\lib\libfreeglut_static.a

In “Other Linker Options”
-lOpenGL32 -lglu32 –lfreeglut

Search Directories Tab => Compiler
C:\TDM-GCC-32\include
Search Directories Tab => Linker
C:\TDM-GCC-32\lib

b.  For 64 bits Compiler version,
Don’t forget to update Toolchain Executables as per need ,here, C:\TDM-GCC-64

For toolchain Executables (in order of appearance)
gcc.exe
g++.exe
g++.exe
ar.exe
windres.exe
mingw32-make.exe

Linker Settings =>Link Libraries
C:\TDM-GCC-64\lib\libfreeglut.a
C:\TDM-GCC-64\lib\libfreeglut_static.a

In “Other Linker Options”
-lOpenGL32 -lglu32 –lfreeglut

Search Directories Tab => Compiler
C:\TDM-GCC-64\include
Search Directories Tab => Linker
                  C:\TDM-GCC-64\lib

4.       Now, create empty project with choosing compiler suitable to your OS. Right click on Project and go to the project properites and Build Targets => build type => GUI application
5.       Now download and extract this zip file:
6.       And place *.h and *.cpp files into CB project locations.
7.       Now again confirm Compiler from Rt Click on project => Build options.
8.       Now rebuild => build => run.

                You will get this output:

Installing GLUT in CodeBlock



For GLUT:
Considering we are having Codeblock and MinGW compiler,
      1.      Download this file: http://www.deannicholls.co.uk/site/files/glut.zip
      2.      Now perform following steps:
Extract the 'Glut.zip' file you downloaded and do the following:-

Copy glut32.dll into your 'C:\Windows\System32' folder (If you're using Windows 7 64-bit, you'll need to copy this file into 'C:\Windows\sysWOW64').

Copy glut.h into the ‘include\GL’ folder in the MinGW compiler folder.This folder will be 'C:\MinGW\include\GL'.

Copy libglut32.a into the 'MinGW\lib' folder. If you installed CodeBlocks and MinGW to the default directory in Step 3, this folder will be 'C:\Program Files\CodeBlocks\MinGW\lib'.

      3.      Go to CB=>Settings=> Compiler => Copy your “GNU GCC Compiler”
      4.      Rename your new compiler.
      5.      In Linker Settings => Link Libraries, add the following lines:
opengl32
glu32
glut32

      6.      Your GLUT compiler is ready and its time to execute program:


 // Created by: Dean Nicholls  
 // Date: 18th June 2010  
 // Created For: http://www.levelbylevel.com  
 //  
 // This is a small OpenGL application which will display a  
 // blue sphere on a black background.  
 //  
 // This Program was created to acompany the tutorial  
 // for setting up CodeBlocks, MinGW, OpenGL and GLUT.  
 // Found at: http://www.levelbylevel.com  
 //  
 // It is not intended to be a guide or even a good  
 // template for building a graphic application. It is  
 // therefore not properly commented or efficient.  
 // However feel free to use this code in any way you wish.  
 #include <iostream>  
 #include <GL/glut.h>  
 using namespace std;  
 GLfloat mat_ambient[] = {0.5, 0.5, 0.5, 1.0};  
 GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};  
 GLfloat mat_shininess[] = {50.0};  
 GLfloat light_position[] = {10.0, 10.0, 10.0, 0.0};  
 GLfloat model_ambient[] = {1.0, 0.5, 0.5, 1.0};  
 void setupMaterials() {  
      glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);  
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);  
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);  
      glLightfv(GL_LIGHT0, GL_POSITION, light_position);  
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);  
 }  
 void changeColour(GLfloat r, GLfloat g, GLfloat b, GLfloat A) {  
      model_ambient[0] = r;  
      model_ambient[1] = g;  
      model_ambient[2] = b;  
      model_ambient[3] = A;  
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);  
 }  
 void init(void) {  
      glClearColor(0.0, 0.0, 0.0, 1.0);  
      setupMaterials();  
      glEnable(GL_LIGHTING);  
      glEnable(GL_LIGHT0);  
      glEnable(GL_DEPTH_TEST);  
      glEnable(GL_CULL_FACE);  
      glFrontFace(GL_CCW);  
      glShadeModel(GL_SMOOTH);  
 }  
 void reshape(int w, int h) {  
      GLfloat fAspect;  
      if(h==0) h=1;  
      glViewport(0,0,w,h);  
      fAspect = (GLfloat)w / (GLfloat)h;  
      glMatrixMode(GL_PROJECTION);  
      glLoadIdentity();  
      gluPerspective(60, fAspect, 0.5, 100.0);  
      glTranslatef(0,0,-1);  
      glMatrixMode(GL_MODELVIEW);  
      glLoadIdentity();  
 }  
 void display(void) {  
      int slices = 30;  
      int stacks = 30;  
      float radius = 0.2;  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
   glPushMatrix();  
     changeColour(0.0, 0.0, 1.0, 1.0);  
     glTranslatef(0.0, 0.0, 0.1);  
     glutSolidSphere(radius, slices, stacks);  
   glPopMatrix();  
      glFlush();  
   glutSwapBuffers();  
 }  
 void keyboard(unsigned char key, int x, int y) {  
      switch (key) {  
           case 27:  
                exit(0); // Exit the application if 'Esc' key is pressed  
      }  
 }  
 void animate() {  
      glutPostRedisplay();  
 }  
 int main(int argc, char * argv[]) {  
   glutInit(&argc, argv);  
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
   glutInitWindowSize (800, 600);  
   glutCreateWindow (argv[0]);  init();  
   glutKeyboardFunc (keyboard);  
   glutDisplayFunc (display);  
   glutReshapeFunc (reshape);  
   glutIdleFunc(animate);  
   glutMainLoop();  
   return 0;  
 }  

7.       Create empty project with new compiler as its current compiler.
8.       Now extract  deannicholls_glut_tutorial_main.zip and paste main.cpp inside the location of CB project.
9.       Now, confirm again by setting compiler from Build Options.
10.   After that, rebuild => build => run.