Thursday 26 March 2015

General Purpose Programming (GPU Programming)

In this post I am going to describe what is general purpose programming or G.P.U. programming is and why we use it over tradition C.P.U. Programming.

Traditional Programming:

C.P.U. programming is the traditional way of doing programming. In which our program is run on the C.P.U. It was the main part of our program execution. But Due to certain limitation now the trend is shifting towards the general purpose computing or G.P.U. computing.

Limitaions of Traditional Programming:

Because we cannot increase the frequency of the C.P.U. further and adding more number of cores to our system is very costly. How much cores we can afford on our own, 2 ,4, 8 ,16. And it will consume so much power that our system's battery will discharge very quickly. Main reason is we want to speed up our computations as now a days the Size of DATA is increasing rapidly. Our C.P.U computation will not give results as fast as our expectation.

G.P.U.( Graphical Processing Unit) :

G.P.U is the abbreviation for Graphical Processing Unit. Which is integrated to our systems for graphical processing. It is being used for graphical processing on our system for long time. Then some people think , why can't we use G.P.U. in general purpose computing instead of using only graphical processing. A single general Graphic Card or G.P.U. contains nearly 65,535 small processors on single chip. Which can do the arithmetic operations simultaneously.



G.P.U. Programming:

In G.P.U. programming we use the G.P.U to do the arithmetic calculations simultaneously. As I described above that a general G.P.U contains almost 65,535 small A.L.Us. We can write our programme logic in a way that our program can be spawn into parts for running simultaneously.

But G.P.U is not a C.P.U. It generally cannot access our Main Ram which is available for C.P.U. So whatever code we write first goes to C.P.U. Then C.P.U send the data and the programme logic to G.P.U. G.P.U. executes it parallely and again send back the data to C.P.U. By parallising the threads on multiple A.L.Us of G.P.U  we speed up the computation. That can be shown in a huge data set program otherwise the transferring cost of DATA from C.P.U. to G.P.U. will be large than the computation speed up we achieved on those data. 

Like: For sorting 10 elements we will not use G.P.U.
For sorting 100,000 elements we will use G.P.U. So that the cost of transferring data can be overcome by computation speed up.

We are using G.P.U. for doing our general processing so this is called general purpose computing.
It solves the problem which can not be solved by C.P.Us in reasonable time. Like: D.N.A. testing.

Next Question is How to do this Programming ???

Next question is how to do this. How can we interact with the G.P.U. as we interact with C.P.U. So there are some Platforms available which you can start with. These platforms provide APIs for interaction with the G.P.U.

1. CUDA(Compute Universal Device Architecture) developed By NVIDIA.
   You can start with this following link. If you are interested.

2. OPEN CL (Open Computing Language) developed by Khronos group.
    You can start with this following link. If you are interested.

But these are two famous tools for G.P.U Programming . I have worked with both the tools. These are awesome.
How to work with these I will explain in the following posts.

Limitations Of  G.P.U. Programming:

1. But every problem's logic cannot be spawned in to the parallel parts means its difficult to think a problem to solve parallely.

2. Even the single G.P.U. might not be sufficient for the task. So now a days people go for more than the one G.P.U.

3. But having more than one G.P.U. also can be problematic. Imagine the scenario if you have two different G.P.U.s developed by two different companies then how to integrate both of them simultaneously.

The research is going on for the improvement of these limitations. But still G.P.U programming is faster then the C.P.U. programming.

I hope it helps. For more posts, like my page on facebook. The link is given below. Subscribe By Email on the side of the page. Thanks for visiting.




Saturday 21 March 2015

Volatile Keyword


In this post I am going to describe why we use volatile keyword.

I am assuming you have a basic knowledge of compiler. Whenever we write a code and compile it. Compiler optimizes our code according to some basic fundamentals.

Like for eg. If in our code we have a constant variable whose value cannot be modified and we have a loop checking this constant variable with another constant. Means if compiler thinks that none part of our code is changing the value of the variable than it can optimize the comparisons.

Ex:
I am not writing the complete code. I am writing only the part which considers the above fact.

const int a=10;

while(a<20){    /// focus on this line
      some task;
}
Now At compile time compiler will check that variable a is constant and its value cannot be changed. And in the while loop it determines that 20 is also constant so it is also not changed. So it evalutes the result of a<20 and put it in the while condition.

