Saliya's Blogs

Mostly technical stuff with some interesting moments of life

Slow Performance with Intet AC 7260 Wireless Card

I recently purchases an Intel dual band AC-7260 pcie card to replace the existing wireless adapter in my desktop machine. Here’s the amzon link http://www.amazon.com/Intel-Wireless-Bluetooth-Desktops-7260HMWDTX1h-R/dp/B00X68U0SU.

I installed the updated drivers from Intel using their driver udpate utility and all seemed to work fine, until I checked for speed. It was too low. After googling a bit I found this is known to be a case with this card. Then came across this nice blog post (http://blog.benkonicek.com/2014/10/24/fixing-the-intel-wireless-n-7260-connection-issues/) that pointed to some older drivers and they worked pretty well.

Get and Set Process Affinity in C

Affinity of a process can be retrieved and set within a C program using sched_getaffinity (man page) and sched_setaffinity (man page) routines available in the sched.h. The following are two examples showing these two methods in action.

Get Process Affinity

#define _GNU_SOURCE
#include 
#include 
#include 
#include 
#include 

int main(int argc, char* argvi[])
{
    pid_t pid = getpid();
    cpu_set_t my_set;
    int ret;

    CPU_ZERO(&my_set);
    ret = sched_getaffinity(0, sizeof(my_set), &my_set);
    char str[80];
    strcpy(str," ");
    int count = 0;
    int j;
    for (j = 0; j < CPU_SETSIZE; ++j)
    {
        if (CPU_ISSET(j, &my_set))
        {
            ++count;
            char cpunum[3];
            sprintf(cpunum, "%d ", j);
            strcat(str, cpunum);
        }
    }
    printf("pid %d affinity has %d CPUs ... %s\n", pid, count, str);
    return 0;
}
You can test this by using taskset command in linux to set the affinity of this program and checking if the program returns the same affinity you set. For example you could do something like,
taskset -c 1,2 ./a.out
Note, you could use the non-standard CPU_COUNT(&my_set) macro routine to retrieve how many cores are assigned to this process instead of using a count variable within the loop as in the above example.

Set Process Affinity

#define _GNU_SOURCE
#include 
#include 
#include 
#include 
#include 

int main(int argc, char* argvi[])
{
    pid_t pid = getpid();
    cpu_set_t my_set;
    int ret;

    CPU_ZERO(&my_set);
    CPU_SET(1, &my_set);
    CPU_SET(2, &my_set);

    ret = sched_setaffinity(0, sizeof(my_set), &my_set);
    printf("pid %d \n", pid);

    // A busy loop to keep the program from terminating while
    // you use taskset to check if affinity is set as you wanted
    long x = 0;
    long i;
    while (i < LONG_MAX)
    {
        x += i;
        ++i;
    }
    printf("%ld\n",x);
    return 0;
}
The program is set to bind to cores 1 and 2 (assuming you have that many cores) using the two CPU_SET macro routines. You can check if this is set correctly using the taskset command again. The output of the program will include its process id, say pid. Use this as follows to check withtaskset.
taskset -pc pid
Note, I've included a busy loop after printing the pid of the program just so that it'll keep running while you check if affinity is set correctly.

සෝමසිරි...’n’ සයිලන්ට්

‘පොෂ්’ වීම ජීවිතය කරගත් මල් සිරි අප්පුහාමිලාගේ සෝමසිරි  තමාගේ නම ‘මෙල් සිරප් සෝම්’ ලෙස වෙනස් කර ගත්තේ ද වෙනකක් නිසා නො ව තම ගමට නිරන්තරයෙන් සිදු වන වල් අලි කරදර වාර්තා කිරීමට එන ඉංග්‍රීසි ජන මාද්‍ය හමුවේ පහසුවෙන් උච්චාරණය කිරීමට ය. එමෙන් ම හේනේ වැවෙන කැකිරිවලට ‘කෙකරි’ කීමටත් වම්බටු, මුරුංගා, බණ්ඩක්කා ආදි මෙකී නො කී සෑම දෙයක් ම පාහේ ‘වැදගත්’ ලෙස උච්චාරණය කිරීමටත් ඔහු වග බල ගත්තේ ය.  මෙම වෙනස හමුවේ ගම් වැසියෝ සෝමසිරි කතා කරන්නේ බුරියෙන් යැයි කීමට පුරුදු වූහ. නූගත් ගැමියන් කුමක් කීවත් ඒවා හමුවේ නො සැලෙන “ඩෙයිරයක්” තමන් සතු බව ඔහු නිතර ම කීවේ ය.

කැලය මාරු කළත් කොටියාගේ පුල්ලි මාරු නො වන්නා සේ ම ශබ්දය වෙනස් වුනත් වචන සියල්ල සිංහල වීම සෝමසිරිට මදි පුංචිකමක් විය. සිංහල වචන අතර නැගෙන ඉංග්‍රීසි වචනය දෙක තිරුවානා ගල් අතර දිලිසෙන මැණික් සේ ඔහුට දිස් වෙන්නට ගත්තේ ද ඒ නිසා ය. ඉංග්‍රීසි බස නිසි ලෙස ඉගෙනීමේ වැදගත්කම කෙසේ වෙතත් මෙය නම් ඔහු විසින් ඉටු කර ගත යුත්තක් ම විය.  සියලු දේ එසැනින් සොයා ගත හැකි අන්තර්ජාලය පිළිබඳ ව සෝමේගේ කණ වැටෙන්නේ මේ අතර තුර ය. හැරෙන පමාවෙන් තම ගමේ සිට හූ සියයක් පමණ දුර සිදාදියේ අන්තර්ජාල ‘කැෆේ’ එකකට ගොඩ වුන සෝමේ තමාගේ වුවමනාව එහි සිටි ‘ටෙක්’ කොලුවාට පවසා සිටියේ ය.

මේ උන්දැ මේ සොයන්නේ ඉංග්‍රීසි කුණු හරප යැයි තම නැණ නුවණින් තේරුම් ගත් කොලුවා ඉස්තරම් ගනයේ ‘වෙබ්’ පිටුවක් සොයා දුන්නේ ය. එපමණකින් නො නැවතුණු ඔහු ඒවායේ සිංහල තේරුම ද සෝමෙට පැහැදිළි කරන්නට විය. කොතරම් ‘පෝෂ්’ වුවත් සුද්දා අමුවෙන් ම කියන මේ වැකි කියන්නට හිත නොදුන් නිසා යන්තම් තෝරා බේරාගෙන එදිනෙදා භාවිත කළ හැකි ‘damn it’ යන්නට සෝමේගේ හිත ගියේ ය.

“අන්කල්, මේක ටිකක් මළ පැනපු බව පෙන්නන්න කියන එකක්. මේකේ ‘n’ සයිලන්ට්, වචන දෙක ම එක දිගට කියල දාන්න ඕනෙ”

‘ටෙක්’ කොලුවා සිදාදියේ බසින් ම සෝමේගේ අලුත් ‘කතුර’ භාවිත කරන අයුරු පැහැදිළි කළේ ය. එවදන් හිස් මුදිනින් පිළි ගත් සෝමේ නැවත ගමට අවේ රටක් රාජ්ජයක් දිනූ පරිද්දෙනි.

පසු දා සති පොළට ගිය ඔහු තම නව සොයා ගැනීම ගැමියන්ට පෙන්නීමට සිතා අහක තිබූ කුඩා ගලක තම කකුල හෙමිහිට වද්දාගෙන එය තමාට සිදු වූ මහා අකරතැබ්බයක් යැයි හුවා දක්වමින් මහා හඬින් ‘damn it’ කීවේ ය. තම හඬ මැකෙනවාත් සමග ම සති පොළේ හූදී ජනයා එකා සේ තමා දෙස නෙත් අයා බලා සිටින බව සෝමේ හොරැහින් නිරීක්ෂණය කළේ ය. ඒ වූ කලී ඔහුගේ ‘පෝෂ්’ දිවියේ වැදගත් ම සංධිස්ථානය විය. කිරි සප්පයාගේ සිට මහල්ලා දක්වා වූ මේ සැම බලා සිටින්නේ ඒ උදාරතර ‘සෝම්’ වන තමා දෙස නො වේ ද? මේ ගම්මානයේ තබා අහල ගම් හතකවත් තමාට සම කළ හැකි අයෙකු වන්නේ ද? එදා ‘බුරි සෝමේ’ කිව් කට කැඩි එවුන් අද මේ හූල්ලන් බලා ඉන්නේ තමා දෙස නොවේ ද? මල් සිරි අප්පුහාමිලාගේ සෝමසිරි හෙවත් ‘මෙල් සිරප් සෝම්’ යන මහා යුග පුරුෂයාගේ කල එළි දැක්ම මේ නො වේ ද?

“මහත්තය කාව ද හොයන්නේ?”

ගමේ පාසලේ ආරියසේන මුල් ගුරුතුමාගේ හඬින් සෝමේ යළි පියවි ලොවට පැමිණයේ ය.

“ආහ්, නැ මේ ප්‍රින්සිපල් මහත්තය මගේ කකුල මේ කරුම ගලේ වැදුන, ඒකයි මේ තරහා ගියේ පොඩ්ඩක්”

“එහෙම ද? අපි එත් බැලු ව මහත්තය ‘දමිත්’ ‘දමිත්’ කිය කියා මේ නැති වුණ බල්ලෙක්වත් හොයනව ද කියල”

“ඔන්න ඉතින් ලොකු මහත්තයා දන්න තරම. දමිත් කියන්නේ කියන්නෙ අනර්ඝ ඉංග්‍රීසි පදයක් නොවැ. කොහේ ද ඉතින් මහත්තයල අලුත් ලෝකෙ තියෙන දේවල් හොයන එකක් යැ. මං මේ අන්තර්ජාලයෙන් තමයි මේවා ඉගෙන ගත්තෙ. ආහ්, මේ තියෙන්නෙ ටවුමේ කඩේ කොල්ල ලියල දීපු කොළේ. ඔය බලන්නකො අපූරුවට ලියල තියෙන්නේ ... ‘ඩී .. ඒ .. එම් ..එන් (සයිලන්ට්) .. අයි .. ටී’ කියල. ‘ඩී .. ඒ .. එම්’ කියන්නේ ‘දම්’, ‘අයි .. ටී’ කියන්නේ ‘ඉත්’. එකට කියවන්නේ ‘දමිත්’ කියල. මහත්තයත් බර කරලා කියල බලන්නකෝ මේකේ තියෙන උජාරුව”



-- සාලිය ඒකනායක --

ඉඩ දෙන්න මා හදට

GLIBCXX_3.4.9 Could Not Be Found with Apache Spark

If you encounter an error similar to the following, which complains that GLIBCXX_3.4.9 could not be found, while running an application with Apache Spark you can avoid this by switching Spark's compression method from snappy to something such aslzf.
...
Caused by: java.lang.UnsatisfiedLinkError: .../snappy-1.0.5.3-1e2f59f6-8ea3-4c03-87fe-dcf4fa75ba6c-libsnappyjava.so: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by.../snappy-1.0.5.3-1e2f59f6-8ea3-4c03-87fe-dcf4fa75ba6c-libsnappyjava.so)
There are a few ways how one can pass configuration options to Spark. The naive way seems to be through command line as,
--conf "spark.io.compression.codec=lzf"
On a side note, you can find what GLIBC versions are available by running strings /usr/lib/libstdc++.so.6 | grep GLIBC
References

Referring Methods that Throw Exceptions in Java

The ability to refer (pass) methods in Java 8 is a convenient feature, however, as a programmer you might face the situtaion where some code that seemingly follow the correct syntax to refer a method that throws an exception gives a compilation error of anUnhandled Exception, which doesn't go away by wrapping the call in a try/catch or adding a throws clause to the method signature. See the following code,
import java.util.function.Function;

public class PassingMethodsThatThrowExceptions {

    public static int addOne(String value) throws NotANumberException{
        int v = 0;
        try{
            v = Integer.parseInt(value);
        } catch (NumberFormatException e){
            throw new NotANumberException();
        }
        return v+1;
    }

    public static void increment(Function<String,Integer> incrementer, String value){
        System.out.println(incrementer.apply(value));
    }

    public static void main(String[] args) {
        increment(PassingMethodsThatThrowExceptions::addOne, "10");
    }
}
This is a simple code, which has
  • an addOne function that takes in a String value representing a number then adds 1 to it and returns the result as an int.
  • an increment function that simply takes a function, which can perform the increment and a value then apply the function to the value.
  • the main method that calls increment with addOne function and value "10"
Note. addOne function is declared to throw possible exception of type NotANumberException (the type of exception is NOT important here).
This code will result in following compilation error,
    Error: java: incompatible thrown types exceptions.NotANumberException in method reference
