From 77fae67d8d448018df83dd00b2d40adc7614bee1 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 27 Feb 2011 03:25:57 +0100 Subject: CachedBufferFactory optimization in CLCommandQueue and CLProgram.create(binaries). --- src/com/jogamp/opencl/CLCommandQueue.java | 15 +++++++++------ src/com/jogamp/opencl/CLProgram.java | 26 ++++++++++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src/com/jogamp/opencl') diff --git a/src/com/jogamp/opencl/CLCommandQueue.java b/src/com/jogamp/opencl/CLCommandQueue.java index 0fc7d383..d24fb115 100644 --- a/src/com/jogamp/opencl/CLCommandQueue.java +++ b/src/com/jogamp/opencl/CLCommandQueue.java @@ -28,7 +28,7 @@ package com.jogamp.opencl; -import com.jogamp.common.nio.Buffers; +import com.jogamp.common.nio.CachedBufferFactory; import com.jogamp.opencl.gl.CLGLI; import com.jogamp.common.nio.PointerBuffer; import java.nio.ByteBuffer; @@ -74,11 +74,14 @@ public class CLCommandQueue extends CLObject implements CLResource { this.device = device; this.properties = properties; - this.ibA = PointerBuffer.allocateDirect(3); - this.ibB = PointerBuffer.allocateDirect(3); - this.ibC = PointerBuffer.allocateDirect(3); - - this.pbA = Buffers.newDirectIntBuffer(1); + int pbsize = PointerBuffer.elementSize(); + CachedBufferFactory factory = CachedBufferFactory.create(9*pbsize + 4, true); + + this.ibA = PointerBuffer.wrap(factory.newDirectByteBuffer(3*pbsize)); + this.ibB = PointerBuffer.wrap(factory.newDirectByteBuffer(3*pbsize)); + this.ibC = PointerBuffer.wrap(factory.newDirectByteBuffer(3*pbsize)); + + this.pbA = factory.newDirectIntBuffer(1); } diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index 544df117..bbae75d9 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.common.nio.CachedBufferFactory; import com.jogamp.opencl.util.CLProgramConfiguration; import com.jogamp.opencl.util.CLUtil; import com.jogamp.common.os.Platform; @@ -40,6 +41,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.Map.Entry; import java.util.Set; import java.util.Map; import java.util.concurrent.locks.ReentrantLock; @@ -91,12 +93,24 @@ public class CLProgram extends CLObject implements CLResource { static CLProgram create(CLContext context, Map binaries) { - PointerBuffer devices = PointerBuffer.allocateDirect(binaries.size()); - PointerBuffer codeBuffers = PointerBuffer.allocateDirect(binaries.size()); - PointerBuffer lengths = PointerBuffer.allocateDirect(binaries.size()); + Set> entries = binaries.entrySet(); + + // calculate buffer size + int binarySize = 0; + for (Map.Entry entry : entries) { + binarySize += entry.getValue().length; + } + int pbSize = PointerBuffer.elementSize(); + int deviceCount = binaries.size(); + + CachedBufferFactory bf = CachedBufferFactory.create(binarySize + pbSize*deviceCount*3 + 4, true); + PointerBuffer devices = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); + PointerBuffer codeBuffers = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); + PointerBuffer lengths = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); + int i = 0; - for (Map.Entry entry : binaries.entrySet()) { + for (Map.Entry entry : entries) { byte[] bytes = entry.getValue(); CLDevice device = entry.getKey(); @@ -104,13 +118,13 @@ public class CLProgram extends CLObject implements CLResource { devices.put(device.ID); lengths.put(bytes.length); - codeBuffers.referenceBuffer(i, newDirectByteBuffer(bytes)); + codeBuffers.referenceBuffer(i, bf.newDirectByteBuffer(bytes)); i++; } devices.rewind(); lengths.rewind(); - IntBuffer errBuffer = newDirectIntBuffer(1); + IntBuffer errBuffer = bf.newDirectIntBuffer(1); // IntBuffer status = newDirectByteBuffer(binaries.size()*4).asIntBuffer(); long id = context.cl.clCreateProgramWithBinary(context.ID, devices.capacity(), devices, lengths, codeBuffers, /*status*/null, errBuffer); -- cgit v1.2.3