From f029fc22ec1863cf239a43d66fbfc66d89f8a7de Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 6 Mar 2019 23:41:35 +0100 Subject: [PATCH] Benchmarking, PXC debugging --- .../src/sznp/virtualcomputer/Test.java | 43 +++++++++++++++++-- libpxc/pxc.c | 24 ++++++++++- libpxc/pxct.c | 12 +++++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/VirtualComputer/src/sznp/virtualcomputer/Test.java b/VirtualComputer/src/sznp/virtualcomputer/Test.java index 80a6e80..427fcb6 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/Test.java +++ b/VirtualComputer/src/sznp/virtualcomputer/Test.java @@ -5,10 +5,16 @@ import com.aparapi.Range; import com.sun.jna.Native; import com.sun.jna.Pointer; import jnr.ffi.LibraryLoader; +import org.bukkit.Color; +import org.bukkit.map.MapPalette; import java.io.File; +import java.lang.reflect.Field; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.stream.IntStream; +import java.util.stream.LongStream; public class Test { public static void main(String[] args) { @@ -20,18 +26,47 @@ public class Test { System.out.println(bb.get(0)); //19 AYY //TO!DO: Use setSource, we don't want to wrap the native array*/ //final int[] a={5,6,8,2,3,10,5,26,5,416,41,85,416,41}; //final int[] b={10,80,10,3,32,20,56,85,51,968,156,5894,10,60,52}; - final int[] a={5,6}; - final int[] b={10,80}; + //final int[] a={5,6}; + //final int[] b={10,80}; + long t=System.nanoTime(); + int[] a= IntStream.range(0, 640*480).toArray(); final int[] res=new int[a.length]; Kernel kernel=new Kernel() { @Override public void run() { int i=getGlobalId(); //System.out.println(i); - res[i]=a[i]+b[i]; + //res[i]=a[i]+b[i]; + Color c=Color.fromBGR((int)a[i]); + res[i]= MapPalette.matchColor(c.getRed(), c.getGreen(), c.getBlue()); } }; kernel.execute(Range.create(res.length)); - System.out.println(Arrays.toString(res)); + //System.out.println(Arrays.toString(res)); + System.out.println(a[10]); + System.out.println("OpenCL time: "+(System.nanoTime()-t)/1000000f+"ms"); + + t=System.nanoTime(); + for (int i = 0; i < res.length; i++) { + Color c=Color.fromBGR((int)a[i]); + res[i]= MapPalette.matchColor(c.getRed(), c.getGreen(), c.getBlue()); + } + System.out.println("For loop time: "+(System.nanoTime()-t)/1000000f+"ms"); + + t=System.nanoTime(); + System.arraycopy(a, 0, res, 0, res.length); + System.out.println("Sys time: "+(System.nanoTime()-t)/1000000f+"ms"); + + PXCLib pxc = LibraryLoader.create(PXCLib.class).search(new File("").getAbsolutePath()).load("pxc"); + ByteBuffer bb=ByteBuffer.allocateDirect(640*480); + try { + Field f=Buffer.class.getDeclaredField("address"); + f.setAccessible(true); + long addr= (long) f.get(bb); + pxc.setSource(addr, 640, 480, 5, 4); + pxc.updateAndGetMap(0, 0, 640, 480, null); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } } } diff --git a/libpxc/pxc.c b/libpxc/pxc.c index fc34bfe..44798d2 100644 --- a/libpxc/pxc.c +++ b/libpxc/pxc.c @@ -38,6 +38,18 @@ void setSource(addr address, short w, short h, short mcx, short mcy) { image=(void*)address; maps=malloc(MAPSIZE*MAPSIZE*mcx*mcy); //1 byte per pixel width=w, height=h, mapcx=mcx, mapcy=mcy; + printf("PXC: w=%d h=%d mcx=%d mcy=%d\n", w, h, mcx, mcy); +} + +NativeColor* getNativeColor(int x, int y) { + if(x<0||x>width||y<0||y>height) return NULL; + return ((NativeColor*)image)+x+y*width; +} + +char* getMapColor(int x, int y) { + if(x<0||x>width||y<0||y>height) return NULL; + int mc=x/MAPSIZE+y/MAPSIZE*mapcx; + return maps+mc*MAPSIZE*MAPSIZE; //TODO: Use } //May return 0 @@ -45,16 +57,25 @@ void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: S if(image==NULL || maps==NULL) return 0; char* mapp = maps; NativeColor* imgp = image; + printf("PXC: mapp=%p imgp=%p\n", mapp, imgp); for(int k=0; k MAPSIZE * MAPSIZE * mapcx * mapcy || @@ -64,6 +85,7 @@ void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: S } } } + printf("PXC: ret maps=%p\n", maps); return maps; //TODO: Return changed only } diff --git a/libpxc/pxct.c b/libpxc/pxct.c index a23e5b3..d8e1243 100644 --- a/libpxc/pxct.c +++ b/libpxc/pxct.c @@ -1,4 +1,5 @@ #include +#include /* https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html gcc -Wall -c -fpic pxc.c @@ -8,8 +9,17 @@ gcc -Wall pxct.c -L. -lpxc -Wl,-rpath=. int convert(int px[], int *out[]); +typedef long long int addr; +void setSource(addr address, short w, short h, short mcx, short mcy); +void* updateAndGetMap(int x, int y, int w, int h, int** out_changed); + int main() { - convert(NULL, NULL); + printf("Setting source..."); + void* p = malloc(640*480*4); + setSource((addr)p, 640, 480, 5, 4); + printf("Updating map..."); + void* x=updateAndGetMap(0, 0, 640, 480, NULL); + free(p); return 0; }