ZealOS/docs/Demo/MultiCore/MPRadix.CC.html
TomAwezome 1b75d91002 Fix Mount AHCI Port selection.
Add arg to SATARep to specify drive types to show.
Add checks in AHCIPortInit to verify port signatures, add helper method to get signatures from port.
2021-08-02 16:40:05 -04:00

189 lines
15 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V0.13">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>/*</span><span class=cF0>
<a name="l2"></a></span><span class=cF2>On an 8-core machine, this takes the top 3-bits</span><span class=cF0>
<a name="l3"></a></span><span class=cF2>of random numbers and distributes them to the 8 cores</span><span class=cF0>
<a name="l4"></a></span><span class=cF2>for sorting.</span><span class=cF0> </span><span class=cF2>Then, it merge sorts them.</span><span class=cF0>
<a name="l5"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l6"></a>
<a name="l7"></a>#</span><span class=cF1>define</span><span class=cF0> NUM </span><span class=cFE>1000000</span><span class=cF0>
<a name="l8"></a>
<a name="l9"></a></span><span class=cF9>I64</span><span class=cF0> my_mp_count = </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF5>Bsr</span><span class=cF0>(</span><span class=cFB>mp_count</span><span class=cF0>);</span><span class=cF2>//Power of 2</span><span class=cF0>
<a name="l10"></a>
<a name="l11"></a></span><span class=cF9>I32</span><span class=cF0> *arg1, *arg2;
<a name="l12"></a></span><span class=cF9>I32</span><span class=cF0> *b[my_mp_count], bn[my_mp_count];
<a name="l13"></a></span><span class=cF9>I64</span><span class=cF0> mp_not_done_flags;
<a name="l14"></a>
<a name="l15"></a></span><span class=cF9>I64</span><span class=cF0> Compare(</span><span class=cF9>I32</span><span class=cF0> *e1, </span><span class=cF9>I32</span><span class=cF0> *e2)
<a name="l16"></a>{
<a name="l17"></a> </span><span class=cF1>return</span><span class=cF0> *e1 - *e2;
<a name="l18"></a>}
<a name="l19"></a>
<a name="l20"></a></span><span class=cF1>U0</span><span class=cF0> QuickSortU32(</span><span class=cF9>I32</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num)
<a name="l21"></a>{</span><span class=cF2>//By customizing, we dramatically improve it!</span><span class=cF0>
<a name="l22"></a></span><span class=cF2>//Cut and paste from </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/QuickSort.CC.html#l1"><span class=cF4>QuickSortI64</span></a><span class=cF2>().</span><span class=cF0>
<a name="l23"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l24"></a> </span><span class=cF9>I32</span><span class=cF0> *less, *greater, pivot;
<a name="l25"></a>
<a name="l26"></a> </span><span class=cF1>if</span><span class=cF0> (num &gt; </span><span class=cFE>1</span><span class=cF0>)
<a name="l27"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l28"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l29"></a> {
<a name="l30"></a> less = base;
<a name="l31"></a> greater = base + num;
<a name="l32"></a> pivot = base[num / </span><span class=cFE>2</span><span class=cF0>];
<a name="l33"></a> </span><span class=cF1>while</span><span class=cF0> (less &lt; greater)
<a name="l34"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l35"></a> </span><span class=cF1>if</span><span class=cF0> (*less &lt;= pivot)
<a name="l36"></a> less++;
<a name="l37"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l38"></a> {
<a name="l39"></a> greater--;
<a name="l40"></a> </span><span class=cF5>SwapU32</span><span class=cF0>(less, greater);
<a name="l41"></a> }
<a name="l42"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l43"></a> i = less - base;
<a name="l44"></a> </span><span class=cF1>if</span><span class=cF0> (i == num)
<a name="l45"></a> </span><span class=cF7>{</span><span class=cF2>//All less or equ to pivot</span><span class=cF0>
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF2>//Point greater to first less</span><span class=cF0>
<a name="l48"></a> </span><span class=cF1>do</span><span class=cF0> greater--;
<a name="l49"></a> </span><span class=cF1>while</span><span class=cF0> (--i &amp;&amp; *greater == pivot);
<a name="l50"></a>
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (i)
<a name="l52"></a> {
<a name="l53"></a> less = base +num / </span><span class=cFE>2</span><span class=cF0>; </span><span class=cF2>//Pivot was not moved, point to it</span><span class=cF0>
<a name="l54"></a> </span><span class=cF1>if</span><span class=cF0> (less &lt; greater)
<a name="l55"></a> </span><span class=cF5>SwapU32</span><span class=cF0>(less, greater);
<a name="l56"></a> num = i;
<a name="l57"></a> }
<a name="l58"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF2>//All equ</span><span class=cF0>
<a name="l59"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l60"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l61"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (i &lt; num / </span><span class=cFE>2</span><span class=cF0>)
<a name="l62"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l63"></a> QuickSortU32(base, i);
<a name="l64"></a> num -= i;
<a name="l65"></a> base = greater;
<a name="l66"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l67"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l68"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l69"></a> QuickSortU32(greater, num - i);
<a name="l70"></a> num = i;
<a name="l71"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l72"></a> }
<a name="l73"></a> </span><span class=cF1>while</span><span class=cF0> (num &gt; </span><span class=cFE>1</span><span class=cF0>);
<a name="l74"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l75"></a>}
<a name="l76"></a>
<a name="l77"></a></span><span class=cF1>U0</span><span class=cF0> MPSort(</span><span class=cF9>I64</span><span class=cF0> dummy=</span><span class=cFE>0</span><span class=cF0>)
<a name="l78"></a>{
<a name="l79"></a> </span><span class=cF1>no_warn</span><span class=cF0> dummy;
<a name="l80"></a> QuickSortU32(b[</span><span class=cF5>Gs</span><span class=cF0>-&gt;num], bn[</span><span class=cF5>Gs</span><span class=cF0>-&gt;num]);
<a name="l81"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;mp_not_done_flags, </span><span class=cF5>Gs</span><span class=cF0>-&gt;num);
<a name="l82"></a>}
<a name="l83"></a>
<a name="l84"></a></span><span class=cF1>U0</span><span class=cF0> MPRadixSortDemo(</span><span class=cF9>I64</span><span class=cF0> dummy=</span><span class=cFE>0</span><span class=cF0>)
<a name="l85"></a>{
<a name="l86"></a> </span><span class=cF1>no_warn</span><span class=cF0> dummy;
<a name="l87"></a> </span><span class=cF9>I64</span><span class=cF0> i, j, k1, k2;
<a name="l88"></a> </span><span class=cF1>F64</span><span class=cF0> t0;
<a name="l89"></a>
<a name="l90"></a> arg1 = </span><span class=cF5>MAlloc</span><span class=cF0>(NUM * </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l91"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; NUM; i++)
<a name="l92"></a> arg1[i] = </span><span class=cF5>RandI32</span><span class=cF0>;
<a name="l93"></a>
<a name="l94"></a> arg2 = </span><span class=cF5>MAlloc</span><span class=cF0>(NUM * </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l95"></a>
<a name="l96"></a> </span><span class=cF6>&quot;$GREEN$QuickSort$FG$\n&quot;</span><span class=cF0>;
<a name="l97"></a> t0 = </span><span class=cF5>tS</span><span class=cF0>;
<a name="l98"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(arg2, arg1, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0> * NUM);
<a name="l99"></a> </span><span class=cF5>QuickSort</span><span class=cF0>(arg2, NUM, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>, &amp;Compare);
<a name="l100"></a> </span><span class=cF6>&quot;Time:%9.6f\n&quot;</span><span class=cF0>, </span><span class=cF5>tS</span><span class=cF0> - t0;
<a name="l101"></a> </span><span class=cF5>D</span><span class=cF0>(arg2 + NUM / </span><span class=cFE>4</span><span class=cF0>);
<a name="l102"></a>
<a name="l103"></a> </span><span class=cF6>&quot;$GREEN$QuickSortU32$FG$\n&quot;</span><span class=cF0>;
<a name="l104"></a> t0 = </span><span class=cF5>tS</span><span class=cF0>;
<a name="l105"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(arg2, arg1, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>*NUM);
<a name="l106"></a> QuickSortU32(arg2, NUM);
<a name="l107"></a> </span><span class=cF6>&quot;Time:%9.6f\n&quot;</span><span class=cF0>, </span><span class=cF5>tS</span><span class=cF0> - t0;
<a name="l108"></a> </span><span class=cF5>D</span><span class=cF0>(arg2 + NUM / </span><span class=cFE>4</span><span class=cF0>);
<a name="l109"></a>
<a name="l110"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; my_mp_count; i++)
<a name="l111"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l112"></a> </span><span class=cF2>//We must do full size, just in case.</span><span class=cF0>
<a name="l113"></a> </span><span class=cF2>//There will be uneven split between cores</span><span class=cF0>
<a name="l114"></a> </span><span class=cF2>//depending on the distribution of rand numbers.</span><span class=cF0>
<a name="l115"></a> b[i] = </span><span class=cF5>MAlloc</span><span class=cF0>(NUM * </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l116"></a> bn[i] = </span><span class=cFE>0</span><span class=cF0>;
<a name="l117"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l118"></a>
<a name="l119"></a> </span><span class=cF1>if</span><span class=cF0> (my_mp_count &lt; </span><span class=cFE>2</span><span class=cF0>)
<a name="l120"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'MultCore'</span><span class=cF0>);
<a name="l121"></a>
<a name="l122"></a> </span><span class=cF6>&quot;$GREEN$MP Radix QuickSortU32$FG$\n&quot;</span><span class=cF0>;
<a name="l123"></a> t0 = </span><span class=cF5>tS</span><span class=cF0>;
<a name="l124"></a> k1 = </span><span class=cFE>32</span><span class=cF0> - </span><span class=cF5>Bsr</span><span class=cF0>(my_mp_count);
<a name="l125"></a> k2 = my_mp_count / </span><span class=cFE>2</span><span class=cF0>;
<a name="l126"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; NUM; i++)
<a name="l127"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l128"></a> j = arg1[i] &gt;&gt; k1 + k2; </span><span class=cF2>//This is a preliminary radix sort.</span><span class=cF0>
<a name="l129"></a> b[j][bn[j]++] = arg1[i];
<a name="l130"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l131"></a> mp_not_done_flags = </span><span class=cFE>1</span><span class=cF0> &lt;&lt; my_mp_count - </span><span class=cFE>1</span><span class=cF0>;
<a name="l132"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; my_mp_count; i++)
<a name="l133"></a> </span><span class=cF5>Spawn</span><span class=cF0>(&amp;MPSort, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF3>NULL</span><span class=cF0>, i);
<a name="l134"></a> </span><span class=cF1>while</span><span class=cF0> (mp_not_done_flags)
<a name="l135"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l136"></a> j = </span><span class=cFE>0</span><span class=cF0>;
<a name="l137"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; my_mp_count; i++)
<a name="l138"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l139"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;arg2[j], b[i], bn[i] * </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l140"></a> j += bn[i];
<a name="l141"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l142"></a> </span><span class=cF6>&quot;Time:%9.6f\n&quot;</span><span class=cF0>, </span><span class=cF5>tS</span><span class=cF0> - t0;
<a name="l143"></a> </span><span class=cF5>D</span><span class=cF0>(arg2 + NUM / </span><span class=cFE>4</span><span class=cF0>);
<a name="l144"></a>
<a name="l145"></a> </span><span class=cF5>Free</span><span class=cF0>(arg1);
<a name="l146"></a> </span><span class=cF5>Free</span><span class=cF0>(arg2);
<a name="l147"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; my_mp_count; i++)
<a name="l148"></a> </span><span class=cF5>Free</span><span class=cF0>(b[i]);
<a name="l149"></a>}
<a name="l150"></a>
<a name="l151"></a>MPRadixSortDemo;
<a name="l152"></a>
<a name="l153"></a></span><span class=cF2>/*</span><span class=cF0> Results on 8 Cores 3.397GHz Core i7:
<a name="l154"></a></span><span class=cF2>QuickSort</span><span class=cF0>
<a name="l155"></a>Time: 0.759998
<a name="l156"></a></span><span class=cF2>QuickSortU32</span><span class=cF0>
<a name="l157"></a>Time: 0.093684
<a name="l158"></a></span><span class=cF2>MP Radix QuickSortU32</span><span class=cF0>
<a name="l159"></a>Time: 0.045450
<a name="l160"></a></span><span class=cF2>*/</span><span class=cF0>
</span></pre></body>
</html>