| 37 | |
| 38 | |
| 39 | cl::Kernel KernelCache::getKernel(cl::CommandQueue& queue, |
| 40 | const std::string& program_name, |
| 41 | const std::string& kernel_name, |
| 42 | const std::string& params) |
| 43 | { |
| 44 | //!! ASSUMPTION: Kernel name == program name; |
| 45 | #if (BUILD_CLVERSION >= 120) |
| 46 | std::string _params = " -cl-kernel-arg-info -cl-std=CL1.2"; |
| 47 | #else |
| 48 | std::string _params = " -cl-std=CL1.1"; |
| 49 | #endif |
| 50 | if (params.length() > 0) |
| 51 | { |
| 52 | // Ensure only one space after the -cl-std. |
| 53 | // >1 space can cause an Apple compiler bug. See clSPARSE issue #141. |
| 54 | if (params.at(0) != ' ') |
| 55 | _params.append(" "); |
| 56 | _params.append(params); |
| 57 | } |
| 58 | std::string key; |
| 59 | key.append( "[" + program_name + "/" + kernel_name + "]"); |
| 60 | key.append(_params); |
| 61 | |
| 62 | auto hash = rsHash(key); |
| 63 | #ifndef NDEBUG |
| 64 | std::cout << "key: " << key << " hash = " << hash << std::endl; |
| 65 | #endif |
| 66 | |
| 67 | auto kernel_iterator = kernel_map.find(hash); |
| 68 | if (kernel_iterator != kernel_map.end()) |
| 69 | { |
| 70 | |
| 71 | #ifndef NDEBUG |
| 72 | std::cout << "kernel found: " << hash <<std::endl; |
| 73 | #endif |
| 74 | return kernel_iterator->second; |
| 75 | } |
| 76 | else //build program and compile the kernel; |
| 77 | { |
| 78 | |
| 79 | #ifndef NDEBUG |
| 80 | std::cout << "kernel not found in cache: " << hash <<std::endl; |
| 81 | #endif |
| 82 | |
| 83 | const cl::Program* program = NULL; |
| 84 | program = getProgram(queue, program_name, _params); |
| 85 | if (program == nullptr) |
| 86 | { |
| 87 | std::cout << "Problem with getting program [" |
| 88 | << program_name << "] " << std::endl; |
| 89 | delete program; |
| 90 | return cl::Kernel(); |
| 91 | } |
| 92 | |
| 93 | cl_int status; |
| 94 | cl::Kernel kernel(*program, kernel_name.c_str(), &status); |
| 95 | |
| 96 | if (status != CL_SUCCESS) |