/** * Get the binaries of the program (one for each device, in order) * * @return map from each device the program was compiled for to the corresponding binary data */ public Map<CLDevice, byte[]> getBinaries() throws CLBuildException { synchronized (this) { if (!built) build(); } Memory s = infos.getMemory(getEntity(), CL_PROGRAM_BINARY_SIZES); int n = (int) s.getSize() / Native.SIZE_T_SIZE; NativeSize[] sizes = readNSArray(s, n); // int[] sizes = new int[n]; // for (int i = 0; i < n; i++) { // sizes[i] = s.getNativeLong(i * Native.LONG_SIZE).intValue(); // } Memory[] binMems = new Memory[n]; Memory ptrs = new Memory(n * Native.POINTER_SIZE); for (int i = 0; i < n; i++) { ptrs.setPointer(i * Native.POINTER_SIZE, binMems[i] = new Memory(sizes[i].intValue())); } error( infos.getInfo( getEntity(), CL_PROGRAM_BINARIES, toNS(ptrs.getSize() * Native.POINTER_SIZE), ptrs, null)); Map<CLDevice, byte[]> ret = new HashMap<CLDevice, byte[]>(devices.length); int iBin = n == devices.length + 1 ? 1 : 0; for (int i = 0; i < n; i++) { CLDevice device = devices[i]; Memory bytes = binMems[iBin + i]; if (bytes != null) ret.put(device, bytes.getByteArray(0, sizes[iBin + i].intValue())); } return ret; }
protected void setBinaries(Map<CLDevice, byte[]> binaries) { if (this.devices == null) { this.devices = new CLDevice[binaries.size()]; int iDevice = 0; for (CLDevice device : binaries.keySet()) this.devices[iDevice++] = device; } int nDevices = this.devices.length; NativeSize[] lengths = new NativeSize[nDevices]; cl_device_id[] deviceIds = new cl_device_id[nDevices]; Memory binariesArray = new Memory(Pointer.SIZE * nDevices); Memory[] binariesMems = new Memory[nDevices]; for (int iDevice = 0; iDevice < nDevices; iDevice++) { CLDevice device = devices[iDevice]; byte[] binary = binaries.get(device); Memory binaryMem = binariesMems[iDevice] = new Memory(binary.length); binaryMem.write(0, binary, 0, binary.length); binariesArray.setPointer(iDevice * Pointer.SIZE, binaryMem); lengths[iDevice] = toNS(binary.length); deviceIds[iDevice] = device.getEntity(); } PointerByReference binariesPtr = new PointerByReference(); binariesPtr.setPointer(binariesArray); IntBuffer errBuff = NIOUtils.directInts(1, ByteOrder.nativeOrder()); int previousAttempts = 0; IntBuffer statuses = NIOUtils.directInts(nDevices, ByteOrder.nativeOrder()); do { entity = CL.clCreateProgramWithBinary( context.getEntity(), nDevices, deviceIds, lengths, binariesPtr, statuses, errBuff); } while (failedForLackOfMemory(errBuff.get(0), previousAttempts++)); }