Archive

Archive for the ‘Programming’ Category

Automated Linux GUI Applications using X11::GuiTest

November 3rd, 2015 No comments

Lately, I wanted to extract from application-embedded medical lexicon to construct a database for easy use of the lexicon data.  The application was ancient (1998) and Windows 32bit.  The application does not work in Windows version beyond Windows XP 32bit.  But fortunately, the application was very runnable in Ubuntu 14.04 over Wine layer.  A snapshot of the application is shown below:

app

There were several restrictions in the application, viz.,  “Select All” does not work, direct copy-paste does not work.  But the traversal of lookup items on the left pane could be fully done using keyboard keys.  So, the process for extracting every item is:

  1. TAB + TAB to go to the left pane containing the lookup items
  2. TAB to move to the right pane.
  3. Ctrl + Shift + End would select the entire text on the right pane.
  4. Ctrl + C copies the selected text to the clipboard
  5. Using “xclip”, it is possible to get the clipboard data into the driver script.
  6. Save the recovered clipboard content to a file.
  7. Move back to the left pane by Shift + TAB.
  8. Down arrow to move to the next item in the list.
  9. Goto Step 2, until exhaustion.

The ideal toolkit for this project is the X11::GuiTest library in Perl (https://metacpan.org/pod/X11::GUITest). The same toolkit is available in software repository, which could be installed using sudo apt-get install libx11-guitest-perl.  The codeblock that I’d used in Perl for getting is done is listed below.

# Start the application 
StartApp('/path/to/app');

# wait unconditionally for 5 seconds to allow the app to come to front.
WaitSeconds(5);

# move the cursor to the scroll list
SendKeys('{TAB 2}');
sleep(2);

for (my $i = 0; $i < 20; $i++ )
{
 # move the cursor to the right pane
 SendKeys('{TAB}');

 # select all
 SendKeys('^(+({END}))');

 # perform copy
 SendKeys('^(c)');
 
 # read the data from clipboard and print that in stdout
 my $clipboard = `xclip -selection clipboard -o`;

 if ( $clipboard ne $clipboard_prev ) { print '-'x 80; print "\n$i\t$clipboard\n" };
 $clipboard_prev = $clipboard;
 
 # move back to scroll list
 SendKeys('+({TAB})');

 # goto next item in the list.
 SendKeys('{DOWN}');
 WaitSeconds(0.7);
}

 

போதையானர் (Vs Pythagoras)

September 7th, 2015 No comments

ஓடும் நீளம் தனை ஒரேஎட்டுக்
கூறு ஆக்கி கூறிலே ஒன்றைத்
தள்ளி குன்றத்தில் பாதியாய்ச் சேர்த்தால்
வருவது கர்ணம் தானே

Premise: Consider the Running length of the base of a right-angle triangle as “a” and the standing height “b” and hypotenuse is “c”. As per Pythagoras theorem we have c2 = a2 + b2[Update] Ensure that the triangle is rotated to have the base side longer than the height side.

Proof:  As per the poem we have to consider (7/8) of the base length, that is  (7a/8) and half of the height, which is (b/2) and sum them up to get the hypotenuse “c”

We have (7/8)a + (1/2)b = c => 7a + 4b = 8c.

Consider an easy example of 3, 4 and 5.  By Bodhaiyanar, we get 7*4+4*3 = 40 = 8*5 => c = 5.  We were able to find the hypotenuse without using square root operator in constant time.

Caveat: This algorithm works only for whole number hypotenuse values.  For others, it gives a quick and approximate estimate.

John 3:30 He must increase and I must decrease.

October 14th, 2013 1 comment
An interesting conversation that I’d lately picked with my beloved student and friend Gnana Sundar over facebook on John 3:30’s interpretation through a computer program snippet.

John 3:30 He must become greater and I must become lesser.

void john_3_30() {
jesus++;
self–;
}

Sudarsun Santhiappan: syntax error, unless instance variables “jesus” and “self” are globally defined (by who is a question you should answer). in some sense, you can also connect this relationship to holy trinity.

Gnana Sundar: Sir, did you check your class”path”? What “compiler” are you using?

Sudarsun Santhiappan: compiler” is analogous to the “religion”, which defines the book of rules for a good living (lifetime of the application). class”path” is analogous to the “help” that you get from other “runnable” wisdom in the “RunTime” called “life”.

Ulagammal Paramasivam: It shud be int john_3_30 as the return of this can’t be void.

Sudarsun Santhiappan: i would argue that the return type can only be a boolean (if at all), whether you attained sanity & solace or not. It cannot be a discrete or continuous evaluation as there is nothing to compare against. typically, add the line “return !self;”. does it make sense? The interpretation could be “have you given up yourself completely (to jesus, if you may add) ?”.

Gnana Sundar: Ulags: 🙂 …. I do not want to return from this method. I want this to run in an eternity loop 🙂

Sudarsun Santhiappan: Pragmatic Error: John 3:30 does not endorse one time ++ or –, it is an iterative process, if i may presume. Moreover, one time ++ or — is not a stable and a viable solution to minimization. Anything, that’s gradually minimized or maximized tend to be stable for better and longer. So, i would disagree to your point Gnana. 🙂

Gnana Sundar: Sir, I totally agree with you that John 3:30 does not endorse a one time ++ or –. It is a continuous process until eternity sets in. But I’m sure that the environment in which this is going to run is going to have infinite stack segment. If you say life is “RunTime”, the one who instantiated me would have to worry about stack overflow . When He can have the memory and a scalable enviroment to hold this universe and its contents, I’m pretty syre he can do better memory management and garbage collection

Just to give you some context. The Book of John is one of the four gospel books from the bible. It was written by John, one of the disciples of Christ.

In John 3:30, John the Baptist (he is not one of Jesus’ disciples, he is different from the author) says, “He must increase, but I must increase”. John the Baptist is considered to be a harbinger for Christ. He lived in the actual times when the Jews were “eagerly and for real” waiting for the Messiah. He had so many disciples and followers. He was considered to be a prophet. His calling in life was to prepare the way for Christ’ entry. When Christ started his ministry of healing people and teaching about the Kingdom of Righteousness, John had more followers than Christ. So pride and envy could have easily set in John. People, John’s disciples, John’s followers were eagerly waiting for John’s reaction. AT this point of time he uttered, its time… “He must increase and I must decrease” (thus the fulfilling the calling in his life).

In the same way when one accepts Christ in His heart, he/she should tune his/her heart and mind to say, “He must increase and I must decrease” and understand the actual calling and meaning of accepting Christ and their existence

By the way, I do not consider bible as a “religious” text. Religion limits the boundaries of this book and one’s life. It is open source . It is a set of God inspired writings, written by disciples for other disciples.

Sudarsun Santhiappan: Well said.

How fast is integer arithmetic than floating point arithmetic?

June 19th, 2013 No comments
You may already know that integer arithmetic is faster than floating point arithmetic. But how fast? I had the same question in my mind. So created the following script to test the “fastness” for each data type.

#include <vector>
#include <iostream>
#include <cstdlib>
#include “Stopwatch.hpp”

template <typename T>
void test( size_t inArraySize, size_t inCorpusSize )
{
    typedef std::vector<T> array_t;
    typedef std::vector<array_t> arrays_t;

    std::cout << “constructing the corpus…”;

    Stopwatch sw;
    sw.Start();
    arrays_t corpus( inCorpusSize + 1);
    for ( size_t i = 0; i < inCorpusSize+1; ++i )
    {
        array_t &array = corpus[i];
        array.resize( inArraySize );
        for ( size_t j = 0; j < inArraySize; ++j )
        {
            array[j] = (T)( rand() );
        }
    }

    sw.Stop();
    std::cout << sw.Elapsed() << ” secs” << std::endl;

    std::cout << “scored in … “;
    sw.Start();
    array_t scores( inCorpusSize );
    const array_t &query = corpus[0];
    for ( size_t i = 1; i < inCorpusSize+1; ++i )
    {
        const array_t &array = corpus[i];

        register T &score = scores[i1];
        score = 0;
        for ( size_t j = 0; j < inArraySize; ++j )
        {
            score += array[j]*query[j];
        }
    }

    sw.Stop();
    std::cout << sw.Elapsed() << ” secs” << std::endl;

}

int main( int argc, char **argv )
{
    if ( argc < 2 )
        return 1;

    int csize = atoi(argv[1]);
    int asize = atoi(argv[2]);

    std::cout << “testing float..” << std::endl;
    test<float>( asize, csize );

    std::cout << “testing double..” << std::endl;
    test<double>( asize, csize );

    std::cout << “testing long double..” << std::endl;
    test<long double>( asize, csize );

    std::cout << “testing int..” << std::endl;
    test<int>( asize, csize );

    std::cout << “testing unsigned int..” << std::endl;
    test<unsigned int>( asize, csize );

    std::cout << “testing short..” << std::endl;
    test<short>( asize, csize );

    std::cout << “testing unsigned short..” << std::endl;
    test<unsigned short>( asize, csize );

    std::cout << “testing char..” << std::endl;
    test<char>( asize, csize );

    std::cout << “testing unsigned char..” << std::endl;
    test<unsigned char>( asize, csize );

    std::cout << “testing long long..” << std::endl;
    test<long long>( asize, csize );

    std::cout << “testing unsigned long long..” << std::endl;
    test<unsigned long long>( asize, csize );

    return 0;
}

Syntax highlighting done by: /usr/bin/highlight -O html -t 4 -I -S cpp Main.cpp –inline-css -o /tmp/m.html

The result of running this script with vector size = 100 and corpus size = 1 million is shown below:
sudar@kiriya /opt/testing/bin $ ./tester 1000000 100
testing float..
constructing the corpus…1.447 secs
scored in … 1.731 secs

testing double..
constructing the corpus…1.543 secs
scored in … 1.836 secs

testing long double..
constructing the corpus…2.135 secs
scored in … 2.614 secs

testing int..
constructing the corpus…1.454 secs
scored in … 1.657 secs

testing unsigned int..
constructing the corpus…1.41 secs
scored in … 1.615 secs

testing short..
constructing the corpus…1.394 secs
scored in … 1.59 secs

testing unsigned short..
constructing the corpus…1.347 secs
scored in … 1.542 secs

testing char..
constructing the corpus…1.313 secs
scored in … 1.507 secs

testing unsigned char..
constructing the corpus…1.328 secs
scored in … 1.522 secs

testing long..
constructing the corpus…1.553 secs
scored in … 1.767 secs

testing unsigned long..
constructing the corpus…1.548 secs
scored in … 1.761 secs

  1. If you observe carefully, floating point calculations have taken more time than the fixed point calculations.
  2. Long Double took the longest time and it is the slowest.
  3. The reverse climax is that 64bit fixed calculation is slower than single-precision arithmetic.  An interesting read on multiplication is here.  The reason could be overflow due to the size of product register being 64 bits.
  4. The fastest is 8 bit calculation.  But unsigned 8 bit arithmetic is slower than signed.  Can’t explain why!
  5. For fixed point, unsigned calculations take less time than signed calculations, which can be understood by the additional time taken for 2’s complement arithmetic.
  6. Results can be different with different CPUs / Operating Systems, so infer accordingly.

Upgrade version of the testing application.
#include <vector>
#include <iostream>
#include <cstdlib>
#include “Stopwatch.hpp”
#include <string>
#include <map>

template <typename T>
double test( size_t inArraySize, size_t inCorpusSize )
{
     typedef std::vector<T> array_t;
     typedef std::vector<array_t> arrays_t;

     arrays_t corpus( 2 );
     for ( size_t i = 0; i < 2; ++i )
     {
          array_t &array = corpus[i];
          array.resize( inArraySize );
          for ( size_t j = 0; j < inArraySize; ++j )
          {
               array[j] = (T)( rand()%1000 );
          }
     }

     Stopwatch sw;
     sw.Start();
     const array_t &query = corpus[0];

     array_t scores( inCorpusSize );
     for ( register size_t i = 0; i < inCorpusSize; ++i )
     {
          const array_t &docu = corpus[1];
          T & score = scores[i];
          score = 0;
          for ( register size_t j = 0; j < inArraySize; ++j )
          {
               score += docu[j]*query[j];
          }
     }

     sw.Stop();
     return sw.Elapsed();
}

int main( int argc, char **argv )
{
     typedef double (*fun_t)( size_t, size_t );

     typedef std::pair < std::string, fun_t > named_fun_t;
     std::vector< named_fun_t > functions;

     functions.push_back( named_fun_t( “float”, test<float>) );
     functions.push_back( named_fun_t( “double”, test<double>) );
     functions.push_back( named_fun_t( “long double”, test<long double>) );
     functions.push_back( named_fun_t( “int”, test<int>) );
     functions.push_back( named_fun_t( “unsigned int”, test<unsigned int>) );
     functions.push_back( named_fun_t( “short”, test<short>) );
     functions.push_back( named_fun_t( “unsigned short”, test<unsigned short>) );
     functions.push_back( named_fun_t( “long long”, test<long long>) );
     functions.push_back( named_fun_t( “unsigned long long”, test<unsigned long long>) );
     functions.push_back( named_fun_t( “char”, test<char>) );
     functions.push_back( named_fun_t( “unsigned char”, test<unsigned char>) );

     if ( argc < 4 )
     {
          std::cout << argv[0] << ” <corpus_size> <array_size> <repeats>” << std::endl;
          return 1;
     }

     int csize = atoi(argv[1]);
     int asize = atoi(argv[2]);
     int iters = atoi(argv[3]);

     typedef std::map<std::string, double> timings_t;
     timings_t timings;

     for ( size_t j = 0; j < iters; ++j )
     {
          std::cout << \niteration #” << j+1 << std::endl;
          for ( size_t i = 0; i < functions.size(); ++i )
          {
               std::string name = functions[i].first;
               double timetaken = functions[i].second(asize, csize);

               timings[name] += timetaken;
               std::cout << “testing “ << name << “….” << timetaken << std::endl;
          }
     }

     std::string minstring;
     double minvalue = 99999.9;
     std::cout << \naverages” <<std::endl;
     for ( timings_t::iterator iter = timings.begin(), end = timings.end(); iter != end; ++iter )
     {
          iter->second /= (double)iters;
          std::cout << “average time for “ << iter->first << “: “ << iter->second << std::endl;

          if ( iter->second < minvalue )
          {
               minvalue = iter->second;
               minstring = iter->first;
          }
     }

     std::cout << “minimum timing is for “ << minstring << “: “ << minvalue << std::endl;
}

The result of running this script with vector size = 500 and corpus size = 1 million with 20 iterations is shown below:

iteration #1
testing float….1.473
testing double….1.469
testing long double….2.025
testing int….1.024
testing unsigned int….1.069
testing short….0.996
testing unsigned short….0.993
testing long long….1.004
testing unsigned long long….1.004
testing char….0.991
testing unsigned char….0.991

iteration #2
testing float….1.449
testing double….1.459
testing long double….2.011
testing int….1.002
testing unsigned int….1
testing short….0.985
testing unsigned short….0.99
testing long long….0.993
testing unsigned long long….0.993
testing char….0.983
testing unsigned char….0.992


..
iteration #20
testing float….1.448
testing double….1.45
testing long double….1.975
testing int….0.99
testing unsigned int….0.991
testing short….0.989
testing unsigned short….0.986
testing long long….0.997
testing unsigned long long….0.992
testing char….0.992
testing unsigned char….0.988

averages
average time for char: 0.99575
average time for double: 1.4665
average time for float: 1.4676
average time for int: 1.0046
average time for long double: 2.00115
average time for long long: 1.00585
average time for short: 0.99985
average time for unsigned char: 0.9945
average time for unsigned int: 1.0085
average time for unsigned long long: 1.0025
average time for unsigned short: 0.9963
minimum timing is for unsigned char: 0.9945

So, even in this test, char is the best choice!!

Graphics not working in L702X Dell XPS 17 while installing Linux Mint 12

January 30th, 2012 No comments

Looks like the faulty nouveau driver is creating the fault during boot up. Without nomodeset and/or acpi=off, booting leads to segmentation fault by the nouveau module. When I disable the nouveau module by blacklisting it under modprobe.d, I am able to boot up cleanly using the Intel Sandybridge Mobile graphics driver.  After updating the blacklist configuration, remember to run update-initramfs -u.

Code:
filename: /etc/modprobe.d/nvidia-nouveau

# disable nouveau driver
blacklist nouveau
options nouveau modeset=0
Tags:

Temperature Monitoring Device

January 1st, 2012 No comments

LM35 Temperature Sensor

The System.

The monitor.

The mixed water line:

The hot water line:

The cold water line:

monitor opened up:

the atmega8 microcontroller

the lcd unit:

theboard:

inside the monitor:

The AVR code.

const int COLD = 0, HOT = 1, MIXED = 2, CALIBRATE = 3;
const int PWMPORT = 5;

float SCALE = 5000.0/1024.0; // 10 bit resolution for ADC
const float LM35SCALE = 10; // 10mV per Centigrade

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8 );

byte smiley[8] = {
  B00010,
  B00101,
  B00010,
  B00000,
  B00000,
  B00000,
  B00000,
};

float Calibrate( void )
{
  // write 5v to PWM port
  analogWrite(PWMPORT, 255);

  // read the 5v analog value in the calibrate port
  delay(500);
  int val = analogRead(CALIBRATE);

  // read again the 5v analog value in the calibrate port
  delay(100);
  val = analogRead(CALIBRATE);

  // whatever digital value we read is the range of output that we would get for 5v input.
  // so, set the scale appropriately.
  SCALE = 5000.0/(float)val;
  
  return SCALE;
}

void setup()
{
  pinMode(PWMPORT, OUTPUT);
  
  lcd.createChar(0, smiley);
  lcd.begin(16,2);
  
  lcd.setCursor(0,0);
  lcd.print(“Calibrating..”);
  float scale = Calibrate();
  lcd.setCursor(0,1);
  lcd.print(“Scale=”);
  lcd.print(scale);
  delay(1000);
}

int Temp( int inADC )
{
  float lm35volts = (float)inADC * SCALE;
  float temp = lm35volts/LM35SCALE;
  
  return (int)temp;
}

int ReadData( int port )
{
  int data = -1;
  for ( int i = 0; i < 3; ++i )
  {
    data = analogRead( port );
    delay(100);
  }
  
  return data;
}

