Assignment 6 due Monday December 10, submitted by ICON

Programming with Erlang

The goals of this assignment are:

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:

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.

sixth assignment (last edited 2007-12-15 00:36:27 by Ted Herman)