cumo 0.15.0 → 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.
@@ -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
- <%="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);
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
- <%="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);
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 %>
@@ -0,0 +1,89 @@
1
+ void cumo_<%=type_name%>_searchsorted_kernel_launch(const dtype *a, uint64_t n, const int64_t *sorter, const dtype *v, int64_t *out, uint64_t m, int right, int *oor);
2
+
3
+ /*
4
+ The places of v in self for Cumo::NArray#searchsorted, which has cast self
5
+ and v to this type and sorter to Int64 by the time it calls here.
6
+ */
7
+ static VALUE
8
+ <%=c_func(3)%>(VALUE self, VALUE v, VALUE right, VALUE sorter)
9
+ {
10
+ VALUE a_cont, v_cont, s_cont = Qnil, out;
11
+ cumo_narray_t *na, *nv, *ns, *nout;
12
+ char *a_ptr, *v_ptr, *s_ptr = NULL, *out_ptr;
13
+ int *oor = NULL;
14
+ int i;
15
+
16
+ CUMO_CHECK_NARRAY_TYPE(self, cT, "self");
17
+ CUMO_CHECK_NARRAY_TYPE(v, cT, "v");
18
+ if (!NIL_P(sorter)) {
19
+ CUMO_CHECK_NARRAY_TYPE(sorter, cumo_cInt64, "sorter");
20
+ }
21
+
22
+ a_cont = cumo_na_as_contiguous_array(self);
23
+ v_cont = cumo_na_as_contiguous_array(v);
24
+ if (!NIL_P(sorter)) {
25
+ s_cont = cumo_na_as_contiguous_array(sorter);
26
+ }
27
+ CumoGetNArray(v_cont, nv);
28
+ out = cumo_na_new(cumo_cInt64, nv->ndim, nv->shape);
29
+
30
+ // Taking a pointer runs allocate, which is Ruby, so every one is taken
31
+ // before anything is measured, and checked afterwards to be still the same.
32
+ a_ptr = cumo_na_get_offset_pointer_for_read(a_cont);
33
+ v_ptr = cumo_na_get_offset_pointer_for_read(v_cont);
34
+ if (!NIL_P(s_cont)) {
35
+ s_ptr = cumo_na_get_offset_pointer_for_read(s_cont);
36
+ }
37
+ out_ptr = cumo_na_get_offset_pointer_for_write(out);
38
+
39
+ // cumo_na_as_contiguous_array answers what dup gave it, which a class is
40
+ // free to define.
41
+ CUMO_CHECK_NARRAY_TYPE(a_cont, cT, "self");
42
+ CUMO_CHECK_NARRAY_TYPE(v_cont, cT, "v");
43
+ if (!NIL_P(s_cont)) {
44
+ CUMO_CHECK_NARRAY_TYPE(s_cont, cumo_cInt64, "sorter");
45
+ }
46
+ CumoGetNArray(a_cont, na);
47
+ CumoGetNArray(v_cont, nv);
48
+ CumoGetNArray(out, nout);
49
+ if (na->ndim != 1) {
50
+ rb_raise(cumo_na_eShapeError, "searchsorted needs a 1-dimensional array");
51
+ }
52
+ if (nout->ndim != nv->ndim) {
53
+ rb_raise(cumo_na_eShapeError, "v or the result was reshaped while it was measured");
54
+ }
55
+ for (i = 0; i < nv->ndim; ++i) {
56
+ if (nout->shape[i] != nv->shape[i]) {
57
+ rb_raise(cumo_na_eShapeError, "v or the result was reshaped while it was measured");
58
+ }
59
+ }
60
+ if (!NIL_P(s_cont)) {
61
+ CumoGetNArray(s_cont, ns);
62
+ if (ns->ndim != 1 || ns->size != na->size) {
63
+ rb_raise(cumo_na_eShapeError, "sorter must be 1-dimensional and %"SZF"u long", na->size);
64
+ }
65
+ CUMO_ROW_CHECK_READ_BUFFER(s_cont, s_ptr, "sorter");
66
+ }
67
+ CUMO_ROW_CHECK_READ_BUFFER(a_cont, a_ptr, "self");
68
+ CUMO_ROW_CHECK_READ_BUFFER(v_cont, v_ptr, "v");
69
+ CUMO_ROW_CHECK_WRITE_BUFFER(out, out_ptr, "the result");
70
+
71
+ if (nv->size > 0) {
72
+ if (s_ptr != NULL) {
73
+ oor = cumo_cuda_runtime_error_flag_new();
74
+ }
75
+ cumo_<%=type_name%>_searchsorted_kernel_launch(
76
+ (const dtype*)a_ptr, na->size, (const int64_t*)s_ptr, (const dtype*)v_ptr,
77
+ (int64_t*)out_ptr, nv->size, RTEST(right), oor);
78
+ if (oor != NULL) {
79
+ CUMO_SHOW_SYNCHRONIZE_WARNING_ONCE("searchsorted", "<%=type_name%>");
80
+ if (cumo_cuda_runtime_error_flag_get(oor)) {
81
+ rb_raise(rb_eIndexError, "sorter has an index out of range");
82
+ }
83
+ }
84
+ }
85
+ RB_GC_GUARD(a_cont);
86
+ RB_GC_GUARD(v_cont);
87
+ RB_GC_GUARD(s_cont);
88
+ return out;
89
+ }
@@ -0,0 +1,43 @@
1
+ // numpy's order: NaN after every number, and equal to another NaN.
2
+ #define CUMO_SEARCHSORTED_LT(x, y) (m_lt(x, y) || (m_isnan(y) && !m_isnan(x)))
3
+
4
+ __global__ void cumo_<%=type_name%>_searchsorted_kernel(const dtype *a, uint64_t n, const int64_t *sorter, const dtype *v, int64_t *out, uint64_t m, int right, int *oor)
5
+ {
6
+ for (uint64_t i = blockIdx.x * blockDim.x + threadIdx.x; i < m; i += blockDim.x * gridDim.x) {
7
+ dtype x = v[i];
8
+ uint64_t lo = 0, hi = n;
9
+
10
+ while (lo < hi) {
11
+ uint64_t mid = lo + (hi - lo) / 2;
12
+ dtype y;
13
+
14
+ if (sorter) {
15
+ int64_t s = sorter[mid];
16
+ if (s < 0 || (uint64_t)s >= n) {
17
+ *oor = 1;
18
+ break;
19
+ }
20
+ y = a[s];
21
+ } else {
22
+ y = a[mid];
23
+ }
24
+ if (right ? !CUMO_SEARCHSORTED_LT(x, y) : CUMO_SEARCHSORTED_LT(y, x)) {
25
+ lo = mid + 1;
26
+ } else {
27
+ hi = mid;
28
+ }
29
+ }
30
+ out[i] = (int64_t)lo;
31
+ }
32
+ }
33
+
34
+ #undef CUMO_SEARCHSORTED_LT
35
+
36
+ void cumo_<%=type_name%>_searchsorted_kernel_launch(const dtype *a, uint64_t n, const int64_t *sorter, const dtype *v, int64_t *out, uint64_t m, int right, int *oor)
37
+ {
38
+ size_t grid_dim = cumo_get_grid_dim(m);
39
+ size_t block_dim = cumo_get_block_dim(m);
40
+
41
+ cumo_<%=type_name%>_searchsorted_kernel<<<grid_dim, block_dim, 0, cumo_cuda_stream()>>>(a, n, sorter, v, out, m, right, oor);
42
+ cumo_cuda_runtime_check_kernel_launch();
43
+ }
@@ -69,11 +69,7 @@ static void
69
69
  if (idx1) {
70
70
  for (i=i1=0; i1<n1 && i<n; i1++) {
71
71
  if (!cumo_na_store_rary_fetch(v1, i1, &x)) break;
72
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
73
72
  if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cArithSeq))
