Assignment 6 due Monday December 10, submitted by ICON
Programming with Erlang
The goals of this assignment are:
- give you experience with Erlang
- learn some patterns of concurrency
ICON
You will submit your work using the "dropbox" feature of ICON, that is, you will log in to icon.uiowa.edu, go to 22C111, and click on the dropbox. There, you can upload your work. Please be mindful of the following:
- Once you submit something, you cannot withdraw it (but you can upload again the same file and ICON will save another copy).
- To avoid confusing the TA and having your work lost or ignored, please submit everything in one file called assignment6.zip (in Windows, a compressed folder named assignment6).
- Please use the browse function to find assignment6.zip within the web browser before you click on the upload button. After upload, you should see a confirmation that the file is in the dropbox.
- If you're working on a Linux machine, here's how you should create the file to submit.
- First, create a directory (folder) called assignment6. You can use the desktop manager, or from a Unix shell the command "mkdir assignment6" will create the directory.
Follow directions on the erlang page to set up your working environment for using Erlang.
- Start working within the assignment6 directory. This is done either by clicking to open the folder or by the shell command "cd assignment6".
- The command "zip -r assignment6.zip assignment6" creates the compressed file assignment6.zip and tells you which files were processed.
The Assignment
The assignment is to write two Erlang programs, one for integration and another for matrix multiplication. The technical description is in a PDF file, attached to this page. Also, some files are attached to help you with the second problem of the assignment; the attached file vectors.txt is larger than the example in the PDF file, so that your pipeline version will have sufficient input to show concurrency.
Attachment
Click on attachments, then use Get to fetch the PDF file; you can also get files matmul.erl and vectors.txt, as described in the PDF file.
Solution
Here is a sample solution to the integrate problem, which I explained on the last day of class.
-module(integrate).
-export([start/1, worker/1, summation/2 ]).
start(T) ->
R = spawn(integrate,summation,[0.0,1]),
register(summation,R),
start(T,0),
"computation initiated".
start(T,NumWorkersSoFar) ->
if
NumWorkersSoFar < T ->
E = spawn(integrate,worker,[T]),
S = integer_to_list(NumWorkersSoFar),
V = "worker" ++ S,
N = list_to_atom(V),
register(N,E),
start( T, 1+NumWorkersSoFar );
true ->
worker0 ! { 0.0, 10.0 }
end.
randomWorker(T) ->
K = random:uniform(T),
Kstring = integer_to_list(K-1),
Name = "worker" ++ Kstring,
Aname = list_to_atom(Name),
Aname.
worker(T) ->
receive
{ From, To } ->
% calculate two rectangle areas
Rectangle1 = (To - From) * ( From*(2.0 + math:sin(From)) ),
Rectangle2 = (To - From) * ( To*(2.0 + math:sin(To)) ),
Diff = abs( Rectangle1 - Rectangle2 ),
if
Diff > 0.000001 ->
MidPoint = (From + To) / 2.0,
randomWorker(T) ! { From, MidPoint },
randomWorker(T) ! { MidPoint, To },
summation ! more_work;
true ->
summation ! { add, Rectangle1 }
end
end,
worker(T),
true.
summation(SumSoFar,Waitfor) ->
receive
more_work ->
Total = SumSoFar,
NewWaitfor = Waitfor + 1;
{ add, Value } ->
Total = SumSoFar + Value,
NewWaitfor = Waitfor - 1
end,
if
NewWaitfor > 0 ->
summation( Total, NewWaitfor );
true ->
io:format("summation total is ~w~n", [Total])
end,
true.
