Java源码示例:org.bytedeco.javacpp.ShortPointer
示例1
/**
* Creates a short indexer to access efficiently the data of a pointer.
*
* @param pointer data to access via a buffer or to copy to an array
* @param direct {@code true} to use a direct buffer, see {@link Indexer} for details
* @return the new short array backed by a buffer or an array
*/
public static ShortIndexer create(final ShortPointer pointer, int[] sizes, int[] strides, boolean direct) {
if (direct) {
return new ShortBufferIndexer(pointer.asBuffer(), sizes, strides);
} else {
final int position = pointer.position();
short[] array = new short[pointer.limit() - position];
pointer.get(array);
return new ShortArrayIndexer(array, sizes, strides) {
@Override public void release() {
pointer.position(position).put(array);
super.release();
}
};
}
}
示例2
private static ShortProcessor makeShortProcessor(Mat mat) {
if (mat.type() != opencv_core.CV_16UC1) {
throw new IllegalArgumentException("wrong Mat type: " + mat.type());
}
final int w = mat.cols();
final int h = mat.rows();
ShortProcessor sp = new ShortProcessor(w, h);
ShortPointer sptr = new ShortPointer(mat.data());
sptr.get((short[]) sp.getPixels());
sptr.close();
return sp;
}
示例3
String[] cppTypeName(Class<?> type) {
String prefix = "", suffix = "";
if (type == Buffer.class || type == Pointer.class) {
prefix = "void*";
} else if (type == byte[].class || type == ByteBuffer.class || type == BytePointer.class) {
prefix = "signed char*";
} else if (type == short[].class || type == ShortBuffer.class || type == ShortPointer.class) {
prefix = "short*";
} else if (type == int[].class || type == IntBuffer.class || type == IntPointer.class) {
prefix = "int*";
} else if (type == long[].class || type == LongBuffer.class || type == LongPointer.class) {
prefix = "jlong*";
} else if (type == float[].class || type == FloatBuffer.class || type == FloatPointer.class) {
prefix = "float*";
} else if (type == double[].class || type == DoubleBuffer.class || type == DoublePointer.class) {
prefix = "double*";
} else if (type == char[].class || type == CharBuffer.class || type == CharPointer.class) {
prefix = "unsigned short*";
} else if (type == boolean[].class) {
prefix = "unsigned char*";
} else if (type == PointerPointer.class) {
prefix = "void**";
} else if (type == String.class) {
prefix = "const char*";
} else if (type == byte.class) {
prefix = "signed char";
} else if (type == long.class) {
prefix = "jlong";
} else if (type == char.class) {
prefix = "unsigned short";
} else if (type == boolean.class) {
prefix = "unsigned char";
} else if (type.isPrimitive()) {
prefix = type.getName();
} else if (FunctionPointer.class.isAssignableFrom(type)) {
Method functionMethod = functionMethod(type, null);
if (functionMethod != null) {
return cppFunctionTypeName(functionMethod);
}
} else {
String scopedType = cppScopeName(type);
if (scopedType.length() > 0) {
prefix = scopedType + "*";
} else {
logger.warn("The class " + type.getCanonicalName() +
" does not map to any C++ type. Compilation will most likely fail.");
}
}
return new String[] { prefix, suffix };
}
示例4
/** @return {@code create(pointer, sizes, strides, true)} */
public static ShortIndexer create(ShortPointer pointer, int[] sizes, int[] strides) {
return create(pointer, sizes, strides, true);
}
示例5
@Override
protected void hgemm(char Order, char TransA, char TransB, int M, int N, int K, float alpha, INDArray A, int lda,
INDArray B, int ldb, float beta, INDArray C, int ldc) {
//A = Shape.toOffsetZero(A);
//B = Shape.toOffsetZero(B);
Nd4j.getExecutioner().push();
CudaContext ctx = allocator.getFlowController().prepareAction(C, A, B);
CublasPointer cAPointer = new CublasPointer(A, ctx);
CublasPointer cBPointer = new CublasPointer(B, ctx);
CublasPointer cCPointer = new CublasPointer(C, ctx);
cublasHandle_t handle = ctx.getHandle();
synchronized (handle) {
cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));
int arch = CudaEnvironment.getInstance().getCurrentDeviceArchitecture();
if (CUDA_VERSION >= 8000 && (arch == 53 || arch == 60)) {
// on these selected archs we run with cublasHgemm
__half alphaHalf = new __half();
__half betaHalf = new __half();
new ShortPointer(alphaHalf).put((short) HalfIndexer.fromFloat(alpha));
new ShortPointer(betaHalf).put((short) HalfIndexer.fromFloat(beta));
cublasHgemm(new cublasContext(handle), convertTranspose(TransA), convertTranspose(TransB), M, N, K,
alphaHalf, new __half(cAPointer.getDevicePointer()), lda,
new __half(cBPointer.getDevicePointer()), ldb, betaHalf,
new __half(cCPointer.getDevicePointer()), ldc);
} else {
// CUDA_R_16F == 2 for CUDA 8
// CUBLAS_DATA_HALF == 2 for CUDA 7.5
cublasSgemmEx(new cublasContext(handle), convertTranspose(TransA), convertTranspose(TransB), M, N, K,
new FloatPointer(alpha), (ShortPointer) cAPointer.getDevicePointer(), 2, lda,
(ShortPointer) cBPointer.getDevicePointer(), 2, ldb, new FloatPointer(beta),
(ShortPointer) cCPointer.getDevicePointer(), 2, ldc);
}
}
allocator.registerAction(ctx, C, A, B);
OpExecutionerUtil.checkForAny(C);
}
示例6
@Override
protected void hgemm(char Order, char TransA, char TransB, int M, int N, int K, float alpha, INDArray A, int lda,
INDArray B, int ldb, float beta, INDArray C, int ldc) {
//A = Shape.toOffsetZero(A);
//B = Shape.toOffsetZero(B);
Nd4j.getExecutioner().push();
CudaContext ctx = allocator.getFlowController().prepareAction(C, A, B);
CublasPointer cAPointer = new CublasPointer(A, ctx);
CublasPointer cBPointer = new CublasPointer(B, ctx);
CublasPointer cCPointer = new CublasPointer(C, ctx);
cublasHandle_t handle = ctx.getCublasHandle();
synchronized (handle) {
cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getCublasStream()));
int arch = CudaEnvironment.getInstance().getCurrentDeviceArchitecture();
if ((CUDA_VERSION >= 8000 && (arch == 53 || arch == 60 || arch >= 70)) || (CUDA_VERSION >= 8000 && CUDA_VERSION < 9020)) {
// on these selected archs we run with cublasHgemm
__half alphaHalf = new __half();
__half betaHalf = new __half();
new ShortPointer(alphaHalf).put((short) HalfIndexer.fromFloat(alpha));
new ShortPointer(betaHalf).put((short) HalfIndexer.fromFloat(beta));
cublasHgemm(new cublasContext(handle), convertTranspose(TransA), convertTranspose(TransB), M, N, K,
alphaHalf, new __half(cAPointer.getDevicePointer()), lda,
new __half(cBPointer.getDevicePointer()), ldb, betaHalf,
new __half(cCPointer.getDevicePointer()), ldc);
} else {
// CUDA_R_16F == 2 for CUDA 8
// CUBLAS_DATA_HALF == 2 for CUDA 7.5
cublasSgemmEx(new cublasContext(handle), convertTranspose(TransA), convertTranspose(TransB), M, N, K,
new FloatPointer(alpha), (ShortPointer) cAPointer.getDevicePointer(), 2, lda,
(ShortPointer) cBPointer.getDevicePointer(), 2, ldb, new FloatPointer(beta),
(ShortPointer) cCPointer.getDevicePointer(), 2, ldc);
}
ctx.getOldStream().synchronize();
}
allocator.registerAction(ctx, C, A, B);
OpExecutionerUtil.checkForAny(C);
}
示例7
/**
* Duplicates {@link ShortProcessor} to the corresponding OpenCV image of
* type {@link Mat}.
*
* @param sp The {@link ShortProcessor} to be converted
* @return The OpenCV image (of type {@link Mat})
*/
public static Mat toMat(ShortProcessor sp) {
final int w = sp.getWidth();
final int h = sp.getHeight();
final short[] pixels = (short[]) sp.getPixels();
return new Mat(h, w, opencv_core.CV_16UC1, new ShortPointer(pixels));
}