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;
        }
Example #3
0
        public void Compile(CompileProperties[] props)
        {
            string ts = string.Empty;
            CompilerOutput = string.Empty;
            _PTXModules.Clear();
            _BinaryModules.Clear();

            foreach (CompileProperties p in props)
            {
                if ((p.CompileMode & eCudafyCompileMode.TranslateOnly) != eCudafyCompileMode.TranslateOnly)
                {
                    bool binary = (p.CompileMode & eCudafyCompileMode.Binary) == eCudafyCompileMode.Binary;
                    
                    // Write to temp file
                    string cuFileName = p.WorkingDirectory + Path.DirectorySeparatorChar + p.InputFile;
                    SourceCodeFile srcCodeFile = GetSourceCodeFile(p.Architecture);
                    if (srcCodeFile == null)
                        throw new CudafyCompileException(CudafyCompileException.csNO_X_SOURCE_CODE_PRESENT_IN_CUDAFY_MODULE_FOR_X,
                            GetLanguageFromArchitecture(p.Architecture), p.Architecture);
                    File.WriteAllText(cuFileName, srcCodeFile.Source, Encoding.Default);
                    var originalPlatformSetting = p.Platform;
                    var platforms = new List<ePlatform>();
                    if (p.Platform == ePlatform.All)
                        platforms.AddRange(new[] { ePlatform.x64, ePlatform.x86 });
                    else if (p.Platform == ePlatform.Auto)
                        platforms.Add(CurrentPlatform);
                    else
                        platforms.Add(p.Platform);
                    foreach (ePlatform platform in platforms)
                    {
                        p.Platform = platform;

                        // Call nvcc
                        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 = string.Format(@"""{0}""", p.CompilerPath);
                        process.StartInfo.Arguments = p.GetCommandString();
                        p.Platform = originalPlatformSetting;
                        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.Start();
                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();

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

                        if (process.ExitCode != 0)
                        {
                            string s = standardOutput.ToString() + "\r\n" + standardError.ToString();

                            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(CompilerOutput);
                            throw new CudafyCompileException(CudafyCompileException.csCOMPILATION_ERROR_X, CompilerOutput);
                        }
                        else
                        {
                            string s = standardError.ToString() + "\r\n" + standardOutput.ToString();
                            CompilerOutput += "\r\n" + s;
                            Debug.WriteLine(s);
                        }

                        // Load ptx file
                        if (binary)
                            this.StoreBinaryFile(srcCodeFile.ID, platform, p.Architecture, p.WorkingDirectory + Path.DirectorySeparatorChar + p.OutputFile);
                        else
                            this.StorePTXFile(srcCodeFile.ID, platform, p.Architecture, p.WorkingDirectory + Path.DirectorySeparatorChar + p.OutputFile);
#if DEBUG

#else
                    if (p.DeleteGeneratedFiles)
                        Delete(p.InputFile, p.OutputFile);
#endif
                    }
                }
            }
        }
Example #4
0
 public void Compile(CompileProperties p)
 {
     Compile(new[] { p });
 }
Example #5
0
 public static CudafyModule Cudafy(CompileProperties prop, params Type[] types)
 {
     var list = new List<CompileProperties>();
     list.Add(prop);
     return Cudafy(list, types);
 }
        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;
        }
Example #7
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);
        }