74
- #else
75
- if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cEnumerator))
76
- #endif
77
73
  {
78
74
  cumo_na_step_sequence(x,&len,&beg,&step);
79
75
  for (c=0; c<len && i<n; c++,i++) {
@@ -91,11 +87,7 @@ static void
91
87
  } else {
92
88
  for (i=i1=0; i1<n1 && i<n; i1++) {
93
89
  if (!cumo_na_store_rary_fetch(v1, i1, &x)) break;
94
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
95
90
  if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cArithSeq))
96
- #else
97
- if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cEnumerator))
98
- #endif
99
91
  {
100
92
  cumo_na_step_sequence(x,&len,&beg,&step);
101
93
  for (c=0; c<len && i<n; c++,i++) {
@@ -127,11 +119,7 @@ static void
127
119
  dtype* host_z = RB_ALLOCV_N(dtype, buf, n);
128
120
  for (i=i1=0; i1<n1 && i<n; i1++) {
129
121
  if (!cumo_na_store_rary_fetch(v1, i1, &x)) break;
130
- #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
131
122
  if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cArithSeq))
132
- #else
133
- if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, rb_cEnumerator))
134
- #endif
135
123
  {
136
124
  cumo_na_step_sequence(x,&len,&beg,&step);
137
125
  for (c=0; c<len && i<n; c++,i++) {
@@ -13,6 +13,7 @@ static VALUE
13
13
  ptr = cumo_cuda_runtime_malloc(CUMO_BIT_BYTES(na->size));
14
14
  CUMO_NA_DATA_PTR(na) = ptr;
15
15
  rb_gc_adjust_memory_usage(CUMO_BIT_BYTES(na->size));
16
+ cumo_na_keep_young_if_large(self, CUMO_BIT_BYTES(na->size));
16
17
  }
17
18
  break;
18
19
  case CUMO_NARRAY_VIEW_T: