
Hey all,
I'm getting an error when trying to do some vector maths using CUDAfy. I'm programming in vb.net. Here's the code:
Public Shared Sub Timesteper(ByVal IncliN() As Double, ByVal RaaN() As Double, ByVal ARgP() As Double, ByVal RadiuS() As Double, ByRef LaT() As Double, ByRef LonGG() As Double, ByRef INCUBE() As Double, ByVal radiuseperation As Double, ByVal latseperation As Double, ByVal longseperation As Double, ByVal len As Double)
Try
Dim watch As New Stopwatch
aaa = IncliN
bbb = RaaN
ccc = ARgP
ddd = RadiuS
rads = radiuseperation
lods = longseperation
asdf = len
' This 'smart' method will Cudafy all members with the Cudafy attribute in the calling type (i.e. Program)
Dim km As CudafyModule = CudafyTranslator.Cudafy()
' If cudafying will not work for you (CUDA SDK + VS not set up right) then comment out above and
' uncomment below. Remember to also comment out the Structs and 3D arrays region below.
' CUDA 4.0 SDK must be installed and cl.exe (VC++ compiler) must be in path. If you are compiling for
' x86 then 32bit CUDA 4.0 must be installed (also on Windows 64bit).
' For x64 then 64bit CUDA 4.0 must be installed.
'CudafyModule km = CudafyModule.Deserialize(typeof(Program).Name);
' Get the first CUDA device and load our module
_gpu = CudafyHost.GetDevice(eGPUType.Cuda)
_gpu.LoadModule(km)
'#Region "Add vectors"
lenn = len
Dim aaaa As Double() = aaa
Dim bbbb As Double() = bbb
Dim cccc As Double() = ccc
Dim dddd As Double() = ddd
Dim eeee As Double() = New Double(lenn  1) {}
Dim ffff As Double() = New Double(lenn  1) {}
Dim gggg As Double() = New Double(lenn  1) {}
Dim hhhh As Double = radiuseperation
Dim iiii As Double = latseperation
Dim jjjj As Double = longseperation
watch.Start()
' copy the arrays 'a' and 'b' to the GPU  these overloads automatically allocate GPU memory
Dim dev_a As Double() = _gpu.CopyToDevice(aaaa)
Dim dev_b As Double() = _gpu.CopyToDevice(bbbb)
Dim dev_c As Double() = _gpu.CopyToDevice(cccc)
Dim dev_d As Double() = _gpu.CopyToDevice(dddd)
' allocate memory on the GPU for the result  this allocate enough memory to hold a vector the
' same length as vector c  it does not copy vector c (same as _gpu.Allocate<int>(c.Length);)
'Dim dev_c As Double() = _gpu.Allocate(Of Double)(c)
Dim dev_e As Double() = _gpu.Allocate(Of Double)(eeee)
Dim dev_f As Double() = _gpu.Allocate(Of Double)(ffff)
Dim dev_g As Double() = _gpu.Allocate(Of Double)(gggg)
' Threads are grouped in Blocks. Blocks are grouped in a Grid. Here we launch N Blocks where
' each block contains 1 thread. Note addVector contains a GThread arg  no need to pass this.
' GThread is the Cudafy equivalent of the builtin CUDA variables. Use it to identify thread id.
_gpu.Launch(1000, 1).TimeStepMark2(dev_a, dev_b, dev_c, dev_d, dev_e, dev_f, dev_g, hhhh, iiii, jjjj)
' copy the array 'c' back from the GPU to the CPU
_gpu.CopyFromDevice(dev_e, eeee)
_gpu.CopyFromDevice(dev_f, ffff)
_gpu.CopyFromDevice(dev_g, gggg)
LaT = eeee
LonGG = ffff
INCUBE = gggg
'Form1.RichTextBox1.AppendText(Chr(13) & a(N  1) & ", " & b(N  1) & ", " & c(N  1) & Chr(13))
'Form1.RichTextBox1.AppendText(Chr(13) & "We just added " & N & " elements of our two vectors in " & N & " parallel threads.")
' This used a bit more precious GPU memory than the earlier examples, so let's free it
_gpu.FreeAll()
'#End Region
watch.Stop()
Form1.RichTextBox1.AppendText(Chr(13) & watch.ElapsedMilliseconds)
Catch cle As CudafyLanguageException
HandleException(cle)
Catch cce As CudafyCompileException
HandleException(cce)
Catch che As CudafyHostException
HandleException(che)
End Try
Console.ReadLine()
End Sub
<Cudafy()> _
Private Sub TimeStepMark2(ByVal thread As GThread, ByVal IncliN() As Double, ByVal RaaN() As Double, ByVal ARgP() As Double, ByVal RadiuS() As Double, ByVal LaT() As Double, ByVal LonGG() As Double, ByVal INCUBE() As Double, ByVal radiuseperation As Double, ByVal latseperation As Double, ByVal longseperation As Double)
Dim tid As Integer = thread.blockIdx.x
If tid < IncliN.Length Then
Dim phi As Double
Dim thett As Double
Dim x As Double
Dim y As Double
Dim z As Double
Dim tempx As Double
Dim tempy As Double
Dim tempz As Double
phi = Rnd() * Math.PI * 2 + ARgP(tid)
thett = IncliN(tid)
x = RadiuS(tid) * Math.Cos(phi)
y = RadiuS(tid) * Math.Sin(phi)
z = 0
tempx = Math.Cos(phi) * x  Math.Sin(phi) * Math.Cos(thett) * y
tempy = Math.Sin(phi) * x + Math.Cos(phi) * Math.Cos(thett) * y
tempz = Math.Sin(thett) * y
x = tempx
y = tempy
z = tempz
Dim tempRadius As Double
Dim TempThett As Double
Dim tempPhi As Double
tempRadius = Math.Sqrt(x ^ 2 + y ^ 2 + z ^ 2)
TempThett = Math.Acos(z / tempRadius)
tempPhi = Math.Atan(y / x)
LaT(tid) = TempThett
LonGG(tid) = tempPhi
Dim a As Double
Dim b As Double
Dim c As Double
Dim d As Double
Dim f As Double
Dim cubetemp As Double
a = Math.Floor((RadiuS(tid)  reee) / radiuseperation)
b = Math.Floor(Math.PI / latseperation)
c = Math.Floor(2 * Math.PI / longseperation)
d = Math.Floor(LaT(tid) / latseperation)
f = Math.Floor(LonGG(tid) / longseperation)
cubetemp = a * b * c + d * c + f
INCUBE(tid) = cubetemp
End If
End Sub
I can't tell why it's throwing the error. I know the error comes from this line:
Dim km As CudafyModule = CudafyTranslator.Cudafy()
But otherwise I'm stumped. Any help would be majorly appreciated. If I've not given enough info please feel free to ask. I'm hoping to implement a group of subs that do calculations similar to this so if you can explain the difference
between the correct way and my way that would be great too!
Thanks in advanced everyone!



Make sure that the methods you are cudafying are static. This is called "shared" in VB.



Ah! Thanks that got me past that error.
Any idea how to use rnd() as that's throwing an error now :)



Can you post some code?



It's on the above code about 3/4 of the way down, this line:
phi = Rnd() * Math.PI * 2 + ARgP(tid)
I've found a way around this (passing an array of random numbers to the code as opposed to making the numbers in the sub) ideally I'd like it to work as above though as it'll save more time! :D
Thanks for responding again! :D



You mean the cudafying fails? I guess it does not know what Rnd() is. Take a look at CURANDTests.cs and the Cudafy.Host.UnitTests project and CURANDHostTests.cs in Cudafy.Math.UnitTests to get an idea of what is possible with random
numbers. A quick review of the NVIDIA docs will not go amiss either.

