Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Writing Parallel Programs with Erlang : Page 5

Erlang, a language for concurrency, is a good choice for writing parallel programs to fully exploit current and future multicore CPUs.


advertisement

How to Run the Code in Debian Linux

In 1998, Ericsson released Erlang as an open-source product and created the web site Erlang.org, which provides the code as well as binaries of the Erlang interpreter for Linux, Mac, and Windows environments. The code download also includes a set of module libraries and a real-time distributed database called Mnesia. The entire framework is called the Erlang Open Telecom Platform (OTP).

In Debian Linux, you simply run the following command:

apt-get install erlang



The complete Erlang OTP platform, with all its tools and utility functions, will be installed on your machine.

You have to compile Erlang code in order to obtain the pseudocode for the interpreter, which is a file with .beam extension.

erlc fact.erl

Although quite verbose, the compiler is clear and helpful in understanding the philosophy of Erlang. For example, it is able to recognize when a variable is unbound and yet is going to be used.

After compilation, you can run all the functions exported in the module fact. This is very useful while debugging, where you need to find out which function doesn't work properly. Run the Erlang emulator erl and enter the following in its environment:

Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false] Eshell V5.7.2 (abort with ^G) 1>

You can recompile the code inside the emulator as follows:

c("fact.erl").

Typically, the function start represents the main to run the program:

2> fact:start(2,12). Time elapsed for factorial of 12 using 2 parallel processes: 1.45e-4 seconds ok 2>

Try to calculate the factorial of a large integer with a number of parallel processes greater than 1 and you will see the differences in elapsed time between a traditional CPU and a multicore machine.

As previously stated, you can also test single functions if you export them and use them with the right input:

3> fact:compute(2,[123,234,456,345]). 4527984240 3> 4> fact:multiplication([123,234,456,345]). 4527984240 4>

Here is how you launch the debugger:

Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false] Eshell V5.7.2 (abort with ^G) 1> c("fact.erl",[debug_info]). {ok,fact} 2> im(). <0.46.0> 3> ii(fact). {module,fact} 4> iaa([init]). true 5> fact:start(2,12).

The statements starting with i launch a graphical processes monitor and a typical debugging window showing the source code (see Figure 2 and Figure 3). Of course, the system will open one of these windows for each process spawned during execution.


Figure 2. Processes Monitor: You can track all the processes managed by the Erlang emulator, which is useful.
 
Figure 3. Debugging Window: Here is a typical debugging window. You can launch one for each process spawned by the Erlang emulator.

The Next Step: LYME

You now hopefully have enough Erlang knowledge to make parallel processes work together concurrently. Nowadays, you will find a lot of production applications developed in Erlang, including Yahoo! del.icio.us, the chat backend of Facebook, Twitterfall (Twitter search engine for trends), and Amazon SimpleDB, many of which take advantage of multicore machines. Why not add your applications to the list? If you want to really dive into the language, the LYME (Linux, Yaws, Mnesia, Erlang) solution stack offers a good environment for writing fully functional solutions for the parallel world.



Roberto Giorgetti is an IT manager and technical writer based in Italy. He is mainly interested in open source exploitation in business and industrial areas. Roberto holds a degree in Nuclear Engineering.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap