예제 #1
0
        private void Test_addVector(eAddVectorMode addMode)
        {
            int[] a = new int[N];
            int[] b = new int[N];
            int[] c = new int[N];

            // allocate the memory on the GPU
            int[] dev_c = _gpu.Allocate <int>(c);

            // fill the arrays 'a' and 'b' on the CPU
            for (int i = 0; i < N; i++)
            {
                a[i] = -i;
                b[i] = i * i;
            }

            // copy the arrays 'a' and 'b' to the GPU
            int[] dev_a = _gpu.CopyToDevice(a);
            int[] dev_b = _gpu.CopyToDevice(b);

            if (addMode == eAddVectorMode.Smart)
            {
                _gpu.Launch(N, 1, "addVectorSmart", dev_a, dev_b, dev_c);
            }
            else if (addMode == eAddVectorMode.GlobalVar)
            {
                _gpu.Launch(N, 1, "addVector", dev_a, dev_b, dev_c);
            }
            else if (addMode == eAddVectorMode.GlobalVarDevice)
            {
                _gpu.Launch(N, 1, "addVectorDevice", dev_a, dev_b, dev_c);
            }
            else
            {
                throw new NotSupportedException(addMode.ToString());
            }

            // copy the array 'c' back from the GPU to the CPU
            _gpu.CopyFromDevice(dev_c, c);

            // test the results
            for (int i = 0; i < N; i++)
            {
                Assert.AreEqual(a[i] + b[i], c[i]);
            }

            // free the memory allocated on the GPU
            _gpu.FreeAll();
        }
        private void Test_addVector(eAddVectorMode addMode)
        {
            int[] a = new int[N];
            int[] b = new int[N];
            int[] c = new int[N];

            // allocate the memory on the GPU
            int[] dev_c = _gpu.Allocate<int>(c);

            // fill the arrays 'a' and 'b' on the CPU
            for (int i = 0; i < N; i++)
            {
                a[i] = -i;
                b[i] = i * i;
            }

            // copy the arrays 'a' and 'b' to the GPU
            int[] dev_a = _gpu.CopyToDevice(a);
            int[] dev_b = _gpu.CopyToDevice(b);

            if (addMode == eAddVectorMode.Smart)
                _gpu.Launch(N, 1, "addVectorSmart", dev_a, dev_b, dev_c);
            else if (addMode == eAddVectorMode.GlobalVar)
                _gpu.Launch(N, 1, "addVector", dev_a, dev_b, dev_c);
            else if (addMode == eAddVectorMode.GlobalVarDevice)
                _gpu.Launch(N, 1, "addVectorDevice", dev_a, dev_b, dev_c);
            else
                throw new NotSupportedException(addMode.ToString());

            // copy the array 'c' back from the GPU to the CPU
            _gpu.CopyFromDevice(dev_c, c);

            // test the results
            for (int i = 0; i < N; i++)
            {
                Assert.AreEqual(a[i] + b[i], c[i]);
            }

            // free the memory allocated on the GPU
            _gpu.FreeAll();
        }