Pages

Friday, December 18, 2015

[Python] Multiprocess with multiple arguments

To run the program in different cores in parallel, we use the library multiprocessing to spawn a pool of processes, and map the function to the processes. A simple problem with a single argument is as follows

from multiprocessing.pool import Pool def my_fun(x):    return 2*x input_list = [3,4,5] num_of_processes = 4 Pool pool = Pool(num_of_processes) result_list = pool.map(my_fun, input_list) # the second argument has to be a list pool.close() pool.join() print result_list # should be [6,8,10]

It is simple and easy to understand: we set up how many processes we want, fit the function and input data to the processes.

What if I want to send one more argument into the function? Say, add a constant to the output (and the constant remain the same for different input x)
def my_fun2(x, general_const): return 2*x + general_const

You can simply wrap the arguments if you wish. But I will introduce a fancier way to do this: use partial. See the code below
from multiprocessing.pool import Pool from functools import partial def my_fun2(x, general_const): return 2*x + general_const input_list = [3,4,5] my_const = 1000 num_of_processes = 4 Pool pool = Pool(num_of_processes) result_list = pool.map(partial(my_fun2, general_const=my_const), input_list) ## if you prefer, you can also separate them (just another layout, does not change anything) # my_fun2_partial = partial(my_fun2, general_const=my_const) # result_list = pool.map(my_func2_partial, input_list) pool.close() pool.join() print result_list # should be [1006,1008,1010]

That's it!

Sunday, December 6, 2015

[Python] from __future__ import

Quick note:

This statement always confuses me

from __future__ import print_function
This is a interpreter directive that does a special import that use implementation in last Python.

For example, use Python 2.x and import a module named as print_function in Python3.x. This also reduce the works have to done from conversion from Python 2.x to Python 3.x

Friday, November 27, 2015

[git] Add ssh key to github account using command line

To push commits onto the remote repository hosted on your github account without password, your need to add your ssh key. Such process can be easily done using a web browser. In this post I will write down how to achieve this via command line.

In the following example, I assume the following"
1. Your github account is spencer
2. You want to label your ssh key on github as myLaptop
3. You know how to get your ssh key

Step 1: Connect to the api using curl
curl -u spencer https://api.github.com

Step 2: Post your key
curl -u spencer -data '{"title":"myLaptop","key":"ssh-rsa blahblahblah"}' https://api.github.com/user/keys


Sunday, November 22, 2015

禍福相依

我有兩輛二手腳踏車,約莫半年前,因為第一輛的鏈條過鬆、掉鏈的狀況十分嚴重,而我又不想花錢去修,索性便再買一輛,而本來那輛從此便停在社區裡佔著一個室外車位,而我有時也會把我現在的車子直接靠在那備用車上。時過半年,目前這輛車也到了極限了:無法變速、毫無煞車可言,車架尺寸不合等等...

今天早上出門時,我的車子不翼而飛,連帶我的備用車鎖也有明顯的破壞狀況,而我今天有事一定要用到腳踏車,真是美好的開始阿!怎麼辦呢?只好騎那上台備用車前去灌氣、買車燈、試車,這時我才發現,其實這輛坐起來蠻舒服的,只是鏈條太常掉無法忍受,於是便上網找了點資料,自己把它調好了。同時也在前往買車燈的時候,在小市集看到有人帶著精靈(elf)的長長尖帽子,豪口愛阿!如果我的車沒有失竊,我還真的沒辦法把這輛車修好,沒辦法得到一輛有煞車的車、也沒辦法看到有趣的畫面呢!(雖然還是這車還是有問題騎不快就是了),然後我也下定決心還是把它牽到室內車位好了,趁早養成習慣也好,不然下次被偷的就是我即將從臺灣帶來的車了。

下午出門赴約,氣溫是零度,下著大雨,同時也夾雜了今年的初雪。我穿著短袖排汗衣加上一件羽絨中層及防風外套,其實並不冷,覺得蠻舒服蠻享受的,回想去年的時候我已經穿了長袖外加羽絨大外套還是有點冷想趕快衝進室內,現在自己好像有比較適應了一些些了,不管是身體還是心靈上。

下午的約也蠻開心的,聽著朋友說著生活大小事,聽起來生活似乎很拼命累到有點崩潰但很充實且精采,聽了很開心。回家的時候顧著看遠方的救護車而摔車,但是人跟車都沒事(希望救護車的目的地的人們也能平安),繼續輕鬆的享受冰晶撲在臉上的感覺回家。哎呀!回家發現又有東西掉了(而且也想不起來我到底是放包包還是口袋),馬上再回到聚會的地點,但已大門深鎖,哈!自此以來,我所有從臺灣帶來、能放在口袋裡的重要物品已經遺失一輪了耶!然後便下了決心,不管我的口袋有多好用,重要物品都要放在包包、且到達和離開前一定要再全部檢查一次,不管待會是否會用到。

騎著騎著,雪的比例越來越多,心情也越來越好,大概也是我喜歡雪不喜歡雨的關係吧!然後突然想到中二時的往事,覺得很像也很不像(蛤?),說不定會舊事重演阿嘖嘖嘖...然後就想到:「嗯,又回到起點了,但這次的話有些事還是不要去想好了,說不定這幾天的事就是個警惕呢!」啊!靈台頓現清明,好多跑馬燈出現了,好溫暖啊(痾...是賣小女孩的火柴的故事嗎?)~

