{"id":15396593,"url":"https://github.com/hupe1980/go-mtl","last_synced_at":"2025-04-16T00:18:05.461Z","repository":{"id":182113910,"uuid":"667540934","full_name":"hupe1980/go-mtl","owner":"hupe1980","description":"🍏 Go Bindings for Apple Metal","archived":false,"fork":false,"pushed_at":"2023-07-19T15:16:31.000Z","size":50,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-16T00:17:57.768Z","etag":null,"topics":["apple","golang","gpu","metal","mtl"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hupe1980.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-17T18:36:28.000Z","updated_at":"2025-02-28T06:24:17.000Z","dependencies_parsed_at":"2024-10-19T03:28:47.477Z","dependency_job_id":null,"html_url":"https://github.com/hupe1980/go-mtl","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"2aec2d64bf4d5f920f59ae61da7f24bbe24c9e3a"},"previous_names":["hupe1980/go-mtl"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Fgo-mtl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Fgo-mtl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Fgo-mtl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Fgo-mtl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hupe1980","download_url":"https://codeload.github.com/hupe1980/go-mtl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249173099,"owners_count":21224485,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple","golang","gpu","metal","mtl"],"created_at":"2024-10-01T15:34:19.386Z","updated_at":"2025-04-16T00:18:05.446Z","avatar_url":"https://github.com/hupe1980.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🍏 go-mtl: Go Bindings for Apple Metal\n![Build Status](https://github.com/hupe1980/go-mtl/workflows/build/badge.svg) \n[![Go Reference](https://pkg.go.dev/badge/github.com/hupe1980/go-mtl.svg)](https://pkg.go.dev/github.com/hupe1980/go-mtl)\n\u003e go-mtl provides seamless integration between Go and Apple Metal, enabling developers to harness the full potential of Metal's high-performance graphics and compute capabilities in their Go applications. With go-mtl, you can write efficient and scalable code for darwin platforms, leveraging Metal's advanced features such as parallel processing, GPU acceleration, and low-level access to the graphics pipeline.\n\n## Installation\nUse Go modules to include go-mtl in your project:\n```bash\ngo get github.com/hupe1980/go-mtl\n```\n\n## Usage\n```go\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"unsafe\"\n\n\t\"github.com/hupe1980/go-mtl\"\n)\n\nconst source = `#include \u003cmetal_stdlib\u003e\n\nusing namespace metal;\n\nkernel void add_arrays(device const float* inA,\n\tdevice const float* inB,\n\tdevice float* result,\n\tuint index [[thread_position_in_grid]])\n{\n// the for-loop is replaced with a collection of threads, each of which\n// calls this function.\nresult[index] = inA[index] + inB[index];\n}\n`\n\nfunc main() {\n\t// Create a Metal device.\n\tdevice, err := mtl.CreateSystemDefaultDevice()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Create a Metal library from the provided source code.\n\tlib, err := device.NewLibraryWithSource(source)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Retrieve the Metal function named \"add_arrays\" from the library.\n\taddArrays, err := lib.NewFunctionWithName(\"add_arrays\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Create a Metal compute pipeline state with the function.\n\tpipelineState, err := device.NewComputePipelineStateWithFunction(addArrays)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Create a Metal command queue to submit commands for execution.\n\tq := device.NewCommandQueue()\n\n\t// Set the length of the arrays.\n\tarrLen := uint(4)\n\n\t// Prepare the input data.\n\tdataA := []float32{0.0, 1.0, 2.0, 3.0}\n\tdataB := []float32{0.0, 1.0, 2.0, 3.0}\n\n\t// Create Metal buffers for input and output data.\n\t// b1 and b2 represent the input arrays, and r represents the output array.\n\tb1 := device.NewBufferWithBytes(unsafe.Pointer(\u0026dataA[0]), unsafe.Sizeof(dataA), mtl.ResourceStorageModeShared)\n\tb2 := device.NewBufferWithBytes(unsafe.Pointer(\u0026dataB[0]), unsafe.Sizeof(dataB), mtl.ResourceStorageModeShared)\n\tr := device.NewBufferWithLength(unsafe.Sizeof(arrLen), mtl.ResourceStorageModeShared)\n\n\t// // Create a Metal command buffer to encode and execute commands.\n\tcb := q.CommandBuffer()\n\n\t// Create a compute command encoder to encode compute commands.\n\tcce := cb.ComputeCommandEncoder()\n\n\t// Set the compute pipeline state to specify the function to be executed.\n\tcce.SetComputePipelineState(pipelineState)\n\n\t// Set the input and output buffers for the compute function.\n\tcce.SetBuffer(b1, 0, 0)\n\tcce.SetBuffer(b2, 0, 1)\n\tcce.SetBuffer(r, 0, 2)\n\n\t// Specify threadgroup size\n\ttgs := pipelineState.MaxTotalThreadsPerThreadgroup\n\tif tgs \u003e arrLen {\n\t\ttgs = arrLen\n\t}\n\n\t// Dispatch compute threads to perform the calculation.\n\tcce.DispatchThreads(mtl.Size{Width: arrLen, Height: 1, Depth: 1}, mtl.Size{Width: tgs, Height: 1, Depth: 1})\n\n\t// End encoding the compute command.\n\tcce.EndEncoding()\n\n\t// Commit the command buffer for execution.\n\tcb.Commit()\n\n\t// Wait until the command buffer execution is completed.\n\tcb.WaitUntilCompleted()\n\n\t// Read the results from the output buffer\n\tresult := (*[1 \u003c\u003c 30]float32)(r.Contents())[:arrLen]\n\n\t// Print the results.\n\tfmt.Println(result)\n```\nOutput:\n```text\n[0 2 4 6]\n```\n\nFor more example usage, see [examples](./examples).\n\n## Contributing\nContributions are welcome! Feel free to open an issue or submit a pull request for any improvements or new features you would like to see.\n\n## License\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhupe1980%2Fgo-mtl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhupe1980%2Fgo-mtl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhupe1980%2Fgo-mtl/lists"}