aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/util/CLUtil.java
blob: 98a6cd7e2a9a5c773a975f4416acddca0094b9fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2009 - 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. 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.
 * 
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community OR
 * 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.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */

package com.jogamp.opencl.util;

import com.jogamp.common.JogampRuntimeException;
import com.jogamp.opencl.llb.CL;
import com.jogamp.opencl.CLDevice;
import com.jogamp.opencl.CLPlatform;
import com.jogamp.opencl.CLProperty;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Michael Bien
 */
public class CLUtil {

    public static String clString2JavaString(byte[] chars, int clLength) {

        // certain char queries on windows always claim to have a fixed length
        // e.g. (clDeviceInfo(CL_DEVICE_NAME) is always 64.. but luckily they are 0 terminated)
        while(clLength > 0 && chars[--clLength] == 0);

        return clLength==0 ? "" : new String(chars, 0, clLength+1);
    }

    public static String clString2JavaString(ByteBuffer chars, int clLength) {
        if (clLength==0) {
            return "";
        }else{
            byte[] array = new byte[clLength];
            chars.get(array).rewind();
            return clString2JavaString(array, clLength);
        }
    }

    /**
     * Returns true if clBoolean == CL.CL_TRUE.
     */
    public static boolean clBoolean(int clBoolean) {
        return clBoolean == CL.CL_TRUE;
    }

    /**
     * Returns b ? CL.CL_TRUE : CL.CL_FALSE
     */
    public static int clBoolean(boolean b) {
        return b ? CL.CL_TRUE : CL.CL_FALSE;
    }

    /**
     * Reads all platform properties and returns them as key-value map.
     */
    public static Map<String, String> obtainPlatformProperties(CLPlatform platform) {
        return readCLProperties(platform);
    }

    /**
     * Reads all device properties and returns them as key-value map.
     */
    public static Map<String, String> obtainDeviceProperties(CLDevice dev) {
        return readCLProperties(dev);
    }

    private static Map<String, String> readCLProperties(Object obj) {
        try {
            return invoke(listMethods(obj.getClass()), obj);
        } catch (IllegalArgumentException ex) {
            throw new JogampRuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new JogampRuntimeException(ex);
        }
    }

    static Map<String, String> invoke(List<Method> methods, Object obj) throws IllegalArgumentException, IllegalAccessException {
        Map<String, String> map = new LinkedHashMap<String, String>();
        for (Method method : methods) {
            Object info = null;
            try {
                info = method.invoke(obj);
            } catch (InvocationTargetException ex) {
                info = ex.getTargetException();
            }

            if(info.getClass().isArray()) {
                info = asList(info);
            }

            String value = method.getAnnotation(CLProperty.class).value();
            map.put(value, info.toString());
        }
        return map;
    }

    static List<Method> listMethods(Class<?> clazz) throws SecurityException {
        List<Method> list = new ArrayList<Method>();
        for (Method method : clazz.getDeclaredMethods()) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof CLProperty) {
                    list.add(method);
                }
            }
        }
        return list;
    }

    private static List<Number> asList(Object info) {
        List<Number> list = new ArrayList<Number>();
        if(info instanceof int[]) {
            int[] array = (int[]) info;
            for (int i : array) {
                list.add(i);
            }
        }else if(info instanceof long[]) {
            long[] array = (long[]) info;
            for (long i : array) {
                list.add(i);
            }
        }else if(info instanceof float[]) {
            float[] array = (float[]) info;
            for (float i : array) {
                list.add(i);
            }
        }else if(info instanceof double[]) {
            double[] array = (double[]) info;
            for (double i : array) {
                list.add(i);
            }
        }
        return list;
    }

}