How to use GPU(Cuda/OpenCL) for mathematical operations 07-04-2013, 01:20 PM
#1
Hey,
so today I'll try to show you how to calculate mathematical operations on your GPU using Cuda(on Nvidia), or OpenCL(on Ati/Intel). We're using "Cudafy.Net" to achive this. I have made a simple Example on which you can see how it works.
Download
Screenshot:
Code explanation:
Our "whole" Code:
and:
So the only thing I'll explain is:
The Rest should be selfexplaining.
With this Code you Define the Target to OpenCL, not to CUDA or emulator, if you are just using a Nvidia Card change both OpenCL to CUDA.
You map the DeviceId 0, the first device.
And define the Language your C# Code will be translated to OpenCL, if you are using Cuda, it should be of course translated to Cuda![Wink Wink](https://sinister.ly/images/smilies/set/wink.png)
1. Defines a new Gpu, the mapped target 0.
2. Defines a new Module, in this case the Translator.
3. The in Step 1. defined Gpu Loads the in Step 2. defined Module.
1. Launches our Void.
2. Extracts the Output of our Void to "c".
The rest should be self explaining, if you have any Questions feel free to ask![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
~noXxio
so today I'll try to show you how to calculate mathematical operations on your GPU using Cuda(on Nvidia), or OpenCL(on Ati/Intel). We're using "Cudafy.Net" to achive this. I have made a simple Example on which you can see how it works.
Download
Screenshot:
Spoiler:
Code explanation:
Our "whole" Code:
PHP Code:
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;
PHP Code:
private void btn_gpu_en_Click(object sender, EventArgs e)
{
CudafyModes.Target = eGPUType.OpenCL;
CudafyModes.DeviceId = 0;
CudafyTranslator.Language = eLanguage.OpenCL;
try
{
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
CudafyModule km = CudafyTranslator.Cudafy();
gpu.LoadModule(km);
Stopwatch sw = new Stopwatch();
int c;
int[] dev_c = gpu.Allocate<int>();
sw.Start();
gpu.Launch().encrypt(Convert.ToInt32(numericUpDown1.Value), dev_c);
gpu.CopyFromDevice(dev_c, out c);
sw.Stop();
txt_gpu.Text = c.ToString();
lbl_gpu_status.Text = sw.ElapsedMilliseconds.ToString() + " msec";
}
catch { }
}
and:
PHP Code:
[Cudafy]
public static void encrypt(int max, int[] c)
{
int a = 8919259;
int b = 8915159;
int i = 0;
int y = 0;
int i_ = max;
while (i != 99999999)
{
while (i_ != 0)
{
y = ((a * b) / 8) * 555;
i_--;
}
i++;
}
c[0] = y;
}
So the only thing I'll explain is:
PHP Code:
private void btn_gpu_en_Click(object sender, EventArgs e)
{
CudafyModes.Target = eGPUType.OpenCL;
CudafyModes.DeviceId = 0;
CudafyTranslator.Language = eLanguage.OpenCL;
try
{
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
CudafyModule km = CudafyTranslator.Cudafy();
gpu.LoadModule(km);
Stopwatch sw = new Stopwatch();
int c;
int[] dev_c = gpu.Allocate<int>();
sw.Start();
gpu.Launch().encrypt(Convert.ToInt32(numericUpDown1.Value), dev_c);
gpu.CopyFromDevice(dev_c, out c);
sw.Stop();
txt_gpu.Text = c.ToString();
lbl_gpu_status.Text = sw.ElapsedMilliseconds.ToString() + " msec";
}
catch { }
}
PHP Code:
CudafyModes.Target = eGPUType.OpenCL;
CudafyModes.DeviceId = 0;
CudafyTranslator.Language = eLanguage.OpenCL;
You map the DeviceId 0, the first device.
And define the Language your C# Code will be translated to OpenCL, if you are using Cuda, it should be of course translated to Cuda
![Wink Wink](https://sinister.ly/images/smilies/set/wink.png)
PHP Code:
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
CudafyModule km = CudafyTranslator.Cudafy();
gpu.LoadModule(km);
2. Defines a new Module, in this case the Translator.
3. The in Step 1. defined Gpu Loads the in Step 2. defined Module.
PHP Code:
gpu.Launch().encrypt(Convert.ToInt32(numericUpDown1.Value), dev_c);
gpu.CopyFromDevice(dev_c, out c);
2. Extracts the Output of our Void to "c".
The rest should be self explaining, if you have any Questions feel free to ask
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
~noXxio