Hired by Plurk.comHi everyone! I have fabulous news -- I have been hired by plurk.com! It's actually been almost two months since I am with Plurk and so far it has been great. It's an interesting story on how I got hired and what I do there. I'll tell about it in more details in this post. And by the way, I got hired thanks to this blog!

But before I do that, let me briefly explain what Plurk is about. Plurk is a startup that focuses on online conversations. It's similar to Twitter, but done completely differently and if I may say it's done right. You send a message to a friend, come back after five hours and you can continue the conversation right there. Others can join the conversation at any time.

This is how Plurk looks:

How Plurk Looks
I just answered to user's shlomif plurk about his new Perl module.

I think it was a short and sweet explanation of what Plurk is. Visit www.plurk.com to try it, and add me as your friend to start having real fun on Plurk! My nickname on Plurk is "pkrumins".

Now let's move on to the essence of the article:

How I Got Hired by Plurk.com

I first heard about Plurk on July 26th, exactly one month after I graduated. Kan, the founder of Plurk, had found my article on MySQL Tuning and decided to contact me. He had browsed my blog and he liked the good melange of different things that I posted about.

The way he approached me was completely different than of any other people interested in talking with me or hiring me. First thing that surprised me was that he found me via Skype and Google Talk at the same time. He probably did that to make sure he gets in touch with me. I had never given out my Skype username/gtalk email (but it's not hard to find), yet Kan took the time to find it. The second thing that surprised me was that he had also researched on me and found things like my polyphasic sleep experiment, which we discussed at length in one of our conversations.

With other people the discussions would usually take place over email, we'd send each other a few emails and then the discussion would die off, because I was always busy and I did not have much time to go into long discussions over email. The email conversations would usually end with "let's keep in touch".

With Kan it all took a different route. In our first conversation over Skype I explained that I was very happy that I had just graduated and that I had made a list of things to do on my own, and was not very interested in working for anyone. He said it was ok and that he just wanted to chit-chat and see what I was up to. Our first chat session lasted just one hour, but we managed to talk about a dozen of different things -- we talked about my current activities and my future plans on how I want to be a great hacker. Then we talked about Plurk's success, how it had become 5th fastest growing website on the net, and how it was praised to be the best communication service on the web. We also chatted about one of world's top hackers, and how useless the Brainbench certificates are (I had 52 of them), we discussed the people who work at Plurk and their accomplishments.

We'd continue chatting like this every few weeks, sometimes even day after day. Kan would always tell me something interesting. One time we'd talk about great generalist hackers like Max Levchin, Bob Ippolito and Gabriel Weinberg. The next time we'd talk how all the Plurk employees telecommute and how other companies do it. Then we'd talk about crazy Silicon Valley people who use startup drugs like Provigil. I could keep on going here forever.

I got more and more intrigued about Plurk but then, suddenly, one day Google sent me an email that they were interesting in hiring me. I told Kan about it but he was not very disappointed. Instead, our discussions shifted towards Google. Well, long story short, I did not get hired by Google and Kan had hypnotized me with his wisdom so I agreed to work at Plurk.

I got hired on January 15th -- almost 6 months later!

The lesson that I learned from Kan's method of hiring is to keep interest in the person you want to hire and occasionally talk with him/her, ask what he's up to, and tell some interesting things that you are expert in. If I ever gonna create my own startup, I'll use Kan's method of hiring.

Job Interview

Before getting hired I had a job interview at Plurk. It was done in a group chat with Kan and the previous Plurk's sysadmin Dima. The interview was more sysadmin oriented and it took 2.5 hours total.

Here are the job interview questions:

  • What OS'es and distributions are you experienced with?
  • If you have access to a Linux server, how would you identify if it's a 32bit or a 64bit system?
  • What process in Linux has ID equal 1?
  • What can you tell about /proc in Linux?
  • What is forking?
  • What are zombie processes?
  • Do zombie processes use system resources?
  • How would look at what a particular process is doing right now?
  • If you have a process in D state, how would you kill it?
  • How do you install software in general?
  • How would you rebuilt a debian package?
  • What if you have a server with a long uptime, 1 year let's say, and it has lots of files on a huge partition, and you need to reboot it. How would you make sure it doesn't check the filesystem on reboot?
  • How does traceroute work?
  • What IP address can be the default gateway for host
  • What is the bridge interface?
  • What is the default password of MySQL?
  • How does the MySQL client connect when the target host is localhost?
  • How would you properly manage the deletion of old binary log files in MySQL?
  • How would you list current bin logs and delete only few of them properly?
  • How would you adjust the size and the interval after which old logs are purged?
  • What is the difference between 'set <var>' and 'set global <var>'?
  • Sometimes MySQL will throw an error showing only some numerical value, how to check what does the number mean?
  • How to effectively increase the maximum number of tables MySQL can have open at one time?
  • If someone set the password for a 'root' account in mysql and no one knows it, how would you recover it?
  • How would you backup MySQL?
  • If you have a mysqldump utility and you have to dump a database consisting of InnoDB tables, large like 20Gb, how would you do that?
  • How would you back up a running MySQL instance that is being used by application, if MySQL data files are on the LVM volume and you need to make sure application is functional during the backup process?
  • What if you have to copy the data from MySQL master server to slave first, what is important step there?
  • What functionality does LVM snapshot provide?
  • What kind of RAID do you know?
  • What is there any difference between hardware and software RAID?
  • Which utility is used to manage software RAID in Linux?
  • What hardware RAID controllers have you dealt with?
  • Have you ever heard of BBU?
  • How to safely upgrade kernel remotely if you are not 100% sure that new kernel will boot successfully?
  • Have you ever used IPMI cards?

At this point I was totally tired and we ended the interview here. I got most of them right, except some MySQL questions, some LVM questions and some RAID questions. I did not feel well about not knowing answers to those, but after the interview Kan told me that he enjoyed the interview a lot. I was happy. :)