void loop()
{
  int cold = ReadData( COLD );
  int hot = ReadData( HOT );
  int mixed= ReadData( MIXED );
  
  int cold_temp = Temp(cold);
  int hot_temp = Temp(hot);
  int mixed_temp = Temp(mixed);
  
  lcd.clear();

  lcd.print( “C=” );
  lcd.print( cold_temp );
  lcd.write(0);
  lcd.print(“C”);
  lcd.print( ” H=” );
  lcd.print( hot_temp );
  lcd.write(0);
  lcd.print(“C”);
  
  lcd.setCursor(0,1);
  lcd.print( “Mixed=” );
  lcd.print( mixed_temp );
  lcd.write(0);
  lcd.print(“C”);
  
  delay(1000);
}

kDevelop 4.2 Debugging

November 7th, 2011 No comments
    When I was using KDevelop 4.2 for debugging a process, I got perplexed having the inferior application closing down whenever I was setting a breakpoint on the fly.  On the contrary, the breakpoints there were setup before the start of the inferior application, things behave as expected.  With little bit of digging, it is found that whenever a new breakpoint is setup or removed (Toggle Breakpoint), kDevelop inherently issues a SIGINT to the inferior application, which in turn stops the application from debugging.  To handle this problem, setup a signal handler for SIGINT, to consume the Interrupt signal (Ctrl+C, Ctrl+Break).  But once the debugging is over, don’t forget to reset the SIGINT handler.

Hash Overflow due to 64 bit upcasting

October 28th, 2011 No comments
    Lately, I had to debug the following piece of code, where it caused overflow on the hash bucket design.  The code worked perfectly on a Windows machine while compiled for Win32, but failed to work on a Linux Mint x64 machine.  The code is listed below, which basically calculates hash value of an input 32 bit unsigned number, limiting the hash value to 2^10 (1Meg).

hash = ( fpArray*2654404609 )>>12; // Calculate the hash and limit the value to 2^20 (1 Meg)

   When the input value for fpArray was 1724463449 (0x66C93959), the hash value generated was 1779068547 (0x6A0A6E83), which is more than (0x000FFFFF) to cause the hash bucket overflow.

unsigned hash = fpArray * 2654404609;
hash = hash >> 12;

    When I rewrote the code like the above, the value of hash was 2800236889 (0xA6E83959).  Upon shifting right by 12 yields 638651 (0x0009BEBB), which is the correct and expected hash value.

    Overall, the first snippet of code appears to be correct.  Do you see a problem there?  I couldn’t find the issue, until I recalled the 32bit vs 64bit difference.  If you carefully look at the multiplier 2654404609 (0x9E370001), although appears to be a valid 32 bit number, what is the default assignment of type to this number by the compiler?  If it was assigned 64bits, what would happen to the results?  To validate this, I changed the 2nd snippet as the following.

