Ejemplo n.º 1
0
def run_blackscholes(args, use_weld):
    p, s, t, r, v = get_data(args.num_els)
    if use_weld:
        p = weldarray(p)
        s = weldarray(s)
        t = weldarray(t)
        r = weldarray(r)
        v = weldarray(v)

    start = time.time()
    call, put = blackscholes(p, s, t, r, v, args.intermediate_eval,
                             args.use_group)

    if isinstance(call, weldarray):
        call = call.evaluate()
        put = put.evaluate()
        print("**************************************************")
        print "weld took: %.4f (result=%.4f)" % (time.time() - start, call[0])
        print("**************************************************")
    else:
        print("**************************************************")
        print "numpy took: %.4f (result=%.4f)" % (time.time() - start, call[0])
        print("**************************************************")

    return call, put
def blackscholes(price, strike, t, rate, vol, threads):
    c05 = np.float64(3.0)
    c10 = np.float64(1.5)
    rsig = rate + (vol * vol) * c05
    vol_sqrt = vol * np.sqrt(t)

    d1 = (np.log(price / strike) + rsig * t) / vol_sqrt
    d2 = d1 - vol_sqrt

    # these are numpy arrays, so use scipy's erf function. scipy's ufuncs also
    # get routed through the common ufunc routing mechanism, so these work just
    # fine on weld arrays.
    d1 = c05 + c05 * ss.erf(d1 * invsqrt2)
    d2 = c05 + c05 * ss.erf(d2 * invsqrt2)

    e_rt = np.exp((0.0 - rate) * t)

    call = (price * d1) - (e_rt * strike * d2)
    put = e_rt * strike * (c10 - d2) - price * (c10 - d1)

    lazy_ops = generate_lazy_op_list([call, put])
    outputs = gr.group(lazy_ops).evaluate(True,
                                          passes=wn.CUR_PASSES,
                                          num_threads=threads)
    call = weldarray(outputs[0])
    put = weldarray(outputs[1])
    return call, put
Ejemplo n.º 3
0
def blackscholes(price, strike, t, rate, vol, intermediate_eval, use_group):
    '''
    Implements the Black Scholes pricing model using NumPy and SciPy.
    Based on the code given by Intel, but cleaned up.

    The following links were used to define the constants c05 and c10:

    http://codereview.stackexchange.com/questions/108533/fastest-possible-cython-for-black-scholes-algorithm
    http://gosmej1977.blogspot.com/2013/02/black-and-scholes-formula.html
    '''
    c05 = np.float64(3.0)
    c10 = np.float64(1.5)
    rsig = rate + (vol * vol) * c05
    vol_sqrt = vol * np.sqrt(t)

    d1 = (np.log(price / strike) + rsig * t) / vol_sqrt
    d2 = d1 - vol_sqrt

    # these are numpy arrays, so use scipy's erf function. scipy's ufuncs also
    # get routed through the common ufunc routing mechanism, so these work just
    # fine on weld arrays.
    d1 = c05 + c05 * ss.erf(d1 * invsqrt2)
    d2 = c05 + c05 * ss.erf(d2 * invsqrt2)

    e_rt = np.exp((0.0 - rate) * t)

    # An alternative to using the group operator is to manually evaluate all
    # the intermediate arrays whose values will be reused later in
    # computations. This is harder to do precisely, and we fail to apply
    # certain optimizations that weld could when using the group operator.
    if isinstance(price, weldarray) and intermediate_eval:
        price = price.evaluate()
        d1 = d1.evaluate()
        d2 = d2.evaluate()
        e_rt = e_rt.evaluate()
        strike = strike.evaluate()

    call = (price * d1) - (e_rt * strike * d2)
    put = e_rt * strike * (c10 - d2) - price * (c10 - d1)

    # the group operator! This essentially does the same thing as the
    # intermediate evaluation option used above - it avoids recomputions.
    if isinstance(call, weldarray) and use_group:
        lazy_ops = generate_lazy_op_list([call, put])
        outputs = gr.group(lazy_ops).evaluate(True, passes=wn.CUR_PASSES)

        call = weldarray(outputs[0])
        put = weldarray(outputs[1])

    # if we were not using the group operator.
    if isinstance(call, weldarray) and not use_group:
        call = call.evaluate()
        put = put.evaluate()

    return call, put
