forked from rapidsai/cuvs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuVSDeviceMatrixImpl.java
More file actions
344 lines (291 loc) · 11 KB
/
CuVSDeviceMatrixImpl.java
File metadata and controls
344 lines (291 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nvidia.cuvs.internal;
import static com.nvidia.cuvs.internal.common.LinkerHelper.C_POINTER;
import static com.nvidia.cuvs.internal.common.Util.*;
import static com.nvidia.cuvs.internal.panama.headers_h.*;
import com.nvidia.cuvs.*;
import com.nvidia.cuvs.internal.panama.DLManagedTensor;
import com.nvidia.cuvs.internal.panama.DLTensor;
import java.lang.foreign.*;
public class CuVSDeviceMatrixImpl extends CuVSMatrixBaseImpl implements CuVSDeviceMatrix {
private static final int CHUNK_BYTES =
8 * 1024 * 1024; // Based on benchmarks, 8MB seems the minimum size to optimize PCIe bandwidth
private final long hostBufferBytes;
private long bufferedMatrixRowStart = 0;
private long bufferedMatrixRowEnd = 0;
private final CuVSResources resources;
private final long rowStride;
private final long columnStride;
private MemorySegment hostBuffer = MemorySegment.NULL;
protected CuVSDeviceMatrixImpl(
CuVSResources resources,
MemorySegment deviceMemorySegment,
long size,
long columns,
DataType dataType,
ValueLayout valueLayout) {
this(resources, deviceMemorySegment, size, columns, -1, -1, dataType, valueLayout);
}
protected CuVSDeviceMatrixImpl(
CuVSResources resources,
MemorySegment deviceMemorySegment,
long size,
long columns,
long rowStride,
long columnStride,
DataType dataType,
ValueLayout valueLayout) {
super(deviceMemorySegment, dataType, valueLayout, size, columns);
this.resources = resources;
this.rowStride = rowStride;
this.columnStride = columnStride;
long rowBytes = columns * valueLayout.byteSize();
long matrixBytes = size * rowBytes;
if (matrixBytes < CHUNK_BYTES) {
this.hostBufferBytes = matrixBytes;
} else if (rowBytes > CHUNK_BYTES) {
// We need to buffer at least one row at time
this.hostBufferBytes = rowBytes;
} else {
var rowCount = (CHUNK_BYTES / rowBytes);
this.hostBufferBytes = rowBytes * rowCount;
}
}
@Override
public MemorySegment toTensor(Arena arena) {
var strides = rowStride >= 0 ? new long[] {rowStride, columnStride} : null;
return prepareTensor(
arena, memorySegment, new long[] {size, columns}, strides, code(), bits(), kDLCUDA());
}
private static MemorySegment createPinnedBuffer(long bufferBytes) {
try (var localArena = Arena.ofConfined()) {
MemorySegment pointer = localArena.allocate(C_POINTER);
checkCudaError(cudaMallocHost(pointer, bufferBytes), "cudaMallocHost");
return pointer.get(C_POINTER, 0);
}
}
private static void destroyPinnedBuffer(MemorySegment bufferSegment) {
checkCudaError(cudaFreeHost(bufferSegment), "cudaFreeHost");
}
private void populateBuffer(long startRow) {
if (hostBuffer == MemorySegment.NULL) {
// System.out.println("Creating a buffer of size " + hostBufferBytes);
hostBuffer = createPinnedBuffer(hostBufferBytes);
}
try (var localArena = Arena.ofConfined()) {
long rowBytes = columns * valueLayout.byteSize();
var endRow = Math.min(startRow + (hostBufferBytes / rowBytes), size);
var rowCount = endRow - startRow;
// System.out.printf(
// Locale.ROOT, "startRow: %d, endRow %d, count: %d\n", startRow, endRow, rowCount);
MemorySegment sliceManagedTensor = DLManagedTensor.allocate(localArena);
DLManagedTensor.dl_tensor(sliceManagedTensor, DLTensor.allocate(localArena));
checkCuVSError(
cuvsMatrixSliceRows(0, toTensor(localArena), startRow, endRow, sliceManagedTensor),
"cuvsMatrixSliceRows");
assert DLTensor.shape(DLManagedTensor.dl_tensor(sliceManagedTensor)).get(C_LONG, 0)
== rowCount;
assert DLTensor.shape(DLManagedTensor.dl_tensor(sliceManagedTensor)).getAtIndex(C_LONG, 1)
== columns;
MemorySegment bufferTensor =
prepareTensor(
localArena, hostBuffer, new long[] {rowCount, columns}, code(), bits(), kDLCPU());
try (var resourceAccess = resources.access()) {
checkCuVSError(
cuvsMatrixCopy(resourceAccess.handle(), sliceManagedTensor, bufferTensor),
"cuvsMatrixCopy");
checkCuVSError(cuvsStreamSync(resourceAccess.handle()), "cuvsStreamSync");
bufferedMatrixRowStart = startRow;
bufferedMatrixRowEnd = endRow;
}
}
}
@Override
public RowView getRow(long row) {
if (row < bufferedMatrixRowStart || row >= bufferedMatrixRowEnd) {
populateBuffer(row);
}
var valueByteSize = valueLayout.byteSize();
var startRow = row - bufferedMatrixRowStart;
return new SliceRowView(
hostBuffer.asSlice(startRow * columns * valueByteSize, columns * valueByteSize),
columns,
valueLayout,
dataType,
valueByteSize);
}
@Override
public void toArray(int[][] array) {
assert dataType == DataType.INT || dataType == DataType.UINT;
assert (array.length >= size) : "Input array is not large enough";
assert (array.length == 0 || array[0].length >= columns) : "Input array is not large enough";
try (var localArena = Arena.ofConfined()) {
var rowBytes = columns * valueLayout.byteSize();
var tmpRowSegment = localArena.allocate(rowBytes);
for (int r = 0; r < size; ++r) {
copyRow(array[r], localArena, r, tmpRowSegment);
}
}
}
@Override
public void toArray(float[][] array) {
assert dataType == DataType.FLOAT;
assert (array.length >= size) : "Input array is not large enough";
assert (array.length == 0 || array[0].length >= columns) : "Input array is not large enough";
try (var localArena = Arena.ofConfined()) {
var rowBytes = columns * valueLayout.byteSize();
var tmpRowSegment = localArena.allocate(rowBytes);
for (int r = 0; r < size; ++r) {
copyRow(array[r], localArena, r, tmpRowSegment);
}
}
}
@Override
public void toArray(byte[][] array) {
assert dataType == DataType.BYTE;
assert (array.length >= size) : "Input array is not large enough";
assert (array.length == 0 || array[0].length >= columns) : "Input array is not large enough";
try (var localArena = Arena.ofConfined()) {
var rowSegmentLayout = MemoryLayout.sequenceLayout(columns, valueLayout);
var tmpRowSegment = localArena.allocate(rowSegmentLayout);
for (int r = 0; r < size; ++r) {
copyRow(array[r], localArena, r, tmpRowSegment);
}
}
}
private void copyRow(Object array, Arena localArena, int r, MemorySegment tmpRowSegment) {
MemorySegment sliceManagedTensor = DLManagedTensor.allocate(localArena);
DLManagedTensor.dl_tensor(sliceManagedTensor, DLTensor.allocate(localArena));
checkCuVSError(
cuvsMatrixSliceRows(0, toTensor(localArena), r, r + 1, sliceManagedTensor),
"cuvsMatrixSliceRows");
MemorySegment bufferTensor =
prepareTensor(
localArena, tmpRowSegment, new long[] {1, columns}, code(), bits(), kDLCUDA());
try (var resourceAccess = resources.access()) {
checkCuVSError(
cuvsMatrixCopy(resourceAccess.handle(), sliceManagedTensor, bufferTensor),
"cuvsMatrixCopy");
checkCuVSError(cuvsStreamSync(resourceAccess.handle()), "cuvsStreamSync");
}
MemorySegment.copy(tmpRowSegment, valueLayout, 0L, array, 0, (int) columns);
}
@Override
public void toHost(CuVSHostMatrix hostMatrix) {
if (hostMatrix.columns() != columns || hostMatrix.size() != size) {
throw new IllegalArgumentException("[hostMatrix] must have the same dimensions");
}
if (hostMatrix.dataType() != dataType) {
throw new IllegalArgumentException("[hostMatrix] must have the same dataType");
}
try (var localArena = Arena.ofConfined()) {
var hostMatrixTensor = ((CuVSMatrixInternal) hostMatrix).toTensor(localArena);
try (var resourceAccess = resources.access()) {
var cuvsRes = resourceAccess.handle();
var deviceMatrixTensor = toTensor(localArena);
checkCuVSError(
cuvsMatrixCopy(cuvsRes, deviceMatrixTensor, hostMatrixTensor), "cuvsMatrixCopy");
checkCuVSError(cuvsStreamSync(cuvsRes), "cuvsStreamSync");
}
}
}
@Override
public CuVSDeviceMatrix toDevice(CuVSResources resources) {
return new CuVSDeviceMatrixDelegate(this);
}
@Override
public void toDevice(CuVSDeviceMatrix targetMatrix, CuVSResources cuVSResources) {
copyMatrix(this, (CuVSMatrixInternal) targetMatrix, cuVSResources);
}
@Override
public void close() {
if (hostBuffer != MemorySegment.NULL) {
destroyPinnedBuffer(hostBuffer);
hostBuffer = MemorySegment.NULL;
}
}
private static class CuVSDeviceMatrixDelegate implements CuVSDeviceMatrix, CuVSMatrixInternal {
private final CuVSDeviceMatrixImpl deviceMatrix;
private CuVSDeviceMatrixDelegate(CuVSDeviceMatrixImpl deviceMatrix) {
this.deviceMatrix = deviceMatrix;
}
@Override
public long size() {
return deviceMatrix.size();
}
@Override
public long columns() {
return deviceMatrix.columns();
}
@Override
public DataType dataType() {
return deviceMatrix.dataType();
}
@Override
public RowView getRow(long row) {
return deviceMatrix.getRow(row);
}
@Override
public void toArray(int[][] array) {
deviceMatrix.toArray(array);
}
@Override
public void toArray(float[][] array) {
deviceMatrix.toArray(array);
}
@Override
public void toArray(byte[][] array) {
deviceMatrix.toArray(array);
}
@Override
public void toHost(CuVSHostMatrix hostMatrix) {
deviceMatrix.toHost(hostMatrix);
}
@Override
public void toDevice(CuVSDeviceMatrix deviceMatrix, CuVSResources cuVSResources) {
deviceMatrix.toDevice(deviceMatrix, cuVSResources);
}
@Override
public CuVSDeviceMatrix toDevice(CuVSResources cuVSResources) {
return this;
}
@Override
public MemorySegment memorySegment() {
return deviceMatrix.memorySegment();
}
@Override
public ValueLayout valueLayout() {
return deviceMatrix.valueLayout();
}
@Override
public int bits() {
return deviceMatrix.bits();
}
@Override
public int code() {
return 0;
}
@Override
public MemorySegment toTensor(Arena arena) {
return deviceMatrix.toTensor(arena);
}
@Override
public void close() {
// Do nothing
}
}
}