2013年4月8日 星期一

llvm analysis notes

refs:
http://163.25.101.87/~yen3/wiki/doku.php?id=llvm:llvm_notes



  • llvm commands



  • Convert C/C++ code to llvm IR
    llvm-g++ -emit-llvm -S test.cpp -o test.ll
  • Convert llvm IR to llvm bitcode
    llvm-as test.ll -o test.bc
  • Print Control Flow Graph
    opt -dot-cfg test.bc
  • Print Dominator Tree Graph
    opt -dot-dom test.bc
  • Conver C++ code to C code
    llvm-g++ -emit-llvm test.cpp -o program.bc -c
    llc -march=c program.bc -o program.c

  • analysis(ADT analysis, rule check, redundant check, frame remap)
    • Alias analysis
      • alias memory/register (immutable/mutable) label 
      • check different two frames when the input arg is depended from another? 
    • CFG analysis
      • domain tree, basic block(entry, exist, loop, if/else), operator, reg/mem location
      • loop (vector)
        • for(i=0;i<10 a1="" a2="" b1="" b2="" c1="" c2="" i="" li="" nbsp="">
        • vector(c1,c2) <= {a1,b1} {a2,b2}
    • register analysis
      • virtual register to physical register(arm r0~r10) , rega, regb, regc
    • memory analysis
      • load/store analysis(store is depended on load?)
      • parallel load/store with different address block in the same BB 
    • dependent analysis
      • (depend memory? load/store check)
        • a[0] = 1; # load from constant input
        • b = a[0]; # b is depend from previous assignment, store to output
      • split iterator
        • for(i=0; i<10 ...="" a="" i="" li="" nbsp="" org="">
        • fcheck each unit memory block dependence

2013年4月2日 星期二

LLVM + ZeroMQ = Remote Compiler flow plan

flow plan

  • portable for different platform (client arm, server x86) balance tasks
    • arm for low power mobile device
    • x86 for high performance server device
  • using zeroMQ to our communication handshaking
    • balance the loading
    • multi protocols(TCP/UDP)
    • distribution communication
    • internal broadcast
  • client part
    • method
      • application to llvm byte code or llvm-IR
      • pull llvm byte code or llvm-IR to server by zeroMQ request
      • get server response when the pull-task is done by zeroMQ response
    • require
      • llvm compiler
  • server part
    • method
      • get llvm byte code or llvm-IR from client request
      • do task reassign based on it's server utility, like 2cores CPU(client) to 64 cores CPU(server)
      • put result to client
    • require
      • listen port to link client request
      • byte / IR ADT remap to physical hardware
      • multi requests/ multi tasks balance timeout issue
      • llvm compiler

Ta-lib + bigdata = stock analysis tool

  • 如何用30分鐘, 打造出自己獨特程式交易系統. 話不多說,我們就直接開始吧!!!!
  • requirements
    • Ta-lib(support finance indicators)
    • bigdata(pandas, numpy, scipy, matplotlib...)
    • Ta-lib Cython wrapper (wrapper Ta-lib to python)
  • how to install in Mac 
    • install Ta-lib (port install ta-lib)
    • install bigdata 
      • port install py27-pandas
      • port install py27-numpy
      • port install py27-matplotlib
      • ...
    • setup python search path for mac port
      • add the below info to "~/.bashrc"
        • "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"
      • source ~/.bashrc
    • install Ta-lib(wrapper)
      • https://github.com/funningboy/ta-lib
  • example case
    • 買點
      • 利用5日平均線大於10日平均線
    • 賣點
      • 持有天數最多只能有三天
    •  結論
      • 還是實在賺錢比較重要
    • ref:
      • https://github.com/funningboy/ta-lib/blob/master/tools/tdr_jump.py
    • results:
total profit is : -20.26
----------------------------------------------------------------------------------------------------
               ma10      ma5   close    high     low    open   volume  entry  leave  profit
2013-01-02      NaN      NaN  723.25  727.00  716.55  719.42  2541300      0      0    0.00
2013-01-03      NaN      NaN  723.67  731.93  720.72  724.93  2318200      0      0    0.00

import sys                                                                                                                                                                           
from datetime import datetime

import numpy as np
import talib
import matplotlib.finance as fin 
from pylab import show

from pandas import Index, DataFrame, Series
from pandas.core.datetools import BMonthEnd
from pandas import ols 


def getQuotes(symbol, start, end):
    """ get stock Quotes from Yahoo """

    quotes = fin.quotes_historical_yahoo(symbol, start, end)
    dates, open, close, high, low, volume = zip(*quotes)

    data = { 
        'open': open,
        'close': close,
        'high': high,
        'low': low,
        'volume': volume
    }   

    dates = Index([datetime.fromordinal(int(d)) for d in dates])
    return DataFrame(data, index=dates)


def getMA(quotes):
    """ get mv_avg indicator and update org quotes by joining two DataFrame """

    ma5 = talib.MA(quotes['close'],5)
    ma10 = talib.MA(quotes['close'],10)

    data = { 
            'ma5' : ma5,
            'ma10': ma10,
            }   

    update = DataFrame(data, index=quotes.index)
    return update.join(quotes)

def setEntryRule1(quotes):
    """ set entry rule when ma5 is cross-over ma10 """

    def rule1(quotes, index):
        rst = 1 if quotes['ma5'][index] > quotes['ma10'][index] else 0
        return rst

    quotes['entry'] = [rule1(quotes, index) for index in quotes.index]
    return quotes


def setLeaveRule1(quotes):
    """ set leave rule when the holding day is large than 3 days"""

    def rule1(quotes, position):
        if position - 3 >= 0:
            if quotes['entry'][position-3] == 1:
                return [1, quotes['close'][position] - quotes['close'][position-3]]
        return [0, 0]

    quotes['leave'], quotes['profit'] = zip(*[rule1(quotes, position) for position, index in enumerate(quotes.index)])
    return quotes


def getProfit(quotes):
    """ get profit report """

    print "total profit is : %s" %(sum(quotes['profit']))
    print "-" * 100
    print quotes


def main():
    # get stock info from start to end
    startDate = datetime(2013, 1, 1)
    endDate = datetime(2013, 1, 30)

    # get stock id
    goog = getQuotes('GOOG', startDate, endDate)
    goog = getMA(goog)

    goog = setEntryRule1(goog)
    goog = setLeaveRule1(goog)
    getProfit(goog)

if __name__ == '__main__':                                                                                                                                                           
    main()