{"id":26601368,"url":"https://github.com/calebwin/safe_ocl","last_synced_at":"2025-03-23T18:39:35.423Z","repository":{"id":57666174,"uuid":"217750400","full_name":"calebwin/safe_ocl","owner":"calebwin","description":"zero-cost wrapper types for safe OpenCL","archived":false,"fork":false,"pushed_at":"2019-10-26T18:22:34.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T10:58:38.600Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calebwin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-26T18:13:32.000Z","updated_at":"2019-10-26T18:23:53.000Z","dependencies_parsed_at":"2022-09-26T20:31:37.930Z","dependency_job_id":null,"html_url":"https://github.com/calebwin/safe_ocl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fsafe_ocl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fsafe_ocl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fsafe_ocl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calebwin%2Fsafe_ocl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calebwin","download_url":"https://codeload.github.com/calebwin/safe_ocl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245152629,"owners_count":20569395,"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":[],"created_at":"2025-03-23T18:39:34.656Z","updated_at":"2025-03-23T18:39:35.418Z","avatar_url":"https://github.com/calebwin.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# safe_ocl\n\n[![Gitter](https://badges.gitter.im/talk-about-emu/thoughts.svg)](https://gitter.im/talk-about-emu/thoughts?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n[![](http://meritbadge.herokuapp.com/safe_ocl)](https://crates.io/crates/safe_ocl)\n[![](https://docs.rs/safe_ocl/badge.svg)](https://docs.rs/safe_ocl)\n\n## about\n\nThis crate introduces zero-cost wrapper types for safe OpenCL. There are 2 wrapper types it introduces...\n\n- `MapKernel`\n- `MapProgram`\n\nCurrently, this is quite a limited set of types. Some of its limitations...\n\n- Only supports map computation\n- Only supports binary arithmetic operators\n- Only safe for single-threaded\n- Only safe for single-GPU\n- Only safe under pre-condition that the read/write flag for buffer is correct\n\nThis is really just a skeleton for adding new things like generics, new wrapper types, subtypes that can eliminate undefined behavior in OpenCL usage.\n\n## example\n\nThis is how you would normally implement an adding map operation.\n\n```rust\nlet src = r#\"\n    __kernel void add(__global float* buffer, float scalar) {\n        buffer[get_global_id(0)] += scalar;\n    }\n\"#;\n\n// (1) Define which platform and device(s) to use. Create a context,\n// queue, and program then define some dims (compare to step 1 above).\nlet platform = Platform::default();\nlet device = Device::first(platform).unwrap();\nlet context = Context::builder()\n    .platform(platform)\n    .devices(device.clone())\n    .build().unwrap();\nlet program = Program::builder()\n    .devices(device)\n    .src(src)\n    .build(\u0026context).unwrap();\nlet queue = Queue::new(\u0026context, device, None).unwrap();\nlet dims = 1 \u003c\u003c 20;\n// [NOTE]: At this point we could manually assemble a ProQue by calling:\n// `ProQue::new(context, queue, program, Some(dims))`. One might want to\n// do this when only one program and queue are all that's needed. Wrapping\n// it up into a single struct makes passing it around simpler.\n\n// (2) Create a `Buffer`:\nlet buffer = Buffer::\u003cf32\u003e::builder()\n    .queue(queue.clone())\n    .flags(flags::MEM_READ_WRITE)\n    .len(dims)\n    .fill_val(0f32)\n    .build().unwrap();\n\n// (3) Create a kernel with arguments matching those in the source above:\nlet kernel = Kernel::builder()\n    .program(\u0026program)\n    .name(\"add\")\n    .queue(queue.clone())\n    .global_work_size(dims)\n    .arg(\u0026buffer)\n    .arg(\u002610.0f32)\n    .build().unwrap();\n\n// (4) Run the kernel (default parameters shown for demonstration purposes):\nunsafe {\n    kernel.cmd()\n        .queue(\u0026queue)\n        .global_work_offset(kernel.default_global_work_offset())\n        .global_work_size(dims)\n        .local_work_size(kernel.default_local_work_size())\n        .enq().unwrap();\n}\n\n// (5) Read results from the device into a vector (`::block` not shown):\nlet mut vec = vec![0.0f32; dims];\nbuffer.cmd()\n    .queue(\u0026queue)\n    .offset(0)\n    .read(\u0026mut vec)\n    .enq().unwrap();\n\nassert_eq!(vec, vec![10.0f32; dims]);\n```\n\nThis is how you do it with the above types.\n\n```rust\n// (1) Define which platform and device(s) to use. Create a context,\n// queue, and program then define some dims (compare to step 1 above).\nlet platform = Platform::default();\nlet device = Device::first(platform).unwrap();\nlet context = Context::builder()\n    .platform(platform)\n    .devices(device.clone())\n    .build().unwrap();\nlet program = MapProgram::from(device, Op::Add, \u0026context).unwrap();\nlet queue = Queue::new(\u0026context, device, None).unwrap();\nlet dims = 1 \u003c\u003c 20;\n// [NOTE]: At this point we could manually assemble a ProQue by calling:\n// `ProQue::new(context, queue, program, Some(dims))`. One might want to\n// do this when only one program and queue are all that's needed. Wrapping\n// it up into a single struct makes passing it around simpler.\n\n// (2) Create a `Buffer`:\nlet buffer = Buffer::\u003cf32\u003e::builder()\n    .queue(queue.clone())\n    .flags(flags::MEM_READ_WRITE) // TODO ensure buffer is read-write\n    .len(dims)\n    .fill_val(0f32)\n    .build().unwrap();\n\n// (3) Create a kernel with arguments matching those in the source above:\nlet kernel = MapKernel::from(\u0026program, queue.clone(), \u0026buffer, \u002610.0f32).unwrap();\n\n// (4) Run the kernel (default parameters shown for demonstration purposes):\nkernel.cmd_enq(\u0026queue);\n\n// (5) Read results from the device into a vector (`::block` not shown):\nlet mut vec = vec![0.0f32; dims];\nbuffer.cmd()\n    .queue(\u0026queue)\n    .offset(0)\n    .read(\u0026mut vec)\n    .enq().unwrap();\n\nassert_eq!(vec, vec![10.0f32; dims]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebwin%2Fsafe_ocl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalebwin%2Fsafe_ocl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalebwin%2Fsafe_ocl/lists"}