Ejemplo n.º 4
0
def run_haversine_with_scalar(args):
    orig_lat = df['latitude'].values
    orig_lon = df['longitude'].values

    if args.use_numpy:
        ########### Numpy stuff ############
        if not os.path.isfile(LATS_NAME):
            lat, lon = gen_data(orig_lat, orig_lon, scale=args.scale)
        else:
            lat, lon = read_data()
        print('num rows in lattitudes: ', len(lat))

        start = time.time()
        dist1 = haversine(40.671, -73.985, lat, lon)
        end = time.time()
        print('****************************')
        print('numpy took {} seconds'.format(end-start))
        print('****************************')
	del(lat) 
	del(lon)
    else:
        print('Not running numpy')
    # just in case let us free memory

    if args.use_weld:
        ####### Weld stuff ############
        if not os.path.isfile(LATS_NAME):
            lat2, lon2 = gen_data(orig_lat, orig_lon, scale=args.scale)
        else:
            lat2, lon2 = read_data()
        print('num rows in lattitudes: ', len(lat2))
        lat2 = weldarray(lat2)
        lon2 = weldarray(lon2)
        start = time.time()
        dist2 = haversine(40.671, -73.985, lat2, lon2) 
        if args.use_group:
            lazy_ops = generate_lazy_op_list([dist2])
            dist2 = gr.group(lazy_ops).evaluate(True, passes=wn.CUR_PASSES)[0]
        else:
            dist2 = dist2.evaluate()
	
	#print(np.sum(dist2))
        end = time.time()
        print('****************************')
        print('weld took {} seconds'.format(end-start))
        print('****************************')
        print('END')
    else:
        print('Not running weld')
    
    if args.use_numpy and args.use_weld:
        compare(dist1, dist2)
def run_blackscholes(args, use_weld):
    sys.stdout.write("Generating data...")
    sys.stdout.flush()
    p, s, t, r, v = get_data(args.size)
    if use_weld:
        p = weldarray(p)
        s = weldarray(s)
        t = weldarray(t)
        r = weldarray(r)
        v = weldarray(v)
    sys.stdout.write("done")
    sys.stdout.flush()

    start = time.time()
    call, put = blackscholes(p, s, t, r, v, args.threads)
    return call, put
def run_haversine_with_scalar(args):

    lat2, lon2 = gen_data(args.scale)

    print('num rows in lattitudes: ', len(lat2))
    lat2 = weldarray(lat2)
    lon2 = weldarray(lon2)

    start = time.time()
    dist2 = haversine(lat2, lon2)
    dist2 = dist2.evaluate()
    end = time.time()

    print('****************************')
    print('weld took {} seconds'.format(end - start))
    print('****************************')
    print dist2[0:5]
Ejemplo n.º 7
0
def get_bs_data(use_weld):
    # SIZE = (2 << 20)
    SIZE = args.num_els
    # random prices between 1 and 101
    price = np.float32(np.random.rand(SIZE) * 100)
    # random prices between 0 and 101
    strike = np.float32(np.random.rand(SIZE) * 100)
    # random maturity between 0 and 4
    t = np.float32(1 + np.random.rand(SIZE) * 6)
    # random rate between 0 and 1
    rate = np.float32(0.01 + np.random.rand(SIZE))
    # random volatility between 0 and 1
    vol = np.float32(0.01 + np.random.rand(SIZE))

    if use_weld:
        return wn.weldarray(price), wn.weldarray(strike), wn.weldarray(t), wn.weldarray(rate),wn.weldarray(vol)
    else:
        return price, strike, t, rate, vol
Ejemplo n.º 8
0
def given_arrays(l, dtype):
    '''
    @l: list.
    returns a np array and a weldarray.
    '''
    test = np.array(l, dtype=dtype)
    np_test = np.copy(test)
    w = weldarray(test)

    return np_test, w
Ejemplo n.º 9
0
def run_blackscholes(args, use_weld):
    p, s, t, r, v = get_data(args.num_els)

    if use_weld:
        p = weldarray(p)
        s = weldarray(s)
        t = weldarray(t)
        r = weldarray(r)
        v = weldarray(v)

    start = time.time()
    call, put = blackscholes(p, s, t, r, v, args.int_eval, args.use_group)

    if use_weld:
        print "-------------> Weld: %.4f (result=%.4f)" % (time.time() - start,
                                                           call[0])
    else:
        print "-------------> Numpy: %.4f (result=%.4f)" % (time.time() -
                                                            start, call[0])

    return call, put
