Exemplo n.º 1
0
def demo_histogram(d_data):

    # sort data to bring equal elements together
    trtc.Sort(d_data)

    # caculate 20 bins from 0~200
    # 1 extra to exclude possible negative values
    d_cumulative_histogram = trtc.device_vector("int32_t", 21)

    d_counter = trtc.DVCounter(trtc.DVFloat(0.0), 21)
    d_range_ends = trtc.DVTransform(
        d_counter, "float", trtc.Functor({}, ['x'],
                                         '        return x*10.0;\n'))

    trtc.Upper_Bound_V(d_data, d_range_ends, d_cumulative_histogram)

    d_histogram = trtc.device_vector("int32_t", 21)
    trtc.Adjacent_Difference(d_cumulative_histogram, d_histogram)

    h_histogram = d_histogram.to_host(1, 21)

    # plot the histogram
    x_axis = [str(x) for x in np.arange(5, 200, 10)]
    positions = np.arange(len(x_axis))
    plt.bar(positions, h_histogram, align='center', alpha=0.5)
    plt.xticks(positions, x_axis)
    plt.ylabel('Count')
    plt.title('Histogram')

    plt.show()
Exemplo n.º 2
0
d_float_in = trtc.device_vector_from_list([0.0, 10.0, 20.0, 30.0, 40.0],
                                          'float')

d_int_out = trtc.device_vector('int32_t', 5)
d_float_out = trtc.device_vector('float', 5)

zipped_in = trtc.DVZipped([d_int_in, d_float_in], ['a', 'b'])
zipped_out = trtc.DVZipped([d_int_out, d_float_out], ['a', 'b'])

trtc.Copy(zipped_in, zipped_out)
print(d_int_out.to_host())
print(d_float_out.to_host())

d_int_in = trtc.DVCounter(trtc.DVInt32(0), 5)
d_float_in = trtc.DVTransform(
    d_int_in, "float",
    trtc.Functor({}, ['i'], '        return (float)i*10.0f +10.0f;\n'))
zipped_in = trtc.DVZipped([d_int_in, d_float_in], ['a', 'b'])
trtc.Copy(zipped_in, zipped_out)
print(d_int_out.to_host())
print(d_float_out.to_host())

const_in = trtc.DVConstant(
    trtc.DVTuple({
        'a': trtc.DVInt32(123),
        'b': trtc.DVFloat(456.0)
    }), 5)
trtc.Copy(const_in, zipped_out)
print(d_int_out.to_host())
print(d_float_out.to_host())
Exemplo n.º 3
0
import ThrustRTC as trtc

darr1 = trtc.device_vector_from_list([1.0, 2.0, 5.0], 'float')
darr2 = trtc.device_vector_from_list([4.0, 1.0, 5.0], 'float')
print(trtc.Inner_Product(darr1, darr2, trtc.DVFloat(0.0)))
print(
    trtc.Inner_Product(darr1, darr2, trtc.DVFloat(0.0), trtc.Plus(),
                       trtc.Multiplies()))
Exemplo n.º 4
0
harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32')
darr = trtc.device_vector_from_numpy(harr)
print(darr.to_host())

# C data type
print(darr.name_view_cls())

harr2 = np.array([6,7,8,9,10], dtype='int32')
darr2 = trtc.device_vector_from_numpy(harr2)

# kernel with auto parameters, launched twice with different types
kernel = trtc.Kernel(['arr_in', 'arr_out', 'k'],
	'''
	size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
	if (idx >= arr_in.size()) return;
	arr_out[idx] = arr_in[idx]*k;
	''')

darr_out = trtc.device_vector('float', 5)
kernel.launch(1,128, [darr, darr_out, trtc.DVFloat(10.0)])
print (darr_out.to_host())

darr_out = trtc.device_vector('int32_t', 5)
kernel.launch(1,128, [darr2, darr_out, trtc.DVInt32(5)])
print (darr_out.to_host())

# create a vector from python list with C type specified
darr3 = trtc.device_vector_from_list([3.0, 5.0, 7.0, 9.0 , 11.0], 'float')
print(darr3.to_host())