我的问题

嗨,所以我要进行此简单的计算,以找到0到100度之间的正弦之和(因为我将其用作系统的基准),因此计算并不是我遇到的问题,因为我是Cudafy的新手我不确定如何正确传入和返回值,以便可以将其打印出来,这是我的代码:



    public const int N = 33 * 1024;
    public const int threadsPerBlock = 256;
    public const int blocksPerGrid = 32;

    public static void Main()
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();
        string Text = "";
        int iterations = 1000000;
        CudafyModule km = CudafyTranslator.Cudafy();
        GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
        gpu.LoadModule(km);
        double[] dev_Value = gpu.Allocate<double>();
        gpu.Launch(blocksPerGrid, threadsPerBlock).SumOfSines(iterations,dev_Value);

        double Value;
        gpu.CopyFromDevice(dev_Value, out Value);
        watch.Stop();
        Text = watch.Elapsed.TotalSeconds.ToString();
        Console.WriteLine("The process took a total of: " + Text + " Seconds");
        Console.WriteLine(Value);
        Console.Read();
        gpu.FreeAll();
    }
    [Cudafy]
    public static void SumOfSines(GThread thread,int iterations,double [] Value)
    {
        double total = new double();
        double degAsRad = Math.PI / 180.0;
        for (int i = 0; i < iterations; i++)
        {
            total = 0.0;
            for (int z = 1; z < 101; z++)
            {
                double angle = (double)z * degAsRad;
                total += Math.Sin(angle);
            }

        }
        Value[0] = total;


    }


我试图从CUDAfy部分中提取的值是总计,然后将其打印出来并打印基准测试的时间。如果有人可以发表建议,将不胜感激(消除任何无用的线条或无效的部分的建议也将是很好的)。

最佳答案

没关系,我找到了答案,但是我将其发布在这里:

    public const int N = 33 * 1024;
    public const int threadsPerBlock = 256;
    public const int blocksPerGrid = 32;

    public static void Main()
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();
        CudafyModule km = CudafyTranslator.Cudafy();

        GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
        gpu.LoadModule(km);

        string Text = "";
        int iterations = 1000000;
        double Value;
        double[] dev_Value = gpu.Allocate<double>(iterations * sizeof(double));
        gpu.Launch(blocksPerGrid, threadsPerBlock).SumOfSines(iterations, dev_Value);
        gpu.CopyFromDevice(dev_Value, out Value);
        watch.Stop();
        Text = watch.Elapsed.TotalSeconds.ToString();
        Console.WriteLine("The process took a total of: " + Text + " Seconds");
        Console.WriteLine(Value);
        Console.Read();
        gpu.FreeAll();
    }

    [Cudafy]
    public static void SumOfSines(GThread thread, int _iterations, double[] Value)
    {
        int threadID = thread.threadIdx.x + thread.blockIdx.x * thread.blockDim.x;
        int numThreads = thread.blockDim.x * thread.gridDim.x;
        if (threadID < _iterations){
            for (int i = threadID; i < _iterations; i += numThreads)
            {
                double _degAsRad = Math.PI / 180;
                Value[i] = 0.0;
                for (int a = 0; a < 100; a++)
                {
                    double angle = (double)a * _degAsRad;
                    Value[i] += Math.Sin(angle);
                }
            }
        }
    }


-插口

关于c# - 如何从Cudafy C#GPU计算返回值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40017649/

10-17 02:11