Then compiler will put either true or false there to optimize. So that at each time it iterate through the loop it does not have to compare the variable.

For ex: After optimization code looks like this.

const int a=10;

while(true){   /// focus on this line
      some task;
}

But sometime this optimization affect the code in the wrong way.
Like : If we have a share variable between two files or two program then we don't want compiler to do any optimization with those variables. 

In this case we can define those variables to volatile. So compiler will not apply any kind of optimization on these variables.

Scenario 1: Variable with Volatile Keyword :

Ex:  we have a variable called a which is shared in two threads thread1 and thread2.

Volatile Keyword


Explanation: We have a volatile variable a=1 in thread 1 and it is shared. When thread 1 runs it goes in to while loop and check for a and goes in to the loop and do the task and switch to thread 2. Now thread 2 also checks a=1 goes in to loop and make a=0 and then again switch to thread 1 at line 8.

Now it checks the value of a which is equal to 0 and come out of the loop.


Scenario 2: Variable without Volatile Keyword.

Now consider the scenario where value of a is optimized. Means without volatile.

In thread 1;
int a=1;

some task;
while(a){  /// this is replaced by while(1)   /// optimized by compiler    ///// line 1
    do some task;  
   switch to thread2;
   //// line8
}


In thread 2:

while(a){
   do some task;
    a=0;
   switch to thread 1;
}


Explanation: We have a variable a=1 in thread 1 and it is shared. When thread 1 runs it check for a and goes in to the loop and do the task and switch to thread 2. Now thread 2 also checks if a!=0 and goes in to loop and make a=0 and then again switch to thread 1 at line 5.

Due to while(1).
Now because of optimization thread 1 now do not check the value of a at line 1.

And goes into infinite loop. A serious problem caused due to optimization. 

So for avoiding this problem use volatile keyword to the variables which we do not want compiler to optimize in any case.



I hope it helps. If you find this useful share it. If you have any problem feel free to comment. How do you rate this post either interesting or cool or funny. See below. Thanks for visiting.


Interrupted Exception (Thread.sleep() )

In this post I am going to describe what is an Interrupted Exception is and another thing is why always we write thread.sleep function in try catch block.

A thread or java execution throws an interrupted exception whenever a thread is interrupted by another thread. There might be some other reason also but this is the most frequent reason for the InterruptedException.

Whenever we use Thread.sleep() function, we have to use it in try catch block because this function can throw an interrupted exception. If we will not provide a catch block for handling that exception it might give an error at running time. Because the sleeping thread might be interrupted by some other running thread.

So the code for using thread.sleep will be.

I am writing only main function.
Note: Do not forget to use throws InterruptedException in whichever function you are using Thread.sleep.

public static void main(String [] args) throws InterruptedException{
     try {

           Thread.sleep(1000);  // sleeping the thread for 1000 ms
     } catch( InterruptedException e){

            System.out.println("our thread is interrupted");
        }
  }



Thursday 19 March 2015

How to write data to an excel file in java (Eclipse)

In this post I am going to describe how to write data in to an excel file in java.

There are number of ways. One of them is by using Apache POI.

Apache POI is a project under the guidance of Apache Software Foundation. This POI libraries are used to write data to Microsoft office formats like excel, word etc.

I am assuming you are using Eclipse IDE for developing Java project.

1. First thing you have to download the apache POI from the following link according to your computers environment.(Operating System, 32/64 bit version)


I prefer to download the stable version.

2. After Downloading unzip the folder. You will see some jar files. There will be a jar file with named like poi-3.11-20141221.jar . Digits may vary in the files name according to their version and their release date.

3. Include this file into your projects external jar files. For doing the same you can visit my previous post.


Now we are done with preprocessing. We have to write code for writing data in an excel file.

