Skip to content

Commit

Permalink
new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
joearms committed Jan 18, 2016
1 parent 68f2eea commit 8a91894
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 26 deletions.
116 changes: 96 additions & 20 deletions lectures/e1.org
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,101 @@ affiliation: KTH, Whoomph Software AB
process: orgmode_plugin_slides
#+END_kv

* Fib
Fibonacci numbers are defined like this:
#+BEGIN_erlang
-module(para1).
...
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
#+END_erlang

And run like this:

#+BEGIN_shell
$ erl
Erlang/OTP 18 [erts-7.1] [source] ...
Eshell V7.1 (abort with ^G)
1> c(para1).
{ok,par1}
2> para1:fib(10).
55
#+END_shell

* Testing Fib

We need an {\sl independent} oracle.

#+BEGIN_image
height:4cm
image:../lectures/fib.png
title: Fibonacci Numbers (Wiki)
#+END_image

#+BEGIN_erlang
test_fib() ->
0 = fib(0),
1 = fib(1),
144 = fib(12),
6765 = fib(20),
yes
#+END_erlang

#+BEGIN_shell
3> para1:test_fib()
yes
#+END_shell

* Parallel Fib

#+BEGIN_erlang
start() ->
start_process(1, 42),
start_process(2, 38),
start_process(3, 20),
start_process(4, 15),
start_process(5, 38),
start_process(6, 22),
wait_replies(6).
#+END_erlang

#+BEGIN_shell
> par1:start().
at {10,46,50} received {process,4,fib,15,is,610}
at {10,46,50} received {process,3,fib,20,is,6765}
at {10,46,50} received {process,6,fib,22,is,17711}
at {10,46,53} received {process,5,fib,38,is,39088169}
at {10,46,53} received {process,2,fib,38,is,39088169}
at {10,47,7} received {process,1,fib,42,is,267914296}
ok
#+END_shell

* The parallelisation code

