使用maven构建项目,在pom中添加:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.4.0</version>
</dependency>
java代码如下:
package test.javacpp.jnatest;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class App
{
//使用jna调用libopenblas.so或libopenblas.dll动态链接库的接口
public interface CpuLevel3Interface extends Library{
CpuLevel3Interface INSTANCE = (CpuLevel3Interface)Native.loadLibrary("openblas", CpuLevel3Interface.class);
void cblas_sgemm(int Order, int TransA, int TransB, int M, int N, int K, float alpha, float[] A, int lda, float[] B, int ldb, float beta, float[] C, int ldc);
}
public static void main( String[] args )
{
System.out.println( "Hello World!" );
int Order=101;
int TransA=111;
int TransB=111;
int M=4;//A的行数,C的行数
int N=2;//B的列数,C的列数
int K=3;//A的列数,B的行数
float alpha=1;
float beta=0;
int lda=K;//A的列
int ldb=N;//B的列
int ldc=N;//C的列
float[] A={1,2,3,4,5,6,7,8,9,8,7,6};
float[] B={5,4,3,2,1,0};
float[] C = new float[M*N];
CpuLevel3Interface.INSTANCE.cblas_sgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
System.out.println("-------------A--------------\n"+Arrays.toString(A));
System.out.println("-------------B--------------\n"+Arrays.toString(B));
System.out.println("-------------C--------------\n"+Arrays.toString(C));
}
}
运行结果: