用于双数组数学的C#OpenCL GPU实现

时间:2017-09-24 16:13:32

标签: c# .net opencl gpu

如何让这个函数的for循环使用GPU和OpenCL?

    public static double[] Calculate(double[] num, int period)
    {          
        var final = new double[num.Length];
        double sum = num[0];
        double coeff = 2.0 / (1.0 + period);

        for (int i = 0; i < num.Length; i++)
        {
            sum += coeff * (num[i] - sum);
            final[i] = sum;
        }

        return final;
    }

3 个答案:

答案 0 :(得分:5)

作为评论者Cory声明请参阅此链接进行设置。

How to use your GPU in .NET

以下是使用此项目的方法:

  1. 添加Nuget Package Cloo
  2. 添加对OpenCLlib.dll的引用
  3. 下载OpenCLLib.zip
  4. 使用OpenCL

    添加
    static void Main(string[] args)
    {
        int[] Primes = { 1,2,3,4,5,6,7 };
        EasyCL cl = new EasyCL();
        cl.Accelerator = AcceleratorDevice.GPU;
        cl.LoadKernel(IsPrime);
        cl.Invoke("GetIfPrime", 0, Primes.Length, Primes, 1.0);
    }
    
    static string IsPrime
    {
        get
        {
            return @"
            kernel void GetIfPrime(global int* num, int period)
            {
                int index = get_global_id(0);
    
                int sum = (2.0 / (1.0 + period)) * (num[index] - num[0]);
                printf("" %d \n"",sum);
            }";
        }
    }
    

答案 1 :(得分:3)

您写的问题不适合在GPU上运行的问题。您不能并行化(以提高性能的方式)对单个数组的操作,因为第n个元素的值取决于元素1到n。但是,您可以利用GPU处理多个阵列,其中每个GPU核心在单独的阵列上运行。

该解决方案的完整代码是在答案的最后,但测试结果,计算10,000个阵列,每个阵列有10,000个元素,生成以下内容(在GTX1080M和带有32GB RAM的i7 7700k上) ):

Task Generating Data: 1096.4583ms
Task CPU Single Thread: 596.2624ms
Task CPU Parallel: 179.1717ms
GPU CPU->GPU: 89ms
GPU Execute: 86ms
GPU GPU->CPU: 29ms
Task Running GPU: 921.4781ms
Finished

在此测试中,我们使用具有一个线程的CPU,具有所有线程的CPU以及最终使用所有核心的GPU来测量我们可以将结果生成到托管C#阵列的速度。 我们使用AreTheSame函数验证每个测试的结果是否相同。

最快的时间是使用所有线程处理CPU上的阵列(任务CPU并行:179ms)。

GPU实际上是最慢的(任务运行GPU:922ms),但这是因为重新格式化C#阵列所需的时间可以转移到GPU上。

如果删除了这个瓶颈(这很可能,取决于你的用例),GPU可能是最快的。如果数据已经以可以立即转移到GPU的方式格式化,则GPU的总处理时间将是204ms(CPU-> GPU:89ms +执行:86ms + GPU-> CPU:29ms = 204ms)。这仍然比并行CPU选项慢,但在不同类型的数据集上,它可能会更快。

为了从GPU(实际使用GPU的最重要部分)获取数据,我们使用ComputeCommandQueue.Read函数。这会将GPU上已更改的阵列传输回CPU。

要运行以下代码,请参考Cloo Nuget Package(我使用0.9.1)。并确保在x64上编译(您将需要内存)。如果找不到OpenCL设备,您可能还需要更新显卡驱动程序。

class Program
{
    static string CalculateKernel
    {
        get
        {
            return @"
            kernel void Calc(global int* offsets, global int* lengths, global double* doubles, double periodFactor) 
            {
                int id = get_global_id(0);
                int start = offsets[id];
                int length = lengths[id];
                int end = start + length;
                double sum = doubles[start];

                for(int i = start; i < end; i++)
                {
                    sum = sum + periodFactor * ( doubles[i] - sum );
                    doubles[i] = sum;
                }
            }";
        }
    }

