001/*
002 * JavaCL - Java API and utilities for OpenCL
003 * http://javacl.googlecode.com/
004 *
005 * Copyright (c) 2009-2013, Olivier Chafik (http://ochafik.com/)
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions are met:
010 * 
011 *     * Redistributions of source code must retain the above copyright
012 *       notice, this list of conditions and the following disclaimer.
013 *     * Redistributions in binary form must reproduce the above copyright
014 *       notice, this list of conditions and the following disclaimer in the
015 *       documentation and/or other materials provided with the distribution.
016 *     * Neither the name of Olivier Chafik nor the
017 *       names of its contributors may be used to endorse or promote products
018 *       derived from this software without specific prior written permission.
019 * 
020 * THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
021 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
023 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
024 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
027 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 */
031        
032
033
034
035
036        
037
038        
039        
040
041
042
043        
044
045        
046        
047        
048
049        
050package com.nativelibs4java.opencl;
051
052import com.nativelibs4java.util.IOUtils;
053import java.io.FileNotFoundException;
054import java.io.IOException;
055import java.io.InputStream;
056import java.util.Map;
057import java.lang.reflect.Array;
058
059/*
060 * To change this template, choose Tools | Templates
061 * and open the template in the editor.
062 */
063/**
064 *
065 * @author ochafik
066 */
067public abstract class CLAbstractUserProgram {
068
069    final CLProgram program;
070    String rawSource;
071    Map<String, String> velocityArgs;
072
073    volatile boolean addedSources;
074    
075        protected Long getSourceChecksum() {
076        return null;
077    }
078
079        public CLProgram getProgram() {
080                return program;
081        }
082
083    protected void checkArrayLength(Object arr, int length, String argName) {
084        if (arr == null)
085            throw new IllegalArgumentException("Argument " + argName + " cannot be null. Expected array of size " + length);
086
087        int len = Array.getLength(arr);
088        if (len != length)
089            throw new IllegalArgumentException("Argument " + argName + " must be an array of size " + length + ", but given array of size " + len);
090    }
091    protected static String readRawSourceForClass(Class<?> c) throws IOException {
092        String simpleName = c.getSimpleName();
093
094                InputStream srcIn = c.getResourceAsStream(simpleName + ".cl");
095        try {
096            if (srcIn == null)
097                srcIn = c.getResourceAsStream(simpleName + ".c");
098
099            if (srcIn == null)
100                throw new FileNotFoundException("OpenCL source code not found : '" + simpleName + "'");
101
102            return IOUtils.readText(srcIn);
103        } finally {
104            if (srcIn != null)
105                srcIn.close();
106        }
107    }
108    protected CLAbstractUserProgram(CLProgram program, String rawSource) {
109        this.program = program;
110        this.rawSource = rawSource;
111    }
112
113    protected CLAbstractUserProgram(CLContext context, String rawSource) {
114        this(context.createProgram(), rawSource);
115    }
116
117    protected synchronized void addSources() {
118        if (addedSources) {
119            return;
120        }
121
122        String src;
123        if (velocityArgs != null) {
124            src = rawSource;//veloTransform(rawSource, velocityArgs);
125        } else {
126            src = rawSource;
127        }
128
129        program.addSource(src);
130        addedSources = true;
131    }
132
133    protected void defineMacro(String name, String value) {
134        if (value == null) {
135            program.undefineMacro(name);
136        } else {
137            program.defineMacro(name, value);
138        }
139    }
140
141    protected synchronized CLKernel createKernel(String name) throws CLBuildException {
142        addSources();
143        return program.createKernel(name);
144    }
145}