Tuesday, August 1, 2017

Installing CUDA on Linux Mint and Hello CUDA Program

CUDA® is a parallel computing platform and programming model invented by NVIDIA. In case you wanted to write parellel programs that runs on GPU. CUDA will be helpful. 
 Things that are need for CUDA programming.
  1. A machine with Nvidia Graphics Card
  2. A Supported Linux, Kernel and GCC  [refer here]
  3. NVIDIA CUDA Toolkit
My System Specs is the following: 

Dell Inspiron. It has a Nvidia Geforce grapics card.


Installation steps:
WARNING- Read the step completely before starting. I ran into issues with some of these step. Only for expert users or else you may have to endup reinstalling you OS.

1. Go here and download NVIDIA CUDA Toolkit deb/runfile. I am using the deb file. It is about 2GB. So, be careful if you got lesser bandwidth or Internet quota.


2. To check if you have a CUDA compatible GPU, version of kernel and gcc.  
rajz@warriorwithin ~ $ lspci | grep -i nvidia
08:00.0 3D controller: NVIDIA Corporation GM108M [GeForce 840M] (rev a2)
rajz@warriorwithin ~ $ uname -a
Linux warriorwithin 4.4.0-71-generic #92-Ubuntu SMP Fri Mar 24 12:59:01 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
rajz@warriorwithin ~ $ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

3. If nouveau is enabled, then we need to disable it. If the below command prints details which means that it is active. So, just disable it by doing the steps below.
rajz@warriorwithin ~ $ lsmod | grep nouveau
rajz@warriorwithin ~ $ sudo vi /etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
options nouveau modeset=0
add above two lines and run the below command.
sudo update-initramfs -u
4. Reboot!!
5.  lsmod | grep nouveau to check if it is active.
6.  Then run the following command based on your file in Step 1. Mine was the following
rajz@warriorwithin ~/install $ dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
7. Run this sudo apt-get update
8. Run this sudo apt-get install cuda
9. Reboot!!
10. Open  ~/.bashrc file and Add the two lines at the end of file
11. Created a folder under Documents(or wherever you wish) to Install sample files and Run the below
rajz@warriorwithin ~/Documents $ cuda-install-samples-8.0.sh cuda-samples/
12. cd into ~/Documents/cuda-samples/NVIDIA_CUDA-8.0_Samples and do make
13.  Then do the following



14. If it shows up like in the figure. Then it is all fine. 
15. So, far so good. Next up is Hello World CUDA program  


I am unsure if the step 3, 4, 5 are really necessary. But still did it for completeness sake.

ALERT!!!
After Doing these steps. My system went blank after a restart!!!!!
I figured it out that My display is by Intel Graphics Controller.  Where as after NVIDIA Driver installation it set nvidia as my default display or card. So, I was getting a blank screen; Grub loaded. Linux mint Logo appeared. Even login happened. but I can't see any thing. even TTY didn't work. 

Workaround to fix this.
1. During Grub loading. Press 'e'. search a line and prefix nomodeset before quick splash and press enter.
2. It will take you to login. But cinnamon may crash. 
3. Using terminal Uninstall all drivers of nvidia sudo apt-get remove nvidia** 
4. reboot. I reverted the changes I did in step 3,4 of prev installation.
5. Get to know you GPU model from lspci cmd
6. Download http://www.nvidia.com/Download/index.aspx?lang=en-us your driver based on your MODEL, and OS; Mine was this
7. Go to TTY by pressing CTRL+ALT +F1
8. Use cmd to stop display using Card[on Linuxmint] using sudo service mdm stop 
8.1 For ubuntu OS; use sudo service lightdm stop 
9. sudo ./NVIDIA-Linux-x86_64-384.59.run --no-opengl-files
 follow instruction
9.1 I said no only for 32 bit libs; Rest say positively.
10. sudo service mdm start will get you back to GUI mode.
11. Reboot! huh! Hoping that it won't go blank!

Cheers,

Let me know if these steps doesn't work or even if it works! Hope it helps!

References
[1] - www.docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
[2] - https://askubuntu.com/questions/799184/how-can-i-install-cuda-on-ubuntu-16-04
[3] - https://kislayabhi.github.io/Installing_CUDA_with_Ubuntu/

