OK, so I have purchased my desktop system with dedicated GPU! The last time I purchased a system with dGPU was back in 1990s, so it's been about 20 years. I bought one because I wanted to study tensorflow / keras, and I simply couldn't do it with just CPUs, so I had to get NVIDIA GPU.
Anyways, I noticed that Ubuntu doesn't install its driver automatically, so here is how to do so manually. First, download CUDA from NVIDIA here. As of now, the latest version is CUDA 8.0. If you are going to use tensorflow, make sure it supports the version of CUDA you are going to download from the official documentation page.
Follow the instruction on NVIDIA download page to install. I am going to download local runfile for Ubuntu 16.04. For this option, I need to run
$ sudo sh cuda_8.0.61_375.26_linux.run
***
Note that the runfile will probably not work unless you make sure 2 things:
1. Install gcc on your system
$ sudo apt-get install build-essential
2. Run it in console mode. Follow the answer by Rey on this post for details.
***
Download cuDNN from here. I am going to download cuDNN v5.1 Library for Linux for CUDA 8.0. Decompress the file into /usr/local/cuda/cudnn folder:
$ tar xfz cudnn-8.0-linux-x64-v5.1.tgz -d cudnn
$ sudo mv cudnn /usr/local/cuda/cudnn
Next, add library paths by creating /etc/ld.so.conf.d/cuda.conf file with the following lines:
/usr/local/cuda/lib64/
/usr/local/cuda/cudnn/lib64/
Refresh ld cache by
$ sudo ldconfig
Finally, install tensorflow-gpu. I am going to use pip:
$ sudo apt-get install python-pip
$ pip install tensorflow-gpu
Now, when you run the following command, you should see all CUDA library opened successfully:
$ python -c 'import tensorflow'
Happy hacking!
Friday, March 31, 2017
Setup XRDP Server with Ubuntu Mate
I want to setup a server that runs Ubuntu Mate (not Ubuntu Server). Although I will usually only connect via ssh, sometimes I may want to remote into Mate desktop. Here is how to configure the server.
First, fresh install Ubuntu Mate. You can download it from here. I used unetbootin to create a bootable USB drive using the downloaded iso file (16.04 LTS).
Once Ubuntu Mate is installed on your server, you will need to update packages first:
$ sudo apt-get update
$ sudo apt-get upgrade
Next, install xrdp:
$ sudo apt-get install xrdp
That's it! You can now connect to your server from Windows/Mac/Linux clients.
From Mac clients, install Microsoft Remote Desktop from here. Add a desktop with IP and username and password to connect to. To find your server's IP address, type the following from the server:
$ hostname -I
If you are not going to use the server locally, you may want to run the server int he console mode to save some system resources. Take a look at this post for how to do so.
First, fresh install Ubuntu Mate. You can download it from here. I used unetbootin to create a bootable USB drive using the downloaded iso file (16.04 LTS).
Once Ubuntu Mate is installed on your server, you will need to update packages first:
$ sudo apt-get update
$ sudo apt-get upgrade
Next, install xrdp:
$ sudo apt-get install xrdp
That's it! You can now connect to your server from Windows/Mac/Linux clients.
From Mac clients, install Microsoft Remote Desktop from here. Add a desktop with IP and username and password to connect to. To find your server's IP address, type the following from the server:
$ hostname -I
If you are not going to use the server locally, you may want to run the server int he console mode to save some system resources. Take a look at this post for how to do so.
How to Resize Virtual Disk
In this post, I will discus how to resize virtual disk using VirtualBox.
Say you have a virtual disk file ~/disk.vdi that you would like to resize. For Linux, run
$ vboxmanage modifyhd ~/disk.vdi --resize 100000
For Mac OS X, run
$ VBoxManage modifyhd ~/disk.vdi --resize 100000
where I am assuming that you have VirtualBox installed on your system. Note that the size number 100000 after --resize command is in KB, so the number 100000 indicates 100GB.
Note that if you have snapshots of your virtual disk, you also need to resize your snapshot disks as well. Otherwise, the VirtualBox won't notice the disk has been resized.
After you are done resizing the virtual disk, you may need to edit your disk partition so that the guest OS can fully manage the resized disk.
Say you have a virtual disk file ~/disk.vdi that you would like to resize. For Linux, run
$ vboxmanage modifyhd ~/disk.vdi --resize 100000
For Mac OS X, run
$ VBoxManage modifyhd ~/disk.vdi --resize 100000
Note that if you have snapshots of your virtual disk, you also need to resize your snapshot disks as well. Otherwise, the VirtualBox won't notice the disk has been resized.
After you are done resizing the virtual disk, you may need to edit your disk partition so that the guest OS can fully manage the resized disk.
Wednesday, March 22, 2017
How to Install YouCompleteMe Vim Plugin for Linux
***If you are like me, using vim as the main editor for coding, then I highly recommend that you read this post.
Here, I will go through a step by step guide for installing YouCompleteMe plugin for vim with C-family support, i.e., C/C++/Objective-C/Objective-C++. Detailed instruction is given in its Github repo, but it took me quite a while to figure out how to do this right, so I just think it will be helpful to other people as well.
First, you will need to install Vundle. Run the following:
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Next, copy and paste the following at the top of ~/.vimrc file:
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
Next, install Vundle and YouCompleteMe in vim. To do this, open up vim and install plugins:
$ vim
:PluginInstall
This command within vim will install Vundle and YouCompleteMe plugins directly from the Github repositories, so make sure that you have Internet connection. Wait until it says "Done" at the bottom of vim.
You are not done yet. You will need to install necessary packages to configure YouCompleteMe.
$ sudo apt-get install build-essential cmake python-dev python3-dev
Next, you will need to change directory to where the YouCompleteMe is installed and setup clang-completion for C-family:
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --clang-completer
This will take a while, so be patient. When this is done, you are done with the installation, but you are not done with C-family auto-completion features just yet. For more info, you will need to read the official documentation regarding the part.
Basically, you will need to let clang know how to compile your project, so that it can suggest auto-completion methods or fields. If your simply want to skip all and see it in action, then create ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py with the following:
def FlagsForFile( filename, **kwargs ):
return {
'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
}
Also, append the following line to ~/.vimrc
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
Now, start editing a C++ file in the same directory with vim
$ vim hello.cpp
As you edit, you will see YouCompleteMe in action with auto-completion for C++ functions!
For Python development, I have a dedicated post here too. By the way, you may wish to read here when using Python modules. It explains how to set the python executable path.
Also, if you are a Mac user, you may want to checkout this post by Oliver.
Here, I will go through a step by step guide for installing YouCompleteMe plugin for vim with C-family support, i.e., C/C++/Objective-C/Objective-C++. Detailed instruction is given in its Github repo, but it took me quite a while to figure out how to do this right, so I just think it will be helpful to other people as well.
First, you will need to install Vundle. Run the following:
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Next, copy and paste the following at the top of ~/.vimrc file:
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
Next, install Vundle and YouCompleteMe in vim. To do this, open up vim and install plugins:
$ vim
:PluginInstall
This command within vim will install Vundle and YouCompleteMe plugins directly from the Github repositories, so make sure that you have Internet connection. Wait until it says "Done" at the bottom of vim.
You are not done yet. You will need to install necessary packages to configure YouCompleteMe.
$ sudo apt-get install build-essential cmake python-dev python3-dev
Next, you will need to change directory to where the YouCompleteMe is installed and setup clang-completion for C-family:
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --clang-completer
This will take a while, so be patient. When this is done, you are done with the installation, but you are not done with C-family auto-completion features just yet. For more info, you will need to read the official documentation regarding the part.
Basically, you will need to let clang know how to compile your project, so that it can suggest auto-completion methods or fields. If your simply want to skip all and see it in action, then create ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py with the following:
def FlagsForFile( filename, **kwargs ):
return {
'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
}
Also, append the following line to ~/.vimrc
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
Now, start editing a C++ file in the same directory with vim
$ vim hello.cpp
As you edit, you will see YouCompleteMe in action with auto-completion for C++ functions!
For Python development, I have a dedicated post here too. By the way, you may wish to read here when using Python modules. It explains how to set the python executable path.
Also, if you are a Mac user, you may want to checkout this post by Oliver.
Saturday, February 18, 2017
Copying to Clipboard in Mac OS X Terminal
In case you want to copy the entire content of a file to clipboard, here is what you can do conveniently.
$ cat some_file.txt | pbcopy
This command will copy the standard output to the clipboard. Since the cat command pipes the file's text to standard output, pbcopy will copy it into clipboard.
That's it!
$ cat some_file.txt | pbcopy
This command will copy the standard output to the clipboard. Since the cat command pipes the file's text to standard output, pbcopy will copy it into clipboard.
That's it!
Tuesday, February 14, 2017
Fundamental Git Concepts and Useful Git Commands
Summary of fundamental git concepts:
- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit
Summary of most frequently used commands in git:
# show current status
$ git status
# show commits in chronological order [with statistics]
$ git log [--stat]
# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2
# show difference between two commits
$ git diff commit1 commit2
# show difference between the specified commit and its direct parent
$ git show commit_hash
# load specified commit
$ git checkout commit_hash
# load specified branch
$ git checkout branch_name
# save current work as a new branch
$ git checkout -b new_branch_name
# show [hidden] branches
$ git branch [-a]
# create a new branch
$ git branch new_branch_name
# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2
# add files to staging area
$ git add .
# remove files from staging area
$ git reset
- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit
Summary of most frequently used commands in git:
# show current status
$ git status
# show commits in chronological order [with statistics]
$ git log [--stat]
# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2
# show difference between two commits
$ git diff commit1 commit2
# show difference between the specified commit and its direct parent
$ git show commit_hash
# load specified commit
$ git checkout commit_hash
# load specified branch
$ git checkout branch_name
# save current work as a new branch
$ git checkout -b new_branch_name
# show [hidden] branches
$ git branch [-a]
# create a new branch
$ git branch new_branch_name
# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2
# add files to staging area
$ git add .
# remove files from staging area
$ git reset
How to Push an Existing Repository to Git Server
Say you have a Github account or a local git server where you want to publish your work either publicly or privately as a bare repository. This post will explain how to create a bare repository on Github or git server. Refer here for official Github documentation.
First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.
If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git
Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.
Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"
Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY
where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.
Finally, push your current repository to the remote repository:
$ git push -u origin master
Now, the two repositories should be in sync!
First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.
If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git
Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.
Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"
Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY
where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.
Finally, push your current repository to the remote repository:
$ git push -u origin master
Now, the two repositories should be in sync!
Sunday, February 12, 2017
Java Threading: Synchronization II
In the previous post, we looked at how synchronized methods can be used to ensure safe access to a single object, where multiple threads try to read/write to it.
Here, we improve upon the example. In the last post, I mentioned that there were still fixes to be made, and today we will discuss one of them.
Below is the copy from the previous lesson. Take a closer look at the run() method of removeThread, line 54-57 below:
You will notice that the thread continuously checks whether there is at least 1 or more elements in the queue, and when there is, it calls dequeue() method. The question is: can we guarantee that element in the queue will remain in between the two calls? That is, what if another thread calls dequeue() in between, intercepting the element first? This is certainly possible...
Solution? I'd say we fix this issue by having dequeue() method check for the number of elements in the queue. Since dequeue() method is synchronized, it is guaranteed that if there is an element, it will be able to retrieve it for sure. What if there is no element in the queue? It will return immediately with an exception stating that there is no element to retrieve. Thus, we need to modify removeThread run() method to loop dequeue() method continuously until it does not throw an exception. Take a look at code below for implementation.
The modified code above certainly is better than the previous version, but we are not done yet. There are still a few more fixes to be made, and we will tackle one by one in the subsequent posts!
Here, we improve upon the example. In the last post, I mentioned that there were still fixes to be made, and today we will discuss one of them.
Below is the copy from the previous lesson. Take a closer look at the run() method of removeThread, line 54-57 below:
You will notice that the thread continuously checks whether there is at least 1 or more elements in the queue, and when there is, it calls dequeue() method. The question is: can we guarantee that element in the queue will remain in between the two calls? That is, what if another thread calls dequeue() in between, intercepting the element first? This is certainly possible...
Solution? I'd say we fix this issue by having dequeue() method check for the number of elements in the queue. Since dequeue() method is synchronized, it is guaranteed that if there is an element, it will be able to retrieve it for sure. What if there is no element in the queue? It will return immediately with an exception stating that there is no element to retrieve. Thus, we need to modify removeThread run() method to loop dequeue() method continuously until it does not throw an exception. Take a look at code below for implementation.
The modified code above certainly is better than the previous version, but we are not done yet. There are still a few more fixes to be made, and we will tackle one by one in the subsequent posts!
Saturday, February 11, 2017
Android Threading: Handler Example
In this post, we will go over a very simple Handler example for Android development. Hopefully this post will explain and demonstrate why and how to use Handler class in Android.
Consider a very simple app, where you have two buttons: task button and increment button.
Upon click, the task button will notify the user that the task is now running, and carry out some heavy task, which may take up to seconds. When complete, it will notify the user that the task is finally done.
Upon click, the increment button will simply increment counter and display its current value.
Let's take a look at a crude attempt to implement this using a simple Thread. Here is the layout file.
Here is the implementation activity file.
Here, upon the task button click, it launches the task, which is simulated by sleep(3000), on a separate thread so that we don't slow down the main UI thread. However, the problem is that we must wait for this task thread to complete because we need to update the button's text upon completion. Thus, we use join() method to wait for the task to complete. With this implementation, we find that the UI thread becomes unresponsive while waiting. This is NOT a good way of implementing the task.
The core problem is that we ask the task thread to carry out some heavy task, and we need to make sure that the main UI thread does not just sit and wait, but rather do its jobs on its own. The task thread then must communicate with the UI thread and let it know the task is complete, at which point, the UI thread can update the UI accordingly.
The Handler class in Android achieves exactly this, and below is modification that makes use of the Hander class.
As you can see, the task thread will do its job, and when the task is complete, it notifies the main UI thread that the job is done through handler. The handler is created in the main activity, so it is bound to the main UI thread. When the handler receives a message from the task thread that the job is complete, it will then update the button's text. In the meantime, the main UI task carries out its own tasks, such as incrementing the counter when the increment button is pressed.
Of course, one can implement this without using the Handler class, shown below. However, this solution is possible because we are communicating with the UI thread in this example. If, however, you have two different task threads that must communicate with each other, then you need to use the Handler.
Consider a very simple app, where you have two buttons: task button and increment button.
Upon click, the task button will notify the user that the task is now running, and carry out some heavy task, which may take up to seconds. When complete, it will notify the user that the task is finally done.
Upon click, the increment button will simply increment counter and display its current value.
Let's take a look at a crude attempt to implement this using a simple Thread. Here is the layout file.
Here is the implementation activity file.
Here, upon the task button click, it launches the task, which is simulated by sleep(3000), on a separate thread so that we don't slow down the main UI thread. However, the problem is that we must wait for this task thread to complete because we need to update the button's text upon completion. Thus, we use join() method to wait for the task to complete. With this implementation, we find that the UI thread becomes unresponsive while waiting. This is NOT a good way of implementing the task.
The core problem is that we ask the task thread to carry out some heavy task, and we need to make sure that the main UI thread does not just sit and wait, but rather do its jobs on its own. The task thread then must communicate with the UI thread and let it know the task is complete, at which point, the UI thread can update the UI accordingly.
The Handler class in Android achieves exactly this, and below is modification that makes use of the Hander class.
As you can see, the task thread will do its job, and when the task is complete, it notifies the main UI thread that the job is done through handler. The handler is created in the main activity, so it is bound to the main UI thread. When the handler receives a message from the task thread that the job is complete, it will then update the button's text. In the meantime, the main UI task carries out its own tasks, such as incrementing the counter when the increment button is pressed.
Of course, one can implement this without using the Handler class, shown below. However, this solution is possible because we are communicating with the UI thread in this example. If, however, you have two different task threads that must communicate with each other, then you need to use the Handler.
Tuesday, February 7, 2017
How to Load Java OpenCV Library to Android Studio
In this tutorial, I will go through a step by step method to load Java OpenCV Library to Android Studio.
First, download OpenCV for Android from here. Extract the zip file, and you should see OpenCV-android-sdk folder.
Next, in Android Studio, open up a project where you want to integrate OpenCV Java library. Then, click File - New - Import Module and select OpenCV-android-sdk/sdk/java folder. Android Studio will ask about import option, and just accept the default.
You should see OpenCVLibrary module in your Android project. Select its build.gradle file and set appropriate versions for compileSdkVersion, buildToolsVersion, etc.
Now, you need to add dependency. Open up your app's build.gradle file and add
compile project(':openCVLibrary310')
into dependencies { ... } section.
Next, you will need to make sure that your app loads in the OpenCV library. Go to the activity class file that first uses OpenCV Library, and add static statement, similar to below:
...
import org.opencv.android.OpenCVLoader;
public class MainActivity extends Activity {
final static String TAG = "Main Activity";
static {
if(!OpenCVLoader.initDebug()){
Log.d(TAG, "OpenCV not loaded");
} else {
Log.d(TAG, "OpenCV loaded");
}
}
...
Finally, you will need to copy native binary files. Copy OpenCV-android-sdk/sdk/native/libs folder as src/main/jniLibs folder in your app's directory.
That's it! You should now be able to use OpenCV functions in your app!
First, download OpenCV for Android from here. Extract the zip file, and you should see OpenCV-android-sdk folder.
Next, in Android Studio, open up a project where you want to integrate OpenCV Java library. Then, click File - New - Import Module and select OpenCV-android-sdk/sdk/java folder. Android Studio will ask about import option, and just accept the default.
You should see OpenCVLibrary module in your Android project. Select its build.gradle file and set appropriate versions for compileSdkVersion, buildToolsVersion, etc.
Now, you need to add dependency. Open up your app's build.gradle file and add
compile project(':openCVLibrary310')
into dependencies { ... } section.
Next, you will need to make sure that your app loads in the OpenCV library. Go to the activity class file that first uses OpenCV Library, and add static statement, similar to below:
...
import org.opencv.android.OpenCVLoader;
public class MainActivity extends Activity {
final static String TAG = "Main Activity";
static {
if(!OpenCVLoader.initDebug()){
Log.d(TAG, "OpenCV not loaded");
} else {
Log.d(TAG, "OpenCV loaded");
}
}
...
Finally, you will need to copy native binary files. Copy OpenCV-android-sdk/sdk/native/libs folder as src/main/jniLibs folder in your app's directory.
That's it! You should now be able to use OpenCV functions in your app!
Subscribe to:
Posts (Atom)