Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/cuda/CudaBuffer.cpp
5990 views
1
/*******************************************************************************
2
* Copyright (c) 2013, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include "CudaCommon.hpp"
24
#include "java/com_ibm_cuda_CudaBuffer.h"
25
26
#ifdef OMR_OPT_CUDA
27
28
namespace
29
{
30
31
/**
32
* A macro that expands the argument macro for each primitive type.
33
*/
34
#define SPECIALIZE_ALL(SPECIALIZATION) \
35
SPECIALIZATION(jbyte, Byte) \
36
SPECIALIZATION(jchar, Char) \
37
SPECIALIZATION(jfloat, Float) \
38
SPECIALIZATION(jdouble, Double) \
39
SPECIALIZATION(jint, Int) \
40
SPECIALIZATION(jlong, Long) \
41
SPECIALIZATION(jshort, Short)
42
43
/**
44
* A template function (specialized for each primitive array type) that
45
* answers the size in bytes of an array of the specified length.
46
*
47
* @param[in] array the array reference
48
* @param[in] length the length of the array
49
* @return the size of the array
50
*/
51
template<typename ArrayType> size_t
52
arraySize(ArrayType array, jint length);
53
54
/**
55
* A macro that expands to one specialization of the arraySize template.
56
*
57
* @param[in] Type the array element type
58
* @param[in] Name the name of the element type
59
*/
60
#define SPECIALIZE_ARRAY_SIZE(Type, Name) \
61
template<> inline size_t \
62
arraySize<Type##Array>(Type##Array array, jint length) \
63
{ return sizeof(Type) * (size_t)length; }
64
65
/**
66
* Template specializations of arraySize for all primitive array types.
67
*/
68
SPECIALIZE_ALL(SPECIALIZE_ARRAY_SIZE)
69
70
/**
71
* A template function (specialized for each primitive array type) that
72
* copies a region of the array into the specified buffer.
73
*
74
* @param[in] env the JNI interface pointer
75
* @param[in] array the array reference
76
* @param[in] start the source starting index (inclusive)
77
* @param[in] length the number of elements to be copied
78
* @param[in] buffer the destination buffer
79
*/
80
template<typename ArrayType> void
81
getArrayRegion(JNIEnv * env, ArrayType array, jint start, jint length, void * buffer);
82
83
/**
84
* A macro that expands to one specialization of the getArrayRegion template.
85
*
86
* @param[in] Type the array element type
87
* @param[in] Name the name of the element type
88
*/
89
#define SPECIALIZE_GET_ARRAY_REGION(Type, Name) \
90
template<> inline void \
91
getArrayRegion<Type##Array>(JNIEnv * env, Type##Array array, jint start, jint length, void * buffer) \
92
{ env->Get##Name##ArrayRegion(array, start, length, (Type *)buffer); }
93
94
/**
95
* Template specializations of getArrayRegion for all primitive array types.
96
*/
97
SPECIALIZE_ALL(SPECIALIZE_GET_ARRAY_REGION)
98
99
/**
100
* A template function (specialized for each primitive array type) that
101
* sets a region of the array from the specified buffer.
102
*
103
* @param[in] env the JNI interface pointer
104
* @param[in] array the array reference
105
* @param[in] start the source starting index (inclusive)
106
* @param[in] length the number of elements to be copied
107
* @param[in] buffer the source buffer
108
*/
109
template<typename ArrayType> void
110
setArrayRegion(JNIEnv * env, ArrayType array, jint start, jint length, void * buffer);
111
112
/**
113
* A macro that expands to one specialization of the setArrayRegion template.
114
*
115
* @param[in] Type the array element type
116
* @param[in] Name the name of the element type
117
*/
118
#define SPECIALIZE_SET_ARRAY_REGION(Type, Name) \
119
template<> inline void \
120
setArrayRegion<Type##Array>(JNIEnv * env, Type##Array array, jint start, jint length, void * buffer) \
121
{ env->Set##Name##ArrayRegion(array, start, length, (Type *)buffer); }
122
123
/**
124
* Template specializations of setArrayRegion for all primitive array types.
125
*/
126
SPECIALIZE_ALL(SPECIALIZE_SET_ARRAY_REGION)
127
128
/**
129
* Copy data from a Java primitive array to a region of device memory.
130
* The ArrayType template parameter identifies the type of the Java array.
131
* The data are copied to temporary host storage before the final transfer
132
* to device memory. The transfer to temporary host storage is required
133
* because j9cuda_memcpyHostToDevice may block, in violation of the contract
134
* for using GetPrimitiveArrayCritical (were it used instead).
135
*
136
* @param[in] env the JNI interface pointer
137
* @param[in] deviceId the device identifier
138
* @param[in] devicePtr the device pointer
139
* @param[in] array the source array
140
* @param[in] fromIndex the source starting index (inclusive)
141
* @param[in] toIndex the source ending index (exclusive)
142
*/
143
template<typename ArrayType> void
144
copyFromHost(
145
JNIEnv * env,
146
jint deviceId,
147
jlong devicePtr,
148
ArrayType array,
149
jint fromIndex,
150
jint toIndex)
151
{
152
jint length = toIndex - fromIndex;
153
size_t byteCount = arraySize(array, length);
154
155
PORT_ACCESS_FROM_ENV(env);
156
157
void * buffer = J9CUDA_ALLOCATE_MEMORY(byteCount);
158
int32_t error = J9CUDA_ERROR_MEMORY_ALLOCATION;
159
160
if (NULL != buffer) {
161
getArrayRegion(env, array, fromIndex, length, buffer);
162
163
error = j9cuda_memcpyHostToDevice(
164
(uint32_t)deviceId,
165
(void *)(uintptr_t)devicePtr,
166
buffer,
167
byteCount);
168
169
J9CUDA_FREE_MEMORY(buffer);
170
}
171
172
if (0 != error) {
173
throwCudaException(env, error);
174
}
175
}
176
177
/**
178
* Copy data to a Java primitive array from a region of device memory.
179
* The ArrayType template parameter identifies the type of the Java array.
180
* The data are copied to temporary host storage before the final transfer
181
* to the array. The transfer to temporary host storage is required because
182
* j9cuda_memcpyDeviceToHost may block, in violation of the contract for
183
* using GetPrimitiveArrayCritical (were it used instead).
184
*
185
* @param[in] env the JNI interface pointer
186
* @param[in] deviceId the device identifier
187
* @param[in] devicePtr the device pointer
188
* @param[in] array the target array
189
* @param[in] fromIndex the target starting index (inclusive)
190
* @param[in] toIndex the target ending index (exclusive)
191
*/
192
template<typename ArrayType> void
193
copyToHost(JNIEnv * env, jint deviceId, jlong devicePtr, ArrayType array, jint fromIndex, jint toIndex)
194
{
195
jint length = toIndex - fromIndex;
196
size_t byteCount = arraySize(array, length);
197
198
PORT_ACCESS_FROM_ENV(env);
199
200
void * buffer = J9CUDA_ALLOCATE_MEMORY(byteCount);
201
int32_t error = J9CUDA_ERROR_MEMORY_ALLOCATION;
202
203
if (NULL != buffer) {
204
error = j9cuda_memcpyDeviceToHost(
205
(uint32_t)deviceId,
206
buffer,
207
(void *)(uintptr_t)devicePtr,
208
byteCount);
209
210
if (0 == error) {
211
setArrayRegion(env, array, fromIndex, length, buffer);
212
}
213
214
J9CUDA_FREE_MEMORY(buffer);
215
}
216
217
if (0 != error) {
218
throwCudaException(env, error);
219
}
220
}
221
222
} // namespace
223
224
#endif /* OMR_OPT_CUDA */
225
226
/**
227
* Allocate a block of memory on the specified device.
228
*
229
* Class: com.ibm.cuda.CudaBuffer
230
* Method: allocate
231
* Signature: (IJ)J
232
*
233
* @param[in] env the JNI interface pointer
234
* @param[in] (unused) the class pointer
235
* @param[in] deviceId the device identifier
236
* @param[in] byteCount the number of bytes requested
237
* @return a pointer to the block of storage
238
*/
239
jlong JNICALL
240
Java_com_ibm_cuda_CudaBuffer_allocate
241
(JNIEnv * env, jclass, jint deviceId, jlong byteCount)
242
{
243
J9VMThread * thread = (J9VMThread *)env;
244
245
Trc_cuda_bufferAllocate_entry(thread, deviceId, byteCount);
246
247
void * devicePtr = NULL;
248
int32_t error = J9CUDA_ERROR_NO_DEVICE;
249
#ifdef OMR_OPT_CUDA
250
PORT_ACCESS_FROM_ENV(env);
251
error = j9cuda_deviceAlloc(deviceId, (uintptr_t)byteCount, &devicePtr);
252
#endif /* OMR_OPT_CUDA */
253
254
if (0 != error) {
255
throwCudaException(env, error);
256
}
257
258
Trc_cuda_bufferAllocate_exit(thread, devicePtr);
259
260
return (jlong)devicePtr;
261
}
262
263
#ifdef OMR_OPT_CUDA
264
265
/**
266
* Allocate a direct NIO byte buffer of the specified capacity.
267
* The resulting buffer is used only internally within CudaBuffer for transfers
268
* involving NIO buffers that are neither direct nor backed by an array.
269
*
270
* Class: com.ibm.cuda.CudaBuffer
271
* Method: allocateDirectBuffer
272
* Signature: (J)Ljava/nio/ByteBuffer;
273
*
274
* @param[in] env the JNI interface pointer
275
* @param[in] (unused) the class pointer
276
* @param[in] capacity the requested buffer capacity in bytes
277
* @return a pointer to the byte buffer
278
*/
279
jobject JNICALL
280
Java_com_ibm_cuda_CudaBuffer_allocateDirectBuffer
281
(JNIEnv * env, jclass, jlong capacity)
282
{
283
J9VMThread * thread = (J9VMThread *)env;
284
285
Trc_cuda_bufferAllocateDirectBuffer_entry(thread, capacity);
286
287
PORT_ACCESS_FROM_ENV(env);
288
289
void * buffer = J9CUDA_ALLOCATE_MEMORY((size_t)capacity);
290
jobject result = NULL;
291
292
if (NULL == buffer) {
293
Trc_cuda_bufferAllocateDirectBuffer_allocFail(thread);
294
throwCudaException(env, J9CUDA_ERROR_MEMORY_ALLOCATION);
295
} else {
296
result = env->NewDirectByteBuffer(buffer, capacity);
297
298
if ((NULL == result) || env->ExceptionCheck()) {
299
Trc_cuda_bufferAllocateDirectBuffer_newFail(thread);
300
J9CUDA_FREE_MEMORY(buffer);
301
buffer = NULL;
302
result = NULL;
303
}
304
}
305
306
Trc_cuda_bufferAllocateDirectBuffer_exit(thread, result, buffer);
307
308
return result;
309
}
310
311
/**
312
* Copy data to device memory from device memory; the source may be on the same
313
* or a different device.
314
*
315
* Class: com.ibm.cuda.CudaBuffer
316
* Method: copyFromDevice
317
* Signature: (IJIJJ)V
318
*
319
* @param[in] env the JNI interface pointer
320
* @param[in] (unused) the class pointer
321
* @param[in] targetDeviceId the target device identifier
322
* @param[in] targetAddress the target device pointer
323
* @param[in] sourceDeviceId the source device identifier
324
* @param[in] sourceAddress the source device pointer
325
* @param[in] byteCount the number of bytes to be copied
326
*/
327
void JNICALL
328
Java_com_ibm_cuda_CudaBuffer_copyFromDevice
329
(JNIEnv * env, jclass, jint targetDeviceId, jlong targetAddress, jint sourceDeviceId, jlong sourceAddress, jlong byteCount)
330
{
331
J9VMThread * thread = (J9VMThread *)env;
332
333
Trc_cuda_bufferCopyFromDevice_entry(
334
thread,
335
targetDeviceId, (uintptr_t)targetAddress,
336
sourceDeviceId, (uintptr_t)sourceAddress, byteCount);
337
338
PORT_ACCESS_FROM_ENV(env);
339
340
int32_t error = 0;
341
342
if (targetDeviceId == sourceDeviceId) {
343
error = j9cuda_memcpyDeviceToDevice(
344
(uint32_t)targetDeviceId,
345
(void *)(uintptr_t)targetAddress,
346
(const void *)(uintptr_t)sourceAddress,
347
(uintptr_t)byteCount);
348
} else {
349
error = j9cuda_memcpyPeer(
350
(uint32_t)targetDeviceId,
351
(void *)(uintptr_t)targetAddress,
352
(uint32_t)sourceDeviceId,
353
(const void *)(uintptr_t)sourceAddress,
354
(uintptr_t)byteCount);
355
}
356
357
if (0 != error) {
358
Trc_cuda_bufferCopyFromDevice_fail(thread, error);
359
throwCudaException(env, error);
360
}
361
362
Trc_cuda_bufferCopyFromDevice_exit(thread);
363
}
364
365
/**
366
* Copy data to device memory from a Java byte array.
367
*
368
* Class: com.ibm.cuda.CudaBuffer
369
* Method: copyFromHostByte
370
* Signature: (IJ[BII)V
371
*
372
* @param[in] env the JNI interface pointer
373
* @param[in] (unused) the class pointer
374
* @param[in] deviceId the device identifier
375
* @param[in] devicePtr the device pointer
376
* @param[in] array the source array
377
* @param[in] fromIndex the source starting index (inclusive)
378
* @param[in] toIndex the source ending index (exclusive)
379
*/
380
void JNICALL
381
Java_com_ibm_cuda_CudaBuffer_copyFromHostByte
382
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jbyteArray array, jint fromIndex, jint toIndex)
383
{
384
J9VMThread * thread = (J9VMThread *)env;
385
386
Trc_cuda_bufferCopyFromHostByte_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
387
388
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
389
390
Trc_cuda_bufferCopyFromHostByte_exit(thread);
391
}
392
393
/**
394
* Copy data to device memory from a Java char array.
395
*
396
* Class: com.ibm.cuda.CudaBuffer
397
* Method: copyFromHostChar
398
* Signature: (IJ[CII)V
399
*
400
* @param[in] env the JNI interface pointer
401
* @param[in] (unused) the class pointer
402
* @param[in] deviceId the device identifier
403
* @param[in] devicePtr the device pointer
404
* @param[in] array the source array
405
* @param[in] fromIndex the source starting index (inclusive)
406
* @param[in] toIndex the source ending index (exclusive)
407
*/
408
void JNICALL
409
Java_com_ibm_cuda_CudaBuffer_copyFromHostChar
410
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jcharArray array, jint fromIndex, jint toIndex)
411
{
412
J9VMThread * thread = (J9VMThread *)env;
413
414
Trc_cuda_bufferCopyFromHostChar_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
415
416
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
417
418
Trc_cuda_bufferCopyFromHostChar_exit(thread);
419
}
420
421
/**
422
* Copy data to device memory from a direct NIO buffer.
423
*
424
* Class: com.ibm.cuda.CudaBuffer
425
* Method: copyFromHostDirect
426
* Signature: (IJLjava/nio/Buffer;JJ)V
427
*
428
* @param[in] env the JNI interface pointer
429
* @param[in] (unused) the class pointer
430
* @param[in] deviceId the device identifier
431
* @param[in] devicePtr the device pointer
432
* @param[in] source the source buffer
433
* @param[in] fromOffset the source starting byte offset (inclusive)
434
* @param[in] toOffset the source ending byte offset (exclusive)
435
*/
436
void JNICALL
437
Java_com_ibm_cuda_CudaBuffer_copyFromHostDirect
438
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jobject source, jlong fromOffset, jlong toOffset)
439
{
440
J9VMThread * thread = (J9VMThread *)env;
441
442
Trc_cuda_bufferCopyFromHostDirect_entry(thread, deviceId, (uintptr_t)devicePtr, source, fromOffset, toOffset);
443
444
jbyte const * buffer = (jbyte const *)env->GetDirectBufferAddress(source);
445
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
446
447
if (NULL != buffer) {
448
PORT_ACCESS_FROM_ENV(env);
449
450
error = j9cuda_memcpyHostToDevice(
451
(uint32_t)deviceId,
452
(void *)(uintptr_t)devicePtr,
453
&buffer[fromOffset],
454
(size_t)(toOffset - fromOffset));
455
}
456
457
if (0 != error) {
458
throwCudaException(env, error);
459
}
460
461
Trc_cuda_bufferCopyFromHostDirect_exit(thread);
462
}
463
464
/**
465
* Copy data to device memory from a Java double array.
466
*
467
* Class: com.ibm.cuda.CudaBuffer
468
* Method: copyFromHostDouble
469
* Signature: (IJ[DII)V
470
*
471
* @param[in] env the JNI interface pointer
472
* @param[in] (unused) the class pointer
473
* @param[in] deviceId the device identifier
474
* @param[in] devicePtr the device pointer
475
* @param[in] array the source array
476
* @param[in] fromIndex the source starting index (inclusive)
477
* @param[in] toIndex the source ending index (exclusive)
478
*/
479
void JNICALL
480
Java_com_ibm_cuda_CudaBuffer_copyFromHostDouble
481
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jdoubleArray array, jint fromIndex, jint toIndex)
482
{
483
J9VMThread * thread = (J9VMThread *)env;
484
485
Trc_cuda_bufferCopyFromHostDouble_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
486
487
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
488
489
Trc_cuda_bufferCopyFromHostDouble_exit(thread);
490
}
491
492
/**
493
* Copy data to device memory from a Java float array.
494
*
495
* Class: com.ibm.cuda.CudaBuffer
496
* Method: copyFromHostFloat
497
* Signature: (IJ[FII)V
498
*
499
* @param[in] env the JNI interface pointer
500
* @param[in] (unused) the class pointer
501
* @param[in] deviceId the device identifier
502
* @param[in] devicePtr the device pointer
503
* @param[in] array the source array
504
* @param[in] fromIndex the source starting index (inclusive)
505
* @param[in] toIndex the source ending index (exclusive)
506
*/
507
void JNICALL
508
Java_com_ibm_cuda_CudaBuffer_copyFromHostFloat
509
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jfloatArray array, jint fromIndex, jint toIndex)
510
{
511
J9VMThread * thread = (J9VMThread *)env;
512
513
Trc_cuda_bufferCopyFromHostFloat_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
514
515
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
516
517
Trc_cuda_bufferCopyFromHostFloat_exit(thread);
518
}
519
520
/**
521
* Copy data to device memory from a Java int array.
522
*
523
* Class: com.ibm.cuda.CudaBuffer
524
* Method: copyFromHostInt
525
* Signature: (IJ[III)V
526
*
527
* @param[in] env the JNI interface pointer
528
* @param[in] (unused) the class pointer
529
* @param[in] deviceId the device identifier
530
* @param[in] devicePtr the device pointer
531
* @param[in] array the source array
532
* @param[in] fromIndex the source starting index (inclusive)
533
* @param[in] toIndex the source ending index (exclusive)
534
*/
535
void JNICALL
536
Java_com_ibm_cuda_CudaBuffer_copyFromHostInt
537
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jintArray array, jint fromIndex, jint toIndex)
538
{
539
J9VMThread * thread = (J9VMThread *)env;
540
541
Trc_cuda_bufferCopyFromHostInt_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
542
543
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
544
545
Trc_cuda_bufferCopyFromHostInt_exit(thread);
546
}
547
548
/**
549
* Copy data to device memory from a Java long array.
550
*
551
* Class: com.ibm.cuda.CudaBuffer
552
* Method: copyFromHostLong
553
* Signature: (IJ[JII)V
554
*
555
* @param[in] env the JNI interface pointer
556
* @param[in] (unused) the class pointer
557
* @param[in] deviceId the device identifier
558
* @param[in] devicePtr the device pointer
559
* @param[in] array the source array
560
* @param[in] fromIndex the source starting index (inclusive)
561
* @param[in] toIndex the source ending index (exclusive)
562
*/
563
void JNICALL
564
Java_com_ibm_cuda_CudaBuffer_copyFromHostLong
565
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jlongArray array, jint fromIndex, jint toIndex)
566
{
567
J9VMThread * thread = (J9VMThread *)env;
568
569
Trc_cuda_bufferCopyFromHostLong_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
570
571
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
572
573
Trc_cuda_bufferCopyFromHostLong_exit(thread);
574
}
575
576
/**
577
* Copy data to device memory from a Java short array.
578
*
579
* Class: com.ibm.cuda.CudaBuffer
580
* Method: copyFromHostShort
581
* Signature: (IJ[SII)V
582
*
583
* @param[in] env the JNI interface pointer
584
* @param[in] (unused) the class pointer
585
* @param[in] deviceId the device identifier
586
* @param[in] devicePtr the device pointer
587
* @param[in] array the source array
588
* @param[in] fromIndex the source starting index (inclusive)
589
* @param[in] toIndex the source ending index (exclusive)
590
*/
591
void JNICALL
592
Java_com_ibm_cuda_CudaBuffer_copyFromHostShort
593
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jshortArray array, jint fromIndex, jint toIndex)
594
{
595
J9VMThread * thread = (J9VMThread *)env;
596
597
Trc_cuda_bufferCopyFromHostShort_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
598
599
copyFromHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
600
601
Trc_cuda_bufferCopyFromHostShort_exit(thread);
602
}
603
604
/**
605
* Copy data from device memory to a Java byte array.
606
*
607
* Class: com.ibm.cuda.CudaBuffer
608
* Method: copyToHostByte
609
* Signature: (IJ[BII)V
610
*
611
* @param[in] env the JNI interface pointer
612
* @param[in] (unused) the class pointer
613
* @param[in] deviceId the device identifier
614
* @param[in] devicePtr the device pointer
615
* @param[in] array the target array
616
* @param[in] fromIndex the target starting index (inclusive)
617
* @param[in] toIndex the target ending index (exclusive)
618
*/
619
void JNICALL
620
Java_com_ibm_cuda_CudaBuffer_copyToHostByte
621
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jbyteArray array, jint fromIndex, jint toIndex)
622
{
623
J9VMThread * thread = (J9VMThread *)env;
624
625
Trc_cuda_bufferCopyToHostByte_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
626
627
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
628
629
Trc_cuda_bufferCopyToHostByte_exit(thread);
630
}
631
632
/**
633
* Copy data from device memory to a Java char array.
634
*
635
* Class: com.ibm.cuda.CudaBuffer
636
* Method: copyToHostChar
637
* Signature: (IJ[CII)V
638
*
639
* @param[in] env the JNI interface pointer
640
* @param[in] (unused) the class pointer
641
* @param[in] deviceId the device identifier
642
* @param[in] devicePtr the device pointer
643
* @param[in] array the target array
644
* @param[in] fromIndex the target starting index (inclusive)
645
* @param[in] toIndex the target ending index (exclusive)
646
*/
647
void JNICALL
648
Java_com_ibm_cuda_CudaBuffer_copyToHostChar
649
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jcharArray array, jint fromIndex, jint toIndex)
650
{
651
J9VMThread * thread = (J9VMThread *)env;
652
653
Trc_cuda_bufferCopyToHostChar_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
654
655
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
656
657
Trc_cuda_bufferCopyToHostChar_exit(thread);
658
}
659
660
/**
661
* Copy data from device memory to a direct NIO buffer.
662
*
663
* Class: com.ibm.cuda.CudaBuffer
664
* Method: copyToHostDirect
665
* Signature: (IJLjava/nio/Buffer;JJ)V
666
*
667
* @param[in] env the JNI interface pointer
668
* @param[in] (unused) the class pointer
669
* @param[in] deviceId the device identifier
670
* @param[in] devicePtr the device pointer
671
* @param[in] target the target buffer
672
* @param[in] fromOffset the target starting byte offset (inclusive)
673
* @param[in] toOffset the target ending byte offset (exclusive)
674
*/
675
void JNICALL
676
Java_com_ibm_cuda_CudaBuffer_copyToHostDirect
677
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jobject target, jlong fromOffset, jlong toOffset)
678
{
679
J9VMThread * thread = (J9VMThread *)env;
680
681
Trc_cuda_bufferCopyToHostDirect_entry(thread, deviceId, (uintptr_t)devicePtr, target, fromOffset, toOffset);
682
683
jbyte * buffer = (jbyte *)env->GetDirectBufferAddress(target);
684
int32_t error = J9CUDA_ERROR_OPERATING_SYSTEM;
685
686
if (NULL != buffer) {
687
PORT_ACCESS_FROM_ENV(env);
688
689
error = j9cuda_memcpyDeviceToHost(
690
(uint32_t)deviceId,
691
&buffer[fromOffset],
692
(void *)(uintptr_t)devicePtr,
693
(size_t)(toOffset - fromOffset));
694
}
695
696
if (0 != error) {
697
throwCudaException(env, error);
698
}
699
700
Trc_cuda_bufferCopyToHostDirect_exit(thread);
701
}
702
703
/**
704
* Copy data from device memory to a Java double array.
705
*
706
* Class: com.ibm.cuda.CudaBuffer
707
* Method: copyToHostDouble
708
* Signature: (IJ[DII)V
709
*
710
* @param[in] env the JNI interface pointer
711
* @param[in] (unused) the class pointer
712
* @param[in] deviceId the device identifier
713
* @param[in] devicePtr the device pointer
714
* @param[in] array the target array
715
* @param[in] fromIndex the target starting index (inclusive)
716
* @param[in] toIndex the target ending index (exclusive)
717
*/
718
void JNICALL
719
Java_com_ibm_cuda_CudaBuffer_copyToHostDouble
720
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jdoubleArray array, jint fromIndex, jint toIndex)
721
{
722
J9VMThread * thread = (J9VMThread *)env;
723
724
Trc_cuda_bufferCopyToHostDouble_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
725
726
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
727
728
Trc_cuda_bufferCopyToHostDouble_exit(thread);
729
}
730
731
/**
732
* Copy data from device memory to a Java float array.
733
*
734
* Class: com.ibm.cuda.CudaBuffer
735
* Method: copyToHostFloat
736
* Signature: (IJ[FII)V
737
*
738
* @param[in] env the JNI interface pointer
739
* @param[in] (unused) the class pointer
740
* @param[in] deviceId the device identifier
741
* @param[in] devicePtr the device pointer
742
* @param[in] array the target array
743
* @param[in] fromIndex the target starting index (inclusive)
744
* @param[in] toIndex the target ending index (exclusive)
745
*/
746
void JNICALL
747
Java_com_ibm_cuda_CudaBuffer_copyToHostFloat
748
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jfloatArray array, jint fromIndex, jint toIndex)
749
{
750
J9VMThread * thread = (J9VMThread *)env;
751
752
Trc_cuda_bufferCopyToHostFloat_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
753
754
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
755
756
Trc_cuda_bufferCopyToHostFloat_exit(thread);
757
}
758
759
/**
760
* Copy data from device memory to a Java int array.
761
*
762
* Class: com.ibm.cuda.CudaBuffer
763
* Method: copyToHostInt
764
* Signature: (IJ[III)V
765
*
766
* @param[in] env the JNI interface pointer
767
* @param[in] (unused) the class pointer
768
* @param[in] deviceId the device identifier
769
* @param[in] devicePtr the device pointer
770
* @param[in] array the target array
771
* @param[in] fromIndex the target starting index (inclusive)
772
* @param[in] toIndex the target ending index (exclusive)
773
*/
774
void JNICALL
775
Java_com_ibm_cuda_CudaBuffer_copyToHostInt
776
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jintArray array, jint fromIndex, jint toIndex)
777
{
778
J9VMThread * thread = (J9VMThread *)env;
779
780
Trc_cuda_bufferCopyToHostInt_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
781
782
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
783
784
Trc_cuda_bufferCopyToHostInt_exit(thread);
785
}
786
787
/**
788
* Copy data from device memory to a Java long array.
789
*
790
* Class: com.ibm.cuda.CudaBuffer
791
* Method: copyToHostLong
792
* Signature: (IJ[JII)V
793
*
794
* @param[in] env the JNI interface pointer
795
* @param[in] (unused) the class pointer
796
* @param[in] deviceId the device identifier
797
* @param[in] devicePtr the device pointer
798
* @param[in] array the target array
799
* @param[in] fromIndex the target starting index (inclusive)
800
* @param[in] toIndex the target ending index (exclusive)
801
*/
802
void JNICALL
803
Java_com_ibm_cuda_CudaBuffer_copyToHostLong
804
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jlongArray array, jint fromIndex, jint toIndex)
805
{
806
J9VMThread * thread = (J9VMThread *)env;
807
808
Trc_cuda_bufferCopyToHostLong_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
809
810
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
811
812
Trc_cuda_bufferCopyToHostLong_exit(thread);
813
}
814
815
/**
816
* Copy data from device memory to a Java short array.
817
*
818
* Class: com.ibm.cuda.CudaBuffer
819
* Method: copyToHostShort
820
* Signature: (IJ[SII)V
821
*
822
* @param[in] env the JNI interface pointer
823
* @param[in] (unused) the class pointer
824
* @param[in] deviceId the device identifier
825
* @param[in] devicePtr the device pointer
826
* @param[in] array the target array
827
* @param[in] fromIndex the target starting index (inclusive)
828
* @param[in] toIndex the target ending index (exclusive)
829
*/
830
void JNICALL
831
Java_com_ibm_cuda_CudaBuffer_copyToHostShort
832
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jshortArray array, jint fromIndex, jint toIndex)
833
{
834
J9VMThread * thread = (J9VMThread *)env;
835
836
Trc_cuda_bufferCopyToHostShort_entry(thread, deviceId, (uintptr_t)devicePtr, array, fromIndex, toIndex);
837
838
copyToHost(env, deviceId, devicePtr, array, fromIndex, toIndex);
839
840
Trc_cuda_bufferCopyToHostShort_exit(thread);
841
}
842
843
/**
844
* Fill device memory with a repeating value.
845
*
846
* Class: com.ibm.cuda.CudaBuffer
847
* Method: fill
848
* Signature: (IJIIJ)V
849
*
850
* @param[in] env the JNI interface pointer
851
* @param[in] (unused) the class pointer
852
* @param[in] deviceId the device identifier
853
* @param[in] devicePtr the device pointer
854
* @param[in] elemSize the value size in bytes
855
* @param[in] value the value to be repeated
856
* @param[in] count the number of copies requested
857
*/
858
void JNICALL
859
Java_com_ibm_cuda_CudaBuffer_fill
860
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr, jint elemSize, jint value, jlong count)
861
{
862
J9VMThread * thread = (J9VMThread *)env;
863
864
Trc_cuda_bufferFill_entry(thread, deviceId, (uintptr_t)devicePtr, elemSize, value, count);
865
866
PORT_ACCESS_FROM_ENV(env);
867
868
int32_t error = J9CUDA_ERROR_INVALID_VALUE;
869
870
switch (elemSize) {
871
case 1:
872
error = j9cuda_memset8 ((uint32_t)deviceId, (void *)(uintptr_t)devicePtr, (uint8_t) value, (uintptr_t)count);
873
break;
874
case 2:
875
error = j9cuda_memset16((uint32_t)deviceId, (void *)(uintptr_t)devicePtr, (uint16_t)value, (uintptr_t)count);
876
break;
877
case 4:
878
error = j9cuda_memset32((uint32_t)deviceId, (void *)(uintptr_t)devicePtr, (uint32_t)value, (uintptr_t)count);
879
break;
880
default:
881
break;
882
}
883
884
if (0 != error) {
885
throwCudaException(env, error);
886
}
887
888
Trc_cuda_bufferFill_exit(thread);
889
}
890
891
/**
892
* Release a direct NIO byte buffer allocated by allocateDirectBuffer.
893
*
894
* Class: com.ibm.cuda.CudaBuffer
895
* Method: allocateDirectBuffer
896
* Signature: (J)Ljava/nio/ByteBuffer;
897
*
898
* @param[in] env the JNI interface pointer
899
* @param[in] (unused) the class pointer
900
* @param[in] byteBuffer the byte buffer
901
902
* Class: com.ibm.cuda.CudaBuffer
903
* Method: freeDirectBuffer
904
* Signature: (Ljava/nio/Buffer;)V
905
*/
906
void JNICALL
907
Java_com_ibm_cuda_CudaBuffer_freeDirectBuffer
908
(JNIEnv * env, jclass, jobject byteBuffer)
909
{
910
J9VMThread * thread = (J9VMThread *)env;
911
912
Trc_cuda_bufferFreeDirectBuffer_entry(thread, byteBuffer);
913
914
void * buffer = env->GetDirectBufferAddress(byteBuffer);
915
916
if (NULL != buffer) {
917
PORT_ACCESS_FROM_ENV(env);
918
919
J9CUDA_FREE_MEMORY(buffer);
920
}
921
922
Trc_cuda_bufferFreeDirectBuffer_exit(thread);
923
}
924
925
/**
926
* Release a block of memory on the specified device.
927
*
928
* Class: com.ibm.cuda.CudaBuffer
929
* Method: release
930
* Signature: (IJ)V
931
*
932
* @param[in] env the JNI interface pointer
933
* @param[in] (unused) the class pointer
934
* @param[in] deviceId the device identifier
935
* @param[in] devicePtr the device pointer
936
*/
937
void JNICALL
938
Java_com_ibm_cuda_CudaBuffer_release
939
(JNIEnv * env, jclass, jint deviceId, jlong devicePtr)
940
{
941
J9VMThread * thread = (J9VMThread *)env;
942
943
Trc_cuda_bufferRelease_entry(thread, deviceId, (uintptr_t)devicePtr);
944
945
PORT_ACCESS_FROM_ENV(env);
946
947
int32_t error = j9cuda_deviceFree(deviceId, (void *)(uintptr_t)devicePtr);
948
949
if (0 != error) {
950
throwCudaException(env, error);
951
}
952
953
Trc_cuda_bufferRelease_exit(thread);
954
}
955
956
#endif /* OMR_OPT_CUDA */
957
958