Saturday, May 13, 2017

Installing Gitlab Server

Let me walk through the steps.
  1. sudo apt-get install curl openssh-server ca-certificates
  2. curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash 
  3. sudo apt-get install gitlab-ce
  4. In file /etc/gitlab/gitlab.rb
    1. nginx['listen_port'] = 8081
    2. external_url 'http://mithun.cse.iitm.ac.in' 
  5. sudo gitlab-ctl reconfigure
  6. Go to localhost or mithun.cse.iitm.ac.in to set to root's password.
  • Note: We tried setting up as gitlab.mithun.cse.iitm.ac.in but wasn't able to as mithun.cse.iitm.ac.in was already running an apache2 webserver from 80 port.
  • Note: We decided to run on 8081 instead
  • Note: We could have setup as "http://mithun.cse.iitm.ac.in/gitlab" --  I was not in favour of it.
If someone knows how to setup gitlab it as subdomain without affecting the existing webserver. Do let us know. Thank you.

Install custom vimrc file

There are basically only two step. Download and copy it in your home dir and save it as .vimrc. But let me walk thorough.

  1. cd ~
  2. wget https://raw.githubusercontent.com/pallavagarwal07/_vim/master/_vimrc
  3. mv _vimrc .vimrc
  4. vim
  5. :PlugInstall
If there is error in the 4th step. Check that you have git as well as curl installed already. Do you get error still?, then.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
 
 
And repeat the steps 4 and 5.

Thanks to Pallav


Saturday, April 1, 2017

Resolve Linux screen freeze due to external monitor

My Laptop has  Linux mint 18 installed. Here is the thing I wanted to do. There is an extra monitor I wanted use it as a secondary screen (or dual display) and set the monitor's display as my primary screen.

However, the max resolution of the laptop(1366x768 16:9 ) and max resolution of the the monitor(1366x768 16:9 ) are different. So, whenever I connect the VGA cable of the monitor to my laptop(through an HDMI converter), it was just mirroring the screen with a low resolution 1024x768. And when is change the resolution to 1366x768 16:9 (or any other) the screen freezes. And you can not do anything. Either you can hard shutdown or reboot from here. Only the mouse pointer movement is seen but the clicks are not recognized . Keyboad's cap/num lights works but keys don't work. Only thing I can do is long press the power button till it is shutdown. Other option is to go to tty(Ctrl + Alt + F1 ) and login to reboot it. This seems like a bug. But whose OS/Kernal /Cinnamon - don't know the root cause.

These are my atttempts / understanding:
  • Updating Linux mint - did NOT work.
  • Updating Kernal - did NOT work.
  • The secondary monitor is not hot pluggable in Linux mint(or in general Linux or due to HDMI - VGA convertion -- I don't understand it yet). 
  • Finally the below steps did work.
Steps to solve the issue.
  • Set the secondary monitor to primary in display option.
  • Shutdown the machine.
  • Make the VGA-HDMI connected to the machine.
  • Start the machine.
  • Go to the display option, changed the external monitor to max resolution [1366x768 16:9]. 
  • Cool. All done and Enjoy the large screen ;-)  

Monday, January 23, 2017

Installing and Using LLVM Clang on Linux

Here are the specs (Hardware and software) of my machine
RAM: 8GB
Proc : Intel Core i7-4510U @ 2.00GHz
Linux mint 18 Cinnamon
word size - 64 bit
Kernel - 4.4.0-21-generic
swap Size - 10GB
HDD :  150GB

There are two ways of installing
1. Install from apt-get
2. Building from src files using tar.gz
Make sure to check this requirements.
GNU Make3.79, 3.79.1 Makefile/build processor
GCC >=4.8.0 C/C++ compiler
python >=2.7 Automated test suite
zlib >=1.2.3.4 Compression library //it worked without this

1. Install from apt-get

 $ sudo apt-get clang-3.9 llvm-3.9

Reference:  http://apt.llvm.org