If you use an IDE such as IntelliJIDEA it'll show Unhandled Exception: NotANumberException for the increment method call in mainand adding try/catch will not work.
What's going wrong here? It's actually a mistake on your end.
The increment function expects a function that takes a String and returns an int, but you forgot to mention that this method may also throw an exception of type NotANumberException.
The solution is to correct the type of incrementer parameter in increment function.
Note. you'll need to write a new functional interface because you can't add throws NotANumberException to thejava.util.function.Function interface that's used to define the type of incrementer parameter here.
Here's the working solution in full.
public class PassingMethodsThatThrowExceptions {
    public interface IncrementerSignature{
        public int apply(String value) throws NotANumberException;
    }

    public static int addOne(String value) throws NotANumberException{
        int v = 0;
        try{
            v = Integer.parseInt(value);
        } catch (NumberFormatException e){
            throw new NotANumberException();
        }
        return v+1;
    }

    public static void increment(IncrementerSignature incrementer, String value) throws NotANumberException {
        System.out.println(incrementer.apply(value));
    }

    public static void main(String[] args) {
        try {
            increment(PassingMethodsThatThrowExceptions::addOne, "10");
        } catch (NotANumberException e) {
            e.printStackTrace();
        }
    }
}
Also, note this is NOT something to do with referring methods or Java 8 in general. You may face a similar situation even in a case where you implement a method of an interface and in the implementation you add the throws SomeException to the signature. Here's a stackoverflow post you'd like to see on this.
Hope this helps!

