| 575 | MTL::Device* device() { return rt().dev; } |
| 576 | |
| 577 | MTL::ComputePipelineState* pipeline(const char* source_name, const char* source, const char* function, std::string& err) { |
| 578 | Runtime& r = rt(); |
| 579 | const std::string key = std::string(source_name) + "::" + function; |
| 580 | std::lock_guard<std::mutex> lk(r.mu); |
| 581 | if (auto it = r.pipes.find(key); it != r.pipes.end()) return it->second; |
| 582 | MTL::Library*& lib = r.libs[source_name]; |
| 583 | NS::Error* e = nullptr; |
| 584 | if (!lib) { |
| 585 | MTL::CompileOptions* co = MTL::CompileOptions::alloc()->init(); |
| 586 | co->setMathMode(MTL::MathModeSafe); // no fast-math reassociation: parity with the CUDA reference first |
| 587 | lib = r.dev->newLibrary(NS::String::string(source, NS::UTF8StringEncoding), co, &e); |
| 588 | co->release(); |
| 589 | if (!lib) { |
| 590 | err = std::string("MSL compile of ") + source_name + ": " + (e ? e->localizedDescription()->utf8String() : "?"); |
| 591 | return nullptr; |
| 592 | } |
| 593 | } |
| 594 | MTL::Function* f = lib->newFunction(NS::String::string(function, NS::UTF8StringEncoding)); |
| 595 | if (!f) { err = std::string("no MSL function ") + function; return nullptr; } |
| 596 | MTL::ComputePipelineState* p = r.dev->newComputePipelineState(f, &e); |
| 597 | f->release(); |
| 598 | if (!p) { err = std::string("pipeline ") + function + ": " + (e ? e->localizedDescription()->utf8String() : "?"); return nullptr; } |
| 599 | r.pipes[key] = p; |
| 600 | return p; |
| 601 | } |
| 602 | |
| 603 | Error dispatch(Stream st, MTL::ComputePipelineState* p, const void* args, size_t arg_bytes, Grid groups, Grid threads, |
| 604 | size_t tg_bytes) { |