contributed by <snoopy831002
>
concurrency 的開始:cpu的演進不再以提昇時脈為方向,多核與多執行序的時代來臨
未來的效能效能提昇方向:
未來程式的執行效能將不仰賴cpu的速度快慢,而是程式的平行化。
lock 與 lock-free programming(核心目標為使程序間可互相溝通順利)
concurrency v.s parallelism
concurrency所產生的問題:
coroutine
因為原始使用fget的緣故,城市無法平行化,所以造成append()的時間一直無法有效縮短。故吳彥寬同學使用了mmap
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
1.addr可以設定起始位置
2.length決定mapping大小
3.flags可以決定是否可供其他thread看見
4.offset則由fd決定
pthread_t a_thread;//定義thread
pthread_attr_t a_thread_attribute;//通常使用pthread_attr_default即可
void thread_function(void *argument);//thread 所要執行的函式
char *some_argument;//thread 所要執行的函式中塞入的參數
pthread_create( &a_thread, a_thread_attribute, (void *)&thread_function,
(void *) &some_argument);
pthread_exit()
pthread_delay_np()
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, pthread_mutexattr_default);//定義mutex
pthread_mutex_lock( &mutex );//鎖定共享資源
pthread_mutex_unlock( &mutex );//解所共享資源
pthread_mutex_destroy();//結束mutex
之前的實做著重在減少findname()的執行時間,現在我們可以利用thread pool的方式優化append()。
實際操做
threadpool_t *pool=threadpool_create(THREAD_NUM,QUEUE SIZE,FLAG);
threadpool_add(threadpool_t *pool, void (*function)(void *)
int threadpool_destroy(threadpool_t *pool, int flags);
最後清理掉threadpool實做結果
沒想到使用一條thread竟然是最快的…
plot ’datafile’ using <xcol>:<ycol>:xticlabels(<labelcol>) with <plotstyle>
2:xtic(1)
: 使用第二行資料與第一行資料作為xlabel作圖using ($0-0.2):($2+0.001):2
: 現在只有一維資料y(所以x使用$0表示label之x座標), $2是第二個column的資料座標, 2則是表示使用output.txt中的第二行資料作為資料來源。-0.2與+0.001則是座標的調整
reset //清除先前設定
set ylabel 'time(sec)' //設定y軸資訊
set style fill solid
set title 'perfomance comparison with different num of threads' //設定圖片標題
set term png enhanced font 'Verdana,10'
set output 'runtime.png' //設定輸出檔名
plot [:][:0.02]'output.txt' using 2:xtic(1) with histogram title '1 thread', \
'' using ($0-0.2):($2+0.001):2 with labels title '', \
'' using 3:xtic(1) with histogram title '2 thread' , \
'' using ($0-0.04):($3+0.0015):3 with labels title '', \
'' using 4:xtic(1) with histogram title '4 thread' , \
'' using ($0+0.01):($4+0.0025):4 with labels title'',\
'' using 5:xtic(1) with histogram title '8 thread' , \
'' using ($0+0.2):($5+0.0021):5 with labels title ' ', \
'' using 6:xtic(1) with histogram title '16 thread', \
'' using ($0+0.45):($6+0.003):6 with labels title ' ', \
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
![image alt](https:// "title") | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | Emoji list | ||
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing