-
Notifications
You must be signed in to change notification settings - Fork 1
/
RawScientificLibrary.java
95 lines (76 loc) · 2.48 KB
/
RawScientificLibrary.java
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
package org.apidesign.demo.matrixultimate.svm;
import java.util.Arrays;
import java.util.List;
import org.apidesign.demo.matrixultimate.GreatScientificLibrary;
import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.nativeimage.c.struct.CField;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;
@CContext(RawScientificLibrary.GslDirectives.class)
public final class RawScientificLibrary implements GreatScientificLibrary<Long> {
@CStruct("gsl_matrix")
static interface GslMatrix extends PointerBase {
@CField long size1();
@CField long size2();
}
private static final RawScientificLibrary GSL = new RawScientificLibrary();
private RawScientificLibrary() {
}
public static GreatScientificLibrary<?> getDefault() {
return GSL;
}
@CFunction
static native GslMatrix gsl_matrix_alloc(long size1, long size2);
@CFunction
static native void gsl_matrix_free(GslMatrix m);
@CFunction
static native double gsl_matrix_get(GslMatrix p, long r, long c);
@CFunction
static native void gsl_matrix_set(GslMatrix p, long r, long c, double v);
@Override
public Long create(long size1, long size2) {
return gsl_matrix_alloc(size1, size2).rawValue();
}
@Override
public void free(Long matrix) {
gsl_matrix_free(WordFactory.pointer(matrix));
}
@Override
public long toRaw(Long m) {
return m;
}
@Override
public Long fromRaw(long m) {
return m;
}
@Override
public double get(Long matrix, long i, long j) {
return gsl_matrix_get(WordFactory.pointer(matrix), i, j);
}
@Override
public void set(Long matrix, long i, long j, double v) {
gsl_matrix_set(WordFactory.pointer(matrix), i, j, v);
}
@Override
public long getSize1(Long m) {
GslMatrix matrix = WordFactory.pointer(m);
return matrix.size1();
}
@Override
public long getSize2(Long m) {
GslMatrix matrix = WordFactory.pointer(m);
return matrix.size2();
}
public static final class GslDirectives implements CContext.Directives {
@Override
public List<String> getHeaderFiles() {
return Arrays.asList("<gsl/gsl_matrix.h>");
}
@Override
public List<String> getLibraries() {
return Arrays.asList("gsl", "gslcblas");
}
}
}