A
A
Alexey Smirnov2017-11-01 19:06:10
GPGPU
Alexey Smirnov, 2017-11-01 19:06:10

How to use the double type in GPU calculations?

I'm doing math on the GPU. For this I wrote the following code:

Correctly working code with type float under the spoiler
using System;
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;

namespace CUDAfy_example
{
    class Program
    {
        static void Main()
        {
            // Создание GPGPU объекта.
            var gpu = CudafyHost.GetDevice(eGPUType.OpenCL);
            CudafyTranslator.Language = eLanguage.OpenCL;
            var module = CudafyTranslator.Cudafy();
            gpu.LoadModule(module);

            int N = 20;

            float[,] a = new float[N, 1];
            float[,] b = new float[N, 1];
            float[,] c = new float[N, 1];

            // Выделение памяти на GPU.
            float[,] dev_c = gpu.Allocate<float>(c);

            // Заполнение массивов на CPU.
            for (int i = 0; i < N; i++)
            {
                a[i, 0] = -i + (float)0.5;
                b[i, 0] = i * i;
            }

            // Копирование массивов 'a' и 'b' из CPU (хоста) на GPU.
            float[,] dev_a = gpu.CopyToDevice(a);
            float[,] dev_b = gpu.CopyToDevice(b);
            gpu.Launch(N, 1).Calculation(dev_a, dev_b, dev_c, N);

            // Копирование массива 'c' обратно с GPU на CPU.
            gpu.CopyFromDevice(dev_c, c);

            // Отображение результатов.
            for (int i = 0; i < N; i++)
                Console.WriteLine("{0} + {1} = {2}", a[i, 0], b[i, 0], c[i, 0]);
            Console.ReadLine();

            // Освобождение памяти выделенной ранее на GPU.
            gpu.FreeAll();
        }

        [Cudafy]
        public static void Calculation(GThread thread, float[,] a, float[,] b, float[,] c, int N)
        {
            int tid = thread.blockIdx.x;

            int period = 10;
            if (tid < N)
            {
                float sum = 0;
                for (int j = tid; j < tid + period; j++)
                    sum += a[j, 0];

                c[tid, 0] = a[tid, 0] + b[tid, 0] + sum;
            }      
        }
    }   
}


The above code (under the spoiler) works well. But in this code, variables and arrays use the type float. I need to use the type double.
When I rewrote the above code, replacing all types floatwithdouble
Code with type double under spoiler (not executed due to an error)
using System;
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;

namespace CUDAfy_example
{
    class Program
    {
        static void Main()
        {
            // Создание GPGPU объекта.
            var gpu = CudafyHost.GetDevice(eGPUType.OpenCL);
            CudafyTranslator.Language = eLanguage.OpenCL;
            var module = CudafyTranslator.Cudafy();
            gpu.LoadModule(module);

            int N = 20;

            double[,] a = new double[N, 1];
            double[,] b = new double[N, 1];
            double[,] c = new double[N, 1];

            // Выделение памяти на GPU.
            double[,] dev_c = gpu.Allocate<double>(c);

            // Заполнение массивов на CPU.
            for (int i = 0; i < N; i++)
            {
                a[i, 0] = -i + (double)0.5;
                b[i, 0] = i * i;
            }

            // Копирование массивов 'a' и 'b' из CPU (хоста) на GPU.
            double[,] dev_a = gpu.CopyToDevice(a);
            double[,] dev_b = gpu.CopyToDevice(b);
            gpu.Launch(N, 1).Calculation(dev_a, dev_b, dev_c, N);

            // Копирование массива 'c' обратно с GPU на CPU.
            gpu.CopyFromDevice(dev_c, c);

            // Отображение результатов.
            for (int i = 0; i < N; i++)
                Console.WriteLine("{0} + {1} = {2}", a[i, 0], b[i, 0], c[i, 0]);
            Console.ReadLine();

            // Освобождение памяти выделенной ранее на GPU.
            gpu.FreeAll();
        }

        [Cudafy]
        public static void Calculation(GThread thread, double[,] a, double[,] b, double[,] c, int N)
        {
            int tid = thread.blockIdx.x;

            int period = 10;
            if (tid < N)
            {
                double sum = 0;
                for (int j = tid; j < tid + period; j++)
                    sum += a[j, 0];

                c[tid, 0] = a[tid, 0] + b[tid, 0] + sum;
            }      
        }
    }   
}

then the compiler began to give an error on the line gpu.LoadModule(module);.
How to replace type floatwith type in the code doubleso that the program will work without errors?
PS My graphics card supports OpenCL 1.2.
Video card model ASUS Radeon HD5570 .
P.S. I'm new to this business.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Smirnov, 2017-11-02
@ERAFY

https://stackoverflow.com/questions/2079906/float-...

Y
Yupa20171123, 2017-11-29
@Yupa20171123

Perhaps this should be turned on. Something like #pragma OPENCL EXTENSION cl_khr_...

cl_khr_fp64 --- Double precision floating-point
see [ https://www.khronos.org/registry/OpenCL/sdk/1.0/do...
I haven't gotten to that yet... :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question