Saliya's Blogs

Mostly technical stuff with some interesting moments of life

Weekend Carpentry: Shaver Holder

Nothing fancy, just a simple holder to keep my shaver in place. The hole is cut at an 30 degree angle. Initially I wanted to hide the tenon completely inside the mortise, but accidentally route the mortise all the way. Anyway, once fixed the joint is not visible.


Running Chapel Programs on Titan

Last week I had the opportunity to use Titan, #2 supercomputer in the world as of June 2013, as part of the ATPESC program hosted by Argonne labs. Titan is a Cray XK7 machine and its user’s guide is available here.
The following is a quick start to get Chapel programs run on multi-locale with Titan.
  • Modules – the default set of modules is all what you need. You may load PrgEnv-cray/4.1.40 instead of the one loaded.
  • Environment – you’ll need to have the following variables exported, usually through ~/.bashrc (in my case I used Chapel 1.7.0)
export CHPL_LOC=/tmp/work/csep65/chapel-1.7.0
export CHPL_HOME=$CHPL_LOC
export CHPL_COMM=gasnet
export GASNET_QUIET=yes
export CHPL_HOST_PLATFORM=cray-xk
export CHPL_LAUNCHER=pbs-aprun
export CHPL_LAUNCHER_QUEUE=batch
export CHPL_LAUNCHER_WALLTIME=00:10:00
export CHPL_LAUNCHER_ACCOUNT=TRN001
export CHPL_HOST_COMPILER=gnu
export PATH=${CHPL_LOC}/bin/$CHPL_HOST_PLATFORM:$PATH
Also, note the values in red need to be changed accordingly. The CHPL_LAUNCHER_ACCOUNT will be the account in Titan to get billed. You can find more information on this in the $CHPL_HOME/doc/platforms/README.cray (search for NCCS).
  • Launcher tweak – note this fix is avaialble in Chapel code trunk following revision 21702 (http://sourceforge.net/p/chapel/code/21702/tree/trunk/) so will be necessary only if you are using a release 1.7.0 or older. A minor change to Chapel’s PBS launcher is necessary for these versions to work with Titan due to some upgrade in the qsub wrapper used by Titan. The fix is to change the two occurrences of –l size with –l nodes in $CHPL_HOME/runtime/src/launch/pbs-aprun/launch-pbs-aprun.c
  • Compile Chapel – once the above steps are completed use the Makefile under $CHPL_HOME to compile Chapel as usual
  • Compile your program – use the chpl compiler to compile your program, which will create two outputs, for example a.out and a.out_real. More information on what each of this means is available in the $CHPL_HOME/doc/README.multilocale
  • Start your program – you can use, for example ./a.out –nl 4 to launch your program on 4 locales. Chapel launcher will take care of submitting the correct PBS job for Titan’s job scheduler.
This is pretty much what you need to do and hope it helps!

Tail Recursion with Python

Nice article on tail recursion with Python at http://web.mit.edu/kmill/www/programming/tailcall.html

Weekend Carpentry: Wall Shelf

Well, it wasn't really a weekend project, but it could have been, hence the title.

Update Aug 2014
 
     Sketchup file at https://www.dropbox.com/s/0qa79linxceagwr/shelf.skp
     PDF file at https://www.dropbox.com/s/5iaalnhmfmkkke8/Shelf.pdf


If you like to give it a try, here's the plan.




The top two are the vertical and horizontal center pieces. The last four pieces are for top, bottom, left, and right dividers. The joints are simply half lap joints (see  http://jawoodworking.com/wp-content/uploads/2008/09/half-lap-joint.jpg).

Just remember to finish wood pieces before assembling. It's much easier than having to apply finish to the assembled product, which unfortunately is what I did.

How to Remove Shelf Liner Adhesive

Never ever use adhesive shelf liners, they suck!

Anyway, since you are here already, here are few tips to help you out.

1.) If you can, use a hairdryer to apply heat while pulling the liner.

2.) No matter how much caution you take, these will always leave a layer of sticky residue behind. This is where it sucks.

