Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JavaCL - Java API and utilities for OpenCL
* http://javacl.googlecode.com/
*
* Copyright (c) 2009-2011, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.nativelibs4java.opencl;
import com.ochafik.util.listenable.Pair;
import static com.nativelibs4java.opencl.CLException.error;
import static com.nativelibs4java.opencl.CLException.errorString;
import static com.nativelibs4java.opencl.CLException.failedForLackOfMemory;
import static com.nativelibs4java.opencl.JavaCL.CL;
import static com.nativelibs4java.opencl.JavaCL.log;
import java.util.logging.Level;
import static com.nativelibs4java.opencl.library.OpenCLLibrary.CL_PROGRAM_BINARIES;
import static com.nativelibs4java.opencl.library.OpenCLLibrary.CL_PROGRAM_BINARY_SIZES;
import static com.nativelibs4java.opencl.library.OpenCLLibrary.CL_PROGRAM_BUILD_LOG;
import static com.nativelibs4java.opencl.library.OpenCLLibrary.CL_PROGRAM_SOURCE;
import static com.nativelibs4java.opencl.library.OpenCLLibrary.CL_SUCCESS;
import static com.nativelibs4java.util.JNAUtils.readNSArray;
import static com.nativelibs4java.util.JNAUtils.toNS;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.net.URL;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import com.nativelibs4java.opencl.library.OpenCLLibrary.cl_device_id;
import com.nativelibs4java.opencl.library.OpenCLLibrary.cl_kernel;
import com.nativelibs4java.opencl.library.OpenCLLibrary.cl_program;
import com.nativelibs4java.util.NIOUtils;
import com.ochafik.io.IOUtils;
import com.ochafik.io.ReadText;
import com.ochafik.lang.jnaerator.runtime.NativeSize;
import com.ochafik.lang.jnaerator.runtime.NativeSizeByReference;
import com.ochafik.util.string.RegexUtils;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Process;
import java.net.URL;
import java.util.Collection;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
/**
* OpenCL program.
* An OpenCL program consists of a set of kernels that are identified as functions declared with the __kernel qualifier in the program source. OpenCL programs may also contain auxiliary functions and constant data that can be used by __kernel functions. The program executable can be generated online or offline by the OpenCL compiler for the appropriate target device(s).
* A program object encapsulates the following information:
*
*
An associated context.
*
A program source or binary.
*
The latest successfully built program executable
*
The list of devices for which the program executable is built
*
The build options used and a build log.
*
The number of kernel objects currently attached.
*
*
* A program can be compiled on the fly (costly) but its binaries can be stored and
* loaded back in subsequent executions to avoid recompilation.
* By default, program binaries are automatically cached on stable platforms (which currently exclude ATI Stream), but the caching can be forced on/off with * {@link CLContext#setCached(boolean) }.
* To create a program from sources, please use {@link CLContext#createProgram(java.lang.String[]) }
* @author Olivier Chafik
*/
public class CLProgram extends CLAbstractEntity {
protected final CLContext context;
private static CLInfoGetter infos = new CLInfoGetter() {
@Override
protected int getInfo(cl_program entity, int infoTypeEnum, NativeSize size, Pointer out, NativeSizeByReference sizeOut) {
return CL.clGetProgramInfo(entity, infoTypeEnum, size, out, sizeOut);
}
};
CLDevice[] devices;
CLProgram(CLContext context, CLDevice... devices) {
super(null, true);
this.context = context;
this.devices = devices == null || devices.length == 0 ? context.getDevices() : devices;
}
CLProgram(CLContext context, Map binaries, String source) {
super(null, true);
this.context = context;
this.source = source;
setBinaries(binaries);
}
protected void setBinaries(Map 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++));
}
/**
* Write the compiled binaries of this program (for all devices it was compiled for), so that it can be restored later using {@link CLContext#loadProgram(java.io.InputStream) }
* @param out will be closed
* @throws CLBuildException
* @throws IOException
*/
public void store(OutputStream out) throws CLBuildException, IOException {
writeBinaries(getBinaries(), getSource(), null, out);
}
private static final void addStoredEntry(ZipOutputStream zout, String name, byte[] data) throws IOException {
ZipEntry ze = new ZipEntry(name);
ze.setMethod(ZipEntry.STORED);
ze.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data,0,data.length);
ze.setCrc(crc.getValue());
zout.putNextEntry(ze);
zout.write(data);
zout.closeEntry();
}
private static final String BinariesSignatureZipEntryName = "SIGNATURE", SourceZipEntryName = "SOURCE", textEncoding = "utf-8";
public static void writeBinaries(Map binaries, String source, String contentSignatureString, OutputStream out) throws IOException {
Map binaryBySignature = new HashMap();
for (Map.Entry e : binaries.entrySet())
binaryBySignature.put(e.getKey().createSignature(), e.getValue()); // Maybe multiple devices will have the same signature : too bad, we don't care and just write one binary per signature.
ZipOutputStream zout = new ZipOutputStream(new GZIPOutputStream(new BufferedOutputStream(out)));
if (contentSignatureString != null)
addStoredEntry(zout, BinariesSignatureZipEntryName, contentSignatureString.getBytes(textEncoding));
if (source != null)
addStoredEntry(zout, SourceZipEntryName, source.getBytes(textEncoding));
for (Map.Entry e : binaryBySignature.entrySet())
addStoredEntry(zout, e.getKey(), e.getValue());
zout.close();
}
public static Pair