Saliya's Blogs

Mostly technical stuff with some interesting moments of life

C# and Java Operators Thread Safety

No comments
Java and C# both have compound assignment operators and the increment/decrement operators. For example see http://msdn.microsoft.com/en-us/library/6a71f45d.aspx for a list of C# operators and http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html for the Java version.

It seems these two step operations, i.e. x op= y, x++, x--, ++x, and --x, are NOT thread safe. It makes sense as these operations require reading the current value of the given variable and then updating it with the computed value. It is possible for threads to interleave between these operations resulting erroneous final values.

There are many ways to guarantee the atomicity in such cases including synchronized blocks, atomic data types such as Java AtomicInteger, and interlocked operations like in C#.

Here are some links that may help,

C# interlocked methods: http://msdn.microsoft.com/en-us/library/System.Threading.Interlocked_methods.aspx

Java atomic classes http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.html

C# library to do threading similar to OpenMP like constructs http://msdn.microsoft.com/en-us/library/dd460717.aspx

A Java alternative OpenMP like parallel constructs https://wiki.rice.edu/confluence/display/HABANERO/Habanero+Multicore+Software+Research+Project


No comments :

Post a Comment

Weekend Carpentry: Shaver Holder

No comments
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.


No comments :

Post a Comment

Running Chapel Programs on Titan

No comments
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!

No comments :

Post a Comment

Tail Recursion with Python

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

No comments :

Post a Comment

Weekend Carpentry: Wall Shelf

No comments
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.

No comments :

Post a Comment

How to Remove Shelf Liner Adhesive

No comments

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.

No comments :

Post a Comment

Excel Histograms

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

No comments :

Post a Comment

C# String Vs Secure String

No comments

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

No comments :

Post a Comment

Running MPI.NET Applications with Mono in Ubuntu

No comments
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

No comments :

Post a Comment

Repair Mr.Coffee (IDS77) Thermal Fuse

9 comments
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 :)





9 comments :

Post a Comment

Homemade Meditation Benches

3 comments
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 !!

3 comments :

Post a Comment

Notes on Windows PowerShell

2 comments
  • 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.

2 comments :

Post a Comment

Study of Biological Sequence Structure: Clustering and Visualization

No comments
My presentation in our SALSA HPC weekly meeting.
http://salsahpc.blogspot.com/2013/02/study-of-biological-sequence-structure.html


No comments :

Post a Comment