Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


How to use GPU(Cuda/OpenCL) for mathematical operations filter_list
Author
Message
How to use GPU(Cuda/OpenCL) for mathematical operations #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:
Spoiler:
[Image: Fo1XTqe.png]

Code explanation:

Our "whole" Code:
PHP Code:
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator

PHP Code:
private void btn_gpu_en_Click(object senderEventArgs e)
        {
            
CudafyModes.Target eGPUType.OpenCL;
            
CudafyModes.DeviceId 0;
            
CudafyTranslator.Language eLanguage.OpenCL;
            try
            {
                
GPGPU gpu CudafyHost.GetDevice(CudafyModes.TargetCudafyModes.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_cout 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 maxint[] c)
        {
            
int a 8919259;
            
int b 8915159;
            
int i 0;
            
int y 0;
            
int i_ max;
            while (
!= 99999999)
            {
                while (
i_ != 0)
                {
                    
= ((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 senderEventArgs e)
        {
            
CudafyModes.Target eGPUType.OpenCL;
            
CudafyModes.DeviceId 0;
            
CudafyTranslator.Language eLanguage.OpenCL;
            try
            {
                
GPGPU gpu CudafyHost.GetDevice(CudafyModes.TargetCudafyModes.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_cout c);
                
sw.Stop();
                
txt_gpu.Text c.ToString();
                
lbl_gpu_status.Text sw.ElapsedMilliseconds.ToString() + " msec";
            }
            catch { }
        } 
The Rest should be selfexplaining.

PHP Code:
CudafyModes.Target eGPUType.OpenCL;
CudafyModes.DeviceId 0;
CudafyTranslator.Language eLanguage.OpenCL
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

PHP Code:
GPGPU gpu CudafyHost.GetDevice(CudafyModes.TargetCudafyModes.DeviceId);
CudafyModule km CudafyTranslator.Cudafy();
gpu.LoadModule(km); 
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.

PHP Code:
gpu.Launch().encrypt(Convert.ToInt32(numericUpDown1.Value), dev_c);
gpu.CopyFromDevice(dev_cout c); 
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

~noXxio

Reply

RE: How to use GPU(Cuda/OpenCL) for mathematical operations #2
This is pretty interesting, I'd like to see a practical implementation of this in newer releases.

Reply

RE: How to use GPU(Cuda/OpenCL) for mathematical operations #3
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!

Reply

RE: How to use GPU(Cuda/OpenCL) for mathematical operations #4
Thank you very much for replying Smile)

Reply

RE: How to use GPU(Cuda/OpenCL) for mathematical operations #5
Great, I will come back to this when I do some more stuff with this language.

Reply







Users browsing this thread: 1 Guest(s)