1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 #include "opencl_backend.h"
 27 
 28 template<typename T>
 29 static T info(cl_device_id device_id, cl_device_info device_info){
 30     T v;
 31     cl_int status = clGetDeviceInfo(device_id, device_info, sizeof(T), &v, nullptr);
 32     return v;
 33 }
 34 
 35 static char *strInfo(cl_device_id device_id, cl_device_info device_info){
 36     size_t sz;
 37     cl_int  status = clGetDeviceInfo(device_id, device_info, 0, nullptr,  &sz);
 38     char *ptr = new char[sz+1];
 39     status = clGetDeviceInfo(device_id, device_info, sz, ptr,nullptr);
 40     return ptr;
 41 }
 42 
 43 static char *strInfo(cl_platform_id platform_id, cl_platform_info platform_info){
 44     size_t sz;
 45     cl_int  status = clGetPlatformInfo(platform_id, platform_info, 0, nullptr,  &sz);
 46     char *ptr = new char[sz+1];
 47     status = clGetPlatformInfo(platform_id, platform_info, sz, ptr,nullptr);
 48     return ptr;
 49 }
 50 
 51 PlatformInfo::DeviceInfo::DeviceInfo(OpenCLBackend *openclBackend):
 52     openclBackend(openclBackend),
 53     maxComputeUnits(info<cl_int>(openclBackend->device_id,CL_DEVICE_MAX_COMPUTE_UNITS)),
 54     maxWorkItemDimensions(info<cl_int>(openclBackend->device_id,CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS)),
 55     maxWorkGroupSize(info<size_t>(openclBackend->device_id,CL_DEVICE_MAX_WORK_GROUP_SIZE)),
 56     maxWorkItemSizes( new size_t[maxWorkItemDimensions]),
 57     maxMemAllocSize(info<cl_ulong>(openclBackend->device_id, CL_DEVICE_MAX_MEM_ALLOC_SIZE)),
 58     globalMemSize(info<cl_ulong>(openclBackend->device_id,CL_DEVICE_GLOBAL_MEM_SIZE)),
 59     localMemSize(info<cl_ulong>(openclBackend->device_id,CL_DEVICE_LOCAL_MEM_SIZE)),
 60     profile(strInfo(openclBackend->device_id,CL_DEVICE_PROFILE)),
 61     deviceVersion(strInfo(openclBackend->device_id, CL_DEVICE_VERSION)),
 62     driverVersion(strInfo(openclBackend->device_id, CL_DRIVER_VERSION)),
 63     cVersion(strInfo(openclBackend->device_id, CL_DEVICE_OPENCL_C_VERSION)),
 64     name(strInfo(openclBackend->device_id, CL_DEVICE_NAME)),
 65     extensions(strInfo(openclBackend->device_id, CL_DEVICE_EXTENSIONS)),
 66     builtInKernels(strInfo(openclBackend->device_id,CL_DEVICE_BUILT_IN_KERNELS)){
 67 
 68     clGetDeviceInfo(openclBackend->device_id, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t) * maxWorkItemDimensions, maxWorkItemSizes, NULL);
 69     clGetDeviceInfo(openclBackend->device_id, CL_DEVICE_TYPE, sizeof(deviceType), &deviceType, NULL);
 70     char buf[512];
 71     buf[0]='\0';
 72     if (CL_DEVICE_TYPE_CPU == (deviceType & CL_DEVICE_TYPE_CPU)) {
 73        std::strcat(buf, "CPU ");
 74     }
 75     if (CL_DEVICE_TYPE_GPU == (deviceType & CL_DEVICE_TYPE_GPU)) {
 76        std::strcat(buf, "GPU ");
 77     }
 78     if (CL_DEVICE_TYPE_ACCELERATOR == (deviceType & CL_DEVICE_TYPE_ACCELERATOR)) {
 79        std::strcat(buf, "ACC ");
 80     }
 81     deviceTypeStr = new char[std::strlen(buf)];
 82     std::strcpy(deviceTypeStr, buf);
 83 }
 84 
 85 PlatformInfo::DeviceInfo::~DeviceInfo(){
 86     delete [] deviceTypeStr;
 87     delete [] profile;
 88     delete [] deviceVersion;
 89     delete [] driverVersion;
 90     delete [] cVersion;
 91     delete [] name;
 92     delete [] extensions;
 93     delete [] builtInKernels;
 94     delete [] maxWorkItemSizes;
 95 }
 96 
 97 PlatformInfo::PlatformInfo(OpenCLBackend *openclBackend):
 98     openclBackend(openclBackend),
 99     versionName(strInfo(openclBackend->platform_id, CL_PLATFORM_VERSION)),
