{"id":34501452,"url":"https://github.com/reuben-sun/pybind-cuda-demo","last_synced_at":"2026-04-07T16:31:04.702Z","repository":{"id":329887668,"uuid":"1120882770","full_name":"Reuben-Sun/pybind-cuda-demo","owner":"Reuben-Sun","description":"一个 基于pybind11实现python调用cuda C++接口 的示例","archived":false,"fork":false,"pushed_at":"2025-12-22T05:17:56.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-23T16:53:17.810Z","etag":null,"topics":["cpp","cuda","pybind11","python","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Cuda","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/Reuben-Sun.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-22T05:12:47.000Z","updated_at":"2025-12-22T05:19:22.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/Reuben-Sun/pybind-cuda-demo","commit_stats":null,"previous_names":["reuben-sun/pybind-cuda-demo"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/Reuben-Sun/pybind-cuda-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Reuben-Sun%2Fpybind-cuda-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Reuben-Sun%2Fpybind-cuda-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Reuben-Sun%2Fpybind-cuda-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Reuben-Sun%2Fpybind-cuda-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Reuben-Sun","download_url":"https://codeload.github.com/Reuben-Sun/pybind-cuda-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Reuben-Sun%2Fpybind-cuda-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27992996,"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","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cpp","cuda","pybind11","python","pytorch"],"created_at":"2025-12-24T02:01:58.605Z","updated_at":"2025-12-24T02:02:15.683Z","avatar_url":"https://github.com/Reuben-Sun.png","language":"Cuda","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pybind-cuda-demo\n基于pybind11实现python调用cuda C++接口\n\n\n\n## install\n\n```bash\ngit clone https://github.com/Reuben-Sun/pybind-cuda-demo.git\ncd pybind-cuda-demo\npip install -e . --no-build-isolation\n```\n\n\n\n## test\n\n```python\nimport torch\nimport time\nimport cuda_demo_ext # 导入我们编译好的 C++ 扩展\n\ndef run_benchmark():\n    # 数据规模：5000万个元素 (约 200MB 数据)\n    size = 50 * 1000 * 1000 \n    print(f\"Testing with vector size: {size}\")\n\n    # 准备数据\n    a_cpu = torch.rand(size, dtype=torch.float32)\n    b_cpu = torch.rand(size, dtype=torch.float32)\n    scalar = 5.0\n\n    a_gpu = a_cpu.cuda()\n    b_gpu = b_cpu.cuda()\n\n    # --- 1. CPU 基准 (使用 PyTorch CPU，已经优化过，但受限于内存带宽和计算力) ---\n    start = time.time()\n    res_cpu = a_cpu + b_cpu + scalar\n    end = time.time()\n    print(f\"PyTorch CPU time:     {(end - start) * 1000:.4f} ms\")\n\n    # --- 2. 自定义 CUDA Kernel ---\n    # 预热 GPU\n    _ = cuda_demo_ext.vector_add(a_gpu, b_gpu, scalar)\n    torch.cuda.synchronize()\n\n    start = time.time()\n    res_custom = cuda_demo_ext.vector_add(a_gpu, b_gpu, scalar)\n    torch.cuda.synchronize() # 等待 GPU 完成\n    end = time.time()\n    print(f\"Custom CUDA Kernel:   {(end - start) * 1000:.4f} ms\")\n\n    # --- 验证正确性 ---\n    # 将 GPU 结果转回 CPU 对比\n    if torch.allclose(res_cpu, res_custom.cpu(), atol=1e-5):\n        print(\"✅ Results match!\")\n    else:\n        print(\"❌ Results mismatch!\")\n\nif __name__ == \"__main__\":\n    run_benchmark()\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freuben-sun%2Fpybind-cuda-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freuben-sun%2Fpybind-cuda-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freuben-sun%2Fpybind-cuda-demo/lists"}