# 2016q3 Homework2 (phonebook-concurrent) contributed by <`HahaSula`> >未曾進行課程表單填寫,可能造成課程成績登記錯誤,請立即補填寫個人基本資料表單,以免權益受損[name=課程助教] ###### tags: `HahaSula` `concurrent` --- ## 預期研究目標 * concurrent vs * POSIX THREAD * 學習效能分析工具 * code refactoring 練習 * 探索 clz 的應用 ## 硬體規格 ```shell= ``` ## concurrent? vs Parallelism? 看完部分指定連結 >> 動作也太慢了吧?之後幾周都是這種等級的自修教材 [name=jserv] 問題癥結點? concurrent 問題? * concurrent ## 閱讀吳彥寬的實驗 (觀看 phonebook--concurrent) 尚未實作,先提出想法 append 改善 header部分 Head entryStart 重複了(使用上重複且其中一個僅使用一次) eptr 是一個mmap對印file的結尾 如果能用一個int 計數器不知效果會不會比較好? ```clike= phonebook-opt.h typedef struct _append_a { char *ptr; char *eptr; int tid; int nthread; entry *entryStart; entry *pHead; entry *pLast; } append_a; ``` 每一個thread 在file裡面的shift 是thread * Length而且為一量每一輪都要進行累加,想法承接上面,傳入entry計算量 這樣shift也不用累加(不知道哪種對cache比較好) ##### 想法示意圖 ![](https://i.imgur.com/g4F2rOm.png) ```clike= phonebook-opt.c void append(void *arg) { struct timespec start, end; double cpu_time; clock_gettime(CLOCK_REALTIME, &start); append_a *app = (append_a *) arg; int count = 0; entry *j = app->entryStart; for (char *i = app->ptr; i < app->eptr; i += MAX_LAST_NAME_SIZE * app->nthread, j += app->nthread,count++) { app->pLast->pNext = j; app->pLast = app->pLast->pNext; app->pLast->lastName = i; dprintf("thread %d append string = %s\n", app->tid, app->pLast->lastName); app->pLast->pNext = NULL; } clock_gettime(CLOCK_REALTIME, &end); cpu_time = diff_in_second(start, end); dprintf("thread take %lf sec, count %d\n", cpu_time, count); pthread_exit(NULL); } ``` phead 在上面有做一個malloc 而且有一個e的aline(沒有用到) 且在malloc之後又做一個 phead = phead->next的動作(NULL) main thread 本身應該也可以去分配append 這樣join 的waiting time 應該可以減少 最後在append各thread 的entry list 的時候可以把[1]給提出來 for迴圈裡面就不用再做branch(for 迴圈不大 應該成效不彰) ```clike= main.c entry *pHead, *e; pHead = (entry *) malloc(sizeof(entry)); printf("size of entry : %lu bytes\n", sizeof(entry)); e = pHead; e->pNext = NULL; ---- for (int i = 0; i < THREAD_NUM; i++) pthread_create( &tid[i], NULL, (void *) &append, (void *) app[i]); for (int i = 0; i < THREAD_NUM; i++) pthread_join(tid[i], NULL); entry *etmp; pHead = pHead->pNext; for (int i = 0; i < THREAD_NUM; i++) { if (i == 0) { pHead = app[i]->pHead->pNext; dprintf("Connect %d head string %s %p\n", i, app[i]->pHead->pNext->lastName, app[i]->ptr); } else { etmp->pNext = app[i]->pHead->pNext; dprintf("Connect %d head string %s %p\n", i, app[i]->pHead->pNext->lastName, app[i]->ptr); } etmp = app[i]->pLast; dprintf("Connect %d tail string %s %p\n", i, app[i]->pLast->lastName, app[i]->ptr); dprintf("round %d\n", i); } ``` thread pool沒做 concurrent-ll 沒分析 ###### tags: `HahaSula` `concurrent`