2.1) Use WD-40 (http://en.wikipedia.org/wiki/WD-40) and scrape off (using a plastic ruler edge or a small scraper) the sticky mess. You’ll have to do this multiple times and when you scrape use smooth single strokes rather going back and forth.

2.2.) Once you remove bulk of the goo, you’ll still end up having a sticky surface. This is where you need to use coconut oil. Apply oil to a piece of cloth and rub to remove the remaining left over adhesive.

3.) Once all this is done, it’s a good idea to wash away any remaining WD-40 and coconut oil with soapy water and wipe clean with a piece of cloth or sponge.

Excel Histograms

In case if you were looking to generate histograms in Excel http://support.microsoft.com/kb/214269

C# String Vs Secure String

Interesting articles on the subject at,

1.) http://blogs.msdn.com/b/shawnfa/archive/2004/05/27/143254.aspx

2.) http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly-convert-securestring-to-string.aspx

In summary, a string object is not the best place to keep sensitive information such as passwords, credit card numbers, etc. due to its nature of not being able to lock in one place of memory, immutability (results multiple copies if modified), and possible page swaps (sensitive data now in tmp files) (see 1). Use SecureString as a solution, but sometimes you may still want to get the unsecured string representation as some APIs still don’t support secure strings (see 2).

While at it, you may also like to read about reading a string securely from console,

3.) http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx

4.) http://stackoverflow.com/questions/3404421/password-masking-console-application

Running MPI.NET Applications with Mono in Ubuntu

Sometime back I played around Mono to get some of our parallel applications running on Ubuntu. These applications were C# based and used MPI.NET.

The following blog post is a great starting point with all the details you'll need. So I'll skip the steps, except to point out couple of caveats you need to consider.

http://blog.biophysengr.net/2011/11/compiling-mpinet-under-ubuntu-oneiric.html


  1. automake versions above 1.9 will give you an error when building MPI.NET. May be you can change the make script to work with them, but I found it easy to just install automake1.9 to solve it.
  2. You'll need to add /usr/local/lib to your LD_LIBRARY_PATH. Essentially what you need to do is add this path to /etc/ld.so.conf and run ldconfig as root. See documenation from Mono on this at http://www.mono-project.com/DllNotFoundException
  3. Make sure to do chmod +x to your dlls

Repair Mr.Coffee (IDS77) Thermal Fuse

If you haven't bought this product yet, then STOP don't buy it!!

If you, however, have bought it and broke it in the first run then continue.

Mine simply stopped working right on the day I bought it, in fact this is the second Mr. Coffee grinder I bought that day. May be I was trying to grind too much, but as a consumer device I'd expect it to "auto shut off" if it's too hot, rather burn itself.

The good news is, it only burns a thermal fuse, which is fairly easy to replace if you get under the hood. Once you remove the grinder cup you can see the following,

Just remove the three plastic hole cover knobs, which you can simply pull out using tweezers or by lifting one side with a sharp point like that of a knife. Once removed these knobs look as shown below.

Get a small flat head screw driver and remove the three screws. Then you can simply take out the motor compartment. You'll need to pop the button panel to find a bolt holding the circuit board. You'll be able to figure out hopefully.

Once the motor is out you can see the thermal fuse wrapped inside the yellow tape covering the motor winding as shown. I cut the pins of the fuse and it's shown left to the motor. 

Now it's time to find a replacement. The original fuse that's there in this one comes from China and here's a link I could find on it. A close up picture is given below.

I found several options in ebay, but either they had low ampere rating or too high functioning temperature. Also, it'd take more than a week to arrive. In the end, I decided to go with an alternative one from RadioShack, which in fact is cheaper (~$1.40) than options from ebay.

If you need some instructions on how to solder these look at this. This one has a high ampere rating, but from what I read having a higher ampere rating than the one used does no harm. It's the temperature that's important.

Once soldered, the unit is alive again. This time I will not grind coffee continuously though :)





