Wednesday, January 28, 2015

SMP erlang has two settings, to determine how to operate erl -smp + SN -smp start SMP Erlang + SN N


"Public" ETS table can be shared by more than one stroke, just know that the word table to identify stroke can read this table, which is quite dangerous, so the programmer must pay attention to program logic, identical twins coupled with restrictions on the use of: 2.1 Only one trip will be to write data, other travel only read information ETS table 2.2 is responsible for writing the trip never to be correct, can not write wrong information distributed ticketing system: identical twins If you want to sell tickets, usually with a single handle all Ticketmaster outlets , but it will produce a sequence bottleneck solution is to open two outlets, the first even-number distribution outlets, the second ticket office assigned odd numbers, identical twins if a ticket office sold out, and then a second ticket outlets moved to the first sale. While solving the problem, but the two-seat tickets will not next door, which is distributed hash table of the study.
If F (H) have to use to process dictionary, then with the map pmap behavior is not the same as pmap is another trip to estimate F (H), so I can not use pmap parallelizing pmap (F, L) - > S = self (), %% make_ref () returns a unique reference %% we'll match on this later Ref = erlang: make_ref (), Pids = map (fun (I) -> spawn (fun () -> do_f (S, Ref, F, I) end) end, L), %% gather the results gather (Pids, Ref) do_f (Parent, Ref, F, I) -.!> Parent {self (), Ref, . (catch F (I))} gather ([Pid | T], Ref) -> receive {Pid, Ref, Ret} -> [Ret | gather (T, Ref)] end; gather ([], _) -> [].
Abstract logical thinking element order pmap importance return identical twins value, if not mind the return value of the order, can be rewritten as pmap1 (F, L) -> S = self (), Ref = erlang: make_ref identical twins (), foreach identical twins (fun ( I) -> spawn (fun () -.> identical twins do_f1 (S, Ref, F, I) end) end, L), %% gather the results gather1 (length (L), Ref, []) do_f1 (Parent, Ref, F, I) -> Parent {Ref, (catch F (I))} gather1 (0, _, L) -> L;!. gather1 (N, Ref, L) -> receive {Ref, Ret} -> gather1 (N-1, Ref, [Ret | L]) end.
SMP erlang has two settings, to determine how to operate erl -smp + SN -smp start SMP Erlang + SN N scheduler to execute erlang, each erlagn scheduler is a complete VM, if not this parameter, preset number for the number of logical processors on SMP machines. -module (ptests). -export ([tests / 1, fib / 1]). -import (lists, [map / 2]). -import (lib_misc, [pmap / 2]). tests ([N]) . -> Nsched = list_to_integer (atom_to_list (N)), run_tests (1, Nsched) run_tests (N, Nsched) -> case test (N) of stop -> init: stop (); Val -> io: format identical twins (" . ~ p ~ n ", [{Nsched, Val}]), run_tests (N + 1, Nsched) end test (1) -.> %% Make 100 lists %% Each list contains 1000 random integers seed (), S = lists: seq (1,100), L = map (fun (_) -> mkList (1000) end, S), {Time1, S1} = timer: tc (lists, map, [fun lists: sort / 1, L ]), {Time2, S2} = timer: tc (lib_misc, pmap, [fun lists: sort / 1, L]), {sort, Time1, Time2, equal (S1, S2)}; test (2) -> %% L = [27,27,27, ..] 100 times L = lists: duplicate (100, 27), {Time1, S1} = timer: tc (lists, map, [fun ptests: fib / 1, L ]), {Time2, S2} = timer: tc (lib_misc, pmap, [fun ptests: fib / 1, L]), {fib, Time1, Time2, equal (S1, S2)}; test (3) -> . stop %% Equal is used to test that map and pmap compute the same thing equal (S, S) -> true;. equal (S1, S2) -> {differ, S1, S2} %% recursive (inefficent) fibonacci fib (0) -> 1; fib (1) -> 1;.. fib (N) -> fib (N-1) + fib (N-2) %% Reset the random number generator This is so we %% get the same sequence of random numbers each time we run %% the program seed () -> random:. seed (44,55,66) %% Make a list of K random numbers %% Each random number in the range 1. .1000000 mkList (K) -> mkList (K, []) mkList (0, L) -> L; mkList (N, L) -> mkList (N-1, [random: uniform (1000000) | L]. ).
Test results, use pmap after smp number more than two, the speed will be about twice as fast. > Erl -boot start_clean -noshell -smp + S 1 -s ptests tests 1 >> results> erl -boot start_clean -noshell -smp + S 2 -s ptests tests 2 >> results> erl -boot start_clean -noshell -smp + S 3 -s ptests tests 3 >> results {1, {sort, 37334,45348, true}}. {1, {fib, 1615806,1625368, true}}. {2, {sort, 36983,24956, true} }. {2, {fib, 1641762,812359, true}}. {3, {sort, identical twins 36484,24912, true}}. {3, {fib, 1590642,902385, true}}. Reference
2015 (5) January (5) 2014 (72) December (9) November (4) October (4) September (5) August (7) seven March (3) June (5) May (7) Rubik's Cube: 8355 Solution notes erlang - edoc erlang - multicore Judge legend: Steel Heart - Brandon. . Sanderson erlang otp gen_fsm erlang identical twins otp WWW wake - Robert Sawyer April (5) March (6) February (9) January (8) 2013 (16) December ( 4) November (1) October (3) September (1) August (2) July (2) April (1) March (1) January (1 ) 2012 (23) October (1) September (1) August (3) June (2) May (2) April (5) March (6) two May (1) January (2) 2011 (25) December (7) November (3) October (2) September (2) August (4) July (1) June (1) April (2) February (3) 2010 (11) October (1) August (1) June (1) May (2) April (1) February (1) January (4) 2009 (28) December (2) November (1) October (4) September (1) August (2)

No comments:

Post a Comment