4. Import these classes to your code.

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class writeToExcel{


       public static void main(Strings [] args) throws InterruptedException{

                  try{

                               FileOutputStream fileOut = new FileOutputStream("test2.xls");
                               HSSFWorkbook workbook = new HSSFWorkbook();
                               HSSFSheet worksheet = workbook.createSheet("test2 Worksheet");

                                // It means we create an output file with name test2.xls and open an workbook 
                                // for working and worksheet for creating data and then we will write to our file

                             HSSFRow row1 = worksheet.createRow(0);
                             HSSFCell column1= row1.createCell(0);
                             column1.setCellValue("hello");
                             HSSFCell column2= row1.createCell(1);
                             column2.setCellValue("Thanks for visiting ");
          
                             // you can iterate through for creating more rows. make sure that it is indexed 
                            // from 0 as array in C 
                            // First create row and then column and set their values.
                               
                                workbook.write(fileOut);
                                  fileOut.flush();
                                fileOut.close();
                                workbook.close();
                   }

                    catch (FileNotFoundException exc) {
                                                 exc.printStackTrace();
                   } catch (IOException exc) {
                                                 exc.printStackTrace();
                   }
      }

}

This code is running. If you get any error comment. I will try to help. If you find the post useful Share it.

How to add external jar files in Eclipse

In this project I am going to describe how to add jar files in eclipse.

1. First of all locate the folder where your jar file is.

2. Then open Eclipse and go to Project Option  in the menu bar

Project -> Properties -> Java Build Path -> Libraries -> Add External Jars

Adding External Jar file in Eclipse


3. Browse to the location where your jar file is located.

Click on the open and then Ok.

You are done. Feel free to comment. If you have another problem comment. If you like this post share it and visit other posts also. Thanks for visiting.

Monday 16 March 2015

How to generate automatically set and get function for elements in eclipse

In this post I am going to describe you how to generate setter and getter function automatically for elements of the class.

First of all go to any of the elements of the class, right click on it and go to sources and there you will find the option of many types like this image.

Generate Getters and Setters


Click on the Generate Getters and Setters.

Now It will ask for which elements you want to create those function. Select those elements by cheking those check boxes and click on finish.

I hope it helps. If you find any problem related this describe it in comment. May be I can help you.
Thanks for visiting my blog. Share it , like it or comment it If you like my post. Check again for new posts.

Sunday 15 March 2015

How to import ns2 in eclipse and start debugging

In this post I am going to describe How to open ns-2 in Eclipse without any make error?

If after installing ns-2 in Eclipse
Error: *****No make target 'all'.stop

1) Error comes because eclipse is not able to find makefile. As the makefile is itself present in ns-allinone-2.35/ns-2.35 folder but it is write-protected and eclipse don't have premissions to read it.

2) Change the permissions of the complete ns-2 folder by following commands
    a)First go to the ns-allione-2.35 folder by using cd command
    b)Then give permissions by "sudo chmod 777 *"
    c)All files permissios in folder will change

3) Now reimport the whole folder in eclipse
Suggestion: change the workspace while importing folder

4) Either change your make target or change the workspace in build configuration because your main makefile is in the ns-2.35 folder but by default it will search in  the main ns-allione-2.35 folder. 
So go to project -> build configuration -> c/c++ -> in workspace browse to the ns-2.35 folder
or 
project -> properties ->c/c++ -> and in workspace browse to the ns-2.35 folder

or

you can type the path for the makefile file in make command like
make -f  path
replace path with where your makefile is in ns-2.35 folder.

then compile it. It will able to compile the ns2.

How to Run your topology from Eclipse for debugging purpose?

In this tutorial "~/ns-allinone-2.35/ns-2.35/tcl/ex/example.tcl" is used.
1- Select Run>>Debug Configurations.
2- In Main Tab>>C/C++ Application, write "ns" without quotation marks.
3- In Arguments Tab>>Program arguments, write "example.tcl" without quotation marks.
4- In Arguments Tab>>Working directory, write "~/ns-allinone-2.35/ns-2.35/tcl/ex/" without quotation marks.
5- In Debugger Tab, uncheck "Stop on start up at main".
6- Click Apply then Debug.

Good luck to all students. If you like this post please like it, share it and comment it. Enjoy and have fun. Visit often for new posts.

Friday 13 March 2015

Installing unity remote and start testing your app on your android device

In this post I am going to describe how to install unity remote on your device and start working with unity. After doing all the following step you will be able to run all the game on your device directly instead of unity.

I hope you have installed the unity on your computers.
Make Sure you installed google usb driver also using sdkmanager.

1. Go to Play store and download unity remote app for android device.
you can install it from this link.
https://play.google.com/store/apps/details?id=com.unity3d.androidremote&hl=en

Now download the android sdk from the below link to your computer.
http://developer.android.com/sdk/index.html

