this post was submitted on 27 Sep 2023
28 points (100.0% liked)

Programming

17313 readers
105 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

Hi!

I'm looking for a C# library for matrix operations and preferably some linear algebra or optimization routines. Basically a NumPy/SciPy or PyTorch.

Ideally there'd be support for various backends (e.g. CPU, CUDA, OpenCL) for operations where possible.

As far as I can tell, there's Math.NET Numerics, Numpy.NET (which binds to Python's numpy), and NumSharp (which hasn't had commits since 2021), which seem to fit the bill mostly, though none are accelerated.

Otherwise, there are some libraries I've forgotten that seem to specifically target CUDA, which is too selective for my purpose. Maybe it was Hybridizer, which seems like its own compiler, which I'm not sure would work for me either.

There's also ComputeSharp which lets you write shaders directly in C#, though targets DirectX if I understand well.

The closest thing I've found is ILGPU, which seems brilliant since it JIT compiles kernels to CPU, CUDA, and OpenCL. The problem is I believe I'd need to write my own operations and kernels and essentially implement my own matrix compute library, though there seems to be some work on it, so maybe what I'm looking for is supported out of the box, minus optimization algorithms and so on.

Basically, does anyone have any pointers?

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 6 points 1 year ago (1 children)
[–] maggoats 2 points 1 year ago

Unfortunately not, though I forgot about SIMD! It doesn't seem to support arbitrary-sized matrices or arrays out of the box, though I guess I could index the vector type myself. Still, it doesn't offer the operations I'd like, as far as I can tell.

Thanks though!

[–] [email protected] 4 points 1 year ago (1 children)

If it binds to numpy, does it not automatically use all accelerations available? I can't really believe that it binds to numpy and does not use them. Maybe a flag needs to be set? I know in rust the numpy bindings have a feature flag for blas etc.

If you don't find one that fits the requirements exactly a few benchmarks are probably best to determine which one is most suited.

[–] maggoats 2 points 1 year ago (1 children)

Unfortunately I don't believe NumPy has any built in accelerations (other than being a C library which is fast already), though I don't really know the ins and outs. There are Python libraries that use the NumPy API or otherwise do some stuff to accelerate it on e.g. CUDA, but the Numpy.NET library as far as I know uses its own embedded Python + numpy, so as far as I can tell that wouldn't be an option.

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

Numpy.NET library as far as I know uses its own embedded Python + numpy,

Wtf, this is so absurd I find it hard to believe. They ship a python interpreter just to get numpy? And then wrote bindings to python instead of writing c# bindings for numpy?

Edit: wtf they really do.

Rust numpy seems to do something similar though. They have ndarray, which is independent from numpy, as a number crunching backend though. This is the part that can use blas.

[–] maggoats 1 points 1 year ago

The closest thing I've been able to find so far (which seems to have been under slow development by 1-2 contributors for the past couple years) is https://github.com/MPSQUARK/BAVCL which is based on ILGPU. I'll probably be keeping an eye on it though.