cumo 0.14.1 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +52 -0
- data/README.md +68 -1
- data/Rakefile +3 -1
- data/ext/cumo/cuda/cub.cu +26 -0
- data/ext/cumo/cumo.c +0 -2
- data/ext/cumo/extconf.rb +1 -2
- data/ext/cumo/include/cumo/intern.h +2 -0
- data/ext/cumo/include/cumo/narray.h +31 -7
- data/ext/cumo/{narray/sort_kernel.cu → include/cumo/sort_kernel.h} +138 -29
- data/ext/cumo/include/cumo/template.h +3 -2
- data/ext/cumo/include/cumo/template_kernel.h +2 -0
- data/ext/cumo/narray/array.c +0 -6
- data/ext/cumo/narray/data.c +7 -4
- data/ext/cumo/narray/gen/cogen_kernel.rb +21 -10
- data/ext/cumo/narray/gen/kernel_parts.rb +5 -3
- data/ext/cumo/narray/gen/spec.rb +10 -0
- data/ext/cumo/narray/gen/tmpl/accum_arg_kernel.cu +2 -2
- data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +1 -1
- data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +2 -2
- data/ext/cumo/narray/gen/tmpl/add_at.c +179 -0
- data/ext/cumo/narray/gen/tmpl/add_at_kernel.cu +122 -0
- data/ext/cumo/narray/gen/tmpl/allocate.c +1 -0
- data/ext/cumo/narray/gen/tmpl/bincount_kernel.cu +117 -2
- data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +17 -3
- data/ext/cumo/narray/gen/tmpl/cum_kernel.cu +2 -0
- data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +15 -3
- data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +19 -1
- data/ext/cumo/narray/gen/tmpl/searchsorted.c +89 -0
- data/ext/cumo/narray/gen/tmpl/searchsorted_kernel.cu +43 -0
- data/ext/cumo/narray/gen/tmpl/sort_kernel.cu +19 -0
- data/ext/cumo/narray/gen/tmpl/store_array.c +0 -12
- data/ext/cumo/narray/gen/tmpl_bit/allocate.c +1 -0
- data/ext/cumo/narray/gen/tmpl_bit/mask.c +10 -6
- data/ext/cumo/narray/gen/tmpl_bit/store_array.c +0 -4
- data/ext/cumo/narray/gen/tmpl_bit/where.c +36 -10
- data/ext/cumo/narray/gen/tmpl_bit/where2.c +8 -9
- data/ext/cumo/narray/index.c +227 -123
- data/ext/cumo/narray/index_kernel.cu +43 -11
- data/ext/cumo/narray/narray.c +16 -16
- data/ext/cumo/narray/ndloop.c +4 -0
- data/ext/cumo/narray/step.c +54 -187
- data/lib/cumo/cuda/elementwise_kernel.rb +13 -5
- data/lib/cumo/cuda/user_kernel.rb +11 -6
- data/lib/cumo/narray/extra.rb +108 -0
- data/lib/cumo/version.rb +1 -1
- metadata +10 -4
data/ext/cumo/narray/data.c
CHANGED
|
@@ -158,6 +158,9 @@ cumo_na_swap_byte(VALUE self)
|
|
|
158
158
|
CUMO_STRIDE_LOOP|CUMO_NDF_INDEXER_LOOP|CUMO_NDF_ACCEPT_BYTESWAP,
|
|
159
159
|
1, 1, ain, aout };
|
|
160
160
|
|
|
161
|
+
if (rb_obj_is_kind_of(self, cumo_cRObject)) {
|
|
162
|
+
rb_raise(rb_eTypeError, "cannot swap bytes of %s", rb_obj_classname(self));
|
|
163
|
+
}
|
|
161
164
|
// Cumo::Bit's element is one bit, so swapping the bytes of an element
|
|
162
165
|
// changes nothing, and a loop stepping by the element stride would walk
|
|
163
166
|
// one byte per bit, eight times past the end of the packed buffer. Only
|
|
@@ -633,7 +636,7 @@ cumo_na_flatten_dim(VALUE self, int sd)
|
|
|
633
636
|
cumo_na_indexer_t indexer;
|
|
634
637
|
|
|
635
638
|
idx2 = (size_t*)cumo_cuda_runtime_malloc(sizeof(size_t)*((shape[sd]==0) ? 1 : shape[sd]));
|
|
636
|
-
cumo_na_index_own(na2,sd,idx2);
|
|
639
|
+
cumo_na_index_own(na2,sd,idx2,(shape[sd]==0) ? 1 : shape[sd]);
|
|
637
640
|
fd = nd-sd;
|
|
638
641
|
iarray.ptr = NULL;
|
|
639
642
|
indexer.ndim = fd;
|
|
@@ -851,7 +854,7 @@ cumo_na_diagonal(int argc, VALUE *argv, VALUE self)
|
|
|
851
854
|
} else {
|
|
852
855
|
idx0 = CUMO_SDX_GET_INDEX(na1->stridx[i]);
|
|
853
856
|
idx1 = (size_t*)cumo_cuda_runtime_malloc(sizeof(size_t)*na->shape[i]);
|
|
854
|
-
cumo_na_index_own(na2,k,idx1);
|
|
857
|
+
cumo_na_index_own(na2,k,idx1,na->shape[i]);
|
|
855
858
|
cumo_cuda_runtime_check_status(cudaMemcpyAsync(idx1,idx0,sizeof(size_t)*na->shape[i],cudaMemcpyDeviceToDevice,cumo_cuda_stream()));
|
|
856
859
|
cumo_cuda_runtime_note_device_write();
|
|
857
860
|
}
|
|
@@ -862,7 +865,7 @@ cumo_na_diagonal(int argc, VALUE *argv, VALUE self)
|
|
|
862
865
|
idx0 = CUMO_SDX_GET_INDEX(na1->stridx[ax[0]]);
|
|
863
866
|
// diag_idx = ALLOC_N(size_t, diag_size);
|
|
864
867
|
diag_idx = (size_t*)cumo_cuda_runtime_malloc(sizeof(size_t)*diag_size);
|
|
865
|
-
cumo_na_index_own(na2,nd-2,diag_idx);
|
|
868
|
+
cumo_na_index_own(na2,nd-2,diag_idx,diag_size);
|
|
866
869
|
if (CUMO_SDX_IS_INDEX(na1->stridx[ax[1]])) {
|
|
867
870
|
idx1 = CUMO_SDX_GET_INDEX(na1->stridx[ax[1]]);
|
|
868
871
|
cumo_na_diagonal_index_index_kernel_launch(diag_idx, idx0, idx1, k0, k1, diag_size);
|
|
@@ -876,7 +879,7 @@ cumo_na_diagonal(int argc, VALUE *argv, VALUE self)
|
|
|
876
879
|
idx1 = CUMO_SDX_GET_INDEX(na1->stridx[ax[1]]);
|
|
877
880
|
// diag_idx = ALLOC_N(size_t, diag_size);
|
|
878
881
|
diag_idx = (size_t*)cumo_cuda_runtime_malloc(sizeof(size_t)*diag_size);
|
|
879
|
-
cumo_na_index_own(na2,nd-2,diag_idx);
|
|
882
|
+
cumo_na_index_own(na2,nd-2,diag_idx,diag_size);
|
|
880
883
|
cumo_na_diagonal_stride_index_kernel_launch(diag_idx, stride0, idx1, k0, k1, diag_size);
|
|
881
884
|
} else {
|
|
882
885
|
stride1 = CUMO_SDX_GET_STRIDE(na1->stridx[ax[1]]);
|
|
@@ -41,14 +41,14 @@ erb_dir.unshift("tmpl_bit") if (type_name == "bit")
|
|
|
41
41
|
erb_dir.map! { |d| File.join(thisdir, d) }
|
|
42
42
|
|
|
43
43
|
TEMPLATE_KERNEL_PARTS = {
|
|
44
|
-
"accum" => %w[reduce reduce_nan],
|
|
45
|
-
"accum_index" => %w[reduce reduce_nan],
|
|
46
|
-
"accum_arg" => %w[reduce reduce_nan],
|
|
47
|
-
"accum_binary" => %w[reduce reduce_nan reduce_from],
|
|
48
|
-
"cum" => %w[
|
|
49
|
-
"layer_norm" => %w[
|
|
50
|
-
"rms_norm" => %w[
|
|
51
|
-
"softmax" => %w[
|
|
44
|
+
"accum" => %w[reduce reduce_rare reduce_nan],
|
|
45
|
+
"accum_index" => %w[reduce reduce_rare reduce_nan],
|
|
46
|
+
"accum_arg" => %w[reduce reduce_rare reduce_nan],
|
|
47
|
+
"accum_binary" => %w[reduce reduce_rare reduce_nan reduce_from],
|
|
48
|
+
"cum" => %w[reduce_rare],
|
|
49
|
+
"layer_norm" => %w[reduce_rare],
|
|
50
|
+
"rms_norm" => %w[reduce_rare],
|
|
51
|
+
"softmax" => %w[reduce_rare],
|
|
52
52
|
"bit_count" => %w[reduce],
|
|
53
53
|
"bit_reduce" => %w[reduce],
|
|
54
54
|
"bit_stat" => %w[reduce],
|
|
@@ -58,11 +58,17 @@ TEMPLATE_KERNEL_PARTS = {
|
|
|
58
58
|
"rand_norm" => %w[misc],
|
|
59
59
|
"poly" => %w[misc],
|
|
60
60
|
"quantize_symmetric" => %w[misc],
|
|
61
|
+
"searchsorted" => %w[misc],
|
|
62
|
+
"add_at" => %w[misc],
|
|
61
63
|
"unary_ret2" => %w[misc],
|
|
64
|
+
"sort" => %w[sort],
|
|
62
65
|
}.freeze
|
|
63
66
|
|
|
64
67
|
MATH_TEMPLATES = %w[unary_s binary_s ternary_s frexp].freeze
|
|
65
68
|
COMMON_MATH = %w[sqrt log log2 log10 exp exp2 sin cos tan tanh erf gelu gelu_tanh silu sigmoid softplus].freeze
|
|
69
|
+
# mulsum is here for dot of two vectors.
|
|
70
|
+
COMMON_REDUCE = %w[sum mean max min mulsum].freeze
|
|
71
|
+
RARE_REDUCE = %w[prod ptp minmax var stddev rms abs_sum abs_max abs_min kahan_sum min_index max_index argmin argmax].freeze
|
|
66
72
|
|
|
67
73
|
abort "unknown kernel part: #{$kernel_part}" unless KERNEL_PARTS.include?($kernel_part)
|
|
68
74
|
undeclared_math = COMMON_MATH - File.read(File.join(thisdir, "spec.rb")).scan(/^\s*math "(\w+)"/).flatten
|
|
@@ -83,8 +89,13 @@ class ErbPP
|
|
|
83
89
|
@children.select { |c| c.kernel_parts.include?($kernel_part) }
|
|
84
90
|
end
|
|
85
91
|
|
|
86
|
-
def kernel_part?(nan: false, from: nil)
|
|
87
|
-
$kernel_part ==
|
|
92
|
+
def kernel_part?(op = nil, nan: false, from: nil)
|
|
93
|
+
return $kernel_part == "reduce_from" if from
|
|
94
|
+
return $kernel_part == "reduce_nan" if nan
|
|
95
|
+
return $kernel_part == "reduce" if COMMON_REDUCE.include?(op)
|
|
96
|
+
return $kernel_part == "reduce_rare" if RARE_REDUCE.include?(op)
|
|
97
|
+
|
|
98
|
+
raise ArgumentError, "#{op.inspect} is in neither COMMON_REDUCE nor RARE_REDUCE"
|
|
88
99
|
end
|
|
89
100
|
|
|
90
101
|
def indexer_dims(narrow = false)
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
KERNEL_PARTS = %w[main reduce reduce_nan reduce_from math math_rare compare cast misc].freeze
|
|
3
|
+
KERNEL_PARTS = %w[main reduce reduce_nan reduce_from reduce_rare math math_rare compare cast misc sort].freeze
|
|
4
4
|
MATH_TYPES = %w[hfloat bfloat sfloat dfloat scomplex dcomplex].freeze
|
|
5
|
+
SORT_TYPES = %w[int8 int16 int32 int64 uint8 uint16 uint32 uint64 hfloat bfloat sfloat dfloat].freeze
|
|
5
6
|
|
|
6
7
|
def kernel_parts_of(type_name)
|
|
7
|
-
return
|
|
8
|
+
return %w[main reduce reduce_nan reduce_from] if type_name == "robject"
|
|
8
9
|
|
|
9
10
|
KERNEL_PARTS.reject do |part|
|
|
10
11
|
(%w[math math_rare].include?(part) && !MATH_TYPES.include?(type_name)) ||
|
|
11
|
-
(%w[compare misc].include?(part) && type_name == "bit")
|
|
12
|
+
(%w[compare misc reduce_rare].include?(part) && type_name == "bit") ||
|
|
13
|
+
(part == "sort" && !SORT_TYPES.include?(type_name))
|
|
12
14
|
end
|
|
13
15
|
end
|
|
14
16
|
|
data/ext/cumo/narray/gen/spec.rb
CHANGED
|
@@ -100,8 +100,12 @@ end
|
|
|
100
100
|
|
|
101
101
|
if is_object
|
|
102
102
|
undef_singleton_method "from_binary"
|
|
103
|
+
undef_singleton_method "from_string"
|
|
104
|
+
undef_method "store_binary"
|
|
103
105
|
undef_method "to_binary"
|
|
106
|
+
undef_method "to_string"
|
|
104
107
|
undef_method "swap_byte"
|
|
108
|
+
undef_method "hton"
|
|
105
109
|
undef_method "to_network"
|
|
106
110
|
undef_method "to_vacs"
|
|
107
111
|
undef_method "to_host"
|
|
@@ -422,6 +426,12 @@ if is_comparable && !is_object
|
|
|
422
426
|
def_method "sort"
|
|
423
427
|
def_method "sort_index"
|
|
424
428
|
def_method "median"
|
|
429
|
+
# Cumo::NArray#searchsorted casts its operands before it calls this
|
|
430
|
+
def_method "insertion_points", "searchsorted", private: true
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
unless is_object
|
|
434
|
+
def_method "add_at"
|
|
425
435
|
end
|
|
426
436
|
|
|
427
437
|
# Math
|
|
@@ -119,7 +119,7 @@ extern "C" {
|
|
|
119
119
|
#endif
|
|
120
120
|
#endif
|
|
121
121
|
|
|
122
|
-
<% if kernel_part? %>
|
|
122
|
+
<% if kernel_part?("argmin") %>
|
|
123
123
|
void cumo_<%=type_name%>_argmin_int<%=i%>_kernel_launch(cumo_na_reduction_arg_t* arg)
|
|
124
124
|
{
|
|
125
125
|
cumo_reduce_arg_split<dtype, idx_t, cumo_<%=type_name%>_argmin_int<%=i%>_impl>(*arg, cumo_<%=type_name%>_argmin_int<%=i%>_impl{});
|
|
@@ -133,7 +133,7 @@ void cumo_<%=type_name%>_argmin_nan_int<%=i%>_kernel_launch(cumo_na_reduction_ar
|
|
|
133
133
|
}
|
|
134
134
|
<% end %>
|
|
135
135
|
|
|
136
|
-
<% if kernel_part? %>
|
|
136
|
+
<% if kernel_part?("argmax") %>
|
|
137
137
|
void cumo_<%=type_name%>_argmax_int<%=i%>_kernel_launch(cumo_na_reduction_arg_t* arg)
|
|
138
138
|
{
|
|
139
139
|
cumo_reduce_arg_split<dtype, idx_t, cumo_<%=type_name%>_argmax_int<%=i%>_impl>(*arg, cumo_<%=type_name%>_argmax_int<%=i%>_impl{});
|
|
@@ -63,7 +63,7 @@ extern "C" {
|
|
|
63
63
|
//<% (is_float ? ["","_nan"] : [""]).each do |nan| %>
|
|
64
64
|
//<% ([nil] + from_types).each do |from| %>
|
|
65
65
|
//<% sfx = from ? "_from_#{from[:id]}" : "" %>
|
|
66
|
-
//<% if kernel_part?(nan: nan == "_nan", from: from) %>
|
|
66
|
+
//<% if kernel_part?(name, nan: nan == "_nan", from: from) %>
|
|
67
67
|
void <%="cumo_#{type_name}_#{name}#{nan}#{sfx}_kernel_launch"%>(cumo_na_reduction_arg_t* arg, cumo_na_iarray_t* in2)
|
|
68
68
|
{
|
|
69
69
|
cumo_reduce_zip_split<dtype, <%= from ? from[:ctype] : "dtype" %>, dtype, <%="cumo_#{type_name}_#{name}#{nan}#{sfx}_impl"%>>(*arg, *in2, <%="cumo_#{type_name}_#{name}#{nan}#{sfx}_impl"%>{});
|
|
@@ -119,7 +119,7 @@ extern "C" {
|
|
|
119
119
|
#endif
|
|
120
120
|
#endif
|
|
121
121
|
|
|
122
|
-
<% if kernel_part? %>
|
|
122
|
+
<% if kernel_part?("min_index") %>
|
|
123
123
|
void cumo_<%=type_name%>_min_index_int<%=i%>_kernel_launch(cumo_na_reduction_arg_t* arg)
|
|
124
124
|
{
|
|
125
125
|
cumo_reduce_split<dtype, idx_t, cumo_<%=type_name%>_min_index_int<%=i%>_impl>(*arg, cumo_<%=type_name%>_min_index_int<%=i%>_impl{});
|
|
@@ -133,7 +133,7 @@ void cumo_<%=type_name%>_min_index_nan_int<%=i%>_kernel_launch(cumo_na_reduction
|
|
|
133
133
|
}
|
|
134
134
|
<% end %>
|
|
135
135
|
|
|
136
|
-
<% if kernel_part? %>
|
|
136
|
+
<% if kernel_part?("max_index") %>
|
|
137
137
|
void cumo_<%=type_name%>_max_index_int<%=i%>_kernel_launch(cumo_na_reduction_arg_t* arg)
|
|
138
138
|
{
|
|
139
139
|
cumo_reduce_split<dtype, idx_t, cumo_<%=type_name%>_max_index_int<%=i%>_impl>(*arg, cumo_<%=type_name%>_max_index_int<%=i%>_impl{});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
void <%="cumo_#{c_iter}_kernel_launch"%>(cumo_na_iarray_stridx_t* a1, cumo_na_iarray_stridx_t* a2, cumo_na_indexer_t* indexer);
|
|
2
|
+
|
|
3
|
+
static void
|
|
4
|
+
<%=c_iter%>(cumo_na_loop_t *const lp)
|
|
5
|
+
{
|
|
6
|
+
cumo_na_indexer_t indexer = cumo_na_make_indexer(&lp->args[0]);
|
|
7
|
+
cumo_na_iarray_stridx_t a1 = cumo_na_make_iarray_stridx(&lp->args[0]);
|
|
8
|
+
cumo_na_iarray_stridx_t a2 = cumo_na_make_iarray_stridx(&lp->args[1]);
|
|
9
|
+
|
|
10
|
+
<%="cumo_#{c_iter}_kernel_launch"%>(&a1, &a2, &indexer);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static VALUE
|
|
14
|
+
<%=c_func%>_root(VALUE a)
|
|
15
|
+
{
|
|
16
|
+
cumo_narray_t *na;
|
|
17
|
+
|
|
18
|
+
CumoGetNArray(a, na);
|
|
19
|
+
return (CUMO_NA_TYPE(na) == CUMO_NARRAY_VIEW_T) ? CUMO_NA_VIEW_DATA(na) : a;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
void <%="cumo_#{c_iter}_flat_kernel_launch"%>(char *p, const char *idx, int idx_size, const char *v, uint64_t v_step, ssize_t size, uint64_t n);
|
|
23
|
+
|
|
24
|
+
// The offset pointer of a view through an index array names only its first
|
|
25
|
+
// element, so such a view is read through the view even with one element.
|
|
26
|
+
static int
|
|
27
|
+
<%=c_func%>_flat_values(VALUE values, size_t n, uint64_t *v_step)
|
|
28
|
+
{
|
|
29
|
+
cumo_narray_t *nv;
|
|
30
|
+
cumo_narray_view_t *vv;
|
|
31
|
+
|
|
32
|
+
CumoGetNArray(values, nv);
|
|
33
|
+
if (nv->ndim > 1 || (nv->size != 1 && nv->size != n)) {
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
if (nv->ndim == 1 && CUMO_NA_TYPE(nv) == CUMO_NARRAY_VIEW_T) {
|
|
37
|
+
vv = (cumo_narray_view_t*)nv;
|
|
38
|
+
if (!CUMO_SDX_IS_STRIDE(vv->stridx[0]) ||
|
|
39
|
+
(nv->size > 1 && CUMO_SDX_GET_STRIDE(vv->stridx[0]) != sizeof(dtype))) {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
*v_step = (nv->size == 1) ? 0 : 1;
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// An Int32 or Int64 index array into a data array is added from where its
|
|
48
|
+
// indices sit, after one pass that checks their range, rather than through a
|
|
49
|
+
// view that first writes them out as offsets. Returns 0 to leave it to the view.
|
|
50
|
+
static int
|
|
51
|
+
<%=c_func%>_flat(VALUE self, VALUE idx, VALUE values)
|
|
52
|
+
{
|
|
53
|
+
cumo_narray_t *na;
|
|
54
|
+
const char *ip, *v;
|
|
55
|
+
char *p;
|
|
56
|
+
uint64_t v_step;
|
|
57
|
+
size_t n;
|
|
58
|
+
int width;
|
|
59
|
+
|
|
60
|
+
CumoGetNArray(self, na);
|
|
61
|
+
if (CUMO_NA_TYPE(na) != CUMO_NARRAY_DATA_T || CUMO_TEST_COLUMN_MAJOR(self)) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
// The kernel reads the indices while it adds into self, so indices held in
|
|
65
|
+
// self's own memory go through the view, which writes them out first.
|
|
66
|
+
if (<%=c_func%>_root(idx) == <%=c_func%>_root(self)) {
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
width = cumo_na_index_in_place_width(idx);
|
|
70
|
+
n = CUMO_RNARRAY_SIZE(idx);
|
|
71
|
+
if (width == 0 || n == 0 || !<%=c_func%>_flat_values(values, n, &v_step)) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
// Taking memory can run Ruby, so all of it is taken before the indices are
|
|
75
|
+
// checked. An unallocated self is refused here, as the view refuses it.
|
|
76
|
+
ip = cumo_na_get_offset_pointer_for_read(idx);
|
|
77
|
+
v = cumo_na_get_offset_pointer_for_read(values);
|
|
78
|
+
p = cumo_na_get_pointer_for_read_write(self);
|
|
79
|
+
cumo_na_index_check_range_in_place(ip, width, n, (ssize_t)na->size);
|
|
80
|
+
<%="cumo_#{c_iter}_flat_kernel_launch"%>(p, ip, width, v, v_step, (ssize_t)na->size, n);
|
|
81
|
+
return 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// numpy refuses values that would have to grow the selection to fit, where
|
|
85
|
+
// ndloop would broadcast the selection instead and add each row of them.
|
|
86
|
+
static void
|
|
87
|
+
<%=c_func%>_check_shape(VALUE selected, VALUE values)
|
|
88
|
+
{
|
|
89
|
+
cumo_narray_t *nw, *nv;
|
|
90
|
+
int i;
|
|
91
|
+
|
|
92
|
+
CumoGetNArray(selected, nw);
|
|
93
|
+
CumoGetNArray(values, nv);
|
|
94
|
+
for (i = 1; i <= nv->ndim; ++i) {
|
|
95
|
+
if (i > nw->ndim ||
|
|
96
|
+
(nv->shape[nv->ndim - i] != 1 && nv->shape[nv->ndim - i] != nw->shape[nw->ndim - i])) {
|
|
97
|
+
rb_raise(cumo_na_eShapeError, "values do not broadcast to the shape of the places selected");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
Adds values at the places the subscripts select, in place, as numpy's
|
|
104
|
+
add.at does: a place selected more than once gets every value added to it,
|
|
105
|
+
where a[index] += values would add only one of them.
|
|
106
|
+
The subscripts are those of []=, so a single subscript of a
|
|
107
|
+
multi-dimensional array counts its elements in order, and a row is
|
|
108
|
+
a.add_at(i, true, values). An index array of more than one dimension
|
|
109
|
+
selects a place for each of its elements, and the values broadcast to its
|
|
110
|
+
shape. A 0-dimensional array takes no subscripts, as in numpy.
|
|
111
|
+
@overload add_at(*index, values)
|
|
112
|
+
@param [Numeric,Array,Range,Cumo::NArray] index subscripts, as for []=.
|
|
113
|
+
@param [Numeric,Array,Cumo::NArray] values what to add, cast to
|
|
114
|
+
Cumo::<%=class_name%> and broadcast to the places selected.
|
|
115
|
+
@return [Cumo::<%=class_name%>] self.
|
|
116
|
+
@example
|
|
117
|
+
a = Cumo::<%=class_name%>.zeros(5)
|
|
118
|
+
a.add_at(Cumo::Int32[1, 3, 1], 1)
|
|
119
|
+
# => Cumo::<%=class_name%>#shape=[5]
|
|
120
|
+
# [0, 2, 0, 1, 0]
|
|
121
|
+
*/
|
|
122
|
+
static VALUE
|
|
123
|
+
<%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
|
|
124
|
+
{
|
|
125
|
+
cumo_ndfunc_arg_in_t ain[2] = {{CUMO_OVERWRITE,0},{cT,0}};
|
|
126
|
+
cumo_ndfunc_t ndf = { <%=c_iter%>, CUMO_FULL_LOOP|CUMO_NDF_INDEXER_LOOP|CUMO_NDF_ANY_ORDER, 2, 0, ain, 0 };
|
|
127
|
+
VALUE view = self, values, sub, idx;
|
|
128
|
+
int nd;
|
|
129
|
+
size_t pos;
|
|
130
|
+
|
|
131
|
+
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
|
132
|
+
argc--;
|
|
133
|
+
values = argv[argc];
|
|
134
|
+
// As in []=, the view below has the root for its data object, so the frozen
|
|
135
|
+
// check of the pointer it takes never reaches self.
|
|
136
|
+
if (OBJ_FROZEN(self)) {
|
|
137
|
+
rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
|
|
138
|
+
}
|
|
139
|
+
// Refused as numpy's add.at does, though []= takes these subscripts as
|
|
140
|
+
// indexing the one element.
|
|
141
|
+
if (argc > 0 && CUMO_RNARRAY_NDIM(self) == 0) {
|
|
142
|
+
rb_raise(rb_eIndexError, "a 0-dimensional array takes no subscripts");
|
|
143
|
+
}
|
|
144
|
+
if (!rb_obj_is_kind_of(values, cT)) {
|
|
145
|
+
values = rb_funcall(cT, cumo_id_cast, 1, values);
|
|
146
|
+
} else if (<%=c_func%>_root(values) == <%=c_func%>_root(self)) {
|
|
147
|
+
// The kernel reads the values while it adds into self, so values held
|
|
148
|
+
// in self's own memory are read from a copy.
|
|
149
|
+
values = cumo_na_copy(values);
|
|
150
|
+
}
|
|
151
|
+
if (argc == 1) {
|
|
152
|
+
sub = argv[0];
|
|
153
|
+
idx = RB_TYPE_P(sub, T_ARRAY) ? rb_apply(cumo_cNArray, rb_intern("[]"), sub) : sub;
|
|
154
|
+
// An index array of more than one dimension selects into a new array
|
|
155
|
+
// rather than a view, so it goes in flattened, and the values with it.
|
|
156
|
+
if (CumoIsNArray(idx) && !rb_obj_is_kind_of(idx, cumo_cBit) && CUMO_RNARRAY_NDIM(idx) > 1) {
|
|
157
|
+
<%=c_func%>_check_shape(idx, values);
|
|
158
|
+
if (CUMO_RNARRAY_NDIM(values) > 0) {
|
|
159
|
+
VALUE spread = cumo_na_new(cT, CUMO_RNARRAY_NDIM(idx), CUMO_RNARRAY_SHAPE(idx));
|
|
160
|
+
<%=c_func.sub(/_add_at/, "_store")%>(spread, values);
|
|
161
|
+
values = cumo_na_flatten(spread);
|
|
162
|
+
}
|
|
163
|
+
sub = cumo_na_flatten(idx);
|
|
164
|
+
}
|
|
165
|
+
// A flat Ruby Array keeps the subscripts of []=, so only an NArray, or
|
|
166
|
+
// the one a nested Array became above, is read directly.
|
|
167
|
+
if (CumoIsNArray(sub) && <%=c_func%>_flat(self, sub, values)) {
|
|
168
|
+
return self;
|
|
169
|
+
}
|
|
170
|
+
nd = cumo_na_get_result_dimension(self, 1, &sub, sizeof(dtype), &pos);
|
|
171
|
+
view = cumo_na_aref_main(1, &sub, self, 0, nd, pos);
|
|
172
|
+
} else if (argc > 1) {
|
|
173
|
+
nd = cumo_na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
|
|
174
|
+
view = cumo_na_aref_main(argc, argv, self, 0, nd, pos);
|
|
175
|
+
}
|
|
176
|
+
<%=c_func%>_check_shape(view, values);
|
|
177
|
+
cumo_na_ndloop(&ndf, 2, view, values);
|
|
178
|
+
return self;
|
|
179
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
<% narrow = %w[int8 uint8 int16 uint16 hfloat bfloat].include?(type_name) %>
|
|
2
|
+
<% if %w[dfloat dcomplex].include?(type_name) %>
|
|
3
|
+
// atomicAdd on double arrived with sm_60, and the default target of the CUDA
|
|
4
|
+
// releases cumo builds against is still below it.
|
|
5
|
+
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 600)
|
|
6
|
+
__device__ static inline void cumo_<%=type_name%>_add_at_double(double *address, double val)
|
|
7
|
+
{
|
|
8
|
+
unsigned long long int *p = (unsigned long long int*)address;
|
|
9
|
+
unsigned long long int old = *p, assumed;
|
|
10
|
+
do {
|
|
11
|
+
assumed = old;
|
|
12
|
+
old = atomicCAS(p, assumed, __double_as_longlong(val + __longlong_as_double(assumed)));
|
|
13
|
+
} while (assumed != old);
|
|
14
|
+
}
|
|
15
|
+
#else
|
|
16
|
+
__device__ static inline void cumo_<%=type_name%>_add_at_double(double *address, double val) { atomicAdd(address, val); }
|
|
17
|
+
#endif
|
|
18
|
+
<% end %>
|
|
19
|
+
<% if narrow %>
|
|
20
|
+
<% bits = %w[int8 uint8].include?(type_name) ? "unsigned char" : "unsigned short" %>
|
|
21
|
+
// No atomic takes fewer than 32 bits on every target, so the element is added
|
|
22
|
+
// by swapping the aligned word around it with only its own bytes changed.
|
|
23
|
+
__device__ static inline void cumo_<%=type_name%>_add_at_narrow(dtype *p, dtype v)
|
|
24
|
+
{
|
|
25
|
+
unsigned int *word = (unsigned int*)((uintptr_t)p & ~(uintptr_t)3);
|
|
26
|
+
unsigned int shift = (unsigned int)((uintptr_t)p & 3) * 8;
|
|
27
|
+
unsigned int mask = (unsigned int)(<%=bits%>)~0u << shift;
|
|
28
|
+
unsigned int old = *word, assumed;
|
|
29
|
+
do {
|
|
30
|
+
<%=bits%> b = (<%=bits%>)((old & mask) >> shift);
|
|
31
|
+
dtype x;
|
|
32
|
+
assumed = old;
|
|
33
|
+
memcpy(&x, &b, sizeof(dtype));
|
|
34
|
+
x = m_add(x, v);
|
|
35
|
+
memcpy(&b, &x, sizeof(dtype));
|
|
36
|
+
old = atomicCAS(word, assumed, (assumed & ~mask) | ((unsigned int)b << shift));
|
|
37
|
+
} while (assumed != old);
|
|
38
|
+
}
|
|
39
|
+
<% end %>
|
|
40
|
+
|
|
41
|
+
__device__ static inline void cumo_<%=type_name%>_add_at_one(dtype *p, dtype v)
|
|
42
|
+
{
|
|
43
|
+
<% case type_name
|
|
44
|
+
when "int32" %>
|
|
45
|
+
atomicAdd((int*)p, (int)v);
|
|
46
|
+
<% when "uint32" %>
|
|
47
|
+
atomicAdd((unsigned int*)p, (unsigned int)v);
|
|
48
|
+
<% when "int64", "uint64" %>
|
|
49
|
+
atomicAdd((unsigned long long int*)p, (unsigned long long int)v);
|
|
50
|
+
<% when "sfloat" %>
|
|
51
|
+
atomicAdd(p, v);
|
|
52
|
+
<% when "dfloat" %>
|
|
53
|
+
cumo_<%=type_name%>_add_at_double(p, v);
|
|
54
|
+
<% when "scomplex" %>
|
|
55
|
+
atomicAdd(&p->dat[0], v.dat[0]);
|
|
56
|
+
atomicAdd(&p->dat[1], v.dat[1]);
|
|
57
|
+
<% when "dcomplex" %>
|
|
58
|
+
cumo_<%=type_name%>_add_at_double(&p->dat[0], v.dat[0]);
|
|
59
|
+
cumo_<%=type_name%>_add_at_double(&p->dat[1], v.dat[1]);
|
|
60
|
+
<% when "hfloat" %>
|
|
61
|
+
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 700)
|
|
62
|
+
atomicAdd(p, v);
|
|
63
|
+
#else
|
|
64
|
+
cumo_<%=type_name%>_add_at_narrow(p, v);
|
|
65
|
+
#endif
|
|
66
|
+
<% when "bfloat" %>
|
|
67
|
+
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800)
|
|
68
|
+
atomicAdd(p, v);
|
|
69
|
+
#else
|
|
70
|
+
cumo_<%=type_name%>_add_at_narrow(p, v);
|
|
71
|
+
#endif
|
|
72
|
+
<% else %>
|
|
73
|
+
cumo_<%=type_name%>_add_at_narrow(p, v);
|
|
74
|
+
<% end %>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
<% indexer_dims.each do |idim| %>
|
|
78
|
+
__global__ void <%="cumo_#{c_iter}_kernel_dim#{idim}"%>(cumo_na_iarray_stridx_t a1, cumo_na_iarray_stridx_t a2, cumo_na_indexer_t indexer)
|
|
79
|
+
{
|
|
80
|
+
for (uint64_t i = blockIdx.x * blockDim.x + threadIdx.x; i < indexer.total_size; i += blockDim.x * gridDim.x) {
|
|
81
|
+
cumo_na_indexer_set_dim<%=idim%>(&indexer, i);
|
|
82
|
+
char* p1 = cumo_na_iarray_stridx_at_dim<%=idim%>(&a1, &indexer);
|
|
83
|
+
char* p2 = cumo_na_iarray_stridx_at_dim<%=idim%>(&a2, &indexer);
|
|
84
|
+
cumo_<%=type_name%>_add_at_one((dtype*)p1, *(dtype*)p2);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
<% end %>
|
|
88
|
+
|
|
89
|
+
void <%="cumo_#{c_iter}_kernel_launch"%>(cumo_na_iarray_stridx_t* a1, cumo_na_iarray_stridx_t* a2, cumo_na_indexer_t* indexer)
|
|
90
|
+
{
|
|
91
|
+
size_t grid_dim = cumo_get_grid_dim(indexer->total_size);
|
|
92
|
+
size_t block_dim = cumo_get_block_dim(indexer->total_size);
|
|
93
|
+
<%= indexer_switch("cumo_#{c_iter}_kernel", "*a1,*a2,*indexer") %>
|
|
94
|
+
cumo_cuda_runtime_check_kernel_launch();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
<% [["int32", "int32_t"], ["int64", "int64_t"]].each do |iname, itype| %>
|
|
98
|
+
__global__ void <%="cumo_#{c_iter}_flat_#{iname}_kernel"%>(dtype *p, const <%=itype%> *idx, const dtype *v, uint64_t v_step, ssize_t size, uint64_t n)
|
|
99
|
+
{
|
|
100
|
+
for (uint64_t i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x) {
|
|
101
|
+
ssize_t k = (ssize_t)idx[i];
|
|
102
|
+
// The range was checked through a flag the whole process shares, so an
|
|
103
|
+
// index that slipped past it is skipped rather than added outside.
|
|
104
|
+
if (k < -size || k >= size) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
cumo_<%=type_name%>_add_at_one(p + ((k < 0) ? k + size : k), v[i * v_step]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
<% end %>
|
|
111
|
+
|
|
112
|
+
void <%="cumo_#{c_iter}_flat_kernel_launch"%>(char *p, const char *idx, int idx_size, const char *v, uint64_t v_step, ssize_t size, uint64_t n)
|
|
113
|
+
{
|
|
114
|
+
size_t grid_dim = cumo_get_grid_dim(n);
|
|
115
|
+
size_t block_dim = cumo_get_block_dim(n);
|
|
116
|
+
if (idx_size == 4) {
|
|
117
|
+
<%="cumo_#{c_iter}_flat_int32_kernel"%><<<grid_dim, block_dim, 0, cumo_cuda_stream()>>>((dtype*)p, (const int32_t*)idx, (const dtype*)v, v_step, size, n);
|
|
118
|
+
} else {
|
|
119
|
+
<%="cumo_#{c_iter}_flat_int64_kernel"%><<<grid_dim, block_dim, 0, cumo_cuda_stream()>>>((dtype*)p, (const int64_t*)idx, (const dtype*)v, v_step, size, n);
|
|
120
|
+
}
|
|
121
|
+
cumo_cuda_runtime_check_kernel_launch();
|
|
122
|
+
}
|
|
@@ -34,6 +34,7 @@ static VALUE
|
|
|
34
34
|
// few-byte object holding an arbitrarily large buffer and does not
|
|
35
35
|
// run until the host itself runs out.
|
|
36
36
|
rb_gc_adjust_memory_usage(sizeof(dtype) * na->size);
|
|
37
|
+
cumo_na_keep_young_if_large(self, sizeof(dtype) * na->size);
|
|
37
38
|
<% end %>
|
|
38
39
|
}
|
|
39
40
|
<% if !is_object && !is_bit %>
|
|
@@ -15,6 +15,53 @@ __device__ static inline void <%=c_iter%>_add_double(double *address, double val
|
|
|
15
15
|
__device__ static inline void <%=c_iter%>_add_double(double *address, double val) { atomicAdd(address, val); }
|
|
16
16
|
#endif
|
|
17
17
|
|
|
18
|
+
// Counting in shared memory first moves the atomics that pile onto a few bins
|
|
19
|
+
// inside each block, at the price of one add per bin per block at the end, so it
|
|
20
|
+
// pays once each bin gets a few dozen items and the bins fit in a block's share.
|
|
21
|
+
#define CUMO_BINCOUNT_SHARED_BLOCK 256
|
|
22
|
+
#define CUMO_BINCOUNT_SHARED_BYTES 49152
|
|
23
|
+
#define CUMO_BINCOUNT_ITEMS_PER_BIN 32
|
|
24
|
+
|
|
25
|
+
// A few bins take every add of a block, and contended double atomics in shared
|
|
26
|
+
// memory are slow, so a small histogram is kept once per warp and summed at the
|
|
27
|
+
// end. Small means the copies still leave room for as many blocks as before.
|
|
28
|
+
#define CUMO_BINCOUNT_COPY_BYTES 8192
|
|
29
|
+
|
|
30
|
+
static int
|
|
31
|
+
cumo_bincount_copies(uint64_t n, size_t bin_bytes)
|
|
32
|
+
{
|
|
33
|
+
int copies = 1;
|
|
34
|
+
while (copies * 2 <= CUMO_BINCOUNT_SHARED_BLOCK / 32 &&
|
|
35
|
+
(size_t)n * bin_bytes * (size_t)(copies * 2) <= CUMO_BINCOUNT_COPY_BYTES) {
|
|
36
|
+
copies *= 2;
|
|
37
|
+
}
|
|
38
|
+
return copies;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// The grid for the shared memory kernel, as many blocks as the device holds at
|
|
42
|
+
// once, or 0 when the global one is the better choice.
|
|
43
|
+
static size_t
|
|
44
|
+
cumo_bincount_shared_grid(const void *kernel, uint64_t count, uint64_t n, size_t bin_bytes)
|
|
45
|
+
{
|
|
46
|
+
int dev, sms, per_sm;
|
|
47
|
+
size_t smem = (size_t)n * bin_bytes, grid, need;
|
|
48
|
+
|
|
49
|
+
if (n == 0 || smem > CUMO_BINCOUNT_SHARED_BYTES || count / CUMO_BINCOUNT_ITEMS_PER_BIN < n) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
// A failed query leaves its error for the launch check that follows, which
|
|
53
|
+
// may be the report of a kernel that faulted earlier.
|
|
54
|
+
if (cudaGetDevice(&dev) != cudaSuccess ||
|
|
55
|
+
cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev) != cudaSuccess ||
|
|
56
|
+
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm, kernel, CUMO_BINCOUNT_SHARED_BLOCK, smem) != cudaSuccess ||
|
|
57
|
+
per_sm <= 0) {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
grid = (size_t)sms * (size_t)per_sm;
|
|
61
|
+
need = (count + CUMO_BINCOUNT_SHARED_BLOCK - 1) / CUMO_BINCOUNT_SHARED_BLOCK;
|
|
62
|
+
return (grid < need) ? grid : need;
|
|
63
|
+
}
|
|
64
|
+
|
|
18
65
|
// A kernel cannot raise, so an item that does not fit the output is reported
|
|
19
66
|
// through the flag and the value is left for the message. Any offending item
|
|
20
67
|
// will do, so the two stores race harmlessly.
|
|
@@ -45,11 +92,45 @@ __global__ void <%="cumo_#{c_iter}_#{bits}_kernel"%>(char *p1, ssize_t s1, size_
|
|
|
45
92
|
}
|
|
46
93
|
}
|
|
47
94
|
|
|
95
|
+
__global__ void <%="cumo_#{c_iter}_#{bits}_shared_kernel"%>(char *p1, ssize_t s1, size_t *idx1, uint64_t count, char *p2, ssize_t s2, uint64_t n, int *oor, size_t *item, int copies)
|
|
96
|
+
{
|
|
97
|
+
extern __shared__ <%=atom_type%> <%="cumo_#{c_iter}_#{bits}_bins"%>[];
|
|
98
|
+
<%=atom_type%> *bins = <%="cumo_#{c_iter}_#{bits}_bins"%>;
|
|
99
|
+
<%=atom_type%> *mine = bins + n * ((threadIdx.x / 32) % copies);
|
|
100
|
+
for (uint64_t x = threadIdx.x; x < n * copies; x += blockDim.x) {
|
|
101
|
+
bins[x] = 0;
|
|
102
|
+
}
|
|
103
|
+
__syncthreads();
|
|
104
|
+
for (uint64_t i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += blockDim.x * gridDim.x) {
|
|
105
|
+
size_t x = (size_t)(*(dtype*)(idx1 ? (p1 + idx1[i]) : (p1 + s1 * i)));
|
|
106
|
+
cumo_check_bincount_item(x, n);
|
|
107
|
+
atomicAdd(&mine[x], <%=one%>);
|
|
108
|
+
}
|
|
109
|
+
__syncthreads();
|
|
110
|
+
for (uint64_t x = threadIdx.x; x < n; x += blockDim.x) {
|
|
111
|
+
<%=atom_type%> c = 0;
|
|
112
|
+
for (int k = 0; k < copies; k++) {
|
|
113
|
+
c += bins[n * k + x];
|
|
114
|
+
}
|
|
115
|
+
if (c != 0) {
|
|
116
|
+
atomicAdd((<%=atom_type%>*)(p2 + s2 * x), c);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
48
121
|
void <%="cumo_#{c_iter}_#{bits}_kernel_launch"%>(char *p1, ssize_t s1, size_t *idx1, uint64_t count, char *p2, ssize_t s2, uint64_t n, int *oor, size_t *item)
|
|
49
122
|
{
|
|
123
|
+
size_t grid;
|
|
124
|
+
int copies;
|
|
50
125
|
<%="cumo_#{c_iter}_#{bits}_init_kernel"%><<<cumo_get_grid_dim(n), cumo_get_block_dim(n), 0, cumo_cuda_stream()>>>(p2,s2,n);
|
|
51
126
|
cumo_cuda_runtime_check_kernel_launch();
|
|
52
|
-
|
|
127
|
+
copies = cumo_bincount_copies(n, sizeof(<%=atom_type%>));
|
|
128
|
+
grid = cumo_bincount_shared_grid((const void*)<%="cumo_#{c_iter}_#{bits}_shared_kernel"%>, count, n, sizeof(<%=atom_type%>) * copies);
|
|
129
|
+
if (grid > 0) {
|
|
130
|
+
<%="cumo_#{c_iter}_#{bits}_shared_kernel"%><<<grid, CUMO_BINCOUNT_SHARED_BLOCK, n * copies * sizeof(<%=atom_type%>), cumo_cuda_stream()>>>(p1,s1,idx1,count,p2,s2,n,oor,item,copies);
|
|
131
|
+
} else {
|
|
132
|
+
<%="cumo_#{c_iter}_#{bits}_kernel"%><<<cumo_get_grid_dim(count), cumo_get_block_dim(count), 0, cumo_cuda_stream()>>>(p1,s1,idx1,count,p2,s2,n,oor,item);
|
|
133
|
+
}
|
|
53
134
|
cumo_cuda_runtime_check_kernel_launch();
|
|
54
135
|
}
|
|
55
136
|
<% end %>
|
|
@@ -74,11 +155,45 @@ __global__ void <%="cumo_#{c_iter}_#{fn}_kernel"%>(char *p1, ssize_t s1, char *p
|
|
|
74
155
|
}
|
|
75
156
|
}
|
|
76
157
|
|
|
158
|
+
__global__ void <%="cumo_#{c_iter}_#{fn}_shared_kernel"%>(char *p1, ssize_t s1, char *p2, ssize_t s2, uint64_t count, char *p3, ssize_t s3, uint64_t n, int *oor, size_t *item, int copies)
|
|
159
|
+
{
|
|
160
|
+
extern __shared__ <%=cnt_type%> <%="cumo_#{c_iter}_#{fn}_bins"%>[];
|
|
161
|
+
<%=cnt_type%> *bins = <%="cumo_#{c_iter}_#{fn}_bins"%>;
|
|
162
|
+
<%=cnt_type%> *mine = bins + n * ((threadIdx.x / 32) % copies);
|
|
163
|
+
for (uint64_t x = threadIdx.x; x < n * copies; x += blockDim.x) {
|
|
164
|
+
bins[x] = 0;
|
|
165
|
+
}
|
|
166
|
+
__syncthreads();
|
|
167
|
+
for (uint64_t i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += blockDim.x * gridDim.x) {
|
|
168
|
+
size_t x = (size_t)(*(dtype*)(p1 + s1 * i));
|
|
169
|
+
cumo_check_bincount_item(x, n);
|
|
170
|
+
<%=add%>(&mine[x], *(<%=cnt_type%>*)(p2 + s2 * i));
|
|
171
|
+
}
|
|
172
|
+
__syncthreads();
|
|
173
|
+
for (uint64_t x = threadIdx.x; x < n; x += blockDim.x) {
|
|
174
|
+
<%=cnt_type%> c = 0;
|
|
175
|
+
for (int k = 0; k < copies; k++) {
|
|
176
|
+
c += bins[n * k + x];
|
|
177
|
+
}
|
|
178
|
+
if (c != 0) {
|
|
179
|
+
<%=add%>((<%=cnt_type%>*)(p3 + s3 * x), c);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
77
184
|
void <%="cumo_#{c_iter}_#{fn}_kernel_launch"%>(char *p1, ssize_t s1, char *p2, ssize_t s2, uint64_t count, char *p3, ssize_t s3, uint64_t n, int *oor, size_t *item)
|
|
78
185
|
{
|
|
186
|
+
size_t grid;
|
|
187
|
+
int copies;
|
|
79
188
|
<%="cumo_#{c_iter}_#{fn}_init_kernel"%><<<cumo_get_grid_dim(n), cumo_get_block_dim(n), 0, cumo_cuda_stream()>>>(p3,s3,n);
|
|
80
189
|
cumo_cuda_runtime_check_kernel_launch();
|
|
81
|
-
|
|
190
|
+
copies = cumo_bincount_copies(n, sizeof(<%=cnt_type%>));
|
|
191
|
+
grid = cumo_bincount_shared_grid((const void*)<%="cumo_#{c_iter}_#{fn}_shared_kernel"%>, count, n, sizeof(<%=cnt_type%>) * copies);
|
|
192
|
+
if (grid > 0) {
|
|
193
|
+
<%="cumo_#{c_iter}_#{fn}_shared_kernel"%><<<grid, CUMO_BINCOUNT_SHARED_BLOCK, n * copies * sizeof(<%=cnt_type%>), cumo_cuda_stream()>>>(p1,s1,p2,s2,count,p3,s3,n,oor,item,copies);
|
|
194
|
+
} else {
|
|
195
|
+
<%="cumo_#{c_iter}_#{fn}_kernel"%><<<cumo_get_grid_dim(count), cumo_get_block_dim(count), 0, cumo_cuda_stream()>>>(p1,s1,p2,s2,count,p3,s3,n,oor,item);
|
|
196
|
+
}
|
|
82
197
|
cumo_cuda_runtime_check_kernel_launch();
|
|
83
198
|
}
|
|
84
199
|
<% end %>
|