Homemade Meditation Benches

My friend, Lahiru, suggested  to make few meditation benches as a donation to Indiana Buddhist Temple few weeks back. I had no idea what a meditation bench was, but luckily he had a basic one with him. After few modifications to the design, here’s what we ended up with.
DSC_0003 DSC_0010 (2)
DSC_0015 (2) DSC_0163

If you like to make one at home too, here are the details.
I think this covers most of the stuff you need to complete this project and any questions are welcome in comments. Happy bench making !!

Notes on Windows PowerShell

  • Is bit slow and creepy ;) but better than command line
  • Running as administrator helps you overcome many of the access denied situations
  • PowerShell script files are simple text files with .ps1 extension
    • To run a script file,
      • Open a PowerShell instance (preferably as administrator) 
      • Type the path to .ps1 file and hit enter
    • If you get an error due to execution-policy is restricted, try
      set-executionpolicy remotesigned 
    • The above will ask for your permission. To avoid that and force use
       set-executionpolicy remotesigned -force
    • If you want this to be done across a Windows HPC cluster using clusrun command
      $nodes="node1,node2,..,noden"
      clusrun /nodes:$nodes powershell set-executionpolicy remotesigned -force
  • To start a new process through a script - http://technet.microsoft.com/en-us/library/hh849848.aspx
    start-process command "options and args"

    • If you want it to be on the same window and wait for it to complete use
      start-process command "options and args" -NoNewWindow –Wait
  • Invoke a command on a remote machine - http://technet.microsoft.com/en-us/library/hh849719.aspx
  • Invoke-Command -ComputerName $node -ScriptBlock {Start-Service MyService}

    Handling variables

    • Local variables
    • $x=10
      $path="C:\users\dd"

    • Command line parameters
    • $param0=$args[0]
      $param1=$args[1]

    • Environment (User/System) variables
    • $javaBin=$env:JAVA_HOME + "\bin"

      • A better alternative – number 2 indicates system variable. Use number 1 for user variable
      • $javaHome=[Environment]::GetEnvironmentVariable("JAVA_HOME",2)

      • Similarly to set a value – again number 2 is for system level.
      • [Environment]::SetEnvironmentVariable("Path","$tmp",2)

  • String operations (few)

    • Concatenation
    • $name="John"
      $x="hello " + $name + "! How are you ?"

    • Substring
    • $name.Substring(1) // "ohn"
      $name.Substring(1,2) // "oh"

    • StartsWith
    • $name.StartsWith("hello") // true

    • IndexOf
    • $name.IndexOf("ohn") // 1

    • Length
    • $name.Length // 4

    • Join paths
    • $javaHome=$env:JAVA_HOME // e.g. "C:\Program Files\Java\jdk1.7.0_10"
      $javaBinString=join-path -path $javaHome "bin" // e.g. "C:\Program Files\Java\jdk1.7.0_10\bin"

  • Loops – a good tutorial at http://www.powershellpro.com/powershell-tutorial-introduction/logic-using-loops/

    • For loop
    for($i=1;$i -le 10; $i++) {
      //body
    }
      • -le is <=
      • -lt is <
      • -gt is >
      • -eq is == in usual programming language notation

    • Foreach loop
    $jars=ls ($env:TWISTER_HOME + "\lib") *.jar
    foreach ($jar in $jars) {
     $jar=$jar.DirectoryName + "\" +  $jar.Name
     $cp=$jar+";"+$cp
    }
  • .NET runtime directory
  • $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
This is pretty much I found useful for my work. Feel free to suggest any.

Study of Biological Sequence Structure: Clustering and Visualization

K-Means Clustering with Chapel

K-Means clustering is simple to comprehend, yet is a good candidate to benefit from parallelization. The idea is to partition N data points into K clusters by assigning points to the nearest cluster center. The program starts with a random K number of centers. These centers are then refined iteratively by taking the mean of nearest points to each center. The refinement continues until the change in centers falls below a predefined threshold or until a fixed number of iterations have completed.

