首页 > 解决方案 > 以下使用 ResidentArrayGeneric 的 Hybridizer 代码有什么问题?

问题描述

以下代码从浮点数(图像)数组中提取正方形选择,并将其放置在 float2 数组中。float2 和原始数组都是表示图像中连续行的一维数组。原始浮点值放在 x 字段中,y 字段设置为零。下面的代码无法使用 Hybridizer 编译,我不知道为什么。错误之一是:-

'不存在合适的构造函数来将“float”转换为“float2”'第 32 行

这是内核右括号的行。我没有直接从 float 转换为 float2 所以我很困惑!

有谁知道出了什么问题?

using Hybridizer.Runtime.CUDAImports;
using System;

namespace residentarraytest
{

    class Program
    {
        [EntryPoint]
        private static void ExtractArea(FloatResidentArray im,
                                ResidentArrayGeneric<int> roi,
                                ResidentArrayGeneric<float2> res)
        {
            int cp = roi[2] / 2;
            int yPos = (1024 - roi[1]) - cp; // Y reversal just because...
            int xPos = roi[0] - cp;
            ExtractLoop(im, res, roi[2], yPos, xPos);
            return;
        }

        [Kernel]
        private static void ExtractLoop(FloatResidentArray im,
                                   ResidentArrayGeneric<float2> res, 
                                   int size, int yPos, int xPos)
        {
            Parallel2D.For(0, size, 0, size, (y, x) =>
            {
                int i = x + y * size;
                int j = x + xPos + (y + yPos) * size;
                float2 tmp = new float2(im[j], 0f);                
                res[i] = tmp;
            });
        }

        static void Main(string[] args)
        {
            ResidentArrayGeneric<int> roi = new ResidentArrayGeneric<int>(3);
            roi[0] = 512; roi[1] = 512; roi[2] = 256;
            FloatResidentArray im = new FloatResidentArray(1024 * 1024);
            // im is filled with data here;

            ResidentArrayGeneric<float2> res = 
                new ResidentArrayGeneric<float2>(roi[2] * roi[2]);

            cudaDeviceProp prop;
            cuda.GetDeviceProperties(out prop, 0);            

            HybRunner runner = HybRunner.Cuda();

            // create a wrapper object to call GPU methods instead of C#
            dynamic wrapped = runner.Wrap(new Program());

            roi.RefreshDevice();
            im.RefreshDevice();
            res.RefreshDevice();
            wrapped.ExtractArea(im, roi, res);

            // program continues
            Console.Out.WriteLine("DONE");
        }
    }
}

标签: c#hybridizer

解决方案


您的代码没有任何问题。这是一个普通的混合器错误(在 cuda 界面中)。

我在 Altimesh github中记录了一个问题。

float2 类型(这是一种内部驱动程序类型)没有以 float 作为参数的构造函数。您可以查看CUDA 文档

因此,不应公开 C# 构造函数 float2::float2(x, y)。

我们将make_float2在下一个版本中公开该功能。

同时,您可以使用以下代码自己完成:

[IntrinsicFunction("make_float2")]
public static float2 make_float2(float x, float y)
{
    return new float2(x, y);
}

[Kernel]
private static void ExtractLoop(FloatResidentArray im,
                           ResidentArrayGeneric<float2> res,
                           int size, int yPos, int xPos)
{
    Parallel2D.For(0, size, 0, size, (y, x) =>
    {
        int i = x + y * size;
        int j = x + xPos + (y + yPos) * size;
        float2 tmp = make_float2(im[j], 0f);
        res[i] = tmp;
    });
}

推荐阅读