Ejemplo n.º 10
0
def test_multiple_array_creation():
    '''
    Minor edge case but it fails right now.
    ---would probably be fixed after we get rid of the loop fusion at the numpy
    level.
    '''
    np_test, w = random_arrays(NUM_ELS, 'float32')
    w = weldarray(w)        # creating array again.
    w2 = np.exp(w)
    weld_result = w2.evaluate()
    np_result = np.exp(np_test)

    assert np.allclose(weld_result, np_result)
Ejemplo n.º 11
0
def random_arrays(num, dtype):
    '''
    Generates random Weld array, and numpy array of the given num elements.
    '''
    # np.random does not support specifying dtype, so this is a weird
    # way to support both float/int random numbers
    test = np.zeros((num), dtype=dtype)
    test[:] = np.random.randn(*test.shape)
    test = np.abs(test)
    # at least add 1 so no 0's (o.w. divide errors)
    random_add = np.random.randint(1, high=10, size=test.shape)
    test = test + random_add
    test = test.astype(dtype)

    np_test = np.copy(test)
    w = weldarray(test, verbose=False)

    return np_test, w
Ejemplo n.º 12
0
def random_galaxy(N):
    """ Generate a galaxy of random bodies """
    m = np.array((np.arange(0.0, 1.0, step=1.0 / N) + np.float64(10)) *
                 np.float64(m_sol / 10))
    x = np.array((np.arange(0.0, 1.0, step=1.0 / N) - np.float64(0.5)) *
                 np.float64(r_ly / 100))
    y = np.array((np.arange(0.0, 1.0, step=1.0 / N) - np.float64(0.5)) *
                 np.float64(r_ly / 100))
    z = np.array((np.arange(0.0, 1.0, step=1.0 / N) - np.float64(0.5)) *
                 np.float64(r_ly / 100))
    vx = np.zeros(N, dtype=np.float64)
    vy = np.zeros(N, dtype=np.float64)
    vz = np.zeros(N, dtype=np.float64)

    assert len(m) == N
    return weldarray(m), weldarray(x), weldarray(y), weldarray(z), weldarray(
        vx), weldarray(vy), weldarray(vz)
Ejemplo n.º 13
0
def array(arr, *args, **kwargs):
    '''
    Wrapper around weldarray - first create np.array and then convert to
    weldarray.
    '''
    return weldarray(np.array(arr, *args, **kwargs))
Ejemplo n.º 14
0
def main():

    parser = argparse.ArgumentParser(
        description="give num_els of arrays used for nbody")
    parser.add_argument('-n',
                        "--num_els",
                        type=int,
                        required=True,
                        help="num_els of 1d arrays")
    parser.add_argument('-t',
                        "--num_times",
                        type=int,
                        required=True,
                        help="num iterations for loop")
    parser.add_argument('-p',
                        "--remove_pass",
                        type=str,
                        default="whatever_string",
                        help="will remove the pass containing this str")
    parser.add_argument('-numpy',
                        "--use_numpy",
                        type=int,
                        required=False,
                        default=0,
                        help="use numpy or not in this run")
    parser.add_argument('-weld',
                        "--use_weld",
                        type=int,
                        required=False,
                        default=0,
                        help="use weld or not in this run")

    args = parser.parse_args()
    print_args(args)

    if args.use_numpy:
        galaxy = random_galaxy(args.num_els)
        # First run numpy
        start = time.time()
        ret1 = simulate(galaxy, args.num_times)
        R = galaxy['x'] + galaxy['y'] + galaxy['z']
        # assert R.dtype == np.float32, 'should be float64'
        end = time.time()
        print('****************************')
        print('numpy took {} seconds'.format(end - start))
        print('****************************')
    else:
        print('Not running numpy')

    if args.use_weld:
        # Part 2: Weld.
        galaxy2 = random_galaxy(args.num_els)
        for k, v in galaxy2.iteritems():
            galaxy2[k] = weldarray(v)

        start = time.time()
        ret2 = simulate(galaxy2, args.num_times)

        R2 = galaxy2['x'] + galaxy2['y'] + galaxy2['z']
        if isinstance(R2, weldarray):
            R2 = R2.evaluate()

        end = time.time()
        print('****************************')
        print('weld took {} seconds'.format(end - start))
        print('****************************')
    else:
        print('Not running weld')

    if args.use_numpy and args.use_weld:
        compare(ret1, ret2)
        compare(R, R2)