Beispiel #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()
Beispiel #2
0
def demo_k_means(d_x, d_y, k):

    n = d_x.size()

    # create a zipped vector for convenience
    d_points = trtc.DVZipped([d_x, d_y], ['x','y'])

    # operations
    point_plus = trtc.Functor({ }, ['pos1', "pos2"],
'''
        return decltype(pos1)({pos1.x + pos2.x, pos1.y + pos2.y});
''')

    point_div = trtc.Functor({ }, ['pos', "count"],
'''
        return decltype(pos)({pos.x/(float)count, pos.y/(float)count});
''')
    
    # initialize centers
    center_ids = [0] * k
    d_min_dis = trtc.device_vector("float", n)

    for i in range(1, k):
        d_count = trtc.DVInt32(i)
        d_center_ids =  trtc.device_vector_from_list(center_ids[0:i], 'int32_t')
        calc_min_dis = trtc.Functor({"points": d_points, "center_ids": d_center_ids, "count": d_count }, ['pos'], 
'''
        float minDis = FLT_MAX;
        for (int i=0; i<count; i++)
        {
            int j = center_ids[i];
            float dis = (pos.x - points[j].x)*(pos.x - points[j].x);
            dis+= (pos.y - points[j].y)*(pos.y - points[j].y);
            if (dis<minDis) minDis = dis;
        }
        return minDis;
''')
        trtc.Transform(d_points, d_min_dis, calc_min_dis)
        center_ids[i] = trtc.Max_Element(d_min_dis)

    d_count = trtc.DVInt32(k)
    d_center_ids =  trtc.device_vector_from_list(center_ids, 'int32_t')

    # initialize group-average values
    d_group_aves_x =  trtc.device_vector("float", k)
    d_group_aves_y =  trtc.device_vector("float", k)
    d_group_aves = trtc.DVZipped([d_group_aves_x, d_group_aves_y], ['x','y'])

    trtc.Gather(d_center_ids, d_points, d_group_aves)

    # initialize labels
    d_labels =  trtc.device_vector("int32_t", n)
    trtc.Fill(d_labels, trtc.DVInt32(-1))

    # buffer for new-calculated lables
    d_labels_new =  trtc.device_vector("int32_t", n)

    d_labels_sink = trtc.DVDiscard("int32_t", k)
    d_group_sums = trtc.device_vector(d_points.name_elem_cls(), k)
    d_group_cumulate_counts = trtc.device_vector("int32_t", k)
    d_group_counts = trtc.device_vector("int32_t", k)

    d_counter = trtc.DVCounter(trtc.DVInt32(0), k)

    # iterations
    while True:
        # calculate new labels
        calc_new_labels = trtc.Functor({"aves": d_group_aves, "count": d_count }, ['pos'], 
'''
        float minDis = FLT_MAX;
        int label = -1;
        for (int i=0; i<count; i++)
        {
            float dis = (pos.x - aves[i].x)*(pos.x - aves[i].x);
            dis+= (pos.y - aves[i].y)*(pos.y - aves[i].y);
            if (dis<minDis) 
            {
                minDis = dis;
                label = i;
            }
        }
        return label;
''')
        trtc.Transform(d_points, d_labels_new, calc_new_labels)
        if trtc.Equal(d_labels, d_labels_new):
            break
        trtc.Copy(d_labels_new, d_labels)

        # recalculate group-average values
        trtc.Sort_By_Key(d_labels, d_points)
        trtc.Reduce_By_Key(d_labels, d_points, d_labels_sink, d_group_sums, trtc.EqualTo(), point_plus)
        trtc.Upper_Bound_V(d_labels, d_counter, d_group_cumulate_counts)
        trtc.Adjacent_Difference(d_group_cumulate_counts, d_group_counts)
        trtc.Transform_Binary(d_group_sums, d_group_counts, d_group_aves, point_div)

    h_x = d_x.to_host()
    h_y = d_y.to_host()
    h_labels = d_labels.to_host()
    h_group_aves_x = d_group_aves_x.to_host()
    h_group_aves_y = d_group_aves_y.to_host()
    h_group_counts = d_group_counts.to_host()

    lines = []

    for i in range(n):
        label = h_labels[i]
        lines.append([(h_x[i], h_y[i]), (h_group_aves_x[label], h_group_aves_y[label]) ] )

    lc = mc.LineCollection(lines)

    fig, ax = plt.subplots()
    ax.set_xlim((0, 1000))
    ax.set_ylim((0, 1000))

    ax.add_collection(lc)

    plt.show()
Beispiel #3
0
d_int_in = trtc.device_vector_from_list([0, 1, 2, 3, 4], 'int32_t')
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())
Beispiel #4
0
import ThrustRTC as trtc



negate = trtc.Functor( {}, ['x'],
'''
         return -x;
''')


darr = trtc.device_vector('int32_t', 10)
trtc.Transform(trtc.DVCounter(trtc.DVInt32(5), 10), darr, trtc.Negate())
print (darr.to_host())
Beispiel #5
0
import ThrustRTC as trtc

trtc.Set_Verbose()

trtc.Transform(trtc.DVCounter(trtc.DVInt32(5), 10), trtc.DVDiscard("int32_t"),
               trtc.Negate())
Beispiel #6
0
import ThrustRTC as trtc



op = trtc.Functor( {}, ['x'], 
'''
         return x % 100;
''')


darr = trtc.device_vector('int32_t', 2000)
trtc.Transform(trtc.DVCounter(trtc.DVInt32(0), 2000), darr, op)
print(trtc.Count(darr, trtc.DVInt32(47)))


op2 = trtc.Functor({}, ['x'],
'''
         return (x % 100)==47;
''')

trtc.Sequence(darr)
print(trtc.Count_If(darr, op2))