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()
import ThrustRTC as trtc darr = trtc.device_vector_from_list([ -1, 0, -2, -2, 1, -3], 'int32_t') absolute_value = trtc.Functor( {}, ['x'], ''' return x<(decltype(x))0 ? -x : x; ''') print(trtc.Transform_Reduce(darr, absolute_value, trtc.DVInt32(0), trtc.Maximum()))
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()
import ThrustRTC as trtc is_less_than_zero = trtc.Functor({}, ['x'], ''' return x<0; ''') darr1 = trtc.device_vector_from_list([1, 2, 3, 1, 2], 'int32_t') trtc.Replace(darr1, trtc.DVInt32(1), trtc.DVInt32(99)) print(darr1.to_host()) darr2 = trtc.device_vector_from_list([1, -2, 3, -4, 5], 'int32_t') trtc.Replace_If(darr2, is_less_than_zero, trtc.DVInt32(0)) print(darr2.to_host()) darr3_in = trtc.device_vector_from_list([1, 2, 3, 1, 2], 'int32_t') darr3_out = trtc.device_vector('int32_t', 5) trtc.Replace_Copy(darr3_in, darr3_out, trtc.DVInt32(1), trtc.DVInt32(99)) print(darr3_out.to_host()) darr4_in = trtc.device_vector_from_list([1, -2, 3, -4, 5], 'int32_t') darr4_out = trtc.device_vector('int32_t', 5) trtc.Replace_Copy_If(darr4_in, darr4_out, is_less_than_zero, trtc.DVInt32(0)) print(darr4_out.to_host())
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())
import ThrustRTC as trtc dIn = trtc.device_vector_from_list([10, 20, 30, 40, 50, 60, 70, 80], 'int32_t') dOut = trtc.device_vector('int32_t', 8) trtc.Copy(dIn, dOut) print(dOut.to_host()) is_even = trtc.Functor({}, ['x'], ''' return x % 2 == 0; ''') dIn = trtc.device_vector_from_list([-2, 0, -1, 0, 1, 2], 'int32_t') dOut = trtc.device_vector('int32_t', 6) count = trtc.Copy_If(dIn, dOut, is_even) print(dOut.to_host(0, count)) dIn = trtc.device_vector_from_list([0, 1, 2, 3, 4, 5], 'int32_t') dStencil = trtc.device_vector_from_list([-2, 0, -1, 0, 1, 2], 'int32_t') dOut = trtc.device_vector('int32_t', 6) count = trtc.Copy_If_Stencil(dIn, dStencil, dOut, is_even) print(dOut.to_host(0, count))
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())
import ThrustRTC as trtc is_even = trtc.Functor({}, ['x'], ''' return ((x % 2) == 0); ''') dvalues = trtc.device_vector_from_list([1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') dmap = trtc.device_vector_from_list([0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') doutput = trtc.device_vector('int32_t', 10) trtc.Gather(dmap, dvalues, doutput) print(doutput.to_host()) dvalues = trtc.device_vector_from_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'int32_t') dstencil = trtc.device_vector_from_list([1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') dmap = trtc.device_vector_from_list([0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') doutput = trtc.device_vector_from_list([7, 7, 7, 7, 7, 7, 7, 7, 7, 7], 'int32_t') trtc.Gather_If(dmap, dstencil, dvalues, doutput) print(doutput.to_host()) dvalues = trtc.device_vector_from_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'int32_t') dstencil = trtc.device_vector_from_list([0, 3, 4, 1, 4, 1, 2, 7, 8, 9], 'int32_t') dmap = trtc.device_vector_from_list([0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') doutput = trtc.device_vector_from_list([7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
import ThrustRTC as trtc is_odd = trtc.Functor({}, ['x'], ''' return x % 2; ''') darr = trtc.device_vector_from_list([-5, 0, 2, -3, 2, 4, 0, -1, 2, 8], 'int32_t') trtc.Transform(darr, darr, trtc.Negate()) print(darr.to_host()) darr_in1 = trtc.device_vector_from_list([-5, 0, 2, 3, 2, 4], 'int32_t') darr_in2 = trtc.device_vector_from_list([3, 6, -2, 1, 2, 3], 'int32_t') darr_out = trtc.device_vector('int32_t', 6) trtc.Transform_Binary(darr_in1, darr_in2, darr_out, trtc.Plus()) print(darr_out.to_host()) darr = trtc.device_vector_from_list([-5, 0, 2, -3, 2, 4, 0, -1, 2, 8], 'int32_t') trtc.Transform_If(darr, darr, trtc.Negate(), is_odd) print(darr.to_host()) darr_data = trtc.device_vector_from_list([-5, 0, 2, -3, 2, 4, 0, -1, 2, 8], 'int32_t') darr_stencil = trtc.device_vector_from_list([1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') trtc.Transform_If_Stencil(darr_data, darr_stencil, darr_data, trtc.Negate(), trtc.Identity()) print(darr_data.to_host()) darr_in1 = trtc.device_vector_from_list([-5, 0, 2, 3, 2, 4], 'int32_t')
import ThrustRTC as trtc square_root = trtc.Functor({}, ['x'], ''' return sqrtf(x); ''') dvalues = trtc.device_vector_from_list([1.0, 4.0, 9.0, 16.0], 'float') doutput = trtc.device_vector('float', 4) dtrans = trtc.DVTransform(dvalues, 'float', square_root) trtc.Transform(dtrans, doutput, trtc.Negate()) print(doutput.to_host())
import ThrustRTC as trtc compare_modulo_two = trtc.Functor({}, ['x', 'y'], ''' return (x % 2) == (y % 2); ''') darr1 = trtc.device_vector_from_list([3, 1, 4, 1, 5, 9, 3], 'int32_t') darr2 = trtc.device_vector_from_list([3, 1, 4, 2, 8, 5, 7], 'int32_t') darr3 = trtc.device_vector_from_list([3, 1, 4, 1, 5, 9, 3], 'int32_t') print(trtc.Equal(darr1, darr2)) print(trtc.Equal(darr1, darr3)) dx = trtc.device_vector_from_list([1, 2, 3, 4, 5, 6], 'int32_t') dy = trtc.device_vector_from_list([7, 8, 9, 10, 11, 12], 'int32_t') print(trtc.Equal(dx, dy, compare_modulo_two))
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))
import ThrustRTC as trtc d_values = trtc.device_vector_from_list([0, 5, 3, 7], 'int32_t') print(trtc.Find(d_values, trtc.DVInt32(3))) print(trtc.Find(d_values, trtc.DVInt32(5))) print(trtc.Find(d_values, trtc.DVInt32(9))) print(trtc.Find_If(d_values, trtc.Functor({}, ['x'], ' return x>4;\n'))) print(trtc.Find_If(d_values, trtc.Functor({}, ['x'], ' return x>10;\n'))) print( trtc.Find_If_Not(d_values, trtc.Functor({}, ['x'], ' return x>4;\n'))) print( trtc.Find_If_Not(d_values, trtc.Functor({}, ['x'], ' return x>10;\n')))
import ThrustRTC as trtc printf_functor = trtc.Functor({}, ['x'], ''' printf("%d\\n", x); ''') darr = trtc.device_vector_from_list([1, 2, 3, 1, 2], 'int32_t') trtc.For_Each(darr, printf_functor)
else: spheres.append( rtrtc.DVSphere( center, 0.2, { 'type': 'dielectric', 'color': (1.0, 1.0, 1.0), 'fuzz': 0.0, 'ref_idx': 1.5 })) mul_sph = rtrtc.DVMultiSpheres(spheres) print("Rendering the scene..") sky = trtc.Functor({}, ['dir'], ''' float t = 0.5f*dir.y + 1.0f; fvec3 ret = { 2.0f - 1.0f*t, 2.0f - 0.6f*t, 2.0f }; return ret; ''') raytracer = rtrtc.RayTracer(img) raytracer.set_camera((15.0, 3.0, 3.0), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), 20.0, 0.2, 12.0) raytracer.set_shutter(0.0, 1.0) raytracer.trace(mul_sph, sky) himg = img.to_host() himg.save("scene1.tga") print("Done.")