8000 GPU Support via OpenCL by Pencilcaseman · Pull Request #1377 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content

GPU Support via OpenCL #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove some clippy warnings and fix some bugs
  • Loading branch information
Pencilcaseman committed Mar 26, 2024
commit 931dde4a9a0906eb06cf4288dde2f2b81293cb8d
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ num-traits = { version = "0.2", default-features = false }
num-complex = { version = "0.4", default-features = false }

# Use via the `opencl` crate feature!
hasty_ = { version = "0.2", optional = true, package = "hasty", default-features = false }
hasty_ = { version = "0.2", optional = true, package = "hasty", default-features = false }
#hasty_ = { path = "../../hasty_dev/hasty", optional = true, package = "hasty", default-features = false }

# Use via the `rayon` crate feature!
Expand Down
47 changes: 35 additions & 12 deletions src/data_repr.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use alloc::borrow::ToOwned;
use alloc::slice;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::ffi::c_void;
use std::mem;
use std::mem::ManuallyDrop;
use std::ptr::NonNull;
Expand Down Expand Up @@ -55,6 +54,7 @@ impl<A> OwnedRepr<A> {
}

/// Move this storage object to a specified device.
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn copy_to_device(self, device: Device) -> Option<Self> {
// println!("Copying to {device:?}");
// let mut self_ = ManuallyDrop::new(self);
Expand All @@ -65,12 +65,13 @@ impl<A> OwnedRepr<A> {

match (self.device, device) {
(Device::Host, Device::Host) => {
todo!()
// println!("Copying to Host");
Some(self)
}

#[cfg(feature = "opencl")]
(Device::Host, Device::OpenCL) => {
let bytes = std::mem::size_of::<A>() * capacity;
let bytes = std::mem::size_of::<A>() * self.capacity;

unsafe {
if let Ok(buffer) =
Expand Down Expand Up @@ -108,7 +109,7 @@ impl<A> OwnedRepr<A> {
data.set_len(self.len);
if let Ok(_) = hasty_::opencl::opencl_read(
data.as_mut_ptr() as *mut std::ffi::c_void,
self.ptr.as_ptr() as *mut c_void,
self.ptr.as_ptr() as *mut std::ffi::c_void,
bytes,
) {
Some(Self {
Expand All @@ -128,8 +129,29 @@ impl<A> OwnedRepr<A> {
todo!();
}

_ => {
panic!("Not Implemented")
#[cfg(feature = "cuda")]
(Device::Host, Device::CUDA) => {
todo!();
}

#[cfg(feature = "cuda")]
(Device::CUDA, Device::Host) => {
todo!();
}

#[cfg(feature = "cuda")]
(Device::CUDA, Device::CUDA) => {
todo!();
}

#[cfg(all(feature = "opencl", feature = "cuda"))]
(Device::OpenCL, Device::CUDA) => {
todo!();
}

#[cfg(all(feature = "opencl", feature = "cuda"))]
(Device::CUDA, Device::OpenCL) => {
todo!();
}
}
}
Expand All @@ -153,7 +175,7 @@ impl<A> OwnedRepr<A> {
// Free `ptr`
// println!("Freeing OpenCL pointer");

hasty_::opencl::opencl_free(ptr as *mut c_void);
hasty_::opencl::opencl_free(ptr as *mut std::ffi::c_void);

// Should be optimised out, since nothing is allocated
Vec::new()
Expand Down Expand Up @@ -190,7 +212,7 @@ impl<A> OwnedRepr<A> {
unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
}

pub(crate) fn len(&self) -> usize {
pub(crate) const fn len(&self) -> usize {
self.len
}

Expand All @@ -200,7 +222,7 @@ impl<A> OwnedRepr<A> {
/// The pointer **may not necessarily point to the host**.
/// Using a non-host pointer on the host will almost certainly
/// cause a segmentation-fault.
pub(crate) fn as_ptr(&self) -> *const A {
pub(crate) const fn as_ptr(&self) -> *const A {
self.ptr.as_ptr()
}

Expand All @@ -210,11 +232,11 @@ impl<A> OwnedRepr<A> {
/// The pointer **may not necessarily point to the host**.
/// Using a non-host pointer on the host will almost certainly
/// cause a segmentation-fault.
pub(crate) fn as_ptr_mut(&self) -> *mut A {
pub(crate) const fn as_ptr_mut(&self) -> *mut A {
self.ptr.as_ptr()
}

/// Return underlying [NonNull] ptr.
/// Return underlying [`NonNull`] ptr.
///
/// ## Safety
/// The pointer **may not necessarily point to the host**.
Expand Down Expand Up @@ -305,7 +327,8 @@ impl<A> OwnedRepr<A> {
}

impl<A> Clone for OwnedRepr<A>
where A: Clone
where
A: Clone,
{
fn clone(&self) -> Self {
match self.device {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(non_null_convenience)]
// Copyright 2014-2020 bluss and ndarray developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
Expand Down
0