1 package net.bmahe.genetics4j.gpu.opencl.model;
2
3 import java.util.Set;
4
5 import org.immutables.value.Value;
6 import org.jocl.cl_platform_id;
7
8 /**
9 * Represents an OpenCL platform providing access to compute devices and their capabilities.
10 *
11 * <p>Platform encapsulates the information about an OpenCL platform, which is a vendor-specific implementation of the
12 * OpenCL specification. Each platform represents a collection of OpenCL devices (CPUs, GPUs, accelerators) provided by
13 * a single vendor with consistent runtime behavior.
14 *
15 * <p>Key platform characteristics include:
16 * <ul>
17 * <li><strong>Vendor identification</strong>: Platform vendor (AMD, NVIDIA, Intel, etc.)</li>
18 * <li><strong>Capability profile</strong>: FULL_PROFILE or EMBEDDED_PROFILE feature support</li>
19 * <li><strong>Version support</strong>: OpenCL specification version implemented</li>
20 * <li><strong>Extension support</strong>: Additional features beyond the core specification</li>
21 * <li><strong>Device count</strong>: Number of compute devices available on this platform</li>
22 * </ul>
23 *
24 * <p>Platform selection considerations:
25 * <ul>
26 * <li><strong>Vendor optimization</strong>: Different vendors may optimize for different workloads</li>
27 * <li><strong>Feature requirements</strong>: Embedded profiles may lack required features</li>
28 * <li><strong>Version compatibility</strong>: Newer OpenCL versions provide additional features</li>
29 * <li><strong>Extension dependencies</strong>: Some algorithms may require specific extensions</li>
30 * <li><strong>Device availability</strong>: Platforms must have compatible devices</li>
31 * </ul>
32 *
33 * <p>Common filtering patterns:
34 *
35 * <pre>{@code
36 * // Select platforms with full OpenCL profile support
37 * Predicate<Platform> fullProfileFilter = platform -> platform.profile() == PlatformProfile.FULL_PROFILE;
38 *
39 * // Select platforms from specific vendors
40 * Predicate<Platform> vendorFilter = platform -> platform.vendor().toLowerCase().contains("nvidia")
41 * || platform.vendor().toLowerCase().contains("amd");
42 *
43 * // Select platforms with minimum OpenCL version
44 * Predicate<Platform> versionFilter = platform -> {
45 * String version = platform.version();
46 * return version.contains("2.") || version.contains("3.");
47 * };
48 *
49 * // Select platforms with required extensions
50 * Predicate<Platform> extensionFilter = platform -> platform.extensions().contains("cl_khr_fp64");
51 *
52 * // Combine filters for comprehensive platform selection
53 * Predicate<Platform> combinedFilter = fullProfileFilter.and(versionFilter).and(platform -> platform.numDevices() > 0);
54 * }</pre>
55 *
56 * <p>Platform discovery workflow:
57 * <ol>
58 * <li><strong>Enumeration</strong>: System discovers all available OpenCL platforms</li>
59 * <li><strong>Information query</strong>: Platform properties are read from OpenCL runtime</li>
60 * <li><strong>Model creation</strong>: Platform objects are created with discovered information</li>
61 * <li><strong>Filtering</strong>: User-defined predicates select appropriate platforms</li>
62 * <li><strong>Device discovery</strong>: Selected platforms are queried for available devices</li>
63 * </ol>
64 *
65 * <p>Performance implications:
66 * <ul>
67 * <li><strong>Driver optimization</strong>: Platform vendors optimize for their hardware</li>
68 * <li><strong>Memory models</strong>: Different platforms may have different memory hierarchies</li>
69 * <li><strong>Kernel compilation</strong>: Platform-specific optimizations during compilation</li>
70 * <li><strong>Runtime behavior</strong>: Platform-specific scheduling and resource management</li>
71 * </ul>
72 *
73 * <p>Error handling considerations:
74 * <ul>
75 * <li><strong>Platform availability</strong>: Platforms may become unavailable at runtime</li>
76 * <li><strong>Version compatibility</strong>: Kernels may require specific OpenCL versions</li>
77 * <li><strong>Extension support</strong>: Missing extensions may cause compilation failures</li>
78 * <li><strong>Device enumeration</strong>: Platform may have no compatible devices</li>
79 * </ul>
80 *
81 * @see Device
82 * @see PlatformProfile
83 * @see net.bmahe.genetics4j.gpu.spec.GPUEAExecutionContext#platformFilters()
84 * @see net.bmahe.genetics4j.gpu.opencl.PlatformUtils
85 */
86 @Value.Immutable
87 public interface Platform {
88
89 /**
90 * Returns the native OpenCL platform identifier.
91 *
92 * @return the OpenCL platform ID for low-level operations
93 */
94 cl_platform_id platformId();
95
96 /**
97 * Returns the OpenCL profile supported by this platform.
98 *
99 * @return the platform profile (FULL_PROFILE or EMBEDDED_PROFILE)
100 */
101 PlatformProfile profile();
102
103 /**
104 * Returns the OpenCL version string supported by this platform.
105 *
106 * @return the OpenCL version (e.g., "OpenCL 2.1")
107 */
108 String version();
109
110 /**
111 * Returns the platform name provided by the vendor.
112 *
113 * @return the human-readable platform name
114 */
115 String name();
116
117 /**
118 * Returns the platform vendor name.
119 *
120 * @return the vendor name (e.g., "NVIDIA Corporation", "AMD")
121 */
122 String vendor();
123
124 /**
125 * Returns the set of OpenCL extensions supported by this platform.
126 *
127 * @return set of extension names (e.g., "cl_khr_fp64", "cl_khr_global_int32_base_atomics")
128 */
129 Set<String> extensions();
130
131 /**
132 * Returns the number of OpenCL devices available on this platform.
133 *
134 * @return the count of devices that can be used for computation
135 */
136 int numDevices();
137
138 /**
139 * Creates a new builder for constructing Platform instances.
140 *
141 * @return a new builder for creating platform objects
142 */
143 static ImmutablePlatform.Builder builder() {
144 return ImmutablePlatform.builder();
145 }
146 }