Skip to content

Commit d458746

Browse files
author
Olivier Chafik
committed
JavaCL: basic handling of structs in JavaCL Generator
1 parent 8bc7679 commit d458746

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

Generator/src/main/java/com/nativelibs4java/opencl/generator/JavaCLGenerator.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.nativelibs4java.opencl.generator;
22

33
import com.nativelibs4java.opencl.*;
4-
import com.ochafik.io.IOUtils;
54
import com.ochafik.lang.jnaerator.*;
6-
import com.ochafik.lang.jnaerator.PreprocessorUtils.MacroUseCallback;
75
import com.ochafik.lang.jnaerator.TypeConversion.JavaPrimitive;
86
import com.ochafik.lang.jnaerator.TypeConversion.TypeConversionMode;
97
import com.ochafik.lang.jnaerator.UniversalReconciliator;
@@ -22,7 +20,6 @@
2220
import java.io.PrintStream;
2321

2422
import java.util.regex.Pattern;
25-
import org.anarres.cpp.LexerException;
2623

2724
public class JavaCLGenerator extends JNAerator {
2825

@@ -37,7 +34,7 @@ public JavaCLGenerator(JNAeratorConfig config) {
3734
config.genCPlusPlus = false;
3835
config.gccLong = true;
3936
config.putTopStructsInSeparateFiles = false;
40-
config.runtime = JNAeratorConfig.Runtime.JNAerator;//NL4JStructs;
37+
config.runtime = JNAeratorConfig.Runtime.BridJ;//NL4JStructs;
4138
config.fileToLibrary = new Adapter<File, String>() {
4239
@Override
4340
public String adapt(File value) {
@@ -89,6 +86,11 @@ protected com.ochafik.lang.jnaerator.parser.ObjCppParser newObjCppParser(TypeCon
8986
public Result createResult(final ClassOutputter outputter, Feedback feedback) {
9087
return new Result(config, outputter, feedback) {
9188

89+
@Override
90+
public Identifier getLibraryClassFullName(String library) {
91+
return null;
92+
}
93+
9294
@Override
9395
public void init() {
9496
typeConverter = new BridJTypeConversion(this) {
@@ -343,7 +345,13 @@ private Conversion convertTypeToJavaCL(Result result, String argName, TypeRef va
343345
ret.outerJavaTypeRef = typeRef(ident(CLBuffer.class, expr(typeRef(pair.getSecond()))));
344346
return ret;
345347
}
348+
Identifier ref = result.typeConverter.findRef(starget.getName(), target, libraryClassName, true);
349+
if (ref != null) {
350+
ret.outerJavaTypeRef = typeRef(ident(CLBuffer.class, expr(typeRef(ref))));
351+
return ret;
352+
}
346353
}
354+
throw new UnsupportedConversionException(valueType, "Unknown pointed target type");
347355
} else if (valueType instanceof TypeRef.SimpleTypeRef) {
348356
TypeRef.SimpleTypeRef sr = (TypeRef.SimpleTypeRef)valueType;
349357
String name = sr.getName() == null ? sr.toString() : sr.getName().toString();
@@ -492,31 +500,31 @@ public SampleUserProgram(CLContext context) throws IOException {
492500
}
493501
}
494502

495-
496-
@Override
497-
protected void autoConfigure() {
498-
super.autoConfigure();
499-
500-
/*
501-
__OPENCL_VERSION__
502-
__ENDIAN_LITTLE__
503-
504-
__IMAGE_SUPPORT__
505-
__FAST_RELAXED_MATH__
506-
*/
507-
508-
}
503+
//
504+
// @Override
505+
// protected void autoConfigure() {
506+
// super.autoConfigure();
507+
//
508+
// /*
509+
// __OPENCL_VERSION__
510+
// __ENDIAN_LITTLE__
511+
//
512+
// __IMAGE_SUPPORT__
513+
// __FAST_RELAXED_MATH__
514+
// */
515+
//
516+
// }
509517

510518
public static void main(String[] args) {
511519
JNAerator.main(new JavaCLGenerator(new JNAeratorConfig()),
512520
new String[] {
513-
"-o", "target/generated-sources/main/java",
521+
"-o", "target/generated-sources/test",
514522
//"-o", "/Users/ochafik/Prog/Java/versionedSources/nativelibs4java/trunk/libraries/OpenCL/Demos/target/generated-sources/main/java",
515523
"-noJar",
516524
"-noComp",
517525
"-v",
518-
"-addRootDir", "src/main/opencl",
519-
"src/main/opencl",
526+
"-addRootDir", "src/test/opencl",
527+
"src/test/opencl/com/nativelibs4java/opencl/generator/Structs.c",
520528
//"-addRootDir", "/Users/ochafik/Prog/Java/versionedSources/nativelibs4java/trunk/libraries/OpenCL/Blas/target/../src/main/opencl",
521529
//"/Users/ochafik/Prog/Java/versionedSources/nativelibs4java/trunk/libraries/OpenCL/Blas/src/main/opencl/com/nativelibs4java/opencl/blas/LinearAlgebraKernels.c"
522530
//"-addRootDir", "/Users/ochafik/Prog/Java/versionedSources/nativelibs4java/trunk/libraries/OpenCL/Demos/target/../src/main/opencl",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.nativelibs4java.opencl.generator;
2+
3+
import com.nativelibs4java.opencl.CLBuffer;
4+
import com.nativelibs4java.opencl.CLContext;
5+
import com.nativelibs4java.opencl.CLEvent;
6+
import com.nativelibs4java.opencl.CLMem;
7+
import com.nativelibs4java.opencl.CLQueue;
8+
import com.nativelibs4java.opencl.JavaCL;
9+
import java.io.IOException;
10+
import org.bridj.Pointer;
11+
import org.junit.Test;
12+
import static org.junit.Assert.*;
13+
14+
public class GeneratorTest {
15+
16+
@Test
17+
public void testStructs() throws IOException {
18+
CLContext context = JavaCL.createBestContext();
19+
CLQueue queue = context.createDefaultQueue();
20+
21+
Structs structs = new Structs(context);
22+
Structs.S s = new Structs.S();
23+
Pointer<Structs.S> pS = Pointer.pointerTo(s);
24+
CLBuffer<Structs.S> b = context.createBuffer(CLMem.Usage.InputOutput, pS);
25+
26+
CLEvent e = structs.f(queue, b, new int[] { 1 }, null);
27+
b.read(queue, pS, true, e);
28+
assertEquals(10, s.a());
29+
assertEquals(100, s.b());
30+
31+
s.a(1).b(2);
32+
b.write(queue, pS, true);
33+
e = structs.f(queue, b, new int[] { 1 }, null);
34+
b.read(queue, pS, true, e);
35+
assertEquals(12, s.a());
36+
assertEquals(120, s.b());
37+
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
typedef struct _S {
2+
int a, b;
3+
} S;
4+
5+
kernel void f(__global S *pS) {
6+
pS->a = pS->a * 2 + 10;
7+
pS->b = pS->b * 10 + 100;
8+
}

MavenPlugin/src/main/java/com/nativelibs4java/opencl/generator/JavaCLGeneratorMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public class JavaCLGeneratorMojo
5454

5555
/**
5656
* Output directory for JNAerated Java sources.
57-
* @parameter expression="${project.build.directory}/generated-sources/main/java"
57+
* @parameter expression="${project.build.directory}/generated-sources/main"
5858
* @optional
5959
*/
6060
private File javaOutputDirectory;
6161

6262
/**
6363
* Output directory for JNAerated Java test sources.
64-
* @parameter expression="${project.build.directory}/generated-sources/test/java"
64+
* @parameter expression="${project.build.directory}/generated-sources/test"
6565
* @optional
6666
*/
6767
private File testJavaOutputDirectory;

0 commit comments

Comments
 (0)