We use three primitive \verb+spawn+, \verb+send+ (written ``!'') and
\verb+receive+
to make a concurrent (and possibly parallel) program {\sl Tip: watch the
CPU meter when running to see if it's parallel}


#+BEGIN_erlang
start_process(K, X) ->
S = self(),
spawn(fun() -> compute_then_reply(K, S, X) end).

compute_then_reply(K, Parent, X) ->
Y = fib(X),
Parent ! {process,K,fib,X,is,Y}.

wait_replies(0) -> ok;
wait_replies(N) ->
receive
Any ->
io:format("at ~p received ~p~n",[erlang:time(), Any]),
wait_replies(N-1)
end.
#+END_erlang
* About

** Assignments and other material are at \url{https://github.com/joearms/paradis_jan_2016}.
Expand Down Expand Up @@ -117,26 +212,7 @@ a
#+END_shell

* e1.erl
#+BEGIN_erlang
tests() ->
1000 = fac(1000) div fac(999),
20 = demo1(),
20 = double(10),
36 = area({square,6}),
60 = perimeter({rectangle,10,20}),
200 = area1({rectangle,10,20}),
Pid1 = spawn(fun() -> area_actor() end),
Pid1 ! {rectangle, 10, 20},
Pid2 = spawn(fun() -> area_server() end),
200 = rpc(Pid2, {rectangle,10,20}),
Pid3 = spawn(fun() -> universal() end),
Cubed = fun(X) -> X*X*X end,
Pid3 ! {become, Cubed},
8 = rpc(Pid3, 2),
K1 = sum_squares_fast(1000),
K1 = sum_squares_slow(1000),
ok.
#+END_erlang

* fac
This file is \verb+e1.erl+
#+BEGIN_erlang
Expand Down
22 changes: 19 additions & 3 deletions lectures/e2.org
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ $erl

* Variables don't vary

** \textcolor{Red}{Variable start with an uppercase letter} and are bound with =:
** \textcolor{Red}{Variables start with an uppercase letter} and are bound with =:

#+BEGIN_shell
$erl
Expand All @@ -92,14 +92,15 @@ $erl
10
2> X = 20.
exception error:
no match of right hand side value 20
no match of right hand side value 20
...
3> X1 = 20.
20
#+END_shell

** Use a new variable each time (more later)


* Data Types
Erlang has two types of data.
There are {\sl Primitive data types} (atoms, integers, floats) etc. and
Expand All @@ -121,14 +122,29 @@ abc

* Strings
** There are no strings in Erlang.
** Strings are lists of integers:
** Strings literals in programs are syntactic sugar (ie a different way of writing) for lists of integers:

#+BEGIN_shell
1> "abc".
"abc".
2> [abc|"abc"].
[abc,97,98,99]
#+END_shell


* Strings (aside)

** Strings are abstract data types which manipulate sequences of characters.
** Characters are NOT bytes.
** Files contain bytes.
** The textual representation of programs contain string literals.
** Character handling and string representation is a total mess in all programming languages and OS's.

Stick to \verb+latin1+, \verb+ASCII+, \verb+Unicode+ and \verb+UTF8+
and don't mix them.

If you really want to know read the Unicode standard.

* Primitive Data Types

** Atoms \verb+monday+ \verb+tuesday+ - \textcolor{Red}{Remember atoms start with a lower case letters}.
Expand Down
Binary file added lectures/fib.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions lectures/para1.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-module(para1).

-export([start/0, fib/1, test_fib/0]).

start() ->
start_process(1, 42),
start_process(2, 38),
start_process(3, 20),
start_process(4, 15),
start_process(5, 38),
start_process(6, 22),
wait_replies(6).

wait_replies(0) -> ok;
wait_replies(N) ->
receive
Any ->
io:format("at ~p received ~p~n",[erlang:time(), Any]),
wait_replies(N-1)
end.

start_process(K, X) ->
S = self(),
spawn(fun() -> compute_then_reply(K, S, X) end).

compute_then_reply(K, Parent, X) ->
Y = fib(X),
Parent ! {process,K,fib,X,is,Y}.

test_fib() ->
0 = fib(0),
1 = fib(1),
144 = fib(12),
6765 = fib(20),
yes.

fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).






18 changes: 15 additions & 3 deletions lectures/problems_1.tex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
\documentclass[12pt]{hitec}

\usepackage{fancyvrb}
%% \usepackage{hyperref}
\usepackage{hyperref}

\title{Problem set 1}
\author{Joe Armstrong}
Expand All @@ -23,7 +23,10 @@ \section{Problems}
The first exercises involve correcting
incorrect programs. The program \verb+bad_prog1.erl+ in my github repository
\newline
\verb+http://joearms.github.com/paradis_2016/src/bad_prog1.erl+ has a
\href{https://github.com/joearms/paradis\_jan\_2016/blob/master/lectures/bad\_prog1.erl}
{https://github.com/joearms/paradis\_jan\_2016/}

has a
large number of errors.

\VerbatimInput[frame=single]{./bad_prog1.erl}
Expand Down Expand Up @@ -52,7 +55,7 @@ \section{Problems}

\section{A bank account}

The follow program implements a simple bank process, where the code
The following program implements a simple bank process, where the code
you have to write has been replaced by the atom \verb+implement_this+.

Replace the \verb+implement_this+ atoms by correct code.
Expand Down Expand Up @@ -117,5 +120,14 @@ \section{Unit Tests}
of \verb+F(X)+ converting them to error tuples of the form \verb+{'EXIT', Why}+
which we match with the pattern \verb+{'EXIT', _}+.

\section*{Notes}

\href{https://github.com/joearms/paradis\_jan\_2016/blob/master/lectures/}
{https://github.com/joearms/paradis\_jan\_2016/blob/master/lectures/}
contains material referred to in the problems.

If you want to build the lectures locally you will need a working verison
of erlang and pdflatex installed on your machine.

\end{document}

0 comments on commit 8a91894

Please sign in to comment.