2.  Building LLVM 3.9.1 from Source

  1. Download two tar file for llvm & clang from here
  2. Extract both the files inside some folder // say llvm
  3. Rename llvm-3.9.1.src folder as llvm-3.9 //LLVM_SRC
  4. Rename cfe-3.9.1.src folder as clang and move inside folder llvm-3.9/tools
  5. Create a folder llvm/llvm-build and cd llvm-build // LLVM_OBJ
  6. cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On /home/rajz/install/llvm/llvm-3.9
  7. // after some time
  8. make -j2
  9. //after some more time
  10. sudo make install
  11. opt --version 
  12. clang --version 
  13. //Done
References: 
1. http://llvm.org/docs/GettingStarted.html
2. https://www.youtube.com/watch?v=4iVc204omI0
3. http://llvm.org/docs/CMake.html

Writing a Sample Pass using LLVM
1. Source folder for LLVM:
    ~/install/llvm/llvm-3.9
2. Build folder for LLVM:
    ~/install/llvm/llvm-build
3. Create a CMakeLists.txt file as below
   add_llvm_loadable_module( LLVMSample
        Sample.cpp
        PLUGIN_TOOL
        opt
        )
4. Create the Sample.cpp as below
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Sample : public FunctionPass {
  static char ID;
  Sample() : FunctionPass(ID) {}

  bool runOnFunction(Function &F) override {
    errs() << "Sample: ";
    errs().write_escaped(F.getName()) << '\n';
    return false;
  }
}; // end of struct Sample
} // end of anonymous namespace

char Sample::ID = 0;
static RegisterPass X("sample", "Sample Hello World Pass ",
                              false /* Only looks at CFG */,
                              false /* Analysis Pass */);
5. Sample pass folders at:
    ~/install/llvm/llvm-3.9/lib/Analysis/Sample (contains Sample.cpp and CMakeLists.txt)
    ~/install/llvm/llvm-build/lib/Analysis/Sample (contains only the CMakeLists.txt)
6. Append a line in ~/install/llvm/llvm-3.9/lib/Analysis/CMakeLists.txt

       add_subdirectory(Sample)

7. Created shared library at ~/install/llvm/llvm-build/lib/LLVMSample.so

       $ cd ~/install/llvm/llvm-build
       $ make

8. To run analysis on a test1.c program:
    $ clang -S -O0 -emit-llvm test1.c -o test1.ll
    $ opt -load ~/install/llvm/llvm-build/lib/LLVMSample.so -disable-output -sample test1.ll

Reference
1. http://llvm.org/docs/WritingAnLLVMPass.html
2. AMR, PA TAs

Sunday, November 6, 2016

Challenges in Linux Mint 18 Cinnamon installation

The scenario is I have a Dell Inspiron machine dual booting Window 8.1 and Debian 8. I wanted to replace the Debian with Linux Mint 18 Cinnamon.


  1. Download the ISO file (64bit) from https://linuxmint.com/download.php
  2. Verify the downloaded ISO file from  https ://linuxmint.com/verify.php
  3. Press F2 during boot screen and make the fast boot to OFF & save and exit.
  4. Press F12 during boot screen and make the boot mode as UEFI and Safeboot off.
  5. Using Unetbootin create a bootable ISO using the USB disk.
  6. In Windows > Settings > System &recovery > Using your Pendrive > restart
  7. It boots into Mint 18, Install option 
  8. At some step it asks for partition choose "something else"
  9. Delete the existing "/" partition and "/home" using Minus "-" 
  10. Create partition "/" and "/home" using "+" make sure that format is checked
  11. After installation grub did not appear and it boots into windows directly.
  12. bcdedit /set {bootmgr} path \EFI\mint\grubx64.efi   did not work
  13.  Attempted this http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd#.UkpRK4Z6Y6Q  
  14. grub-install /dev/sdX  showed error, did not work - showed efi error
  15. Then I installed boot-repair from LIVE CD terminal as told here https://help.ubuntu.com/community/Boot-Repair. Run it with "Recommended repair"
  16. That's it. It fixed everything(I should have done it before itself). Here is a view of the Workspace. 


Thursday, May 19, 2016

Debian and it's love ...!

