Skip to content
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

I am a VB.NET developer using Winforms .NET 5 project testing out ILGPU, but errors!!! #385

Closed
tridexanders opened this issue Jan 18, 2021 · 9 comments
Labels
question workaround required Incompatibility with a particular platform that is not a bug in principle

Comments

@tridexanders
Copy link

### I am a VB.NET developer using Winforms .NET 5 project testing out ILGPU

I am having issues with code errors below:

Test code

#Region " .................... TEST KERNELS SIMPLE ......................"

Private Shared Sub MyKernel(ByVal idx As Index, ByVal a As ArrayView(Of Single), ByVal b As ArrayView(Of Single), ByVal c As ArrayView(Of Single), ByVal d As Single)
    a(idx) = b(Index) * c(Index) + d
End Sub

Private Shared Sub MyGroupedKernel(ByVal a As ArrayView(Of Single), ByVal b As ArrayView(Of Single), ByVal c As ArrayView(Of Single), ByVal d As Single)
    Dim globalIdx = Grid.GlobalIndex.X
    If globalIdx >= a.Length Then Return
    a(globalIdx) = b(globalIdx) * c(globalIdx) + d
End Sub

Private Shared Sub MyGroupedKernelAssert(ByVal a As ArrayView(Of Single), ByVal b As ArrayView(Of Single), ByVal c As ArrayView(Of Single), ByVal d As Single)
    Dim globalIdx = Grid.GlobalIndex.X
    a(globalIdx) = b(globalIdx) * c(globalIdx) + d
End Sub

#End Region

Errors

1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,59): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,59): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,92): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,92): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,125): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(80,125): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,46): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,46): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,79): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,79): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,112): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(84,112): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,52): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,52): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,85): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,85): error BC30649: '' is an unsupported type.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,118): error BC32044: Type argument 'Single' does not inherit from or implement the constraint type '?'.
1>C:\Users\Tridex\Desktop\Tridex ILGPU 2020\Tridex ILGPU 2020\Form1.vb(90,118): error BC30649: '' is an unsupported type.

@MoFtZ
Copy link
Collaborator

MoFtZ commented Jan 18, 2021

@tridexanders The issue is due to the use of the unmanaged constraint in ILGPU.

Unfortunately, based on this issue in the VBLang repository, it is unlikely to get fixed by the VB.NET compiler.

C# is the main supported language, although some people have gotten F# to work too.

Alternatively, the unmanaged constraint was added to ILGPU v0.8.0. If you're happy to use an older/unsupported version of ILGPU, you could try v0.7.1.

@tridexanders
Copy link
Author

Hi,

  1. thanks for your help, v07.1. works as you suggested, can i get the link to the original documentation for that version or has it all been updated?

  2. am I missing much functionality in v0.71 that has been applied to recent version, I KNOW THE SHARP VERSION WILL NOT WORK unless I use c#?

thanks again to your team....

@MoFtZ
Copy link
Collaborator

MoFtZ commented Jan 18, 2021

The documentation is currently available from http://www.ilgpu.net (direct link).

ILGPU v0.7.1 contains all the core functionality, and I have previously used it in projects. I do recall trying to use ILGPU with VB.NET back in v0.6.x, but ran into a few issues because some of my VB code internally used native method calls, which cannot be translated by ILGPU. I ended up writing the GPU section in C#, and mixing with the original VB.NET codebase. Unfortunately, I do not remember which VB instructions caused my issues.

