Esempio n. 1
0
 public void PtxCompileNVCCMultiprocess()  ///  NVCC
 {
     Parallel.ForEach(sourceCodes, (sc) =>
     {
         var km         = new CudafyModule();
         km.SourceCode  = sc.rendered;
         var co         = NvccCompilerOptions.Createx64(new Version(8, 0), eArchitecture.sm_30);
         co.CompileMode = eCudafyCompileMode.Binary;
         co.AddOption("-w");
         km.CompilerOptionsList.Add(co);
         km.Compile(eGPUCompiler.CudaNvcc, false, eCudafyCompileMode.Binary, rng.Next().ToString());
         sc.cubin = km.Binary.Binary;
     });
 }
Esempio n. 2
0
        private void btnCompile_Click(object sender, EventArgs e)
        {
            try
            {
                if (_module == null)
                {
                    _module = new CudafyModule();
                }

                if (_module != null)
                {
                    eArchitecture arch = (eArchitecture)Enum.Parse(typeof(eArchitecture), cbArch.SelectedItem as string);
                    if (arch == eArchitecture.OpenCL)
                    {
                        MessageBox.Show(this, "OpenCL modules are not compiled.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (!cb32bit.Checked && !cb64bit.Checked)
                    {
                        MessageBox.Show(this, "Select a platform.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    _module.SourceCode = tbSource.Text;

                    NvccCompilerOptions opt = null;
                    _module.CompilerOptionsList.Clear();
                    _module.RemovePTXModules();
                    if (cb64bit.Checked)
                    {
                        opt = NvccCompilerOptions.Createx64(arch);
                        _module.CompilerOptionsList.Add(opt);
                    }
                    if (cb32bit.Checked)
                    {
                        opt = NvccCompilerOptions.Createx86(arch);
                        _module.CompilerOptionsList.Add(opt);
                    }


                    _module.Compile(eGPUCompiler.CudaNvcc, false);
                    FillForm();
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Cudafies the specified types. Working directory will be as per CudafyTranslator.WorkingDirectory.
        /// </summary>
        /// <param name="platform">The platform.</param>
        /// <param name="arch">The CUDA or OpenCL architecture.</param>
        /// <param name="cudaVersion">The CUDA version. Specify null to automatically use the highest installed version.</param>
        /// <param name="compile">if set to <c>true</c> compile to PTX.</param>
        /// <param name="types">The types.</param>
        /// <returns>A CudafyModule.</returns>
        public static CudafyModule CudafyOld(ePlatform platform, eArchitecture arch, Version cudaVersion, bool compile, params Type[] types)
        {
            CudafyModule km = null;

            CUDALanguage.ComputeCapability = GetComputeCapability(arch);
            _architecture = arch;
            if (arch > eArchitecture.OpenCL)
            {
                CudafyTranslator.Language = eLanguage.OpenCL;
            }
            km = DoCudafy(null, types);
            if (km == null)
            {
                throw new CudafyFatalException(CudafyFatalException.csUNEXPECTED_STATE_X, "CudafyModule km = null");
            }
            km.WorkingDirectory = WorkingDirectory;
            if (compile && LanguageSpecifics.Language == eLanguage.Cuda)
            {
                if (platform == ePlatform.Auto)
                {
                    platform = IntPtr.Size == 8 ? ePlatform.x64 : ePlatform.x86;
                }
                if (platform != ePlatform.x86)
                {
                    km.CompilerOptionsList.Add(NvccCompilerOptions.Createx64(cudaVersion, arch));
                }
                if (platform != ePlatform.x64)
                {
                    km.CompilerOptionsList.Add(NvccCompilerOptions.Createx86(cudaVersion, arch));
                }
                km.GenerateDebug = GenerateDebug;
                km.TimeOut       = TimeOut;
                km.Compile(eGPUCompiler.CudaNvcc, DeleteTempFiles);
            }
            Type lastType = types.Last(t => t != null);

            if (lastType != null)
            {
                km.Name = lastType.Name;
            }
            return(km);
        }
Esempio n. 4
0
        public static IEnumerable <string> TestCUDASDK()
        {
            StringBuilder sb = new StringBuilder();

            NvccCompilerOptions nvcc = null;

            if (IntPtr.Size == 8)
            {
                nvcc = NvccCompilerOptions.Createx64();
            }
            else
            {
                nvcc = NvccCompilerOptions.Createx86();
            }
            yield return(string.Format("Platform={0}", nvcc.Platform));

            yield return("Checking for CUDA SDK at " + nvcc.CompilerPath);

            if (!nvcc.TryTest())
            {
                yield return("Could not locate CUDA Include directory.");
            }
            else
            {
                yield return(string.Format("CUDA SDK Version={0}", nvcc.Version));

                yield return("Attempting to cudafy a kernel function.");

                var mod = CudafyTranslator.Cudafy(nvcc.Platform, eArchitecture.sm_11, nvcc.Version, false, typeof(CUDACheck));
                yield return("Successfully translated to CUDA C.");

                yield return("Attempting to compile CUDA C code.");

                string s = mod.Compile(eGPUCompiler.CudaNvcc, true);
                yield return("Successfully compiled CUDA C into a module.");

                if (CudafyHost.GetDeviceCount(eGPUType.Cuda) > 0)
                {
                    yield return("Attempting to instantiate CUDA device object (GPGPU).");

                    var gpu = CudafyHost.GetDevice(eGPUType.Cuda, 0);
                    yield return("Successfully got CUDA device 0.");

                    yield return("Attempting to load module.");

                    gpu.LoadModule(mod);
                    yield return("Successfully loaded module.");

                    yield return("Attempting to transfer data to GPU.");

                    int[]  a    = new int[1024];
                    int[]  b    = new int[1024];
                    int[]  c    = new int[1024];
                    Random rand = new Random();
                    for (int i = 0; i < 1024; i++)
                    {
                        a[i] = rand.Next(16384);
                        b[i] = rand.Next(16384);
                    }
                    int[] dev_a = gpu.CopyToDevice(a);
                    int[] dev_b = gpu.CopyToDevice(b);
                    int[] dev_c = gpu.Allocate(c);
                    yield return("Successfully transferred data to GPU.");

                    yield return("Attempting to launch function on GPU.");

                    gpu.Launch(1, 1024).TestKernelFunction(dev_a, dev_b, dev_c);
                    yield return("Successfully launched function on GPU.");

                    yield return("Attempting to transfer results back from GPU.");

                    gpu.CopyFromDevice(dev_c, c);
                    yield return("Successfully transferred results from GPU.");

                    yield return("Testing results.");

                    int errors = 0;
                    for (int i = 0; i < 1024; i++)
                    {
                        if (a[i] + b[i] != c[i])
                        {
                            errors++;
                        }
                    }
                    if (errors == 0)
                    {
                        yield return("Successfully tested results.");
                    }
                    else
                    {
                        yield return("Test failed - results not as expected.");
                    }

                    yield return("Checking for math libraries (FFT, BLAS, SPARSE, RAND).");

                    var fft     = GPGPUFFT.Create(gpu);
                    int version = fft.GetVersion();
                    if (version > 0)
                    {
                        yield return("Successfully detected.");
                    }
                }
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            try
            {
                if (args == null || args.Length == 0)
                {
                    Console.WriteLine("--list for architecture targets");
                    Console.WriteLine("--print for some SDK/GPU info");
                    Console.WriteLine("--all to iterate over all target");

                    return;
                }

                if (args?.FirstOrDefault() == "--print")
                {
                    NvccCompilerOptions nvcc;
                    if (IntPtr.Size == 8)
                    {
                        nvcc = NvccCompilerOptions.Createx64();
                    }
                    else
                    {
                        nvcc = NvccCompilerOptions.Createx86();
                    }

                    Console.WriteLine(string.Format("Platform={0}", nvcc.Platform));
                    Console.WriteLine("CUDA SDK at " + nvcc.CompilerPath);
                    Console.WriteLine("Test: " + nvcc.TryTest());
                    Console.WriteLine("Press anykey for cards...");
                    Console.ReadKey();
                    Console.WriteLine("Reading...");

                    foreach (var t in new eGPUType[] { eGPUType.Cuda, eGPUType.Emulator, eGPUType.OpenCL })
                    {
                        CudafyModes.Target = t;
                        printInfo();
                    }
                    return;
                }

                var ars = new eArchitecture[] {
                    eArchitecture.OpenCL,
                    eArchitecture.Emulator,
                    eArchitecture.sm_10,
                    eArchitecture.sm_11,
                    eArchitecture.sm_12,
                    eArchitecture.sm_13,
                    eArchitecture.sm_20,
                    eArchitecture.sm_21,
                    eArchitecture.sm_30,
                    eArchitecture.sm_35,
                    eArchitecture.sm_37,
                    eArchitecture.sm_50,
                    eArchitecture.sm_52,
                    eArchitecture.OpenCL11,
                    eArchitecture.OpenCL12
                };

                if (args?.FirstOrDefault() == "--list")
                {
                    Console.WriteLine(string.Join(Environment.NewLine, ars.Select(a => a + ": " + (int)a)));
                    return;
                }

                if (args?.FirstOrDefault() != "--all")
                {
                    ars = args.Select(ii => (eArchitecture)int.Parse(ii)).ToArray();
                }

                foreach (var a in ars)
                {
                    try
                    {
                        Random rand = new Random(4);

                        Console.WriteLine("Benching " + a);
                        Console.WriteLine("Init: " + MeasureTime(() => RuneCalc.init(a, true, true)));

                        int[] num_runes = new int[] { 79, 103, 81, 93, 88, 90 };

                        int num_stats = 8;

                        RuneCalc.flat = new int[num_runes.Sum(), 8];
                        RuneCalc.mult = new int[num_runes.Sum(), 8];

                        Console.WriteLine("gen: " + MeasureTime(() =>
                        {
                            for (int slot = 0; slot < 6; slot++)
                            {
                                for (int rune = 0; rune < num_runes[slot]; rune++)
                                {
                                    for (int s = 0; s < num_stats; s++)
                                    {
                                        if (s < 3)
                                        {
                                            RuneCalc.flat[rune, s] = rand.Next(0, 50);
                                            RuneCalc.mult[rune, s] = rand.Next(0, 20);
                                        }
                                        else
                                        {
                                            RuneCalc.flat[rune, s] = rand.Next(0, 10);
                                            RuneCalc.mult[rune, s] = 0;
                                        }
                                    }
                                }
                            }

                            RuneCalc.stat = new int[] { 3500, 530, 403, 101, 15, 50, 15, 0 };
                        }));

                        Console.WriteLine("seed: " + MeasureTime(() => RuneCalc.Seed()));

                        List <RunData> rd         = new List <RunData>();
                        int            reps       = 10;
                        int            num_builds = 2 << 23;              // 2^31 / 32 (int) / 8 (2d size)
                        for (int i = 0; i < reps; i++)
                        {
                            Console.WriteLine("\rRun " + i);
                            Console.Write("Building data\r");
                            int[,] b = new int[num_builds, 6];
                            var d = MeasureTime(() =>
                            {
                                for (var j = 0; j < num_builds; j++)
                                {
                                    int rn = 0;
                                    for (int k = 0; k < 6; k++)
                                    {
                                        b[j, k] = rand.Next(rn, rn + num_runes[k]);
                                        rn     += num_runes[k];
                                    }
                                }
                            });
                            var runData = RuneCalc.Execute(b);
                            runData.dataTime = d;
                            rd.Add(runData);
                            Console.CursorTop -= 1;
                        }
                        Console.WriteLine();
                        Console.WriteLine("Av Dat: " + rd.Average(qr => qr.dataTime));
                        Console.WriteLine("Av CPU: " + rd.Average(qr => qr.cpuTime));
                        Console.WriteLine("Av GPU: " + rd.Average(qr => qr.gpuTime));
                        Console.WriteLine("Av On: " + rd.Average(qr => qr.gpuOn));
                        Console.WriteLine("Av Off: " + rd.Average(qr => qr.gpuOff));
                        Console.WriteLine("Av SPD: " + 100 * (1 / (rd.Average(qr => qr.gpuTime) / rd.Average(qr => qr.cpuTime))));
                        Console.WriteLine("Av Ttl: " + 100 * (1 / ((rd.Average(qr => qr.gpuTime) + rd.Average(qr => qr.gpuOn) + rd.Average(qr => qr.gpuOff)) / rd.Average(qr => qr.cpuTime))));
                        Console.WriteLine("Av sse: " + rd.Average(qr => qr.sumSq));
                        Console.WriteLine();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.GetType() + ": " + e.Message + Environment.NewLine + e.StackTrace);
                    }
                }
            }
            finally
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.WriteLine("Press anykey to exit...");
                    Console.Read();
                }
            }
        }