Blogging with Markdown in Blogger

tl;dr - Use Dillinger and paste the formatted content directly to blogger
Recently, I tried many techinques, which will allow me to write blogs in markdown. The available choice in broad categories are,
  • Use makrdown aware static blog generator such as Jekyll or something based on it like Octopress
  • Use a blogging solution based on markdown such as svbtle
  • Use a tool that'll either enable markdown support in blogger (see this post) or can post to blogger (like StackEdit)
First is the obvious choice if you need total control over your blog, but I didn't want to get into too much trouble just to blog because it involes hosting the generated static html pages on your own - not to mention the trouble of enabling comments. I like the second solution from and went the distance to even move my blog to svbtle. It's pretty simple and straightforward, but after doing a post or two I realized the lack of comments is a showstopper. I agree it's good for posts intended for "read only" use, but usually it's not the case for me.
This is when I started investigating on the third option and thought StackEdit to be a nice solution as it'll allow posting to blogger directly. However, it doesn't support syntax highlighting for code blocks - bummer!
Then came the "aha!" moment. I've been using Dillinger to edit markdown regularly as it's very simple and gives you instant formatted output. I thought why not just copy the formatted content and paste it in the blog post - duh. No surprises - it worked like a charm. Dillinger beatifully formats everything including syntax highligting for code/scripts. Also, it allows you to link with either Dropbox or Github where I use Github.
All in all, I found Dillinger to be the easiest solution and if you like to see a formatted post see my first post with it.

Weekend Carpentry: Baby Gate

My 10 month old son is pioneering his crawling skills and has just begun to cruise. It's been hard to keep him out of the shoe rack with these mobile skills, so I decided to make this little fence.
Download Sketchup file
Download PDF file
Here's a video of the sliding lock mechanism I made.

Running C# MPI.NET Applications with Mono and OpenMPI

I wrote an earlier post on the same subject, but just realized it's not detailed enough even for me to retry, hence the reason for this post.
I've tested this in FutreGrid with Infiniband to run our C# based pairwise clustering program on real data up to 32 nodes (I didn't find any restriction to go above this many nodes - it was just the maximum I could reserve at that time)
What you'll need
  • Mono 3.4.0
  • MPI.NET source code revision 338.
      svn co https://svn.osl.iu.edu/svn/mpi_net/trunk -r 338 mpi.net
    
  • OpenMPI 1.4.3. Note this is a retired version of OpenMPI and we are using it only because that's the best that I could get MPI.NET to compile against. If in future MPI.NET team provides support for a newer version of OpenMPI, you may be able to use it as well.
  • Automake 1.9. Newer versions may work, but I encountered some errors in the past, which made me stick with version 1.9.
