问题
I'm a hobbyist and would like to utilize my GPU for my personal projects. I've gotten the Alea GPU package installed and working.
This below produces the same output:
Dim y(10) As Integer
For i = 0 To 10 - 1
y(i) = i
Next
Dim y2(10) As Integer
Array.Copy(y, y2, y.Length)
Parallel.For(0, y.Length - 1, Sub(i) y(i) += i)
Debug.WriteLine(y.Aggregate(Function(now, future) now + future))
Alea.Gpu.Default.For(0, y2.Length - 1, Sub(i) y2(i) += i)
Debug.WriteLine(y2.Aggregate(Function(now, future) now + future))
Both return 90. That's the most basic but what i need is a lot more.
I'm trying to convert my other more resource intensive parallel.foreach loops into GPU.Default.For, so i can utilize the full power of my PC.
Keep in mind that all this worked flawlessly as a parallel.foreach loop. The rest of the code is currently commented out, this is the thing that prevents it from working.
Gpu.Default.For(0, Inventory.ItemsInventory.Count - 1,
Sub(i)
Dim Level_1 = Inventory.ItemsInventory.ElementAt(i) 'Exception on this line, doesn't happen if commented out.
end sub)
'Inventory' is a custom class, where 'ItemsInventory' is a dictionary(of string, InventoryItem) 'InventoryItem' is also a custom class.
The exception i'm getting is:
ArgumentException thrown: 'System.Exception' in Alea.dll Additional information: Cannot get field "$VB$Local_Inventory".
Next i tried to define an Array of 'InventoryItem' as that was what i was interested in for this particular loop.
Dim ItemsArray() As InventoryItem = Inventory.ItemsInventory.Select(Function(f) f.Value).ToArray
Gpu.Default.For(0, ItemsArray.Length - 1,
Sub(i)
Dim Level_1 = ItemsArray(i)
end sub)
This is what i get now:
Exception thrown: 'System.Exception' in Alea.dll Additional information: Non-blittable array MyApp.MainWindow+InventoryItem[] transferring is not allowed, you can change this by app.config.
But i don't know how that part looks like, that i 'can' add to the app.config file, i haven't found anything online to solve this.
回答1:
With regards to the second exception, the following page shows the basics of setting up Alea GPU in a .NET config file:
http://www.aleagpu.com/release/3_0_2/doc/faq.html
After reading that, I checked the documentation for the Alea.Settings
type and found that it has a Memory
property of type SettingElements.MemoryElement
.
http://www.aleagpu.com/release/3_0_2/api/html/73614a0a-9c5c-cce6-7114-fc6833cb31f2.htm
That type has a Boolean
property AllowNonBlittableMemoryTransfer
.
That suggests that, to allow non-blittable types in your scenario, your config file should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="aleaSettings" type="Alea.Settings, Alea"/>
</configSections>
<aleaSettings>
<memory allowNonBlittableMemoryTransfer="true"/>
</aleaSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
来源:https://stackoverflow.com/questions/42638015/iterate-over-a-collection-of-custom-classes-with-alea-gpu