// Enqueues a kernel, waits for completion, and checks for errors void RunKernel(Kernel &kernel, Queue &queue, const Device &device, std::vector<size_t> global, const std::vector<size_t> &local, EventPointer event, const std::vector<Event> &waitForEvents) { if (!local.empty()) { // Tests for validity of the local thread sizes if (local.size() > device.MaxWorkItemDimensions()) { throw RuntimeErrorCode(StatusCode::kInvalidLocalNumDimensions); } const auto max_work_item_sizes = device.MaxWorkItemSizes(); for (auto i=size_t{0}; i<local.size(); ++i) { if (local[i] > max_work_item_sizes[i]) { throw RuntimeErrorCode(StatusCode::kInvalidLocalThreadsDim); } } auto local_size = size_t{1}; for (auto &item: local) { local_size *= item; } if (local_size > device.MaxWorkGroupSize()) { throw RuntimeErrorCode(StatusCode::kInvalidLocalThreadsTotal); } // Make sure the global thread sizes are at least equal to the local sizes for (auto i=size_t{0}; i<global.size(); ++i) { if (global[i] < local[i]) { global[i] = local[i]; } } } // Tests for local memory usage const auto local_mem_usage = kernel.LocalMemUsage(device); if (!device.IsLocalMemoryValid(local_mem_usage)) { throw RuntimeErrorCode(StatusCode::kInvalidLocalMemUsage); } // Prints the name of the kernel to launch in case of debugging in verbose mode #ifdef VERBOSE queue.Finish(); printf("[DEBUG] Running kernel '%s'\n", kernel.GetFunctionName().c_str()); const auto start_time = std::chrono::steady_clock::now(); #endif // Launches the kernel (and checks for launch errors) kernel.Launch(queue, global, local, event, waitForEvents); // Prints the elapsed execution time in case of debugging in verbose mode #ifdef VERBOSE queue.Finish(); const auto elapsed_time = std::chrono::steady_clock::now() - start_time; const auto timing = std::chrono::duration<double,std::milli>(elapsed_time).count(); printf("[DEBUG] Completed kernel in %.2lf ms\n", timing); #endif }
// Retrieves a parameter from the database size_t operator[](const std::string &key) const { for (const auto &kernel_name : kernel_names_) { const auto &kernel_db = databases_.find(kernel_name)->second; if (kernel_db.exists(key)) { return kernel_db[key]; } } throw RuntimeErrorCode(StatusCode::kDatabaseError); }
void Routine::InitProgram(std::initializer_list<const char *> source) { // Determines the identifier for this particular routine call auto routine_info = routine_name_; for (const auto &kernel_name : kernel_names_) { routine_info += "_" + kernel_name + db_(kernel_name).GetValuesString(); } log_debug(routine_info); // Queries the cache to see whether or not the program (context-specific) is already there bool has_program; program_ = ProgramCache::Instance().Get(ProgramKeyRef{ context_(), device_(), precision_, routine_info }, &has_program); if (has_program) { return; } // Sets the build options from an environmental variable (if set) auto options = std::vector<std::string>(); const auto environment_variable = std::getenv("CLBLAST_BUILD_OPTIONS"); if (environment_variable != nullptr) { options.push_back(std::string(environment_variable)); } // Queries the cache to see whether or not the binary (device-specific) is already there. If it // is, a program is created and stored in the cache const auto device_name = GetDeviceName(device_); const auto platform_id = device_.PlatformID(); bool has_binary; auto binary = BinaryCache::Instance().Get(BinaryKeyRef{platform_id, precision_, routine_info, device_name }, &has_binary); if (has_binary) { program_ = std::make_shared<Program>(device_, context_, binary); program_->Build(device_, options); ProgramCache::Instance().Store(ProgramKey{ context_(), device_(), precision_, routine_info }, std::shared_ptr<Program>{program_}); return; } // Otherwise, the kernel will be compiled and program will be built. Both the binary and the // program will be added to the cache. // Inspects whether or not FP64 is supported in case of double precision if ((precision_ == Precision::kDouble && !PrecisionSupported<double>(device_)) || (precision_ == Precision::kComplexDouble && !PrecisionSupported<double2>(device_))) { throw RuntimeErrorCode(StatusCode::kNoDoublePrecision); } // As above, but for FP16 (half precision) if (precision_ == Precision::kHalf && !PrecisionSupported<half>(device_)) { throw RuntimeErrorCode(StatusCode::kNoHalfPrecision); } // Collects the parameters for this device in the form of defines auto source_string = std::string{""}; for (const auto &kernel_name : kernel_names_) { source_string += db_(kernel_name).GetDefines(); } // Adds routine-specific code to the constructed source string for (const char *s: source) { source_string += s; } // Completes the source and compiles the kernel program_ = CompileFromSource(source_string, precision_, routine_name_, device_, context_, options, 0); // Store the compiled binary and program in the cache BinaryCache::Instance().Store(BinaryKey{platform_id, precision_, routine_info, device_name}, program_->GetIR()); ProgramCache::Instance().Store(ProgramKey{context_(), device_(), precision_, routine_info}, std::shared_ptr<Program>{program_}); }