Make Sure you also installed google usb driver using sdkmanager.
Sdk manager is in the android bundle you have downloaded from the above link.
sdkmanager.exe -> Extras -> google usb driver  tick the check box and click install.

2. Now connect your device to computer using usb cable.

3. Make sure that usb debugging is enable on your device. To enable usb debugging go to settings on  your android device and in settings go to Developer Option and in Developer Option you will find the usb debugging option , Check that check box.

4. Now start unity remote on your android device.

5. Now start unity software in your computer.

NOTE : If you start unity software first in your computer and then unity remote in your android device then your unity software will not pick your unity remote. So whenever you start unity remote in your phone then again restart unity software in your computer.

6.Set the android sdk path in unity software by going in to Edit -> Preferences -> Set Path and browse to the android sdk you already downloaded.

7. Now in unity software go to Edit -> project Setting -> Editor -> Select any android device in that 
Menu.

Now you are done. Whenever you will click play it will play in your android device. 

Enjoy and have fun in developing game.
If you find this post useful please share it on google+ and like this post and comment. Thank you.

Thursday 12 March 2015

Import CloudSim Simulator in to Eclipse

In this post I am going to describe how to import cloudsim simulator in to Eclipse and start working with it.

First of all we need to download the cloudsim source code. Which you can dowload it from the following address.


If you are working in windows then download the latest .zip file or if you are working in Linux environment download the .tar.gz file.

 Now you will need Eclipse IDE to open cloudsim project. You can download the Eclipse from this link.

After downloading and installing Eclipse. Open the Eclipse and follow the below procedure.

 1. Go to File menu in the top left corner of Eclipse.

 2 . Create a new folder using new and then folder, name it whatever you want. We will use this   folder for importing cloudsim later on.

 3. Then go to import option in file menu.

 4. Select Archive File in General Option and click on Next.


Select the Archive File and click Next
5. Now browse that compressed file which you have downloaded for cloudsim. Which may be look like this.
Cloudsim-3.0.3.tar.gz or Cloudsim-3.0.3.zip

Click on Open. It will ask you to select folder. Select the folder which we made on the second step.
Then click on finish. It will take some time to import all the files.

You can access all the resources of cloudsim by going to this folder.
In further posts I will describe How to work with cloudsim simulator.

Till than have fun. And don't hesitate to comment If you have any doubt. May be I can help.



Wednesday 11 March 2015

Install virtual box/machine in host and guest operating system(Nested virtual machines)

Today I am going to describe how to install virtual box and creating a virtual machine in host operating system (windows ) as well as guest operating system(operating system installed on virtual machine already).

