summaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/demos/info/CLInfo.java
blob: 0975f7bb2d513fa32ae59b1e0d5d2faf3dba545a (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
/*
 * Created on Tuesday, September 07 2010 21:33
 */

package com.jogamp.opencl.demos.info;

import com.jogamp.common.JogampRuntimeException;
import com.jogamp.opencl.CLDevice;
import com.jogamp.opencl.CLPlatform;
import com.jogamp.opencl.util.ExceptionReporter;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

/**
 * Displays OpenCL information in a table.
 * @author Michael Bien
 */
public class CLInfo {

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(CLInfo.class.getName()).log(Level.INFO, null, ex);
        }

        try{
            CLPlatform.initialize();
        }catch(JogampRuntimeException ex) {
            Logger.getLogger(CLInfo.class.getName()).log(Level.SEVERE, null, ex);
            ExceptionReporter.appear("I tried hard but I really can't initialize JOCL. Is OpenCL properly set up?", ex);
            return;
        }

        JFrame frame = new JFrame("OpenCL Info");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Container contentPane = frame.getContentPane();

        JEditorPane area = new JEditorPane();
        area.setContentType("text/html");
        area.setEditable(false);

        contentPane.add(new JScrollPane(area));

        String html = createOpenCLInfoHTML();

        area.setText(html.toString());

        frame.setSize(800, 600);
        frame.setVisible(true);

    }

    private static String createOpenCLInfoHTML() {

        StringBuilder html = new StringBuilder();

        html.append("<table border=\"1\">");
        CLPlatform[] platforms = CLPlatform.listCLPlatforms();

        // platforms
        List<Map<String, String>> platProps = new ArrayList<Map<String, String>>();
        List<Integer> spans = new ArrayList<Integer>();
        for (CLPlatform p : platforms) {
            platProps.add(p.getProperties());
            spans.add(p.listCLDevices().length);
        }
        fillTable(platProps, spans, html);

        // devices
        ArrayList<Map<String, String>> devProps = new ArrayList<Map<String, String>>();
        for (CLPlatform p : platforms) {
            CLDevice[] devices = p.listCLDevices();
            for (CLDevice d : devices) {
                devProps.add(d.getProperties());
            }
        }
        fillTable(devProps, html);
        html.append("</table>");

        return html.toString();
    }

    private static void fillTable(List<Map<String, String>> properties, StringBuilder sb) {
        ArrayList<Integer> spans = new ArrayList<Integer>(properties.size());
        for (int i = 0; i < properties.size(); i++) {
            spans.add(1);
        }
        fillTable(properties, spans, sb);
    }

    private static void fillTable(List<Map<String, String>> properties, List<Integer> spans, StringBuilder sb) {
        boolean header = true;
        for (String key : properties.get(0).keySet()) {
            sb.append("<tr>");
                cell(sb, key);
                int i = 0;
                for (Map<String, String> map : properties) {
                    cell(sb, spans.get(i), map.get(key), header);
                    i++;
                }
            sb.append("</tr>");
            header = false;
        }
    }

    private static void cell(StringBuilder sb, String value) {
        sb.append("<td>").append(value).append("</td>");
    }

    private static void cell(StringBuilder sb, int span, String value, boolean header) {
        if(header) {
            sb.append("<th colspan=\"").append(span).append("\">").append(value).append("</th>");
        }else{
            sb.append("<td colspan=\"").append(span).append("\">").append(value).append("</td>");
        }
    }
}