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()
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 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())