過往,遇到麻煩事我多半有兩種反應:一是頭腦全力想解決方法而顧不到表情所以結屎臉,二是覺得刺激而開心大笑。今天起又多了微笑了–覺得幸福的微笑。

我是個怪人,很怪。我解讀事情喜歡從反面去思考,例如我想知道某人或某店有多「好」,我去查的其實是批評:如果他人批評的理由有理,那我真的會對評價打折扣;相反地、要是大部分批評都是雞蛋挑骨頭,那我反而會對他加分。或許因為這樣,我的快樂除了很大一部分來自於他人的喜事外,開始有一部分是來自於失去某些事物、犯了些過錯的時候檢視自己而有些微成長而開心。今天掉的東西裡面除了有照片、護身符、可能已經破了我打死不敢用的幸運符和哩哩扣扣的東西外,有一些自我許願的小卡,其中有一張是很假掰的「幸福」。哈!在我丟了幸福的時候,我才真的感到幸福啊!好弔詭好幸福。

能夠在得到和失去的時候都感到幸福,真是太作弊了,不對社會和朋友作點事情真的有點慚愧,請盡量差遣我,謝謝。

Saturday, November 21, 2015

[Linux] Unzip tarballs to corresponding directories

Assume you have a tarball mySource.tar, which contains multiple tarballs inside, each inner tar balls contains multiple files. Now we want to unzip the tar balls and create one directory for each tar ball.

This task can be split into two steps:
1. Unzip a tarball, and put it into a directory
mkdir -p myOut && tar -C myOut -xv mySource.tar
2. Unzip multiple tarball, and put it into its corresponding directory. For example, the files in group1.tar will be extracted to a directory group1.

# in the directory, say, myOut
for  i in *.tar; do mkdir -p {i%.*};&& tar -C {i%.*} -xv $i; done
Please note that it works for a flat (not recursive).

Tuesday, October 13, 2015

[OSX] OpenMP on OSX

If you are new to openMP and your operation system is OSX. You may encounter similar problems when compiling your first openMP code as I did.

Typically, the tutorial will ask you compile your source code with

g++ -fopenmp mycode.cpp

However, you may get confusing error messages like
ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I will not talk about the reasons in detail, all you/I have to know is that this is because on OSX our dear compiler g++ is  by default linked to clang (i.e. when you call g++ on OSX, we actually call clang), which is another compiler that has not support openMP yet. So the solution is: use another compiler supporting openMP.

Version 1: Download the clang-omp
It seems that this issue only happens on OSX, so a OSX-only solution is sufficient: use Homebrew to install another compiler named clang-omp.

brew install clang-omp
Then use this when compiling the code

clang-omp -fopenmp mycode.cpp


Version 2: Compile and isolate the latest gcc (still from Homebrew)
One alternative way is to compile and install another gcc from source code. In this way, we need to download the gcc source code and its dependencies by our own and compile and install them. That is, we are going to install gmp, mpc, mpfr before gcc, and we will put this compiler under a directory/prefix like the others (e.g. /usr/local/gcc5.0.2/)

However, I have encountered some issues so I switched back to Homebrew for installing them.
(you probably do not need to install the dependencies one by one, but I did not test this :p)

brew install gmp
brew install mpc
brew install mpfr
brew tap homebrew/versions
brew install gcc5 --enable-gcc

After the above steps, the gcc5 from Homebrew will be under something like /usr/local/Cellar/gcc5/5.2.0/bin
Next, we should modify our environment variable to easily call it

Let's say your shell file is ~/.bashrc (mine is ~/.zshrc though)
edit ~/.bashrc and add the statements like (your latest gcc might be different)

export GCC5_HOME='/usr/local/Cellar/gcc5/5.2.0' export PATH=$PATH:$GCC5_HOME/bin/ alias gcc5='g++-5'

Reload the shell (or reopen your terminal)
then we can use gcc5 to compile the openMP files and remain the gcc/clang untouched

gcc5 -fopenmp mycode.cpp


Reference:
https://solarianprogrammer.com/2015/05/01/compiling-gcc-5-mac-os-x/

Friday, February 6, 2015

逐夢,築夢

自我學習而拿到學位,不再因為對學程或他人有所期待而失落。人只能靠自己,而那恰好也是我最自在的生活態度。

至於為何而戰呢?此時先放下以後那些廣大的夢想與願景吧!我現在想的是經濟自足與開闊的心,讓我能夠去流浪,當然也包含以往排斥的地方。

畢業 -> 賺錢 -> 辭職流浪 -> 尋根

愛情和親情的牽掛?嗯...

跟隨自己的心吧,自私一些。

Tuesday, January 13, 2015

連題目都想不出來了


看了這邊文章後,深深覺得自己真的是在行屍走肉無法思考。
http://www.parenting.com.tw/article/article.action?id=5063509

我不買樂透,但是留在這裡念書本身就是個只等運氣的決定。

一直在念著不知道寫什麼的文章,擠著連屎都不如的報告,每天都很辛苦的在...退步。如果下學期還是都這樣的話就真的要回家吃屎然後不見任何人了我想,等下學期再來看開獎吧。

期末考中的靠北文。