unsigned long hash = (unsigned long)fpArray * 2654404609;
hash = hash >> 12;
unsigned h2 = (unsigned)hash;

    Now, when the input is the same 1724463449 (0x66C93959), the value of hash becomes 4577423727077636441 (0x3F8646A0A6E83959) and upon right shifting by 12 bits yields 1117535089618563 (0x0003F8646A0A6E83). Followed by downcasting to unsigned yield 1779068547 (0x6A0A6E83). Bingo!

    So, what is happening here? While performing (fpArray * 2654404609), the computation is upcasted to 64bit computation by the 64 bit compiler.  So, what is the solution? Just put a “U” at the end of the constant.

hash = ( fpArray*2654404609U )>>12; // Calculate the hash and limit the value to 2^20 (1 Meg)
(or)
const unsigned multipler = 2654404609; // here U suffix is not needed as the constant is explicitly made unsigned
hash = ( fpArray * multiplier ) >> 12;

    Now, the computation will happen with 32 bit numbers to get the expected outputs.

Lessons Learned here:

  1. While using constants, beware of the upcasting and downcasting. So use proper suffixes like U, L, F etc.
  2. Instead of using constants directly in expressions, use them as constant variables.
  3. Be conscious about the compiler type and the assumptions made by the compiler in different build modes.

kDevelop Editing Freezes Problem

August 20th, 2011 No comments
While working with KDevelop 4.2, I was quite disturbed by suddenly freezing of editing window. In the sense, the opening file is uneditable, “find” strings does not work and literally kDevelop becomes unusable. I had to restart kDevelop 4.2 to resume editing the code. I believed that kDevelop is going into Vi mode and was trying to undo that. But that’s not the problem.

When kDevelop loses focus, somehow the editing capability is disabled temporarily. When you switch context a couple of time by Alt-Tab and land is kDevelop again, the editing capability is resumed. I don’t know whether this is a bug, but context-switching has been my breather for now.

PROXIM ORINOCO 802.11 a/b/g/n

June 30th, 2011 No comments

Did you try this page for the driver?
http://list.driverguide.com/list/company861/LINUX/

Procedure for setting up proxim driver is given here:-
http://questier.com/howto.html#Proxim

If you want to try an alternate device, use this list linux compatible devices:-
http://www.cyberciti.biz/tips/linux-usb-wireless-compatibility-adapter-list.html

How to on Wireless networking:-
http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch13_:_Linux_Wireless_Networking

If there is a windows driver, you can use “ndiswrapper” to setup a Linux module atop the windows driver sys and inf files. Try this as well.