100     vendorName(strInfo(openclBackend->platform_id, CL_PLATFORM_VENDOR)),
101     name(strInfo(openclBackend->platform_id, CL_PLATFORM_NAME)),
102     deviceInfo(openclBackend){
103 }
104 PlatformInfo::~PlatformInfo(){
105     delete [] versionName;
106     delete [] vendorName;
107     delete [] name;
108 }
109 
110 
111 void OpenCLBackend::info() {
112     PlatformInfo platformInfo(this);
113     cl_int status;
114     std::cerr << "platform{" <<std::endl;
115     std::cerr << "   CL_PLATFORM_VENDOR..\"" << platformInfo.vendorName <<"\""<<std::endl;
116     std::cerr << "   CL_PLATFORM_VERSION.\"" << platformInfo.versionName <<"\""<<std::endl;
117     std::cerr << "   CL_PLATFORM_NAME....\"" << platformInfo.name <<"\""<<std::endl;
118     std::cerr << "         CL_DEVICE_TYPE..................... " <<  platformInfo.deviceInfo.deviceTypeStr << " "<<  platformInfo.deviceInfo.deviceType<<std::endl;
119     std::cerr << "         CL_DEVICE_MAX_COMPUTE_UNITS........ " <<  platformInfo.deviceInfo.maxComputeUnits<<std::endl;
120     std::cerr << "         CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS. " <<  platformInfo.deviceInfo.maxWorkItemDimensions << " {";
121     for (unsigned dimIdx = 0; dimIdx <  platformInfo.deviceInfo.maxWorkItemDimensions; dimIdx++) {
122         std::cerr<<  platformInfo.deviceInfo.maxWorkItemSizes[dimIdx] << " ";
123     }
124     std::cerr<< "}"<<std::endl;
125     std::cerr <<  "         CL_DEVICE_MAX_WORK_GROUP_SIZE...... "<<  platformInfo.deviceInfo.maxWorkGroupSize<<std::endl;
126     std::cerr <<  "         CL_DEVICE_MAX_MEM_ALLOC_SIZE....... "<<  platformInfo.deviceInfo.maxMemAllocSize<<std::endl;
127     std::cerr <<  "         CL_DEVICE_GLOBAL_MEM_SIZE.......... "<<  platformInfo.deviceInfo.globalMemSize<<std::endl;
128     std::cerr <<  "         CL_DEVICE_LOCAL_MEM_SIZE........... "<<  platformInfo.deviceInfo.localMemSize<<std::endl;
129     std::cerr <<  "         CL_DEVICE_PROFILE.................. "<<  platformInfo.deviceInfo.profile<<std::endl;
130     std::cerr <<  "         CL_DEVICE_VERSION.................. "<<  platformInfo.deviceInfo.deviceVersion<<std::endl;
131     std::cerr <<  "         CL_DRIVER_VERSION.................. "<<  platformInfo.deviceInfo.driverVersion<<std::endl;
132     std::cerr <<  "         CL_DEVICE_OPENCL_C_VERSION......... "<<  platformInfo.deviceInfo.cVersion<<std::endl;
133     std::cerr <<  "         CL_DEVICE_NAME..................... "<<  platformInfo.deviceInfo.name<<std::endl;
134     std::cerr <<  "         CL_DEVICE_EXTENSIONS............... "<<  platformInfo.deviceInfo.extensions<<std::endl;
135     std::cerr <<  "         CL_DEVICE_BUILT_IN_KERNELS......... "<<  platformInfo.deviceInfo.builtInKernels<<std::endl;
136     std::cerr <<  "}"<<std::endl;
137 }
138 
139 int OpenCLBackend::getMaxComputeUnits() {
140     PlatformInfo platformInfo(this);
141     return platformInfo.deviceInfo.maxComputeUnits;
142 }
143