生成矩陣
命名模塊,配置編譯選項
-module(matrix_test).
-compile(export_all).
隨機生成M*N矩陣
%% 隨機產(chǎn)生M*N矩陣
generateMatrix(M,N) ->
row(M,N,[]).
row(M,N,Result) ->
case 0 =:= M of
true -> Result;
false -> row(M-1,N,Result++[column(N,[])])
end.
column(N,Result) ->
case 0 =:= N of
true -> Result;
false -> column(N-1,Result++[random:uniform(2)])
end.
測試,在erlang shell輸入:
1> c(matrix_test).
[2,1,2,2,2]]
{ok,matrix_test}
2> matrix_test:generateMatrix(5,5).
[[2,1,1,1,1],
[2,2,1,1,2],
[1,2,2,1,2],
[1,1,2,2,2],
單線程實現(xiàn)
%% 單線程
matrixMultiply(A,B) ->
R = lists:foldl(fun(Element,Acc) -> Acc++[rowMultiplyColumn(Element,B,[],1)] end, [], A),
printMatrix(R,'C').
%%@param
% A,B:list
% Result:存放結果,初始為[]
% Count:控制列數(shù),初始為1
% Result:存放結果,初始為[]
%%將某一行A與矩陣B相乘,得出一組向量
rowMultiplyColumn(A,B,Result,Count) ->
case Count =:= length(lists:nth(1,B))+1 of
true -> Result;
false ->
rowMultiplyColumn(A,B,Result ++ [multiply(A,getMatrixColumn(B,Count),0)],Count+1)
end.
%% 得到矩陣B的第Num列
getMatrixColumn(B,Num) ->
lists:foldl(fun(A,Acc) -> Acc++[lists:nth(Num, A)] end, [],B).
%% 計算兩個向量List的數(shù)量積
multiply([H1|T1],[H2|T2],Sum) when length(T1) =:= length(T2) ->
multiply(T1,T2,Sum+H1*H2);
multiply([],[],Sum) -> Sum.
為便于查看,按一定格式打印矩陣
% ????????
printMatrix(A,Name) ->
% P = fun(A, AccIn) -> io:format("~p ~n", [A]) end,
io:format("Matrix ~p is :~n",[Name]),
lists:foldl(fun printRow/2, [], A).
% ????????
printRow(Row, AccIn) ->
io:format("| "),
P = fun(Row,AccIn) -> io:format(" ~p ", [Row]) end,
lists:foldl(P, [], Row),
io:format("| ~n").
測試入口:
%%????????
io:format("Single Thread:~p[ms]~n",[Time/1000]).
test(M,N,R) ->
A = generateMatrix(M,N),
B = generateMatrix(N,R),
printMatrix(A,'A'),
printMatrix(B,'B'),
{Time,Value} = timer:tc(matrix_test,matrixMultiply,[A,B]),
測試,在erlang shell輸入:
1> c(matrix_test).
matrix_test.erl:10: Warning: variable 'Value' is unused
{ok,matrix_test}
2> matrix_test:test(5,5,5).
Matrix 'A' is :
|12211|
|12212|
|12111|
|12112|
|12221|
Matrix 'B' is :
|11122|
|21111|
|12212|
|22112|
|21211|
Matrix 'C' is :
| 11 10 10 8 11 |
| 13 11 12 9 12 |
| 10 8 8 7 9|
| 12 9 10 8 10|
| 13 12 11 9 13 |
Single Thread:0.619[ms]
ok
5*5矩陣相乘的單線程實現(xiàn)時間為0.619ms
,經(jīng)檢驗,計算結果也正確。
多線程實現(xiàn)
由于要比較串行運算與并行運算的時間花銷,所以將matrixMultiply
的打印結果語句去掉,改為:
%% 單線程
matrixMultiply(A,B) ->
R = lists:foldl(fun(Element,Acc) -> Acc++[rowMultiplyColumn(Element,B,[],1)] end, [], A).
多線程實現(xiàn)矩陣相乘
%% 多線程
multiThread(A,B) ->
P = self(),
%% 啟動矩陣A的行數(shù)個進程,分別發(fā)送A的每一行,計算A的每一行與矩陣B的乘積
lists:foldl(fun(Element,Acc) -> spawn(fun() -> P! {self(),Acc,rowMultiplyColumn(Element,B,[],1)} end) end, 1, A),
%% 接收,當前進程如果收到了A行數(shù)個消息,表明已經(jīng)計算完畢
loop(length(A)).
%% 統(tǒng)計當前進程是否已經(jīng)收到了Count條消息
loop(Count) ->
receive
{Pid,N,Result} ->
%% io:format("Count=~p,Message=~w~n",[Count,{Pid,N,Result}]),
case 1=:= Count of
true -> io:format("Compute end~n");
false -> loop(Count-1)
end
end.
矩陣相乘結果的正確性已經(jīng)驗證,打印高維矩陣意義不大,所以在測試入口將打印矩陣的語句去掉,并加入多線程運算:
%%測試入口
test(M,N,R) ->
A = generateMatrix(M,N),
B = generateMatrix(N,R),
% 串行運算
{Time,Value} = timer:tc(matrix_test,matrixMultiply,[A,B]),
io:format("Single Thread:~p[ms]~n",[Time/1000]),
% 并行運算
{Time1,Value1} = timer:tc(matrix_test,multiThread,[A,B]),
io:format("Multi Thread:~p[ms]~n",[Time1/1000]).
注意:為了你的筆記本,不要測試過高維的矩陣相乘。
測試,在erlang shell輸入:
14> c(matrix_test).
matrix_test.erl:11: Warning: variable 'Value' is unused
{ok,matrix_test}
15> matrix_test:test(5,5,5).
Single Thread:0.033[ms]
Compute end
Multi Thread:0.121[ms]
ok
5*5矩陣相乘的單線程實現(xiàn)時間為0.033ms
,多線程計算時間為0.121ms
,造成多線程時間花銷高于單線程的原因主要是計算量還較小,而建立線程的時間花銷大于并行計算節(jié)省的時間。
進而測試100*100矩陣相乘:
19> matrix_test:test(100,100,100).
Single Thread:1507.563[ms]
Compute end
Multi Thread:836.394[ms]
ok
單線程實現(xiàn)時間為1507.563ms
,多線程計算時間為836.394ms
,由此可知多線程對高維矩陣運算的計算速度有所提高。
測試200*200矩陣相乘:
20> matrix_test:test(200,200,200).
Single Thread:25639.889[ms]
Compute end
Multi Thread:15294.531[ms]
ok
單線程實現(xiàn)時間為25639.889ms
,多線程計算時間為15294.531ms
,隨著矩陣維度的增加,多線程的優(yōu)勢又下降了。
測試400*400矩陣相乘:
21> matrix_test:test(400,400,400).
當運行到多線程時,CPU負載達到400%以上,筆記本鋁合金外殼都發(fā)黑了,著實嚇出一身冷汗,迅速關閉終端,單線程運行的結果也就很遺憾地沒有保存。所以大家還是不要在自己的筆記本上測試這個了。