![]() |
|
How to use GPU(Cuda/OpenCL) for mathematical operations - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: How to use GPU(Cuda/OpenCL) for mathematical operations (/Thread-How-to-use-GPU-Cuda-OpenCL-for-mathematical-operations) |
How to use GPU(Cuda/OpenCL) for mathematical operations - noXxio - 07-04-2013 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: 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 ![]() 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 ![]() ~noXxio RE: How to use GPU(Cuda/OpenCL) for mathematical operations - Shebang - 07-04-2013 This is pretty interesting, I'd like to see a practical implementation of this in newer releases. RE: How to use GPU(Cuda/OpenCL) for mathematical operations - Wet - 07-14-2013 C# is a dear language for me. And this is a great tutorial. I knew about this, and i know it's useful, so what should i say? GJ and keep HQ work up! RE: How to use GPU(Cuda/OpenCL) for mathematical operations - noXxio - 07-23-2013 Thank you very much for replying )
RE: How to use GPU(Cuda/OpenCL) for mathematical operations - Platinum - 07-23-2013 Great, I will come back to this when I do some more stuff with this language. |