Archive

Archive for June, 2011

Publish MATLAB output as a document(html, doc, LaTex, Powerpoint etc.,)


Here I wanted to share how did I convert the matlab output to the specific document using the publish command of matlab. We can also run the matlab code from outside matlab window.

The following steps are to be followed to run the matlab code from command prompt.
step1: First edit PATH system variable and set the matlab path as [MATLABINSTALLEDFOLDER/bin/]
step2: Now from the command prompt we can run any file using the following code
<pre>
matlab -r scriptname (without the .m extension but first we have to change directory to the source existing folder)
ex: c:\Users\MyWindows7\MATLAB > matlab -r example
[example.m is the actual script to be run]
</pre>
Now let us actually see how we can publish an output of script as a document

function [ret]= publishToFormat(filename, format)
    options_doc_nocode.format= format;
    options_doc_nocode.outputDir = 'd:/matlabNDVIOutput';
    options_doc_nocode.showCode = false;
    publish(filename,options_doc_nocode)
    ret=1;

In the above code we are providing the name of the function and format to which the script output has to be stored. The above code is simple and straight forward it says the format of the output file and where that output file has to be stored. whether the source code of the script file has to be written to the output or not.

The following links are the ways of using java code in matlab and convert the matlab file into the java file and java web services.

http://www.mathworks.com/products/javabuilder/
http://www.mathworks.com/help/techdoc/matlab_external/f44062.html
http://blogs.mathworks.com/desktop/2009/07/06/calling-java-from-matlab/

Advertisements

Open-ssh Server installation in ubuntu


If you want to remotely login to any system which is running linux you can do so by using ssh command which is very famous for its security awareness. In ssh all the communication via port 22 is encrypted unlike other communication protocols such as TELNET, ftp, rlogin etc., where all the data and passwords can be hacked.

In all the flavors of linux there is no guarantee that ssh is pre installed. Particularly I’m running ubuntu LT 10.04 in which it is not pre-installed. We have install it manually. Openssh is a free open source version of ssh protocol implementation.

We can install openssh client and server using the below command

# sudo apt-get install openssh-server openssh-client

then we can test it by using

ssh localhost
# ssh user@your-server-ip-address
Ex: #ssh sshVasnath@vasanth 192.16.36.17

if you want to get the sudo privileges to sshVasanth user running on the machine IP address 192.16.36.17, you should
use the run the following command in the server(192.16.36.7) with sudo

# sudo adduser <ACCOUNTNAME> admin

Ex: sudo adduser sshVasanth admin

SOAP message to run the service in server


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Chittampally Vasanth Raja
 */
public class SOAPMessage1 {

         //First create the connection
    public static void main(String[] args) throws IOException, TransformerConfigurationException, TransformerException {
        try {
            SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnFactory.createConnection();
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage message = messageFactory.createMessage();
            //Create objects for the message parts
            SOAPPart soapPart = message.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPBody body = envelope.getBody();
            //Populate the body
            //Create the main element and namespace
            SOAPElement bodyElement = body.addChildElement(envelope.createName("add", "ns2", "http://me.org/"));
            //Add content
            bodyElement.addChildElement("num1").addTextNode("12");
            bodyElement.addChildElement("num2").addTextNode("34");
            // bodyElement.setEncodingStyle("text/xml");
            //Save the message
            message.saveChanges();
            //Check the input
            System.out.println("\nREQUEST:\n");
            message.writeTo(System.out);
            System.out.println();
            //Populate the Message
            //        StreamSource preppedMsgSrc = new StreamSource(
            //                 new FileInputStream("./prepped.msg"));
            //        soapPart.setContent(preppedMsgSrc);
            //String destination = "http://localhost:8084/TestAT/HelloWorld";
            String destination="http://localhost:8080/GlassWS/HelloWorldService";
            //Send the message
            SOAPMessage reply = connection.call(message, destination);
            System.out.println("" + reply);
            System.out.println("\nRESPONSE:\n");
            //Create the transformer
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
            //Extract the content of the reply
            Source sourceContent = reply.getSOAPPart().getContent();
            //Set the output for the transformation
            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);
            System.out.println();
            //Close the connection
            connection.close();
        } catch (SOAPException ex) {
            Logger.getLogger(SOAPMessage1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedOperationException ex) {
            Logger.getLogger(SOAPMessage1.class.getName()).log(Level.SEVERE, null, ex);
        }
}

}

Parallel OpenMP Search algorithm


Here is the parallel version of the linear search algorithm using OpenMP

//Author: Chittampally Vasanth Raja
//The following program is parallized version of search algorithm
#include<stdio.h>
#include<omp.h>
#define MAXTHREADS 10
#define ARRAYSIZE 44
int main(void)
{
	int a[]={3,5,1,2,34,67,90,43,53,3,4,26,34,35,54,67,87,21,34,56,33,45,12,34,5,6,7,8,123,45,32,455,666,444,333,222,11,22,44,55,333,222},i,j,found=0,key=222;
	double start_time,run_time;
	for(j=1;j<=5;j++)
        {
	    	omp_set_num_threads(j);
		found=0;
start_time = omp_get_wtime();
		#pragma omp parallel private(i)
		{

			int start,noofsteps;
			#pragma omp single
				printf("num of threads in action: %d\n",j);

			if(found==0)
			{

				start=(omp_get_thread_num())*(ARRAYSIZE/omp_get_num_threads());
				noofsteps=start+(ARRAYSIZE/omp_get_num_threads());
				if(ARRAYSIZE%j!=0)
					noofsteps+=(ARRAYSIZE%j);
				for(i=start;i<noofsteps;i++)				
if(key==a[i]) {
						printf("Key has been found in %d thread at %d position\n",omp_get_thread_num(),i+1);
						found=1;
						break;
					}

			}
		}
		run_time = omp_get_wtime() - start_time;
	  printf("\n %f seconds %d threds \n ",run_time,j);
	}

return 0;
}

My First OpenMP program


This is my first OpenMP program

//Author: Chittampally Vasanth Raja
//First Hello World simple OpneMP program

using namespace std;
#include <iostream>
#include <omp.h>

int main(void)
{
	#pragma omp parallel
	{
	cout<<"Hello World !! It has "<<omp_get_num_threads()<<" threads.. This line is printed by Thread: "<<omp_get_thread_num()<<"\n";
	}
	return 0;
}

compile the above program using
g++ -o hello -fopenmp First.cpp
./hello