For nested first we have to install a guest operating system using virtual box. I am demonstrating here for windows as host operating system and Linux mint ( http://www.linuxmint.com/ ) as guest operating system but you can use any os image for guest operating system. Make sure that os image is not corrupted and you have the latest version of virtual box.

1.Download virtual box from the official oracle link. (Make sure you download the latest version)

2. After Downloading is completed go to the installed .exe file (for windows only) and run it. It will install virtual box on windows.(it will take few minutes)

3. After installing virtual box start virtual box click on new to add new guest operating system

Adding New Guest Operating System





Select the type of operating system u want to install as guest. I am installing mint whose base is inux 
so I am selecting Linux and Ubuntu(32 bit). Make sure you are selecting the correct version 32 bit or 64 bit. I am installing a 32 bit version of Linuxmint. Click on Next.

4. Select the RAM you want to give to guest operating system. There will be a recommended memory size on the basis of your total RAM. Give a little more than the recommended memory. Because we are going to install another os in that guest os.

5. Select the radio button which has the option 
Create a virtual hard drive now.

Click on create then again a pop up window will come.

6. Hard drive file type
Select the option  VDI( Virtualbox Disk Image)      then click on Next.

7. Next pop up comes for Storage on physical hard drive . 
Select the option Dynamically allocated

8. Select the file location where u want to install files in hard drive. And also give the size of the virtual hard drive around 20-30 gb (Because we are going to install another os in that host os).

9. Click on create and an virtual machine will be created in the left dialogue box of virtual box.

10. Right click on the newly created virtual machine and go to settings

Selecting the iso image from the hard disk or cd/Dvd.
in settings -> storage -> controller:IDE -> Empty(click) -> Attributes (select IDE Secondary Master) and click on DVD Icon to select the iso file. Choose the iso image file from where u have put that file.( If u don't have an iso for guest operating system. Download Linux mint from the below link.)

NOTE: Select the correct version 32 bit or 64 bit whatever you want to install.

After selecting iso image file click on ok.

11. Again go to that newly created virtual machine. Right click on it and click on start.
     It will start installing mint.
     It will take time ( around 10 - 15 minutes).

12. After that it will ask to set username for your os and password. Set that.
    And if you want , you can restart that guest operating system.

Installing virtual box in the guest LINUX MINT operating system.

1. Go to terminal. (ctrl+alt+t)

2. Make sure internet is working in guest operating system by running sudo apt-get update.

3. Then run following commands
for adding sources from where u can download run this command
sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian trusty contrib" >> /etc/apt/sources.list'

4. run the command
wget http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

5. then again update
sudo apt-get update

6. install virtual box (whatever latest version is)
sudo apt-get install virtualbox-x.y

where x.y is version of latest virtual box.

7. download the dkms for virtual box
sudo apt-get install virtualbox-dkms


It will install latest virtual box in the mint.

For starting virtual box : Type virtualbox in the terminal. It will start the virtual box.

And for another guest on that guest operating system use this virtual box as the same process as above. First Download an iso in the guest operating system and repeat the above process.


For further detail you can visit this link.


I hope it helps. Feel free to comment. If you have a problem post. I will try to find a solution. Till then enjoy.






Installing Android Studio and Deploy App on USB connected Android Device Instead of Emulator

In this post I am going to describe how to install Android Studio and make it work with your android device directly. The app will run directly on your android device which is connected using usb cable. If you don't have an android device than you can test your app on emulator. But Emulator is too slow to work with. If you have any android device( having min. android version ) can be used instead of emulator. That would be very much faster instead of working with emulator.


Installation:


1. First of we will see whether your system meet all the requirements which Android Studio needs.
   Visit this link to see the requirement.(depending which Operating System you are using)

2. If you meet all the hardware requirements, make sure that you have the latest version of java jdk and jre installed on your system. 

Note: The latest (till date) Android Studio needs java jdk7 or higher version.

You can download and install Java jdk and jre from the site below.

3. Now if you installed new java jdk and jre then set the environment variable PATH( in windows specifically) to the path where you have installed that jdk and jre.


 start(right click) -> system -> system advance setting ->advance -> environment variable

Set PATH Variable in Windows
If you have other than windows operating system you can search on google "how to set path variable for java jdk".

4. Now you are ready to install Android studio. Download it from the official website.


5. After downloading you will have the Android Studio adt bundle in your downloads.
                  android-studio-bundle-xxx.xxxxxxx.exe
         xxx.xxxxxxx will be your version which you have downloaded.

 click on it(for windows) and setup will start. It will ask simple things where you want to install ,language etc. go through it and install it. It will take little time. This process is easy.

Your android studio is installed . You can start developing android app but for changing the setting of the emulator to change it to your device go through the next process.

Setting your android device as default for running app instead of emulator:


1. First of all you will need an android device with min. android OS version(whichever you will  use  for developing app).

2. You need to download adb driver for your device. It is specific to every device. So search on google.
Download and install adb driver for your device.

3. You need to enable usb debugging in your phone. In older version(less than 4.2) there will be an option for developer in settings or application settings. Click on it you will get an option usb debugging.  Check that check box.

If you have android device having version 4.2 or higher go to settings -> about phone -> tap the build number 7 times.
Now go back to settings, you will find a developer option there. In which you will find the option for usb debugging.

Of course you have your phone is connected to system using usb.

Now your phone is ready. Just make a single change to android studio settings.

Changing Setting in Android Studio:



Near the button which is used to run the app a rotated green triangle in the following image. You will see a dropdown(app is written in the image) in which go to edit configuration and in Android Application go to app and general and set target device as usb. Following photo shows the Exact setting

Changing Setting in Android Studio


Now click on apply and ok. Attach your phone with usb. Restart Android Studio. Now whenever you run any app in Android Studio. It will be automatically deploy to your device instead of emulator. It is very much faster than the emulator.

I hope it helps. If you have any problems or queries do not hesitate to comment. May be I can help.
Till then enjoy developing your app.

Related Posts

Related Posts Plugin for WordPress, Blogger...