    public static double[] Calculate(double[] num, int period)
    {
        var final = new double[num.Length];
        double sum = num[0];
        double coeff = 2.0 / (1.0 + period);

        for (int i = 0; i < num.Length; i++)
        {
            sum += coeff * (num[i] - sum);
            final[i] = sum;
        }

        return final;
    }


    static void Main(string[] args)
    {

        int maxElements = 10000;
        int numArrays = 10000;
        int computeCores = 2048;

        double[][] sets = new double[numArrays][];

        using (Timer("Generating Data"))
        {
            Random elementRand = new Random(1);
            for (int i = 0; i < numArrays; i++)
            {
                sets[i] = GetRandomDoubles(elementRand.Next((int)(maxElements * 0.9), maxElements), randomSeed: i);
            }
        }

        int period = 14;

        double[][] singleResults;
        using (Timer("CPU Single Thread"))
        {
            singleResults = CalculateCPU(sets, period);
        }

        double[][] parallelResults;
        using (Timer("CPU Parallel"))
        {
            parallelResults = CalculateCPUParallel(sets, period);
        }

        if (!AreTheSame(singleResults, parallelResults)) throw new Exception();

        double[][] gpuResults;
        using (Timer("Running GPU"))
        {
            gpuResults = CalculateGPU(computeCores, sets, period);
        }

        if (!AreTheSame(singleResults, gpuResults)) throw new Exception();


        Console.WriteLine("Finished");
        Console.ReadKey();
    }

    public static bool AreTheSame(double[][] a1, double[][] a2)
    {
        if (a1.Length != a2.Length) return false;
        for (int i = 0; i < a1.Length; i++)
        {
            var ar1 = a1[i];
            var ar2 = a2[i];
            if (ar1.Length != ar2.Length) return false;
            for (int j = 0; j < ar1.Length; j++)
                if (Math.Abs(ar1[j] - ar2[j]) > 0.0000001) return false;

        }
        return true;
    }

    public static double[][] CalculateGPU(int partitionSize, double[][] sets, int period)
    {
        ComputeContextPropertyList cpl = new ComputeContextPropertyList(ComputePlatform.Platforms[0]);
        ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero);


