templeos-info/public/Wb/Demo/MultiCore/Primes.HC.HTML

107 lines
7.1 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>U8</span><span class=cF0> *master_bitmap;
<a name="l2"></a></span><span class=cF9>I64</span><span class=cF0> prime_range,my_mp_cnt,pending;
<a name="l3"></a>
<a name="l4"></a></span><span class=cF1>U0</span><span class=cF0> PrimesJob(</span><span class=cF9>I64</span><span class=cF0> i)
<a name="l5"></a>{
<a name="l6"></a> </span><span class=cF9>I64</span><span class=cF0> j,k,l=</span><span class=cF5>Sqrt</span><span class=cF0>(prime_range),
<a name="l7"></a> lo=i*prime_range/my_mp_cnt,
<a name="l8"></a> hi=(i+1)*prime_range/my_mp_cnt,
<a name="l9"></a> lo2=lo+</span><span class=cFB>sys_cache_line_width</span><span class=cF0>,
<a name="l10"></a> hi2=hi-</span><span class=cFB>sys_cache_line_width</span><span class=cF0>;
<a name="l11"></a> </span><span class=cF1>if</span><span class=cF0> (lo2&gt;hi2) lo2=hi2;
<a name="l12"></a> </span><span class=cF1>for</span><span class=cF0> (j=2;j&lt;=l;j++) </span><span class=cF7>{</span><span class=cF0>
<a name="l13"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>master_bitmap,j</span><span class=cF7>)</span><span class=cF0>) {
<a name="l14"></a> </span><span class=cF1>if</span><span class=cF0> (j&gt;=lo)
<a name="l15"></a> k=j+j;
<a name="l16"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l17"></a> k=lo-lo%j;
<a name="l18"></a> </span><span class=cF1>if</span><span class=cF0> (k&lt;lo)
<a name="l19"></a> k+=j;
<a name="l20"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l21"></a> </span><span class=cF1>while</span><span class=cF0> (k&lt;lo2) </span><span class=cF7>{</span><span class=cF0>
<a name="l22"></a> </span><span class=cF5>LBts</span><span class=cF0>(master_bitmap,k);
<a name="l23"></a> k+=j;
<a name="l24"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l25"></a> </span><span class=cF1>while</span><span class=cF0> (k&lt;hi2) </span><span class=cF7>{</span><span class=cF0>
<a name="l26"></a> </span><span class=cF5>Bts</span><span class=cF0>(master_bitmap,k);
<a name="l27"></a> k+=j;
<a name="l28"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l29"></a> </span><span class=cF1>while</span><span class=cF0> (k&lt;hi) </span><span class=cF7>{</span><span class=cF0>
<a name="l30"></a> </span><span class=cF5>LBts</span><span class=cF0>(master_bitmap,k);
<a name="l31"></a> k+=j;
<a name="l32"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l33"></a> }
<a name="l34"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l35"></a> </span><span class=cF1>lock</span><span class=cF0> pending--;
<a name="l36"></a>}
<a name="l37"></a>
<a name="l38"></a></span><span class=cF9>I64</span><span class=cF0> Primes(</span><span class=cF9>I64</span><span class=cF0> range,</span><span class=cF9>I64</span><span class=cF0> _my_mp_cnt)
<a name="l39"></a>{
<a name="l40"></a> </span><span class=cF9>I64</span><span class=cF0> i,prime_cnt=0;
<a name="l41"></a> </span><span class=cF1>F64</span><span class=cF0> t0=</span><span class=cF5>tS</span><span class=cF0>,tf;
<a name="l42"></a> prime_range=range;
<a name="l43"></a> </span><span class=cF1>if</span><span class=cF0> (_my_mp_cnt&lt;=</span><span class=cFB>mp_cnt</span><span class=cF0>)
<a name="l44"></a> my_mp_cnt=_my_mp_cnt;
<a name="l45"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l46"></a> my_mp_cnt=</span><span class=cFB>mp_cnt</span><span class=cF0>;
<a name="l47"></a> master_bitmap=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>prime_range+7</span><span class=cF7>)</span><span class=cF0>/8+1);
<a name="l48"></a> </span><span class=cF5>Bts</span><span class=cF0>(master_bitmap,0);
<a name="l49"></a> </span><span class=cF5>Bts</span><span class=cF0>(master_bitmap,1);
<a name="l50"></a> pending=my_mp_cnt;
<a name="l51"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;my_mp_cnt;i++)
<a name="l52"></a> </span><span class=cF5>JobQue</span><span class=cF0>(&amp;PrimesJob,i,i);
<a name="l53"></a> </span><span class=cF1>while</span><span class=cF0> (pending)
<a name="l54"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l55"></a> tf=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l56"></a>
<a name="l57"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;prime_range;i++)
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>master_bitmap,i</span><span class=cF7>)</span><span class=cF0>)
<a name="l59"></a> prime_cnt++;
<a name="l60"></a>
<a name="l61"></a> </span><span class=cF1>for</span><span class=cF0> (i=</span><span class=cF5>MaxI64</span><span class=cF7>(</span><span class=cF0>prime_range-100,0</span><span class=cF7>)</span><span class=cF0>;i&lt;prime_range;i++)
<a name="l62"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>master_bitmap,i</span><span class=cF7>)</span><span class=cF0>)
<a name="l63"></a> </span><span class=cF6>&quot;%d &quot;</span><span class=cF0>,i;
<a name="l64"></a>
<a name="l65"></a> </span><span class=cF6>&quot;\n$RED$CPUs:%d PrimeRange:%,d PrimeCnt:%,d Time:%9.7,f$FG$\n&quot;</span><span class=cF0>,
<a name="l66"></a> my_mp_cnt,prime_range,prime_cnt,tf-t0;
<a name="l67"></a>
<a name="l68"></a> </span><span class=cF5>Free</span><span class=cF0>(master_bitmap);
<a name="l69"></a> </span><span class=cF1>return</span><span class=cF0> prime_cnt;
<a name="l70"></a>}
<a name="l71"></a>
<a name="l72"></a>Primes(100,1);
<a name="l73"></a>Primes(100,</span><span class=cFB>mp_cnt</span><span class=cF0>);
<a name="l74"></a>Primes(1000000,1);
<a name="l75"></a>Primes(1000000,</span><span class=cFB>mp_cnt</span><span class=cF0>);
</span></pre></body>
</html>