Exemplo n.º 1
0
        double[] ExecuteGPU(int size)
        {
            // いったんメインメモリ上に変数を準備
            List <double> a = new List <double>();
            List <double> b = new List <double>();

            for (int i = 0; i < size * size; i++)
            {
                a.Add(i + 1);
                b.Add((i + 1) * 10);
            }

            // デバイス上にメモリを転送
            DeviceMemory memory = new DeviceMemory();

            memory.Add <double>("a", a);
            memory.Add <double>("b", b);
            memory.Alloc <double>("c", size * size);

            // 関数の実行
            CallMethod(
                "matrixDot", size, size,
                memory["a"],
                memory["b"],
                memory["c"],
                size,
                size
                );

            // 全てのスレッドが終了するまで待つ
            context.Synchronize();

            // 結果を取得して出力画面に表示
            double[] result = memory.Read <double>("c", size * size);

            // リソースを解放する
            memory.Dispose();

            return(result);
        }
Exemplo n.º 2
0
 public void Register(IDisposable disposable) => _cache.Add(disposable);
Exemplo n.º 3
0
        private void TestCompile()
        {
            // プログラムのコンパイル (cu から PTX へ)
            RuntimeCompiler compiler = new RuntimeCompiler();

            //compiler.AddHeader("curand_kernel.h", @"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\curand_kernel.h");
            compiler.AddOptions(
                RuntimeCompiler.OPTION_TARGET_30,
                RuntimeCompiler.OPTION_FMAD_FALSE,
                RuntimeCompiler.OPTION_LINE_INFO,
                RuntimeCompiler.OPTION_DEVICE_AS_DEFAULT_EXECUTION_SPACE
                //RuntimeCompiler.OPTION_INCLUDE_PATH_ + @"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\"
                );

            string ptx = compiler.Compile("addKernel.cu", addKernelString);

            if (ptx == null)
            {
                Console.WriteLine("Compile Error:");
                Console.WriteLine();
                Console.WriteLine(compiler.Log);
                return;
            }

            // コンパイル時のログを出力画面に表示
            Console.WriteLine("----- <Compile Log>");
            Console.WriteLine(compiler.Log);
            Console.WriteLine("----- </Compile Log>");

            // コンパイル済みプログラムを出力画面に表示
            Console.WriteLine("----- <PTX>");
            Console.WriteLine(ptx);
            Console.WriteLine("----- </PTX>");

            // プログラムの実行準備
            Device  device  = new Device(0);
            Context context = device.CreateContext();
            Module  module  = new Module();

            Console.WriteLine(device.Name);
            Console.WriteLine(device.PCIBusId);
            Console.WriteLine(device.TotalMem);
            //Console.WriteLine(device.GetProperties());
            Console.WriteLine(context.ApiVersion);
            //return;

            // PTX データをロード
            module.LoadData(ptx);

            // いったんメインメモリ上に変数を準備
            const int  arraySize = 5;
            List <int> a         = new List <int>();
            List <int> b         = new List <int>();

            for (int i = 0; i < arraySize; i++)
            {
                a.Add(i + 1);
                b.Add((i + 1) * 10);
            }

            // デバイス上にメモリを転送
            DeviceMemory memory = new DeviceMemory();

            memory.Add <int>("a", a);
            memory.Add <int>("b", b);
            memory.Alloc <int>("c", arraySize);

            // 関数の実行
            module.SetBlockCount(1, 1, 1);
            module.SetThreadCount(arraySize, 1, 1);
            module.Excecute(
                "addKernel",
                memory["c"],
                memory["a"],
                memory["b"]
                );

            // 全てのスレッドが終了するまで待つ
            context.Synchronize();

            // 結果を取得して出力画面に表示
            int[] results = memory.Read <int>("c", arraySize);
            Console.WriteLine("----- <Execute Log>");
            for (int i = 0; i < arraySize; i++)
            {
                Console.WriteLine("{0} + {1}  = {2}", a[i], b[i], results[i]);
            }
            Console.WriteLine("----- </Execute Log>");

            // リソースを解放する
            memory.Dispose();
            module.Dispose();
            context.Dispose();
        }