コード例 #1
0
    def moments(moment_0, moments, n, attr, cell_id, idx, length, specs_idx,
                specs_rank, min_x, max_x, x_id):
        # TODO print("Numba import!: ThrustRTC.moments(...)")

        from PySDM.backends.numba.numba import Numba
        from PySDM.backends.thrustRTC._storage_methods import StorageMethods
        host_moment_0 = StorageMethods.to_ndarray(moment_0)
        host_moments = StorageMethods.to_ndarray(moments)
        host_n = StorageMethods.to_ndarray(n)
        host_attr = StorageMethods.to_ndarray(attr)
        host_cell_id = StorageMethods.to_ndarray(cell_id)
        host_idx = StorageMethods.to_ndarray(idx)
        host_specs_idx = StorageMethods.to_ndarray(specs_idx)
        host_specs_rank = StorageMethods.to_ndarray(specs_rank)
        Numba.moments(host_moment_0, host_moments, host_n, host_attr,
                      host_cell_id, host_idx, length, host_specs_idx,
                      host_specs_rank, min_x, max_x, x_id)
        device_moment_0 = StorageMethods.from_ndarray(host_moment_0)
        device_moments = StorageMethods.from_ndarray(host_moments)
        trtc.Copy(device_moment_0, moment_0)
        trtc.Copy(device_moments, moments)
コード例 #2
0
 def __setitem__(self, key, value):
     if hasattr(value, 'data'):
         trtc.Copy(value.data, self.data)
     else:
         if isinstance(value, int):
             dvalue = trtc.DVInt64(value)
         elif isinstance(value, float):
             dvalue = trtc.DVDouble(value)
         else:
             raise TypeError("Only Storage, int and float are supported.")
         trtc.Fill(self.data, dvalue)
     return self
コード例 #3
0
ファイル: _storage_methods.py プロジェクト: NinaCrnivec/PySDM
    def to_ndarray(data):
        if isinstance(data, StorageMethods.storage):
            pass
        elif isinstance(data, trtc.DVVector.DVRange):
            data_copy = StorageMethods.array(data.shape, float)
            trtc.Copy(data, data_copy)
            data = data_copy
        else:
            raise NotImplementedError()

        result = data.to_host()
        result = np.reshape(result, data.shape)
        return result
コード例 #4
0
    def detach(self):
        if isinstance(self.data, trtc.DVVector.DVRange):
            if self.dtype is Storage.FLOAT:
                elem_cls = 'double'
            elif self.dtype in Storage.INT:
                elem_cls = 'int64_t'
            else:
                raise NotImplementedError()

            data = trtc.device_vector(elem_cls, self.data.size())

            trtc.Copy(self.data, data)
            self.data = data
コード例 #5
0
ファイル: thrustRTC.py プロジェクト: huangynj/PySDM
        def to_ndarray(data):
            # TODO: move to __equip??
            if isinstance(data, ThrustRTC.storage):
                pass
            elif isinstance(data, trtc.DVVector.DVRange):
                data_copy = ThrustRTC.array(data.shape, float)
                trtc.Copy(data, data_copy)
                data = data_copy
            else:
                raise NotImplementedError()

            result = data.to_host()
            result = np.reshape(result, data.shape)
            return result
コード例 #6
0
ファイル: thrustRTC.py プロジェクト: huangynj/PySDM
        def write_row(array, i, row):
            row_length = array.shape[1]
            start = row_length * i
            stop = start + row_length

            trtc.Copy(row, array.range(start, stop))
コード例 #7
0
ファイル: thrustRTC.py プロジェクト: huangynj/PySDM
 def shuffle(data, length, axis):
     from PySDM.backends.numba.numba import Numba
     host_arr = ThrustRTC.to_ndarray(data)
     Numba.shuffle(host_arr, length, axis=axis)
     dvce_arr = ThrustRTC.from_ndarray(host_arr)
     trtc.Copy(dvce_arr, data)
コード例 #8
0
ファイル: k-means.py プロジェクト: sumit-byte/ThrustRTC
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()
コード例 #9
0
import ThrustRTC as trtc

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)
コード例 #10
0
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))
コード例 #11
0
ファイル: _storage_methods.py プロジェクト: cycle13/PySDM
 def shuffle(data, length, axis):
     from PySDM.backends.numba.numba import Numba
     host_arr = StorageMethods.to_ndarray(data)
     Numba.shuffle_global(host_arr, length)
     dvce_arr = StorageMethods.from_ndarray(host_arr)
     trtc.Copy(dvce_arr, data)
コード例 #12
0
ファイル: _storage_methods.py プロジェクト: cycle13/PySDM
 def upload(np_data, backend_target):
     tmp = trtc.device_vector_from_numpy(np_data)
     trtc.Copy(tmp, backend_target)
コード例 #13
0
 def write_row(self, i, row):
     start = self.shape[1] * i
     stop = start + self.shape[1]
     trtc.Copy(row.data, self.data.range(start, stop))
コード例 #14
0
 def upload(self, data):
     trtc.Copy(trtc.device_vector_from_numpy(data.flatten()), self.data)
コード例 #15
0
 def ravel(self, other):
     if isinstance(other, Storage):
         trtc.Copy(other.data, self.data)
     else:
         self.data = trtc.device_vector_from_numpy(other.ravel())