You would be missing out on many optimisations, and several bug fixes for edge cases when using Cuda (e.g. #93).

Some functionality will not affect VB.NET as it is not available in the language (e.g. #200).

The version also may not support the very latest graphic cards - for example, we had to put out a new version to support the Nvidia RTX 3080 cards (#302).

Support for OpenCL was also much more limited.

Finally, there is a very recent feature that allows basic logging from within the GPU kernel.

The releases notes can be found here.

@m4rs-mt m4rs-mt added question workaround required Incompatibility with a particular platform that is not a bug in principle labels Jan 19, 2021
@MoFtZ
Copy link
Collaborator

MoFtZ commented Feb 11, 2021

@tridexanders How did you go with using ILGPU?

As previously mentioned, the VB.NET compiler does not like the unmanaged constraint.

I did some more investigation into this issue, and have found that it is happy to use existing C# code that already fulfils the unmanaged constraint.

So for example, ILGPU provides the ArrayView<T> structure, where T has the unmanaged constraint. The VB.NET compiler does not like ArrayView(Of Integer). As a workaround, we can create a C# wrapper such as:

public struct ArrayViewOfInteger
{
    public ArrayView<int> InnerView { get; }

    public ArrayViewOfInteger(ArrayView<int> view)
    {
        InnerView = view;
    }

    public static implicit operator ArrayView<int>(ArrayViewOfInteger v) => v.InnerView;
    public static implicit operator ArrayViewOfInteger(ArrayView<int> v) => new ArrayViewOfInteger(v);
}

This would require a wrapper for each data type.

Generic functions also needed a wrapper, such as Accelerator.Allocate<T>:

public static MemoryBuffer<int> AllocateOfInteger(this Accelerator accelerator, long extent)
{
    return accelerator.Allocate<int>(extent);
}

I have attached a small working example of this workaround: VisualBasicExample.zip

@tridexanders
Copy link
Author

Hi,

  1. thanks for you reply will try the code out, in a bit.

  2. I found an interesting article below the other day, does this have context as to the issues for VB developers?


https://developercommunity2.visualstudio.com/t/shVBshshNETsh-can-actually-us/1297047?q=vb.net&space=61


We found a simple trick to use C# ref structs in VB, but this seems upsetting one of Roslyn team, so, he filed this issue:dotnet/roslyn#50118 to ask to prevent this workaround and deny VB any access to ref structs, which makes VB unable to use important classes and libraries in .NET Core such as System.Text.Json. He even closed the discussion on that topic to suppress VB community voice on the matter, so, I am asking the Roslyn team to re-consider supporting ref types in VB, as our experiment with it was promising.

Here is some details about that:

The problem

The problem is that ref structs found their way to .NET Core to benefit from their performance, so, they are used to create many types (like Span(Of T)) that are used with many important classes like string and file classes, and more recently, the System.Text.Json library. But VB.NET can't use any of that, which mean that VB will be out of the game over time as it can't use more and more APIs of .NET Core!

But, I found a workaround, and it works just fine!

Let's try a simple code:

Sub main()

Dim s As New Span(Of Integer)({1, 2, 3})

Console.WriteLine(s(1))

End Sub

if you tried this, the code editor will show an error line under 'Span(Of Integer)`, with the message:

Span(Of Integer)' is obsolete. Types with embedded references are not supported in this version of your compiler.

The solution

I tried to workaround this until I found a very simple solution: Just mark the method (or the whole module/calss) with the Obsolete attribute, and pass false to its second parameter:

<Obsolete("Allow ref structs", False)>

Sub main()

Dim s As New Span(Of Integer)({1, 2, 3})

Console.WriteLine(s(1))

End Sub

Now this code will compile and run successfully.

But be aware

VB knows nothing about ref structs, and think they are just normal structs, so, it will not prevent you from boding them. Try this:

Dim s As New Span(Of Integer)({1, 2, 3})

Dim o As Object = s

VB will not object boxing the span into an object, and the code will compile, but the .NET RunTime will not allow this, and will give you a runtime error:

System.InvalidProgramException: 'Cannot create boxed ByRef-like values.'

Also, you must know that this error is external to VB.NET, and will crash your app. Ypu can't even handle this error probably by a Try Catch block as it will not prevent the crash. If you used this code:

Dim s As New Span(Of Integer)({1, 2, 3})

Try

Dim o As Object = s

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

The Catch statement will catch the error and the error message will be printed to the consol, but the app will still crash and shut down.

So, you must use ref structs with cautious, and don't try to violate their rules.

What we need:

If MS is not willing to support ref structs in VB.NET compiler, at least let the back door open for us to use it on our responsibility. When we use thee obsolete attr, we are fully aware of the risks and limitations, so, we will not ask MS for anything to fix or enhance. This is faire enough. Besides, it is not understandable to put effort and money to make changes to Roslyn, Runtime, C# and VB, just to make VB developers unhappy!

Please, let us handle our language as we desire.

@MoFtZ
Copy link
Collaborator

MoFtZ commented Feb 13, 2021

@tridexanders My impression is that MS will support VB.NET, but will not be adding new features (e.g. ref structs, unmanaged contraints, etc).

From https://devblogs.microsoft.com/vbteam/visual-basic-support-planned-for-net-5-0/

Going forward, we do not plan to evolve Visual Basic as a language. This supports language stability and maintains compatibility between the .NET Core and .NET Framework versions of Visual Basic.

@m4rs-mt
Copy link
Owner

m4rs-mt commented Oct 14, 2021

@NullandKale @MoFtZ @tridexanders While rereading this PR, I was wondering if we should include a brief summary in the ILGPU documentation and/or provide a simplified example that demonstrates a possible workaround? What do you think?

@MoFtZ
Copy link
Collaborator

MoFtZ commented Oct 17, 2021

@m4rs-mt Unfortunately, there is not much of a workaround. VB.NET does not support the unmanaged constraint, and likely never will. The workaround I proposed was to write a C# wrapper to allow VB.NET to use the ILGPU structures. However, if the user is able to mix VB.NET with C#, they may as well write all the GPGPU code in C#.

@m4rs-mt
Copy link
Owner

m4rs-mt commented Jun 9, 2022

We are closing this issue for now. If the general problem persists, we can safely reopen this issue.

@m4rs-mt m4rs-mt closed this as completed Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question workaround required Incompatibility with a particular platform that is not a bug in principle
Projects
None yet
Development

No branches or pull requests

3 participants