public static CompileProperties Create(ePlatform platform = ePlatform.Auto, eArchitecture arch = eArchitecture.Default, eCudafyCompileMode mode = eCudafyCompileMode.Default, string workingDir = null, bool debugInfo = false)
        {
            CompileProperties tp       = new CompileProperties();
            eLanguage         language = GetLanguage(arch);

            if (language == eLanguage.Cuda)
            {
                string progFiles = Utility.ProgramFiles();

                tp.CompilerPath           = NvccExe.getCompilerPath();
                tp.IncludeDirectoryPath   = NvccExe.getIncludePath();
                tp.PathEnvVarExtraEntries = new string[1] {
                    NvccExe.getClExeDirectory()
                };

                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.Default : arch;
                bool   binary         = ((mode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary);
                string tempFileName   = "CUDAFYSOURCETEMP.tmp";
                string cuFileName     = tempFileName.Replace(".tmp", ".cu");
                string outputFileName = tempFileName.Replace(".tmp", binary ? ".cubin" : ".ptx");
                tp.InputFile  = cuFileName;
                tp.OutputFile = outputFileName;
                if ((mode & eCudafyCompileMode.DynamicParallelism) == eCudafyCompileMode.DynamicParallelism)
                {
                    tp.AdditionalInputArgs = "cudadevrt.lib  cublas_device.lib  -dlink";
                }
                if (arch == eArchitecture.Emulator)
                {
                    mode = eCudafyCompileMode.TranslateOnly;
                }
            }
            else
            {
                mode            = eCudafyCompileMode.TranslateOnly;
                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.OpenCL : arch;
            }
            tp.WorkingDirectory = Directory.Exists(workingDir) ? workingDir : Environment.CurrentDirectory;

            tp.Platform          = platform;
            tp.CompileMode       = mode;
            tp.GenerateDebugInfo = debugInfo;

            return(tp);
        }
        public static CompileProperties Create(ePlatform platform = ePlatform.Auto, eArchitecture arch = eArchitecture.sm_20, eCudafyCompileMode mode = eCudafyCompileMode.Default, string workingDir = null, bool debugInfo = false)
        {
            CompileProperties tp = new CompileProperties();
            eLanguage language = GetLanguage(arch);
            if (language == eLanguage.Cuda)
            {
                string progFiles = Utility.ProgramFiles();

                tp.CompilerPath = NvccExe.getCompilerPath();
                tp.IncludeDirectoryPath = NvccExe.getIncludePath();
                tp.PathEnvVarExtraEntries = new string[ 1 ] { NvccExe.getClExeDirectory() };

                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.sm_20 : arch;
                bool binary = ((mode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary);
                string tempFileName = "CUDAFYSOURCETEMP.tmp";
                string cuFileName = tempFileName.Replace(".tmp", ".cu");
                string outputFileName = tempFileName.Replace(".tmp", binary ? ".cubin" : ".ptx");
                tp.InputFile = cuFileName;
                tp.OutputFile = outputFileName;
                if ((mode & eCudafyCompileMode.DynamicParallelism) == eCudafyCompileMode.DynamicParallelism)
                {
                    tp.AdditionalInputArgs = "cudadevrt.lib  cublas_device.lib  -dlink";                    
                }
                if (arch == eArchitecture.Emulator)
                    mode = eCudafyCompileMode.TranslateOnly;
            }
            else
            {
                mode = eCudafyCompileMode.TranslateOnly;
                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.OpenCL : arch;
            }
            tp.WorkingDirectory = Directory.Exists(workingDir) ? workingDir : Environment.CurrentDirectory;

            tp.Platform = platform;
            tp.CompileMode = mode;         
            tp.GenerateDebugInfo = debugInfo;
            
            return tp;
        }
Exemplo n.º 3
0
        public static CompileProperties Create(ePlatform platform = ePlatform.Auto, eArchitecture arch = eArchitecture.sm_13, eCudafyCompileMode mode = eCudafyCompileMode.Default, string workingDir = null, bool debugInfo = false)
        {
            CompileProperties tp = new CompileProperties();
            eLanguage language = GetLanguage(arch);
            if (language == eLanguage.Cuda)
            {
                // Get ProgramFiles directory and CUDA directories
                // Get architecture
                string progFiles = null;
                switch (platform)
                {
                    case ePlatform.x64:
                        progFiles = Utility.ProgramFilesx64();
                        break;
                    case ePlatform.x86:
                        progFiles = Utility.ProgramFilesx86();
                        break;
                    default:
                        progFiles = Utility.ProgramFiles();
                        if (platform == ePlatform.Auto)
                            platform = IntPtr.Size == 4 ? ePlatform.x86 : ePlatform.x64;
                        break;
                }
                string toolkitbasedir = progFiles + Path.DirectorySeparatorChar + csGPUTOOLKIT;
                Version selVer;
                string cvStr = GetCudaVersion(toolkitbasedir, out selVer);
                if (string.IsNullOrEmpty(cvStr))
                    throw new CudafyCompileException(CudafyCompileException.csCUDA_DIR_NOT_FOUND);
                string gpuToolKit = progFiles + Path.DirectorySeparatorChar + csGPUTOOLKIT + cvStr;
                tp.CompilerPath = gpuToolKit + Path.DirectorySeparatorChar + @"bin" + Path.DirectorySeparatorChar + csNVCC;
                tp.IncludeDirectoryPath = gpuToolKit + Path.DirectorySeparatorChar + @"include";
                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.sm_13 : arch;
                bool binary = ((mode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary);
                string tempFileName = "CUDAFYSOURCETEMP.tmp";
                string cuFileName = tempFileName.Replace(".tmp", ".cu");
                string outputFileName = tempFileName.Replace(".tmp", binary ? ".cubin" : ".ptx");
                tp.InputFile = cuFileName;
                tp.OutputFile = outputFileName;
                if ((mode & eCudafyCompileMode.DynamicParallelism) == eCudafyCompileMode.DynamicParallelism)
                {
                    tp.AdditionalInputArgs = "cudadevrt.lib  cublas_device.lib  -dlink";
                    
                }
            }
            else
            {
                mode = eCudafyCompileMode.TranslateOnly;
                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.OpenCL : arch;
            }
            tp.WorkingDirectory = Directory.Exists(workingDir) ? workingDir : Environment.CurrentDirectory;

            tp.Platform = platform;
            tp.CompileMode = mode;         
            tp.GenerateDebugInfo = debugInfo;

            
            return tp;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compiles the module based on current Cuda source code and options.
        /// </summary>
        /// <param name="mode">The mode.</param>
        /// <param name="deleteGeneratedCode">if set to <c>true</c> delete generated code on success.</param>
        /// <param name="binary">Compile to binary if true.</param>
        /// <returns>The compile arguments.</returns>
        /// <exception cref="CudafyCompileException">No source code or compilation error.</exception>
        public string Compile(eGPUCompiler mode, bool deleteGeneratedCode = false, eCudafyCompileMode compileMode = eCudafyCompileMode.Default)
        {
            string ts = string.Empty;
            if ((mode & eGPUCompiler.CudaNvcc) == eGPUCompiler.CudaNvcc)
            {
                CompilerOutput = string.Empty;
                _PTXModules.Clear();
                _BinaryModules.Clear();
                if (!HasSourceCode)
                    throw new CudafyCompileException(CudafyCompileException.csNO_X_SOURCE_CODE_PRESENT_IN_CUDAFY_MODULE, "CUDA");

                if (CompilerOptionsList.Count == 0)
                {
                    var opt = IntPtr.Size == 4 ? NvccCompilerOptions.Createx86() : NvccCompilerOptions.Createx64();
                    opt.CompileMode = compileMode;
                    CompilerOptionsList.Add(opt);
                }
                bool binary = (compileMode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary;
                // Write to temp file
                string tempFileName = "CUDAFYSOURCETEMP.tmp";
                string cuFileName = WorkingDirectory + Path.DirectorySeparatorChar + tempFileName.Replace(".tmp", ".cu");
                string ptxFileName = WorkingDirectory + Path.DirectorySeparatorChar + tempFileName.Replace(".tmp", binary ? ".cubin" : ".ptx");
                File.WriteAllText(cuFileName, SourceCode, Encoding.Default);

                foreach (CompilerOptions co in CompilerOptionsList)
                {
                    co.GenerateDebugInfo = GenerateDebug;
                    co.TimeOut = TimeOut;
                    co.ClearSources();
                    co.AddSource(cuFileName);

                    co.ClearOutputs();
                    co.AddOutput(ptxFileName);

                    CompilerOutput += "\r\n" + co.GetSummary();
                    CompilerOutput += "\r\n" + co.GetArguments();

                    // Convert to ptx
                    Process process = new Process();
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = SuppressWindow;//WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    process.StartInfo.FileName = co.GetFileName();
                    process.StartInfo.Arguments = co.GetArguments();
                    CompilerArguments = process.StartInfo.Arguments;
                    process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                    process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
                    Debug.WriteLine(process.StartInfo.FileName);
                    Debug.WriteLine(CompilerArguments);
                    standardError.Clear(); standardOutput.Clear();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();
                    process.Start();
                    

                    //while (!process.HasExited)
                    //    Thread.Sleep(10);
                    int waitCounter = 0;
                    bool procTimedOut = false;
                    int timeout = co.TimeOut;
                    while (!process.HasExited && !(procTimedOut = ++waitCounter >= timeout)) // 1m timeout
                        //Thread.Sleep(10);
                        process.WaitForExit(10);
                    if (procTimedOut)
                        throw new CudafyCompileException(CudafyCompileException.csCOMPILATION_ERROR_X, "Process timed out");

                    if (process.ExitCode != 0)
                    {
                        string s = standardError.ToString(); //process.StandardError.ReadToEnd();
                        
                        CompilerOutput += "\r\n" + s;
                        if (s.Contains("Cannot find compiler 'cl.exe' in PATH"))
                            CompilerOutput += "\r\nPlease add the Visual Studio VC bin directory to PATH in Environment Variables.";
                        Debug.WriteLine(s);
                        throw new CudafyCompileException(CudafyCompileException.csCOMPILATION_ERROR_X, s);
                    }
                    else
                    {
                        string s = standardError.ToString() + "\r\n" + standardOutput.ToString();
                        CompilerOutput += "\r\n" + s;
                        Debug.WriteLine(s);
                    }

                    // Load ptx file
                    if ((compileMode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary)
                        this.StoreBinaryFile("na", co.Platform, co.Architecture, ptxFileName);                        
                    else
                        this.StorePTXFile("na", co.Platform, co.Architecture, ptxFileName);
#if DEBUG

#else
                    if (deleteGeneratedCode)
                        Delete(cuFileName, ptxFileName);
#endif
                }
            }
            return CompilerArguments;
        }
Exemplo n.º 5
0
        public static CompileProperties Create(ePlatform platform = ePlatform.Auto, eArchitecture arch = eArchitecture.sm_13, eCudafyCompileMode mode = eCudafyCompileMode.Default, string workingDir = null, bool debugInfo = false)
        {
            CompileProperties tp       = new CompileProperties();
            eLanguage         language = GetLanguage(arch);

            if (language == eLanguage.Cuda)
            {
                // Get ProgramFiles directory and CUDA directories
                // Get architecture
                string progFiles = null;
                switch (platform)
                {
                case ePlatform.x64:
                    progFiles = Utility.ProgramFilesx64();
                    break;

                case ePlatform.x86:
                    progFiles = Utility.ProgramFilesx86();
                    break;

                default:
                    progFiles = Utility.ProgramFiles();
                    if (platform == ePlatform.Auto)
                    {
                        platform = IntPtr.Size == 4 ? ePlatform.x86 : ePlatform.x64;
                    }
                    break;
                }
                string  toolkitbasedir = progFiles + Path.DirectorySeparatorChar + csGPUTOOLKIT;
                Version selVer;
                string  cvStr = GetCudaVersion(toolkitbasedir, out selVer);
                if (string.IsNullOrEmpty(cvStr))
                {
                    throw new CudafyCompileException(CudafyCompileException.csCUDA_DIR_NOT_FOUND);
                }
                string gpuToolKit = progFiles + Path.DirectorySeparatorChar + csGPUTOOLKIT + cvStr;
                tp.CompilerPath         = gpuToolKit + Path.DirectorySeparatorChar + @"bin" + Path.DirectorySeparatorChar + csNVCC;
                tp.IncludeDirectoryPath = gpuToolKit + Path.DirectorySeparatorChar + @"include";
                tp.Architecture         = (arch == eArchitecture.Unknown) ? eArchitecture.sm_13 : arch;
                bool   binary         = ((mode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary);
                string tempFileName   = "CUDAFYSOURCETEMP.tmp";
                string cuFileName     = tempFileName.Replace(".tmp", ".cu");
                string outputFileName = tempFileName.Replace(".tmp", binary ? ".cubin" : ".ptx");
                tp.InputFile  = cuFileName;
                tp.OutputFile = outputFileName;
                if ((mode & eCudafyCompileMode.DynamicParallelism) == eCudafyCompileMode.DynamicParallelism)
                {
                    tp.AdditionalInputArgs = "cudadevrt.lib  cublas_device.lib  -dlink";
                }
            }
            else
            {
                mode            = eCudafyCompileMode.TranslateOnly;
                tp.Architecture = (arch == eArchitecture.Unknown) ? eArchitecture.OpenCL : arch;
            }
            tp.WorkingDirectory = Directory.Exists(workingDir) ? workingDir : Environment.CurrentDirectory;

            tp.Platform          = platform;
            tp.CompileMode       = mode;
            tp.GenerateDebugInfo = debugInfo;


            return(tp);
        }