Can Alea Gpu just copy the last element in an array?

176 views Asked by At

I am using Alea GPU to program on GPU using C# language. In my project, I want to reduce all zero elements in an array on GPU, so I plan to use exclusive sum scan to implement it. The next step is I have to get the last element of my scan results in order to find all non-zero elements in an algorithm. In this step, I don't want to copy all results to host, because it is very expensive, but I fail to find a method to extract the last element without copying all elements(Maybe there's a way I don't know?).

Here's my code on my scan part. d_voxeOccupy is a sprase array on the device. What should I do for getting the last element of d_voxeOccupyScan?

    var op = new Func<int, int, int>((a, b) => { return a + b; });
    Alea.Session session = new Alea.Session(gpu);
    var d_voxeOccupyScan = gpu.Allocate<int>(numVoxels);
    GpuExtension.Scan<int>(session, d_voxeOccupyScan, d_voxeOccupy, 0, op, 0);

===UPDATE===

I made an example to explain this problem clearly.

        static void Main(string[] args)
        {
            int[] arrayA = new int[14]{ 0, 0, 3, 0, 0, 6, 0, 9, 0, 12, 0, 0, 0, 15 };

            var gpu = Gpu.Default;
            var op = new Func<int, int, int>((a, b) => { return a + b; });
            Alea.Session session = new Alea.Session(gpu);
            var d_voxeOccupyScan = gpu.AllocateDevice<int>(14);
            var d_voxeOccupy = gpu.AllocateDevice<int>(arrayA);
            GpuExtension.Scan<int>(session, d_voxeOccupyScan.Ptr, d_voxeOccupy.Ptr, 0, 14,op, 0);

            var result = Gpu.CopyToHost(d_voxeOccupyScan);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

Run this code, we will get an array, the last element is 45. How to extract the last element from this array rather than copying all elements?

0

There are 0 answers