1 package net.bmahe.genetics4j.gpu.opencl; 2 3 import org.apache.commons.lang3.StringUtils; 4 import org.apache.commons.lang3.Validate; 5 import org.apache.logging.log4j.LogManager; 6 import org.apache.logging.log4j.Logger; 7 import org.jocl.CL; 8 import org.jocl.cl_platform_id; 9 10 import net.bmahe.genetics4j.gpu.opencl.model.Platform; 11 import net.bmahe.genetics4j.gpu.opencl.model.PlatformProfile; 12 13 public class PlatformReader { 14 public static final Logger logger = LogManager.getLogger(PlatformReader.class); 15 16 public static final String PROFILE_FULL_STR = "FULL_PROFILE"; 17 public static final String PROFILE_EMBEDDED = "EMBEDDED_PROFILE"; 18 19 public Platform read(final cl_platform_id platformId) { 20 Validate.notNull(platformId); 21 22 final var platformBuilder = Platform.builder(); 23 24 platformBuilder.platformId(platformId); 25 26 final String platformName = PlatformUtils.getStringParameter(platformId, CL.CL_PLATFORM_NAME); 27 platformBuilder.name(platformName); 28 29 final String platformVendor = PlatformUtils.getStringParameter(platformId, CL.CL_PLATFORM_VENDOR); 30 platformBuilder.vendor(platformVendor); 31 32 final String platformVersion = PlatformUtils.getStringParameter(platformId, CL.CL_PLATFORM_VERSION); 33 platformBuilder.version(platformVersion); 34 35 final String platformExtensions = PlatformUtils.getStringParameter(platformId, CL.CL_PLATFORM_EXTENSIONS); 36 if (StringUtils.isNotBlank(platformExtensions)) { 37 final String[] platformExtensionsArr = platformExtensions.split(StringUtils.SPACE); 38 if (platformExtensionsArr.length > 0) { 39 platformBuilder.addExtensions(platformExtensionsArr); 40 } 41 } 42 43 final String platformProfileStr = PlatformUtils.getStringParameter(platformId, CL.CL_PLATFORM_PROFILE); 44 final var platformProfile = switch (platformProfileStr) { 45 case PROFILE_FULL_STR -> PlatformProfile.FULL_PROFILE; 46 case PROFILE_EMBEDDED -> PlatformProfile.FULL_PROFILE; 47 default -> throw new IllegalArgumentException("Unexpected value: " + platformProfileStr); 48 }; 49 platformBuilder.profile(platformProfile); 50 51 final int numDevices = PlatformUtils.numDevices(platformId); 52 platformBuilder.numDevices(numDevices); 53 54 return platformBuilder.build(); 55 } 56 }