How to install
  1. I suggest installing everything to a user directory, which will avoid you requiring super user privileges. Let's create a directory called build_mono inside home directory.
     mkdir ~/build_mono
    
    The following lines added to your ~/.bashrc will help you follow the rest of the document.
     BUILD_MONO=~/build_mono
     PATH=$BUILD_MONO/bin:$PATH
     LD_LIBRARY_PATH=$BUILD_MONO/lib
     ac_cv_path_ILASM=$BUILD_MONO/bin/ilasm
    
     export BUILD_MONO PATH LD_LIBRARY_PATH ac_cv_path_ILASM
    
    Once these lines are added do,
     source ~/.bashrc
    
  2. Build automake by first going to the directory that containst automake-1.9.tar.gz and doing,
     tar -xzf automake-1.9.tar.gz
     cd automake-1.9
     ./configure --prefix=$BUILD_MONO
     make
     make install
    
    You can verify the installation by typing which automake, which should point to automake inside $BUILD_MONO/bin
  3. Build OpenMPI. Again, change directory to where you downloaded openmpi-1.4.3.tar.gz and do,
     tar -xzf openmpi-1.4.3.tar.gz
     cd openmpi-1.4.3
     ./configure --prefix=$BUILD_MONO
     make
     make install
    
    Optionally if Infiniband is available you can point to the verbs.h (usually this is in /usr/include/infiniband/) by specifying the folder /usr in the above configure command as,
     ./configure --prefix=$BUILD_MONO --with-openib=/usr
    
    If building OpenMPI is successfull, you'll see the following output for mpirun --version command,
     mpirun (Open MPI) 1.4.3
    
     Report bugs to http://www.open-mpi.org/community/help/
    
    Also, to make sure the Infiniband module is built correctly (if specified) you can do,
     ompi_info|grep openib
    
    which, should output the following.
     MCA btl: openib (MCA v2.0, API v2.0, Component v1.4.3)
    
  4. Build Mono. Go to directory containing mono-3.4.0.tar.bz2 and do,
     tar -xjf mono-3.4.0.tar.bz2
     cd mono-3.4.0
    
    Mono 3.4.0 release is missing a file, which you'll need to add by pasting the following content to a file called./mcs/tools/xbuild/targets/Microsoft.Portable.Common.targets
     <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
         <Import Project="..\Microsoft.Portable.Core.props" />
         <Import Project="..\Microsoft.Portable.Core.targets" />
     </Project>
    
    You can continue with the build by following,
     ./configure --prefix=$BUILD_MONO
     make
     make install
    
    There are several configuration parameters that you can play with and I suggest going through them either in README.md or in./configure --help. One parameter, in particular, that I'd like to test with is --with-tls=pthread
  5. Build MPI.NET. If you were wonder why we had that ac_cv_path_ILASM variable in ~/.bashrc then this is where it'll be used. MPI.NET by default tries to find the Intermediate Language Assembler (ILASM) at /usr/bin/ilasm2, which for 1. does not exist because we built Mono into $BUILD_MONO and not /usr 2. does not exist because newer versions of Mono calls this ilasm notilasm2. Therefore, after digging through the configure file I found that we can specify the path to the ILASM by exporting the above environment variable.
    Alright, back to building MPI.NET. First copy the downloaded Unsafe.pl.patch to the subversion checkout of MPI.NET. Then change directory there and do,
     patch MPI/Unsafe.pl < Unsafe.pl.patch
    
    This will say some hunks failed to apply, but that should be fine. It only means that those are already fixed in the checkout. Once patching is completed continue with the following.
     ./autogen.sh
     ./configure --prefix=$BUILD_MONO
     make
     make install
    
    At this point you should be able to find MPI.dll and MPI.dll.config inside MPI directory, which you can use to bind against your C# MPI application.
How to run
  • Here's a sample MPI program written in C# using MPI.NET.
      using System;
      using MPI;
      namespace MPINETinMono
      {
          class Program
          {
              static void Main(string[] args)
              {
                  using (new MPI.Environment(ref args))
                  {
                      Console.Write("Rank {0} of {1} running on {2}\n",
                                      Communicator.world.Rank,
                                      Communicator.world.Size,
                                      MPI.Environment.ProcessorName);
                  }
              }
          }
      }
    
  • There are two ways that you can compile this program.
    1. Use Visual Studio referring to MPI.dll built on Windows
    2. Use mcs from Linux referring to MPI.dll built on Linux
      mcs Program.cs -reference:$MPI.NET_DIR/tools/mpi_net/MPI/MPI.dll
      
      where $MPI.NET_DIR refers to the subversion checkout directory of MPI.NET
      Either way you should be able to get Program.exe in the end.
  • Once you have the executable you can use mono with mpirun to run this in Linux. For example you can do the following within the directory of the executable,
      mpirun -np 4 mono ./Program.exe
    
    which will produce,
      Rank 0 of 4 running on i81
      Rank 2 of 4 running on i81
      Rank 1 of 4 running on i81
      Rank 3 of 4 running on i81
    
    where i81 is one of the compute nodes in FutureGrid cluster.
    You may also use other advance options with mpirun to determine process mapping and binding. Note. the syntax for such controlling is different from latest versions of OpenMPI. Therefore, it's a good idea to look at different options from mpirun --help. For example you may be interested in specifying the following options,
      hostfile=<path-to-hostfile-listing-available-computing-nodes>
      ppn=<number-of-processes-per-node>
      cpp=<number-of-cpus-to-allocate-for-a-process>
    
      mpirun --display-map --mca btl ^tcp --hostfile $hostfile --bind-to-core --bysocket --npernode $ppn --cpus-per-proc $cpp -np $(($nodes*$ppn)) ...
    
    where, --display-map will print how processes are bind to processing units and --mca btl ^tcp forces to turn off tcp
That's all you'll need to run C# based MPI.NET applications in Linux with Mono and OpenMPI. Hope this helps!