Full working code is at https://www.dropbox.com/s/ikfktb3sswmc8vy/kmeans.chpl
  • Declaration of points, dimension of a point, iterations, and number of clusters. Note. These are configurable at start time by passing them as command line arguments. For example passing --numPoints 50000 will set the number of points to 50000 instead of 2000.
     config var numDim: int = 2,
                numClusters: int = 4,
                numPoints: int  = 2000,
                numIter: int = 50,
                threshold: real = 0.0001;
  • Definitions of domains. The {0 .. #N} notation indicates the integer range starting at zero and counting up to N number of values, i.e. 0, 1, 2 ... N-1
     const PointsSpace = {0..#numPoints,0..#numDim};
     const ClusterSpace = {0..#numClusters,0..#numDim};
     const ClusterNumSpace = {0..#numClusters};
  • Block distribute points along zeroth dimension across locales. The array Locales and the numLocales are made available to programs by the Chapel runtime and we have reshaped the locales into a two dimensional array of numLocales x 1 size. This ensures components of each point stay in the same locale when blocked. 
     var blockedLocaleView = {0..#numLocales,1..1};
     var blockedLocales: [blockedLocaleView] locale = reshape(Locales, blockedLocaleView);
     const BlockedPointsSpace = PointsSpace 
       dmapped  Block(boundingBox=PointsSpace, targetLocales=blockedLocales);
     var points: [BlockedPointsSpace] real;
  • Arrays to hold current centers. Chapel’s rich domain operations make it possible to assign a subset of points as current centers by specifying a range as shown in second line. 
     var currCenters: [ClusterSpace] real;
     currCenters = points[ClusterSpace];  
  • Replicated arrays to keep local centers and updates. Chapel allows transparent access to array elements on remote locales; however, performance may suffer when data is transferred over network. Therefore, it is beneficial to use local arrays when operating on points in a particular locale. Chapel provides a convenient distribution to achieve this, ReplicatedDist, which creates a copy of the array in each locale. The array reference resolves to the local copy in the particular locale where the referring code runs. Also note the use of atomic real and integer variables, which is done as a workaround to non-implemented atomic blocks. 
     const ReplClusterSpace = ClusterSpace dmapped ReplicatedDist();
     var localCurrCenters: [ReplClusterSpace] real;
     // using atomic primitives as a work around to not implemented atomic blocks
     var localCenterUpdates: [ReplClusterSpace] atomic real;
     const ReplClusterNumSpace = ClusterNumSpace dmapped ReplicatedDist();
     var localCenterPCounts: [ReplClusterNumSpace] atomic int;
  • The next steps happen iteratively while refining centers.
    • We start by resetting local arrays as follows. The first line copies the current centers to localCurrCenters array in each locale. The ReplicatedDist in Chapel guarantees the array assignment in the first line happens in each locale. The next two lines initialize the two local arrays, i.e. localCenterUpdates and localCenterPCounts, to zero. Again, the distribution guarantees the two forall loops happen for each local copy of the arrays. These three statements are run in parallel by wrapping inside a cobegin clause.          
          cobegin {
            localCurrCenters = currCenters;
            forall lcu in localCenterUpdates do lcu.write(0.0);
            forall lcpc in localCenterPCounts do lcpc.write(0);
          }
    • Next is to compare the distance for each point against all cluster centers and decide the cluster it belongs to.  Note the shifting of locales using the on clause in line 2, which in turn guarantee the access of current centers, center updates, and center point counts arrays local to the particular locale. We have placed the atomic construct to show where the updates should be done atomically, but it is not implemented in Chapel yet. However, the use of atomic variables, overcomes this and guarantees proper atomic updates. Also note if the number of clusters was large, the for loop could be changed to the parallel forall version with slight modification to the code. Moreover, if parallel task creation was unnecessarily expensive one could easily change the code to use serial execution.
          forall p in {0..#numPoints} {
            on points[p,0] {
              var closestC: int = -1;
              var closestDist: real = MaxDist;
              for c in {0..#numClusters} { // possibility to parallelize
                var dist: atomic real;
                dist.write(0.0);
                forall d in {0..#numDim} {
                  var tmp = points[p,d] - localCurrCenters[c,d];
                  dist.add(tmp*tmp);
                }
                if (dist.read() < closestDist) {
                  closestDist = dist.read();
                  closestC = c;
                }
              }
              forall d in {0..#numDim} {
                atomic { // here's where we need atomic
                  localCenterUpdates[closestC,d].add(points[p,d]);
                }
              }
              localCenterPCounts[closestC].add(1);
            }
          }
    • Then we collate centers in each locale. Note the use of nested forall and cobegin constrcuts. Also, the tmpLCU and tmpLCPC arrays are not distributed, yet Chapel gives seamless access to their elements even when the referring code runs on remote locales.
          var tmpLCU: [ClusterSpace] atomic real;
          forall tlcu in tmpLCU do tlcu.write(0.0);
          var tmpLCPC: [0..#numClusters] atomic int;
          forall tlcpc in tmpLCPC do tlcpc.write(0);

          forall loc in Locales {
            on loc do {
              cobegin {
                forall (i,j) in ClusterSpace {
                  tmpLCU[i,j].add(localCenterUpdates[i,j].read());
                }
                forall i in {0..#numClusters} {
                  tmpLCPC[i].add(localCenterPCounts[i].read());
                }
              }
            }
          }
    • Finally, we test for convergence. If it has converged or the number of iterations has exceeded the given limit we will stop and will print results.
          var b: atomic bool;
          b.write(true);
          forall (i,j) in ClusterSpace {
            var center: real = tmpLCU[i,j].read()/tmpLCPC[i].read();
            if (abs(center - currCenters[i,j]) > threshold){
              b.write(false);
            }
            currCenters[i,j] = center;
          }
          converged = b.read();

P.S. I thought I should mention here that this code may not be the most optimal due to the fact that for each point it'll do a locale shift. Ideally, I'd like to implement this in a fashion as "for each locale do for each point", but this requires you knowing the points assigned to a particular locale. Currently, Chapel doesn't have a readily available method to retrieve this info. However, Chapel developers suggested some workaround to my mail on Identifying Local Indices of Distributed Arrays

Automate Phylogenetic Tree Coloring: Dendroscope

Dendroscope (http://ab.inf.uni-tuebingen.de/software/dendroscope/welcome.html) is a neat tool that we (SALSA group in IU) have been using in our research work related to phylogenetic trees. Recently we encountered a scenario where leaf nodes need to be colored according to a corresponding cluster number. We already had generated the mapping from leaf node labels to RGB color strings. To complicate it further, we wanted to color the “lowest single ancestor” (LSA) induced sub trees for each cluster in the same color.

Hard Labor

Open tree file using Dendroscope –> select leaf nodes for a particular cluster –> click “Select –> Advanced Selection –> LSA Induced Network” –> Ctrl+J –> check “Label Color” and “Line  Color” –> pick the desired color –> hit “close” –> click somewhere to unselect nodes/edges
Repeat this (except opening file) for each cluster.

Hard Labor with Pain Killer

You can avoid the step in bold above by having a text file with label names for the particular cluster. Then after opening the tree file you can hit Ctrl+f and specify your text file there (using the open folder icon in that). This will go through each line of the text file and select the labeled leaf nodes. This avoids having to click numerous times just to select :). Still you have to go through the pain of selecting the LSA sub tree and modifying colors.

No Pain, YES! gain

The clean way is to automate this process and Dendroscope facilitates this through its command line. Here’s what you need to generate using a simple script written by you.
open file=<path-to-your-tree-file>;select all;set edgewidth=2;set color=0 0 0;set labelcolor=0 0 0;deselect all;
find searchtext=abc;find searchtext=def
select LSA induced network;set color=r g b;set labelcolor=r g b;deselect all;
Some explanation please …
Line 1 will simply open your tree file and set all the edges to a width of 2 and everything to black
For each label in a particular cluster you need to add find searchtext=<label>; as in Line 2 (remember to separate with semi-colon)
Line 3 will select the LSA sub tree for the labels in Line 2 and line and label color to whatever color specified by r g b values
You need to include corresponding Line 2 and 3 for each cluster.
Once you generate this file simply open Dendroscope and go to “Window –> Command Input”. In the pop-up command window type source file=<path-to-your-generated-file> and hit “Apply”. That’s it enjoy!!
A good reference is available in Dendroscope’s manual at http://ab.inf.uni-tuebingen.de/data/software/dendroscope3/download/manual.pdf

How to Compute The Sum of Squares for First N Integers

We need to find,
clip_image002[7]
answer 
clip_image002[47]
How to get that?
Let’s consider the expansion of clip_image002[9]
clip_image002[11]
Therefore,
clip_image002[13]
Now let’s substitute values for clip_image002[15]
clip_image002[21]
when
clip_image002[17]
clip_image002[23]
when
clip_image002[25]
…
clip_image002[27]
when
clip_image002[29]
clip_image002[31]
when
clip_image002[33]
if you sum them from top to bottom
clip_image002[35]
Taking the sum of arithmetic series,
clip_image002[37]
Therefore,
clip_image002[39]
clip_image002[43]
clip_image002[45]
Special thanks to my friend Jayampathi Rathnayake (http://www.math.indiana.edu/people/profile.phtml?id=jratnaya) for his insight on the approach.

Another good resource http://www.trans4mind.com/personal_development/mathematics/series/sumNaturalSquares.htm

Drilling Straight with a Hand Drill

If you don’t have a drill guide like http://www.sears.com/shc/s/p_10153_12605_00967173000P?BV_UseBVCookie=Yes&vertical=TOOL&pid=00967173000 then drilling straight using a hand drill will be a pain and will often result angled holes.

Here’s a simple and elegant (though not precise) solution. You will need a square ruler like http://image.made-in-china.com/2f0j00tvREcekzhJbq/Angle-L-Square-Ruler.jpg. The trick is that all drills have visible center line. Use the square ruler to guide this in a straight line.

image

When you drill without a guide as shown above, the drill is free to move along both blue and red axes. You can restrict this by using it as shown below with the square ruler.

image

The square ruler has a flat edge, so when you keep it as above, it will stay perpendicular to the drilling surface. You can rotate the drill to have its center line and the square ruler to be in the same vertical plane as blue axis is. Then, with bit of patience, you can guide the drill while keeping the center line in the same plane. Still the drill is free to move in both directions, but you can use the ruler to guide it very well restricting any movement along red axis.

OK, what about movement along blue axis? This is where you need to have some practice. You can use your eyesight to keep the distance between drill bit and the vertical edge of the ruler constant while drilling to make sure you are not moving along blue axis.

As I said earlier, this is not perfect or highly precise, but when you don’t have any the fancy guiding tools, this works like a charm.

 

Image source for the drill, hand, and wooden piece http://en.wikipedia.org/wiki/File:Drill_scheme.svg

Weekend Carpentry: Monitor Stand

I’ve been wanting a monitor stand for sometime now as the Dell monitor that I am having doesn’t have an adjustable stand. Apparently, these stands cost around $30-40 and it didn’t seem worth the money.

So I ended up building one out of some old furniture parts I had. In the end it turned out to be better than I expected :). In fact comparable to what you can buy online http://www.amazon.com/OFC-Express-Monitor-Stand-Black/dp/B0020LB28Q/ref=sr_1_9?s=office-products&ie=UTF8&qid=1345923737&sr=1-9&keywords=monitor+stand.

Here are some photos.

IMG_4733 IMG_4735

Finished product

It has enough space underneath to store my files

IMG_4743 IMG_4742

Top view

Bottom view

IMG_4740 IMG_4746

Just before I cleaned up the place

 

My old monitor stand; just a cardboard box from Amazon :)