What do I do at Plurk?

My official job title is "hacker extraordinaire" and I am allowed to hack on anything that interests me at Plurk. So far I have managed to hack mostly on two things, but I have also helped with several smaller sysadmin things.

My first task at Plurk was to expand the collaborative translation platform that Plurk already had. Plurk has hundreds of thousands of users from all over the world and the collective intelligence they create can be used in many fascinating ways. One of the ways is to use their help to translate Plurk more dynamically. Instead of just asking users to translate new static strings, the system I created asks users to translate them on the fly. It can be used, for example, to translate system messages. Before a system message is sent to users, it's first sent to translators, and only after they translated it, it's sent off to all the users. If you are on Plurk and would like to help us with translations, visit "Plurk Collaborative Translation Project" to join!

While doing this project, I learned a bunch of new tools like ExtJS, Mako templates and Werkzeug.

My current task is to implement a real-time search engine for Plurk. As a great coder who reuses software, I am using a combination of Tokyo Dystopia and Sphinx to implement it. I am allowed to blog about the things I do, so expect a few articles on real-time search from me soon!

I have also prepared an article on a tool that I wrote that splits mysqldump into tables and imports N tables in parallel (it's quicker on multiprocessor machine to import several tables in parallel in mysql).

Please subscribe to my blog to get all these tasty posts automatically!

Btw, the article about pipe viewer utility originated from me helping Plurk's sysadmin to backup a 500GB database.

People at Plurk

We are just six that run Plurk. We all work remotely and each one of us is in a different country.

  • Kan is the founder and overlord of Plurk and he lives in Canada.
  • Alvin is a co-founder of Plurk, and the UI and design guy. He currently resides in Taiwan.
  • Amir is also a co-founder of Plurk and he works as the lead developer of Plurk. He's originally from Bosnia but now lives in Denmark. He has the most elegant todo manager on the web todoist. Check it out!
  • Gleber is Erlang ninja. His home is Poland.
  • Ryan is our beloved sysadmin from USA.

We are all very friendly and help out each other a lot. When we have some serious issues we all come to a group chat on IRC to solve them together. Otherwise we communicate via private plurks and Skype.

Open Source at Plurk

All of the Plurk is built using open source software -- Linux, MySQL, Python, Nginx, Haproxy, Nagios, Cacti, Memcached, Memcachedb, Tokyo Tyrant, and these are just the big names that I can remember off my head.

As a result we also want to give back to open source community and we have set up Plurk dev labs:

Plurk Development Labs, Plurk Open Source

We have released our first project there called "LightCloud", that builds on top of Tokyo Tyrant to make it scale horizontally.

What Do People Think About Plurk? A Case Study.

After a few weeks at Plurk, I started inviting many of my FreeNode #perl friends to Plurk. Some of them were very skeptical and said things like "eww, another social website". Still, I somehow managed to get them on and suddenly their view changed. Here is what they said about Plurk after trying it for a couple of hours:

Shlomi Fish, a well known Israeli hacker:

[18:12:01] <pkrumins> how do you like plurk?
[18:12:46] <rindolf> So far, it's great.
[18:12:51] <rindolf> I think I'm addicted.

# click here for his plurk profile: <a href="http://www.plurk.com/shlomif">shlomif</a>

Kent Fredric, a Perl hacker wannabee from New Zealand:

[17:29:29] <pkrumins> so plurk seems nice?
[17:29:54] <kent\n> definitely, its probably the first
    web-based chat medium which makes any sense

# click here for his plurk profile: <a href="http://www.plurk.com/kentfredric">kentfredric</a>

f00li5h, a Perl overlord from Australia:

[18:24:16] <pkrumins> like plurks?
[18:24:44] <f00li5h> better than twitters

# click here for his plurk profile: <a href="http://www.plurk.com/f00li5h">f00li5h</a>

Overall we are having tremendous fun at Plurk, all the Perl guys are schmoozing together, discussing everything about programming, cats and turtles. Come join us: Altreus, Caelum, iank, jawnsy, simcop2387, SubStack, whoppix and apeiron.

Here is a widget of my most recent Plurks:

Finally, if you are interested in working with me and other great hackers at Plurk, see the Jobs page and send your cv to jobs@plurk.com or kan@plurk.com!

Happy times!

Google Python Search LibraryHere is another quick hack that I wrote a while ago. It complements the xgoogle library that I published in my previous post with an API for Google Sponsored Links search.

Let me quickly explain why this library is useful, and what the Google Sponsored Links are.

For a typical search, Google shows regular web search results on the left side of the page, and "Sponsored Links" in a column on the right side. "Sponsored" means the results are pulled from Googe's advertising network (Adwords).

Here is a screenshot that illustrates the Sponsored Links:

Google Sponsored Links
Google Sponsored Links results for search term "security" are in red.

Okay, now why would I need a library to search the Sponsored results? Suppose that I am an advertiser on Adwords, and I buy some software related keywords like "video software". It is in my interests to know my competitors, their advertisement text, what are they up to, the new players in this niche, and their websites. Without my library it would be practically impossible to keep track of all the competitors. There can literally be hundreds of changes per day. However, with my library it's now piece of cake to keep track of all the dynamics.

How does the library work?

The sponsored links library pulls the results from this URL: http://www.google.com/sponsoredlinks. Here is an example of all the sponsored results for a query "security":

Google Sponsored Links for Security
Sponsored links results for "security".

The library just grabs page after page, calls BeautifulSoup, and extracts the search result elements. Elementary.

How to use the library?

As I mentioned, this library is part of my xgoogle library. Download and extract it first:

Download: xgoogle library (.zip)
Downloaded: 28609 times.
Download url: http://www.catonmat.net/download/xgoogle.zip

Now, the source file that contains the implementation of this library is "xgoogle/sponsoredlinks.py". To use it, do the usual import "from xgoogle.sponsoredlinks import SponsoredLinks, SLError".

SponsoredLinks is the class that provides the API and SLError is exception class that gets thrown in case of errors, so it's a good idea to import both.

The SponsoredLinks has a similar interface as the xgoogle.search (the plain google search module). The constructor of SponsoredLinks takes the keyword you want to search for, and the constructed object has several public methods and properties:

  • method get_results() - gets a page of results, returning a list of SponsoredLink objects. It returns an empty list if there are no more results.
  • property num_results - returns number of search results found.
  • property results_per_page - sets/gets the number of results to get per page (max 100).

The returned SponsoredLink objects have four attributes -- "title", "desc", "url", and "display_url". Here is a picture that illustrates what each attribute stands for:

Coresspondence between sponsored link and result object

The picture does not show the "display_url" attribute as it's the actual link the result links to (href of blue link in the pic).

Here is an example usage of this library. It retrieves first 100 Sponsored Links results for keyword "video software":

from xgoogle.sponsoredlinks import SponsoredLinks, SLError
  sl = SponsoredLinks("video software")
  sl.results_per_page = 100
  results = sl.get_results()
except SLError, e:
  print "Search failed: %s" % e

for result in results:
  print result.title.encode('utf8')
  print result.desc.encode('utf8')
  print result.display_url.encode('utf8')
  print result.url.encode('utf8')


Photoshop Video Software
Time saving software for video. Work faster in Photoshop. 


That's about it for this time. Use it to find your competitors and outsmart them!

Next time I am going to expand the library for Google Sets search.

Download "xgoogle" library:

Download: xgoogle library (.zip)
Downloaded: 28609 times.
Download url: http://www.catonmat.net/download/xgoogle.zip

Have fun!

Google Python Search LibraryHere is a quick hack that I wrote. It's a Python library to search Google without using their API. It's quick and dirty, just the way I love it.

Why didn't I use Google's provided REST API? Because it says "you can only get up to 8 results in a single call and you can't go beyond the first 32 results". Seriously, what am I gonna do with just 32 results?

I wrote it because I want to do various Google hacks automatically, monitor popularity of some keywords and sites, and to use it for various other reasons.

One of my next post is going to extend on this library and build a tool that perfects your English. I have been using Google for a while to find the correct use of various English idioms, phrases, and grammar. For example, "i am programmer" vs. "i am a programmer". The first one is missing an indefinite article "a", but the second is correct. Googling for these terms reveal that the first has 6,230 results, but the second has 136,000 results, so I pretty much trust that the 2nd is more correct than the first.

Subscribe to my posts via catonmat's rss, if you are intrigued and would love to receive my posts automatically!

How to use the library?

First download the xgoogle library, and extract it somewhere.

Download: xgoogle library (.zip)
Downloaded: 28609 times.
Download url: http://www.catonmat.net/download/xgoogle.zip

At the moment it contains just the code for Google search, but in the future I will add other searches (google sets, google suggest, etc).

To use the search, from "xgoogle.search" import "GoogleSearch" and, optionally, "SearchError".

GoogleSearch is the class you will use to do Google searches. SearchError is an exception class that GoogleSearch throws in case of various errors.

Pass the keyword you want to search as the first parameter to GoogleSearch's constructor. The constructed object has several public methods and properties:

  • method get_results() - gets a page of results, returning a list of SearchResult objects. It returns an empty list if there are no more results.
  • property num_results - returns number of search results found.
  • property results_per_page - sets/gets the number of results to get per page. Possible values are 10, 25, 50, 100.
  • property page - sets/gets the search page.

As I said, get_results() method returns a SearchResult object. It has three attributes -- "title", "desc", and "url". They are Unicode strings, so do a proper encoding before outputting them.

Here is a screenshot that illustrates the "title", "desc", and "url" attributes:

Google Search Result, url, title, description
Google search result for "catonmat".

Here is an example program of doing a Google search. It takes the first argument, does a search on it, and prints the results:

from xgoogle.search import GoogleSearch, SearchError
  gs = GoogleSearch("quick and dirty")
  gs.results_per_page = 50
  results = gs.get_results()
  for res in results:
    print res.title.encode("utf8")
    print res.desc.encode("utf8")
    print res.url.encode("utf8")
except SearchError, e:
  print "Search failed: %s" % e

This code fragment sets up a search for "quick and dirty" and specifies that a result page should have 50 results. Then it calls get_results() to get a page of results. Finally it prints the title, description and url of each search result.

Here is the output from running this program:

Quick-and-dirty - Wikipedia, the free encyclopedia
Quick-and-dirty is a term used in reference to anything that is an easy way to implement a kludge. Its usage is popular among programmers, ...

Grammar Girl's Quick and Dirty Tips for Better Writing - Wikipedia ...
"Grammar Girl's Quick and Dirty Tips for Better Writing" is an educational podcast that was launched in July 2006 and the title of a print book that was ...Writing - 39k - 

Quick & Dirty Tips :: Grammar  Girl
Quick & Dirty Tips(tm) and related trademarks appearing on this website are the property of Mignon Fogarty, Inc. and Holtzbrinck Publishers Holdings, LLC. ...

Quick and Dirty search on Google
Compare these results to the output above.

You could also have specified which search page to start the search from. For example, the following code will get 25 results per page and start the search at 2nd page.

gs = GoogleSearch("quick and dirty")
  gs.results_per_page = 25
  gs.page = 2
  results = gs.get_results()

You can also quickly write a scraper to get all the results for a given search term:

from xgoogle.search import GoogleSearch, SearchError
  gs = GoogleSearch("quantum mechanics")
  gs.results_per_page = 100
  results = []
  while True:
    tmp = gs.get_results()
    if not tmp: # no more results were found
  # ... do something with all the results ...
except SearchError, e:
  print "Search failed: %s" % e

You can use this library to constantly monitor how your website is ranking for a given search term. Suppose your website has a domain "catonmat.net" and the search term you want to find your position for is "python videos".

Here is a code that outputs your ranking: (it looks through first 100 results, if you need more, put a loop there)

import re
from urlparse import urlparse
from xgoogle.search import GoogleSearch, SearchError

target_domain = "catonmat.net"
target_keyword = "python videos"

def mk_nice_domain(domain):
    convert domain into a nicer one (eg. www3.google.com into google.com)
    domain = re.sub("^www(\d+)?\.", "", domain)
    # add more here
    return domain

gs = GoogleSearch(target_keyword)
gs.results_per_page = 100
results = gs.get_results()
for idx, res in enumerate(results):
  parsed = urlparse(res.url)
  domain = mk_nice_domain(parsed.netloc)
  if domain == target_domain:
    print "Ranking position %d for keyword '%s' on domain %s" % (idx+1, target_keyword, target_domain)

Output of this program:

Ranking position 6 for keyword python videos on domain catonmat.net
Ranking position 7 for keyword python videos on domain catonmat.net

Here is a much wicked example. It uses the GeoIP Python module to find all 10 websites for keyword "wicked code" that are physically hosting in California or New York in USA. Make sure you download GeoCityLite database from "http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz" and extract it to "/usr/local/geo_ip".

import GeoIP
from urlparse import urlparse
from xgoogle.search import GoogleSearch, SearchError

class Geo(object):
  GEO_PATH = "/usr/local/geo_ip/GeoLiteCity.dat"

  def __init__(self):
    self.geo = GeoIP.open(Geo.GEO_PATH, GeoIP.GEOIP_STANDARD)

  def detect_by_host(self, host):
      gir = self.geo.record_by_name(host)
      return {'country': gir['country_code'].lower(),
              'region': gir['region'].lower()}
    except Exception, e:
      return {'country': 'none', 'region': 'none'}

dst_country = 'us'
dst_states = ['ca', 'ny']
dst_keyword = "wicked code"
num_results = 10
final_results = []
geo = Geo()

gs = GoogleSearch(dst_keyword)
gs.results_per_page = 100

seen_websites = []
while len(final_results) < num_results:
  results = gs.get_results()
  domains = [urlparse(r.url).netloc for r in results]
  for d in domains:
    geo_loc = geo.detect_by_host(d)
    if (geo_loc['country'] == dst_country and
                 geo_loc['region'] in dst_states and
                 d not in seen_websites):
      final_results.append((d, geo_loc['region']))
      if len(final_results) == num_results:

print "Found %d websites:" % len(final_results)
for w in final_results:
    print "%s (state: %s)" % w

Here is the output of running it:

Found 10 websites:
www.wickedcode.com (state: ca)
www.retailmenot.com (state: ca)
www.simplyhired.com (state: ca)
archdipesh.blogspot.com (state: ca)
wagnerblog.com (state: ca)
answers.yahoo.com (state: ca)
devsnippets.com (state: ca)
friendfeed.com (state: ca)
www.thedacs.com (state: ny)
www.tipsdotnet.com (state: ca)

You may modify these examples the way you wish. I'd love to hear some comments about what you can come up with!

And just for fun, here are some other simple uses:

You can make your own Google Fight:

import sys
from xgoogle.search import GoogleSearch, SearchError

args = sys.argv[1:]
if len(args) < 2:
 print 'Usage: google_fight.py "keyword 1" "keyword 2"'

  n0 = GoogleSearch('"%s"' % args[0]).num_results
  n1 = GoogleSearch('"%s"' % args[1]).num_results
except SearchError, e:
  print "Google search failed: %s" % e

if n0 > n1:
  print "%s wins with %d results! (%s had %d)" % (args[0], n0, args[1], n1)
elif n1 > n0:
  print "%s wins with %d results! (%s had %d)" % (args[1], n1, args[0], n0)
  print "It's a tie! Both keywords have %d results!" % n1

Download: google_fight.py
Downloaded: 5140 times.
Download url: http://www.catonmat.net/download/google_fight.py

Here is an example usage of google_fight.py:

$ ./google_fight.py google microsoft
google wins with 2680000000 results! (microsoft had 664000000)

$ ./google_fight.py "linux ubuntu" "linux gentoo"
linux ubuntu wins with 4300000 results! (linux gentoo had 863000)

After I wrote this, I generalized this Google Fight to take N keywords, and made their passing to program easier by allowing them to be separated by a comma.

import sys
from operator import itemgetter
from xgoogle.search import GoogleSearch, SearchError

args = sys.argv[1:]
if not args:
  print "Usage: google_fight.py keyword one, keyword two, ..."

keywords = [k.strip() for k in ' '.join(args).split(',')]
  results = [(k, GoogleSearch('"%s"' % k).num_results) for k in keywords]
except SearchError, e:
  print "Google search failed: %s" % e

results.sort(key=itemgetter(1), reverse=True)
for res in results:
    print "%s: %d" % res

Download: google_fight2.py
Downloaded: 4489 times.
Download url: http://www.catonmat.net/download/google_fight2.py

Here is an example usage of google_fight2.py:

$ ./google_fight2.py earth atmospehere, sun atmosphere, moon atmosphere, jupiter atmosphere
earth atmospehere: 685000
jupiter atmosphere: 31400
sun atmosphere: 24900
moon atmosphere: 8130

I am going to expand on this library and add search for Google Sets, Google Sponsored Links, Google Suggest, and perhaps some other Google searches. Then I'm going to build various tools on them, like a sponsored links competitor finder, use Google Suggest together with Google Sets to find various phrases in English, and apply them to tens of other my ideas.

Download "xgoogle" library and examples:

Download: xgoogle library (.zip)
Downloaded: 28609 times.
Download url: http://www.catonmat.net/download/xgoogle.zip

Download: google_fight.py
Downloaded: 5140 times.
Download url: http://www.catonmat.net/download/google_fight.py

Download: google_fight2.py
Downloaded: 4489 times.
Download url: http://www.catonmat.net/download/google_fight2.py

This article is part of the article series "MIT Introduction to Algorithms."
<- previous article next article ->

MIT AlgorithmsThis is the thirteenth post in an article series about MIT's lecture course "Introduction to Algorithms." In this post I will review lectures twenty and twenty-one on parallel algorithms. These lectures cover the basics of multithreaded programming and multithreaded algorithms.

Lecture twenty begins with a good overview of multithreaded programming paradigm, introduces to various concepts of parallel programming and at the end talks about the Cilk programming language.

Lecture twenty-one implements several multithreaded algorithms, such as nxn matrix addition, nxn matrix multiplication, and parallel merge-sort. It then goes into great detail to analyze the running time and parallelism of these algorithms.

Lecture 20: Parallel Algorithms I

The lecture starts with an introduction to a particular model of parallel computation called dynamic multithreading. It's easiest to understand this model with an example. Here is a multithreaded algorithm that computes Fibonacci numbers:

1   if n < 2:                     | thread A
2     return n                    | thread A
3   x = spawn Fibonacci(n-1)      | thread A
4   y = spawn Fibonacci(n-2)      | thread B
5   sync                          | thread C
6   return x + y                  | thread C

# this is a really bad algorithm, because it's
# exponential time.
# see lecture three for four different
# classical algorithms that compute Fibonacci numbers

(A thread is defined to be a maximal sequence of instructions not containing the parallel control statements spawn, sync, and return, not something like Java threads or Posix threads.)

I put the most important keywords in bold. They are "spawn" and "sync".

The keyword spawn is the parallel analog of an ordinary subroutine call. Spawn before the subroutine call in line 3 indicates that the subprocedure Fibonacci(n-1) can execute in parallel with the procedure Fibonacci(n) itself. Unlike an ordinary function call, where the parent is not resumed until after its child returns, in the case of a spawn, the parent can continue to execute in parallel with the child. In this case, the parent goes on to spawn Fibonacci(n-2). In general, the parent can continue to spawn off children, producing a high degree of parallelism.

A procedure cannot safely use the return values of the children it has spawned until it executes a sync statement. If any of its children have not completed when it executes a sync, the procedure suspends and does not resume until all of its children have completed. When all of its children return, execution of the procedure resumes at the point immediately following the sync statement. In the Fibonacci example, the sync statement in line 5 is required before the return statement in line 6 to avoid the anomaly that would occur if x and y were summed before each had been computed.

The spawn and sync keywords specify logical parallelism, not "actual" parallelism. That is, these keywords indicate which code may possibly execute in parallel, but what actually runs in parallel is determined by a scheduler, which maps the dynamically unfolding computation onto the available processors.

The lecture then continues with a discussion on how a parallel instruction stream can be viewed as a directed acyclic graph G = (V, E), where threads make up the set V of vertices, and each spawn and return statement makes up an edge in E. There are also continuation edges in E that exist between statements.

In Fibonacci example there are three threads: lines 1, 2, 3 make up the thread A, line 4 is thread B and lines 5, 6 are thread C.

Here is how a graph of Fibonacci(4) computation looks like:

Multithreading Directed Acyclic Graph

A dag representing multithreaded computation of Fibonacci(4).

Here the threads are show as circles, and each group of threads belonging to the same procedure are surrounded by a rounded rectangle. Downward edges are spawn dependencies, horizontal edges represent continuation dependencies within a procedure, and upward edges are return dependencies. Every computation starts with a single initial thread, and ends with a single final thread.

So to compute Fib(4), first Fib(3) and Fib(2) have to be computed. Fib(4) spawns one thread to compute Fib(3) and another thread to compute Fib(2) and sync'es. In order to compute Fib(3), values of Fib(2) and Fib(1) have to be known, and to compute Fib(2) values of Fib(1) and Fib(0) have to be known. Fib(3) and Fib(2) spawn their threads and sync. This way the computation unwinds.

Next, the lecture introduces some concepts for measuring performance of multithreaded algorithms.

First some time measures:

  • Tp - running time of an algorithm on p processors,
  • T1 - running time of algorithm on 1 processor, and
  • T as critical path length, that is the longest time to execute the algorithm on infinite number of processors.

Based on these measures, the speedup of a computation on p processors is T1/Tp, that is how much faster a p processor machine executes the algorithm than a one processor machine. If the speedup is O(p), then we say that the computation has a linear speedup. If, however, speedup > p, then we call it a super-linear speedup. The maximum possible speedup is called parallelism and is computed by T1/T.

After these concepts the lecture puts forward the concept of scheduling and greedy scheduler.

A scheduler maps computation to p processors.

The greedy scheduler schedules as much as it can at every time step. On a p-processor machine every step can be classified into two types. If there are p or more threads ready to execute, the scheduler executes any p of them (this step is also called a complete step). If there are fewer than p threads, it executes all of them (called incomplete step). The greedy scheduler is an offline scheduler and it's 2-competitive (see lecture 13 on online/offline algorithms and the meaning of competitive algorithm).

The lecture ends with an introduction to Cilk parallel, multithreaded programming language. It uses a randomized online scheduler. It was used to write two chess programs *Socrates and Cilkchess.

See Charles Leiseron's handout for a much more comprehensive overview of dynamic multithreading: A Minicourse on Dynamic Multithreaded Algorithms (pdf).

You're welcome to watch lecture twenty:

Topics covered in lecture twenty:

  • [01:33] Parallel algorithms.
  • [02:33] Dynamic multi-threading.
  • [03:15] A multithreaded algorithm for fibonnaci numbers.
  • [04:50] Explanation of spawn and sync.
  • [06:45] Logical parallelism.
  • [08:00] Multithreaded computation.
  • [12:15] Fibonacci(4) computation graph, and its concepts - init thread, spawn edge, continuation edge, return edge, final thread.
  • [17:45] Edges: spawn, return, continuation.
  • [19:40] Performance measures: running time on p processors Tp, work (serial time) T1, critcial path length (longest path) Tinfinity.
  • [24:15] Lower bounds on Tp (running time on p processors).
  • [29:35] Speedup, linear speedup, superlinear speedup.
  • [32:55] Maximum possible speedup.
  • [36:40] Scheduling.
  • [38:55] Greedy scheduler.
  • [43:40] Theorem by Grant and Brent (bound on Tp for greedy scheduler).
  • [46:20] Proof of Grant, Brent theorem.
  • [56:50] Corollary on greedy scheduler's linear speedup.
  • [01:02:20] Cilk programming language.
  • [01:06:00] Chess programs: *Socrates, Clikchess.
  • [01:06:30] Analysis of *Socrates running time on 512 processors.

Lecture twenty notes:

MIT Algorithms Lecture 20 Notes Thumbnail. Page 1 of 2.
Lecture 20, page 1 of 2: dynamic multithreading, multithreaded computation, performance measures, speedup, scheduling, greedy scheduler.

MIT Algorithms Lecture 20 Notes Thumbnail. Page 2 of 2.
Lecture 20, page 2 of 2: grant, brent theorem, cilk, socrates, cilkchess.

Lecture 21: Parallel Algorithms II

Lecture twenty-one is all about real multithreaded algorithms and their analysis.

It starts with a hugly important problem of multithreaded matrix multiplication: given two nxn matrices A and B, write a multithreaded algorithm Matrix-Multiply that multiplies them, producing matrix C. The natural approach is to use divide and conquer method from lecture three, and formulate the problem as follows:

Multithreaded Matrix Multiplication

Each nxn matrix can be expressed as 8 multiplications and 4 additions of (n/2)x(n/2) submatrices. So the first thing we need is Matrix-Add algorithm that adds two matrices in parallel.

Here is the parallel Matrix-Add algorithm:

Matrix-Add(C, T, n):
   // Adds matrices C and T in-place, producing C = C + T
   // n is power of 2 (for simplicity).
   if  n == 1:
     C[1, 1] = C[1, 1] + T[1, 1] 
     partition C and T into (n/2)x(n/2) submatrices
     spawn Matrix-Add(C11, T11, n/2) 
     spawn Matrix-Add(C12, T12, n/2) 
     spawn Matrix-Add(C21, T21, n/2) 
     spawn Matrix-Add(C22, T22, n/2) 

The base case for this algorithm is when matrices are just scalars, then it just adds the only element of both matrices. In all other cases, it partitions matrices C and T into (n/2)x(n/2) matrices and spawns four subproblems.

Now we can implement the Matrix-Multiply algorithm, by doing 8 multiplications in parallel and then calling Matrix-Add to do 4 additions.

Here is the parallel Matrix-Multiply algorithm:

Matrix-Multiply(C, A, B, n):
   // Multiplies matrices A and B, storing the result in C.
   // n is power of 2 (for simplicity).
   if  n == 1:
     C[1, 1] = A[1, 1] · B[1, 1] 
     allocate a temporary matrix T[1...n, 1...n] 
     partition A, B, C, and T into (n/2)x(n/2) submatrices 
     spawn Matrix-Multiply(C12,A11,B12, n/2) 
     spawn Matrix-Multiply(C21,A21,B11, n/2) 
     spawn Matrix-Multiply(C22,A21,B12, n/2) 
     spawn Matrix-Multiply(T11,A12,B21, n/2) 
     spawn Matrix-Multiply(T12,A12,B22, n/2) 
     spawn Matrix-Multiply(T21,A22,B21, n/2) 
     spawn Matrix-Multiply(T22,A22,B22, n/2) 
     Matrix-Add(C, T, n)

The lecture then proceeds to calculating how good the algorithm is, turns out the parallelism (ratio of time of algorithm running on a single processor T1 to running on infinite number of processors Tinf) for doing 1000x1000 matrix multiplication is 107, which the parallel algorithm is fast as hell.

Matrix-Multiply used a temporary matrix which was actually unnecessary. To achieve higher performance, it's often advantageous for an algorithm to use less space, because more space means more time. Therefore a faster algorithm can be added that multiplies matrices and adds them in-place. See lecture at 33:05 for Matrix-Multiply-Add algorithm. The parallelism of this algorithm for 1000x1000 matrix multiplication is 106, smaller than the previous one, but due to fact that no temporary matrices had to be used, it beats Matrix-Multiply algorithm in practice.

The lecture then proceeds to parallel sorting. The classical sorting algorithms were covered in lectures four and five. This lecture parallelizes the Merge-Sort algorithm.

Here is the parallel merge-sort algorithm:

Parallel-Merge-Sort(A, p, r): // sort A[p...r]
  if p < r:
    q = floor((p+r)/2)
    spawn Parallel-Merge-Sort(A, p, q)
    spawn Parallel-Merge-Sort(A, q+1, r)
    Merge(A, p, q, r) // merge A[p...q] with A[q+1...r]

Instead of recursively calling Parallel-Merge-Sort for subproblems, we spawn them, thus utilizing the power of parallelism. After doing analysis turns out that due to fact that we use the classical Merge() function, the parallelism is just lg(n). That's puny!

To solve this problem, a Parallel-Merge algorithm has to be written. The lecture continues with presenting a Parallel-Merge algorithm, and after that does the analysis of the same Parallel-Merge-Sort algorithm but with Parallel-Merge function. Turns out this algorithm has parallelism of n/lg2(n), that is much better. See lecture at 51:00 for Parallel-Merge algorithm.

The best parallel sorting algorithm know to date has the parallelism of n/lg(n).

You're welcome to watch lecture twenty-one:

Topics covered in lecture twenty-one:

  • [00:35] Multithreaded algorithms.
  • [01:32] Multithreaded matrix multiplication.
  • [02:05] Parallelizing with divide and conquer.
  • [04:30] Algorithm Matrix-Multiply, that computes C = A*B.
  • [10:13] Algorithm Matrix-Add, that computes C = C+T.
  • [12:45] Analysis of running time of Matrix-Multiply and Matrix-Add.
  • [19:40] Analysis of critical path length of Matrix-Multiply and Matrix-Add.
  • [26:10] Parallelism of Matrix-Multiply and Matrix-Add.
  • [33:05] Algorithm Matrix-Multiply-Add, that computes C = C + A*B.
  • [35:50] Analysis of Matrix-Multiply-Add.
  • [42:30] Multithreaded, parallel sorting.
  • [43:30] Parallelized Merge-Sort algorithm.
  • [45:55] Analysis of multithreaded Merge-Sort.
  • [51:00] Parallel-Merge algorithm.
  • [01:00:30] Analysis of Parallel-Merge algorithm.

Lecture twenty-one notes:

MIT Algorithms Lecture 21 Notes Thumbnail. Page 1 of 2.
Lecture 21, page 1 of 2: multithreaded algorithms, matrix multiplication, analysis (critical path length, parallelism).

MIT Algorithms Lecture 21 Notes Thumbnail. Page 2 of 2.
Lecture 21, page 2 of 2: paralel sorting, merge sort, parallel merge, analysis.

Have fun with parallel, multithreaded algorithms! The next post is going to be on cache oblivious algorithms! They are a class of algorithms that exploit all the various caches in modern computer architecture to make them run notably faster.

JavaScript: The Good PartsI found a really nice video lecture on JavaScript. I'm a JavaScript journeyman myself so I decided to review it to learn something new. I have written about JavaScript in the past -- the previous post on JavaScript was "Learning JavaScript through Video Lectures".

This lecture is given by JavaScript guru Doug Crockford. He's the author of JSON, JSMin JavaScript minifier and JSLint.

The talk is based on Douglas's recent book "JavaScript: The Good Parts". It's excellent.

The lecture begins with a brief intro of why JavaScript is the way it is and how it came to be so. An interesting point Doug makes is that JavaScript is basically Scheme, except with Java syntax. He talks about the bad and good parts of JavaScript and gives a few examples of common JavaScript problems, their solutions and patterns. Douglas also talks about how he discovered JSON. After the talk there is a 13 minute Q and A.

You're welcome to watch the video lecture. It's 1 hour long and it will definitely make your understanding of JavaScript better:

Here are some interesting points from the lecture:

[09:57] JavaScript was influenced by Scheme, Java and Perl. From Scheme it borrowed lambda functions and loose typing. From Java it took most of the syntax, and from Perl some of its regular expressions. And finally, it derived the idea of prototypal inheritance and dynamic objects from Self. See my previous post on JavaScript for explanation of prototypal inheritance.

[11:38] JavaScript bad parts:

  • Global variables. Global variables make it harder to run independent subprograms in the same program. If the subprograms happen to have global variables that share the same names, then they will interfere with each other and likely fail, usually in difficult to diagnose ways.
  • Newlines get converted into semicolons. JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. It sometimes inserts semicolons in places where they are not welcome. For example:
        status: true
    returns undefined because JavaScript inserts ';' after return.Correct way:
    return {
        status: true

  • Operator typeof is not very helpful. For example, "typeof null" is "object", "typeof [1,2,3]" is also "object".
  • Operator + adds numbers and concatenates. The + operator can add or concatenate. Which one it does depends on the types of the parameters. If either operand is an empty string, it produces the other operand converted to a string. If both operands are numbers, it produces the sum. Otherwise, it converts both operands to strings and concatenates them. This complicated behavior is a common source of bugs. If you intend + to add, make sure that both operands are numbers.
  • Operators == and != do type coercion. Use of the === and !== operators is always preferred.
  • Too many falsy values. 0, Nan, '' (empty string), false, null, undefined are all false.

[17:25] for ... in operator mixes inherited functions with the desired data members (it does deep reflection). Use object.hasOwnProperty to filter out names that belong to object itself:

for (name in object) {
    if (object.hasOwnProperty(name)) {

[22:00] Javascript good parts:

  • Lambda. Enough said.
  • Dynamic objects. Just add a property to an object, or remove it, no need for classes and derivation to create a similar object.
  • Loose Typing. No need for type declarations.
  • Object Literals. {} for object literals, [] for array literals and // for regexp literals.

[23:00] Two schools of inheritance - classical and prototypal. Prototypal inheritance means that objects can inherit properties directly from other objects. The language is class-free.

[24:35] Realization of prototypal inheritance in JavaScript:

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();

Now a newObject can be created by inheriting from oldObject:

newObject = Object.create(oldObject);

[26:05] Example of global variables and why they are bad and how to solve the problem by using closures.

var my_thing = function () {
    var names = ["zero", "one", ... ];
    return function(n) {
        return names[n];

[29:00] There are four ways to create a new object in JavaScript:

  • Object literal -- var object = { }.
  • New -- var object = new Object()
  • Object.create -- var object = Object.create(old_object).
  • Call another constructor (use different inheritance model).

[42:42] JSLint. JSLint defines a professional subset of JavaScript. JSLint will hurt your feelings.

[52:00] Q/A: Does strict mode change the behavior or does it take things out? -- Can't have "with" in strict mode, changes the way eval() works, changes error handling.

[53:00] Q/A: What's going on with DOM? -- Ajax libraries fix DOM, these changes should be propagated back into DOM API.

[55:30] Q/A: How do you spell lambda in JavaScript? -- function.

[55:54] Q/A: How to solve global variable problem? -- Each of compilation unit should be isolated, but there should be a way how they can introduce (link) to each other.

[56:30] Q/A: How do JavaScript objects differ from hash tables, they seem the same to me?

[57:23] Q/A: What's wrong with HTML 5 and web apps? -- They are doing too much and there are way too many of them.

[59:10] Q/A: How come JSON and JavaScript have almost the same syntax? -- Doug admits he forgot to include Unicode Line Separator (LS) and Paragraph Separator (PS) control codes as whitespace chars.

[01:00:32] Q/A: Why does JSON require quotes around the property names? -- Three reasons: 1. Wanted to align with Python where quotes are required, 2. Wanted to make the grammar of standard simpler, 3. JavaScript has stupid reserved word policy.

[01:02:40] Q/A: Are there any prospects for adding concurrency to the language? -- Definitely no threads. Could be some kind of a messaging model.

If you liked this talk, I recommend that you get Doug's book:

Happy javascripting! ;)