I have been using a lot of Linux/based distribution during my UG days. It all started with installing solaris on my 512MB RAM machine after an installation demo by CSE seniors(Krithika, Balachandran and others) . Then, I went on to Ubuntu, Fedora, OpenSUSE, Debian for reasons during my project work. Of all, my Favorite is Debian for their less memory or/and power consumption with simple neat looks. 

During my PG days I installed Ubuntu 14.04 and were using for the past 2 years (almost). Now that I wanted to get back to my fav Debian and went into so many issues which I haven't faced during those days. Moreover, hardware/software itself got too complicated with BIOS/UEFI/Legacy mode/Safeboot/Fastboot/Win8/Win10. 

Objective, I wanted to replace Ubuntu with Debian and still use it a dual boot alongside windows. Specs: Debian 8.4, Dell Inspiron 3000 series, I7, 8GB RAM

Initial steps 
0. To live boot F12 key at boot, I have to choose boot from pendrive under Legacy option in boot menu options. In UEFI mode with safeboot OFF already.
1. Downloaded Debian with Mate desktop environment[DE] as I preferred Mate over other DE than cinnamon, current gnome, KDE, etc
2. I installed with manual partitioning. Formatted the existing ubuntu "/" and "/home" and then while writing to boot loader it said Ubuntu detected. OMG. windoz? :-/ 
3. I went ahead and completed. As I booted, I could see only grub prompt. However, when I live booted all my partitions are safe.

Errors and steps taken to resolve
4.
grub>  ls
hd0 (hd0,gpt12) (hd0,gpt11) (hd0,gpt9) (hd0,gpt8) ...

It will list the partitions in Harddisk


5. Find out where "/"  of linux files is installed by pressing "tab"

press tab after typing as below..
grub> (hd0,gpt11)/
/bin /boot /etc /opt /home /root /sys lost+found  ...

you can figure out what for you, for me it is 11 here in this example. It is used below        

6.
grub>  set boot=(hd0,gpt11)
grub>  set prefix=(hd0,gpt11)/boot/grub
grub>  insmod normal  
grub>  normal

This will make the grub OS chooser window to appear. But it showed only Debian. Where is the windoz ? :-/

7.
Do the below in termial after login to debain
# grub-mkdevicemap
# update-grub
# grub-install /dev/sdX

If these 3 steps are complete then you are done!!

8.
find out X as below
​root@warriorwithin:~# lsblk -f
NAME    FSTYPE LABEL      UUID                                 MOUNTPOINT
sda                                                            
├─sda1  vfat   ESP        CAE4-7B30                            
├─sda2  vfat   DIAGS      F640-9997                            
├─sda3                                                         
├─sda4  ntfs   WINRETOOLS 8A1442471442370B                     
├─sda5  ntfs   OS         7C80471B8046DB72                     
├─sda6  ntfs   New Volume 0604B4F304B4E6B9                     
├─sda7  ntfs   New Volume 1EA62DCBA62DA3ED                     
├─sda8  ntfs   New Volume 24786E8A786E5B16                     
├─sda9  ntfs   PBR Image  A2E878FBE878CED3                     
├─sda10 swap              f594b846-2211-4e70-bbcd-f97a6e05e036 [SWAP]
├─sda11 ext4              32ae4ff5-10fd-4992-a70d-21b07dfce9da /
└─sda12 ext4              331172f1-2eea-4589-a5ef-c1ab2f4975e0 /home

X for me is "a"  

9.
root@warriorwithin:~# grub-install /dev/sda
Installing for x86_64-efi platform.
grub-install: error: cannot find EFI directory.

what to do now? :-(

10.
Check if /sys/firmware/efi exists if so then mount as below. And we know that step 8 ESP is in sda1 we will mount that in efi
root@warriorwithin:~# mkdir -p /boot/efi
root@warriorwithin:~# mount /dev/sda1 /boot/efi

11.
root@warriorwithin:~# grub-install /dev/sda
Installing for x86_64-efi platform.
Installation finished. No error reported.
root@warriorwithin:~# 

All Done!!
Cheers




Installing CUDA on Linux Mint and Hello CUDA Program

CUDA ® is a parallel computing platform and programming model invented by NVIDIA. In case you wanted to write parellel programs that runs ...