Answer the question
In order to leave comments, you need to log in
How to use the double type in GPU calculations?
I'm doing math on the GPU. For this I wrote the following code:
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;
}
}
}
}
float
. I need to use the type double
. float
withdouble
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;
}
}
}
}
gpu.LoadModule(module);
. float
with type in the code double
so that the program will work without errors? Answer the question
In order to leave comments, you need to log in
Perhaps this should be turned on. Something like #pragma OPENCL EXTENSION cl_khr_...
cl_khr_fp64 --- Double precision floating-pointsee [ https://www.khronos.org/registry/OpenCL/sdk/1.0/do...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question