1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 * 7 */ 8 9 // 10 // 11 // 12 13 #include <string.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 // 18 // 19 // 20 21 #include "find_cl.h" 22 #include "assert_cl.h" 23 #include "../macros.h" 24 25 // 26 // search platforms and devices for a match 27 // 28 29 cl_int 30 clFindIdsByName(char const * const target_platform_substring, 31 char const * const target_device_substring, 32 cl_platform_id * const platform_id, 33 cl_device_id * const device_id, 34 size_t const matched_device_name_size, 35 char * const matched_device_name, 36 size_t * const matched_device_name_size_ret, 37 bool const is_verbose) 38 { 39 bool match_platform=false, match_device=false; 40 41 // 42 // get number of platforms 43 // 44 cl_uint platform_count; 45 46 cl(GetPlatformIDs(0,NULL,&platform_count)); 47 48 cl_platform_id * const platform_ids = ALLOCA_MACRO(sizeof(*platform_ids) * platform_count); 49 50 cl(GetPlatformIDs(platform_count,platform_ids,NULL)); 51 52 // 53 // search platforms 54 // 55 for (cl_uint ii=0; ii<platform_count; ii++) 56 { 57 size_t platform_name_size; 58 59 cl(GetPlatformInfo(platform_ids[ii], 60 CL_PLATFORM_NAME, 61 0, 62 NULL, 63 &platform_name_size)); 64 65 char * const platform_name = ALLOCA_MACRO(platform_name_size); 66 67 cl(GetPlatformInfo(platform_ids[ii], 68 CL_PLATFORM_NAME, 69 platform_name_size, 70 platform_name, 71 NULL)); 72 73 74 if (!match_platform && (strstr(platform_name,target_platform_substring) != NULL)) 75 { 76 match_platform = true; 77 *platform_id = platform_ids[ii]; 78 } 79 80 if (is_verbose) { 81 fprintf(stdout,"%2u: %s\n",ii,platform_name); 82 } 83 84 // 85 // find devices for current platform 86 // 87 cl_uint device_count; 88 cl_int cl_err; 89 90 cl_err = clGetDeviceIDs(platform_ids[ii], 91 CL_DEVICE_TYPE_ALL, 92 0, 93 NULL, 94 &device_count); 95 96 cl_device_id * const device_ids = ALLOCA_MACRO(sizeof(*device_ids) * device_count); 97 98 cl_err = clGetDeviceIDs(platform_ids[ii], 99 CL_DEVICE_TYPE_ALL, 100 device_count, 101 device_ids, 102 NULL); 103 104 if (cl_err != CL_DEVICE_NOT_FOUND) 105 cl_ok(cl_err); 106 107 for (cl_uint jj=0; jj<device_count; jj++) 108 { 109 size_t device_name_size; 110 size_t driver_version_size; 111 112 cl(GetDeviceInfo(device_ids[jj], 113 CL_DEVICE_NAME, 114 0, 115 NULL, 116 &device_name_size)); 117 118 cl(GetDeviceInfo(device_ids[jj], 119 CL_DRIVER_VERSION, 120 0, 121 NULL, 122 &driver_version_size)); 123 124 char * const device_name = ALLOCA_MACRO(device_name_size); 125 char * const driver_version = ALLOCA_MACRO(driver_version_size); 126 127 cl(GetDeviceInfo(device_ids[jj], 128 CL_DEVICE_NAME, 129 device_name_size, 130 device_name, 131 NULL)); 132 133 cl(GetDeviceInfo(device_ids[jj], 134 CL_DRIVER_VERSION, 135 driver_version_size, 136 driver_version, 137 NULL)); 138 139 if (!match_device && match_platform && (strstr(device_name,target_device_substring) != NULL)) 140 { 141 match_device = true; 142 *device_id = device_ids[jj]; 143 144 if (matched_device_name != NULL) 145 { 146 size_t bytes = 0; 147 148 if (matched_device_name_size >= 1) 149 matched_device_name[matched_device_name_size-1] = 0; 150 151 if (matched_device_name_size > 1) 152 { 153 bytes = MIN_MACRO(device_name_size,matched_device_name_size-1); 154 155 memcpy(matched_device_name,device_name,bytes); 156 } 157 158 if (matched_device_name_size_ret != NULL) 159 *matched_device_name_size_ret = bytes; 160 } 161 162 if (is_verbose) { 163 fprintf(stdout," >>>"); 164 } 165 } 166 else if (is_verbose) 167 { 168 fprintf(stdout," "); 169 } 170 171 if (is_verbose) 172 { 173 fprintf(stdout, 174 " %1u: %s [ %s ]\n", 175 jj, 176 device_name, 177 driver_version); 178 } 179 } 180 } 181 182 if (is_verbose) { 183 fprintf(stdout,"\n"); 184 } 185 186 // 187 // get target platform and device 188 // 189 if (!match_platform) 190 { 191 if (is_verbose) 192 fprintf(stderr,"no match for target platform substring %s\n",target_platform_substring); 193 194 return CL_INVALID_PLATFORM; 195 } 196 if (!match_device) 197 { 198 if (is_verbose) 199 fprintf(stderr,"no match for target device substring %s\n",target_device_substring); 200 201 return CL_DEVICE_NOT_FOUND; 202 } 203 204 return CL_SUCCESS; 205 } 206 207 // 208 // 209 // 210