        ComputeProgram program = new ComputeProgram(context, new string[] { CalculateKernel });
        program.Build(null, null, null, IntPtr.Zero);

        ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None);

        ComputeEventList events = new ComputeEventList();

        ComputeKernel kernel = program.CreateKernel("Calc");


        double[][] results = new double[sets.Length][];

        double periodFactor = 2d / (1d + period);

        Stopwatch sendStopWatch = new Stopwatch();
        Stopwatch executeStopWatch = new Stopwatch();
        Stopwatch recieveStopWatch = new Stopwatch();


        int offset = 0;
        while (true)
        {
            int first = offset;
            int last = Math.Min(offset + partitionSize, sets.Length);
            int length = last - first;

            var merged = Merge(sets, first, length);

            sendStopWatch.Start();

            ComputeBuffer<int> offsetBuffer = new ComputeBuffer<int>(
                context,
                ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                merged.Offsets);

            ComputeBuffer<int> lengthsBuffer = new ComputeBuffer<int>(
                context,
                ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                merged.Lengths);

            ComputeBuffer<double> doublesBuffer = new ComputeBuffer<double>(
                context,
                ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
                merged.Doubles);



            kernel.SetMemoryArgument(0, offsetBuffer);
            kernel.SetMemoryArgument(1, lengthsBuffer);
            kernel.SetMemoryArgument(2, doublesBuffer);
            kernel.SetValueArgument(3, periodFactor);

            sendStopWatch.Stop();

            executeStopWatch.Start();

            commands.Execute(kernel, null, new long[] { merged.Lengths.Length }, null, events);

            executeStopWatch.Stop();

            using (var pin = Pinned(merged.Doubles))
            {
                recieveStopWatch.Start();
                commands.Read(doublesBuffer, false, 0, merged.Doubles.Length, pin.Address, events);
                commands.Finish();
                recieveStopWatch.Stop();
            }

            for (int i = 0; i < merged.Lengths.Length; i++)
            {
                int len = merged.Lengths[i];
                int off = merged.Offsets[i];

                var res = new double[len];
                Array.Copy(merged.Doubles,off,res,0,len);

                results[first + i] = res;
            }


            offset += partitionSize;
            if (offset >= sets.Length) break;
        }

        Console.WriteLine("GPU CPU->GPU: " + recieveStopWatch.ElapsedMilliseconds + "ms");
        Console.WriteLine("GPU Execute: " + executeStopWatch.ElapsedMilliseconds + "ms");
        Console.WriteLine("GPU GPU->CPU: " + sendStopWatch.ElapsedMilliseconds + "ms");


        return results;
    }

    public static PinnedHandle Pinned(object obj) => new PinnedHandle(obj);
    public class PinnedHandle : IDisposable
    {
        public IntPtr Address => handle.AddrOfPinnedObject();
        private GCHandle handle;
        public PinnedHandle(object val)
        {
            handle = GCHandle.Alloc(val, GCHandleType.Pinned);
        }
        public void Dispose()
        {
            handle.Free();
        }
    }

    public class MergedResults
    {
        public double[] Doubles { get; set; }
        public int[] Lengths { get; set; }
        public int[] Offsets { get; set; }
    }



    public static MergedResults Merge(double[][] sets, int offset, int length)
    {
        List<int> lengths = new List<int>(length);
        List<int> offsets = new List<int>(length);

        for (int i = 0; i < length; i++)
        {
            var arr = sets[i + offset];
            lengths.Add(arr.Length);
        }
        var totalLength = lengths.Sum();

        double[] doubles = new double[totalLength];
        int dataOffset = 0;
        for (int i = 0; i < length; i++)
        {
            var arr = sets[i + offset];
            Array.Copy(arr, 0, doubles, dataOffset, arr.Length);
            offsets.Add(dataOffset);
            dataOffset += arr.Length;
        }

        return new MergedResults()
        {
            Doubles = doubles,
            Lengths = lengths.ToArray(),
            Offsets = offsets.ToArray(),
        };
    }


    public static IDisposable Timer(string name)
    {
        return new SWTimer(name);
    }

    public class SWTimer : IDisposable
    {
        private Stopwatch _sw;
        private string _name;
        public SWTimer(string name)
        {
            _name = name;
            _sw = Stopwatch.StartNew();
        }
        public void Dispose()
        {
            _sw.Stop();
            Console.WriteLine("Task " + _name + ": " + _sw.Elapsed.TotalMilliseconds + "ms");
        }

    }

    public static double[][] CalculateCPU(double[][] arrays, int period)
    {
        double[][] results = new double[arrays.Length][];
        for (var index = 0; index < arrays.Length; index++)
        {
            var arr = arrays[index];
            results[index] = Calculate(arr, period);
        }
        return results;
    }

    public static double[][] CalculateCPUParallel(double[][] arrays, int period)
    {
        double[][] results = new double[arrays.Length][];
        Parallel.For(0, arrays.Length, i =>
         {
             var arr = arrays[i];
             results[i] = Calculate(arr, period);
         });
        return results;
    }


    static double[] GetRandomDoubles(int num, int randomSeed)
    {
        Random r = new Random(randomSeed);
        var res = new double[num];
        for (int i = 0; i < num; i++)
            res[i] = r.NextDouble() * 0.9 + 0.05;
        return res;
    }
}

答案 2 :(得分:2)

    for (int i = 0; i < num.Length; i++)
    {
        sum += coeff * (num[i] - sum);
        final[i] = sum;
    }

表示第一个元素乘以coeff 1次,并从第2个元素中减去。第一个元素也乘以coeff的平方,这个时间加到第三个元素。然后第一个元素乘以coeff的立方体,并从第4个元素中减去。

这是这样的:

-e0*c*c*c + e1*c*c - e2*c = f3
e0*c*c*c*c - e1*c*c*c + e2*c*c - e3*c = f4
-e0*c*c*c*c*c + e1*c*c*c*c - e2*c*c*c + e3*c*c - e4*c =f5  

对于所有元素,扫描所有较小的id元素并计算:

如果元素的id值(让我们称之为k)的差异为奇数,则采用减法,否则则采用加法。在加法或减法之前,将该值乘以系数的k次方。最后,将当前num值乘以系数,并将其添加到当前单元格。当前单元格值是最终的(i)。

这是O(N * N),看起来像一对全对计算内核。使用开源C#OpenCL项目的示例:

ClNumberCruncher cruncher = new ClNumberCruncher(ClPlatforms.all().gpus(), @"
    __kernel void foo(__global double * num, __global double * final, __global int *parameters)
    {
        int threadId           = get_global_id(0);
        int period             = parameters[0];
        double coeff           = 2.0 / (1.0 + period);    
        double sumOfElements   = 0.0;
        for(int i=0;i<threadId;i++)
        {
            // negativity of coeff is to select addition or subtraction for different powers of coeff
            double powKofCoeff =  pow(-coeff,threadId-i);
            sumOfElements     +=  powKofCoeff * num[i];                     
        }
        final[threadId]        =  sumOfElements + num[threadId] * coeff;
    }
");
cruncher.performanceFeed = true; // getting benchmark feedback on console
double[] numArray = new double[10000];
double[] finalArray = new double[10000];
int[] parameters = new int[10];
int period = 15;
parameters[0] = period;
ClArray<double> numGpuArray = numArray;
numGpuArray.readOnly = true; // gpus read this from host
ClArray<double> finalGpuArray = finalArray; // finalArray will have results
finalGpuArray.writeOnly = true; // gpus write this to host
ClArray<int> parametersGpu = parameters;
parametersGpu.readOnly = true;

// calculate kernels with exact same ordering of parameters
// num(double),final(double),parameters(int)
// finalGpuArray points to __global double * final
numGpuArray.nextParam(finalGpuArray, parametersGpu).compute(cruncher, 1, "foo", 10000, 100);

// first compute always lags because of compiling the kernel so here are repeated computes to get actual performance
numGpuArray.nextParam(finalGpuArray, parametersGpu).compute(cruncher, 1, "foo", 10000, 100);
numGpuArray.nextParam(finalGpuArray, parametersGpu).compute(cruncher, 1, "foo", 10000, 100);

结果在finalArray数组中包含10000个元素,每个工作项组使用100个工作项。

GPGPU部分在rx550 gpu上占用82ms,这对于64位到32位计算性能的比率非常低(因为消费者游戏卡在新系列中不擅长双倍精度)。 Nvidia Tesla或Amd Vega可以轻松地计算这个内核,而不会造成严重的性能损失。 Fx8150(8核)在683ms内完成。如果您只需要专门选择集成GPU及其CPU,则可以使用

创建ClPlatforms.all().gpus().devicesWithHostMemorySharing() + ClPlatforms.all().cpus()实例时

ClNumberCruncher

api的二进制文件:

https://www.codeproject.com/Articles/1181213/Easy-OpenCL-Multiple-Device-Load-Balancing-and-Pip

或在您的电脑上编译的源代码:

https://github.com/tugrul512bit/Cekirdekler

如果你有多个gpus,它会使用它们而不需要任何额外的代码。在计算中包含一个cpu会在第一次迭代中将gpu效率降低(使用cpu + gpu在76ms内完成重复),因此最好使用2-3 GPU而不是CPU + GPU。

我没有检查数值稳定性(在向同一个变量中添加数百万或更多值时应该使用Kahan-Summation但是我没有使用它来提高可读性,并且不知道64位值是否需要这个太像32位的)或任何值的正确性,你应该这样做。 foo内核也没有优化。它使核心时间的50%空闲,所以它应该更好地安排如下:

thread-0: compute element 0 and element N-1
thread-1: compute element 1 and element N-2
thread-m: compute element N/2-1 and element N/2

所以所有的工作项目都得到类似的工作量。除此之外,使用100作为工作组大小并​​不是最佳选择。它应该是128,256,512或1024(对于Nvidia),但这意味着数组大小也应该是这个的整数倍。然后,它需要内核中的额外控制逻辑才能脱离数组边界。为了获得更高的性能,for循环可以有多个部分和来进行“循环展开”。