Пример #1
0
        public GPU_func()
        {
            km = CudafyTranslator.Cudafy();

            gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
            gpu.LoadModule(km);

            GPU_prop = gpu.GetDeviceProperties();
        }
Пример #2
0
        ///// <summary>
        ///// Initializes a new instance of the <see cref="KernelMethodInfo"/> class.
        ///// </summary>
        ///// <param name="type">The type.</param>
        ///// <param name="method">The method.</param>
        ///// <param name="gpuMethodType">Type of the gpu method.</param>
        ///// <param name="noDummyInclude"></param>
        ///// <param name="parentModule"></param>
        //public KernelMethodInfo(Type type, MethodInfo method, eKernelMethodType gpuMethodType, bool noDummyInclude, CudafyModule parentModule)
        //    : this(type, method, gpuMethodType, false, false, parentModule)
        //{
        //}

        /// <summary>
        /// Initializes a new instance of the <see cref="KernelMethodInfo"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="method">The method.</param>
        /// <param name="gpuMethodType">Type of the gpu method.</param>
        /// <param name="isDummy">if set to <c>true</c> is dummy.</param>
        /// <param name="behaviour"></param>
        /// <param name="parentModule">Module of which this is a part.</param>
        public KernelMethodInfo(Type type, MethodInfo method, eKernelMethodType gpuMethodType, bool isDummy, eCudafyDummyBehaviour behaviour, CudafyModule parentModule)
        {
            Type = type;
            Method = method;
            MethodType = gpuMethodType;
            DeserializedChecksum = 0;
            IsDummy = isDummy;
            Behaviour = behaviour;
            ParentModule = parentModule;
        }
Пример #3
0
 private void miOpen_Click(object sender, EventArgs e)
 {
     try
     {
         if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             _module = CudafyModule.Deserialize(openFileDialog.FileName);
             FillForm();
         }
     }
     catch (Exception ex)
     {
         HandleException(ex);
     }
 }
Пример #4
0
        public GpuRenderer()
        {
            var availableOpenCLDevices = CudafyHost.GetDeviceProperties(eGPUType.OpenCL);
            if (availableOpenCLDevices.Any() == false)
            {
                throw new Exception("No OpenCL devices found...");
            }
            var device = availableOpenCLDevices.First();
            Module = CudafyTranslator.Cudafy(eArchitecture.OpenCL12);
            var blockSide =
                Enumerable
                .Range(1, 15)
                .Reverse()
                .First(count => count * count <= device.MaxThreadsPerBlock);
            BlockSize = new dim3(blockSide, blockSide);

            // Initialize gpu and load the module (avoids reloading every time)
            gpu = CudafyHost.GetDevice(eGPUType.OpenCL);
            gpu.LoadModule(Module);
        }
Пример #5
0
        public static void cudaTransposeAndMultiply(ref MathNet.Numerics.LinearAlgebra.Double.DenseMatrix dm)
        {
            Cudafy.CudafyModule km = Cudafy.Translator.CudafyTranslator.Cudafy();
            km.Serialize();
            GPGPU gpu = CudafyHost.GetDevice(eGPUType.Cuda);
            int   cols = dm.ColumnCount, rows = dm.RowCount;

            dm.Storage.ToColumnMajorArray();
            double[] a = dm.ToColumnWiseArray();
            dm = new MathNet.Numerics.LinearAlgebra.Double.DenseMatrix(1, 1);
            double[]  dev_a = gpu.Allocate <double>(a.Length);
            GPGPUBLAS blas  = GPGPUBLAS.Create(gpu);

            double[] a_d = gpu.CopyToDevice <double>(a);
            double[] c_d = gpu.Allocate <double>(cols * cols);
            gpu.StartTimer();
            blas.GEMM(cols, rows, cols, 1, a_d, a_d, 0, c_d, Cudafy.Maths.BLAS.Types.cublasOperation.T);
            a = new double[cols * cols];
            gpu.CopyFromDevice <double>(c_d, a);
            gpu.FreeAll();
            dm = new MathNet.Numerics.LinearAlgebra.Double.DenseMatrix(cols, cols, a);
        }
Пример #6
0
 public static double DTW(double[] _x, double[] _y)
 {
     // gpu.EnableMultithreading();
        // gpu.EnableSmartCopy();
     CudafyModes.Target = eGPUType.Cuda;
     CudafyTranslator.AllowClasses = true;
     CudafyTranslator.GenerateDebug = true;
     gpu = CudafyHost.GetDevice(CudafyModes.Target, 0);
     arch = gpu.GetArchitecture();
     km = CudafyTranslator.Cudafy(arch);
        // km = CudafyTranslator.Cudafy(arch);
     // gpu.EnableMultithreading();
       //  gpu.SetCurrentContext();
     gpu.LoadModule(km);
     return CalcDTW(_x, _y);
 }
Пример #7
0
        private static CudafyModule Deserialize(CudafyModule km, XDocument doc, string path)
        {
            XElement root = doc.Element(csCUDAFYMODULE);
            if (root == null)
                throw new XmlException(string.Format(GES.csELEMENT_X_NOT_FOUND, csCUDAFYMODULE));

            string vStr = root.GetAttributeValue(csVERSION);
            Version version = new Version(vStr);
            Version curVers = typeof(CudafyModule).Assembly.GetName().Version;
            if (version < new Version(1, 25))
                throw new CudafyException(CudafyException.csVERSION_MISMATCH_EXPECTED_X_GOT_X, "1.25 or later", version);
            if (version.Major != curVers.Major || version.Minor > curVers.Minor)
                throw new CudafyException(CudafyException.csVERSION_MISMATCH_EXPECTED_X_GOT_X, curVers, version);

            string name = root.TryGetAttributeValue(csNAME);
            km.Name = name != null ? name : km.Name;

            // Cuda Source
            bool? hasCudaSrc = root.TryGetAttributeBoolValue(csHASCUDASOURCECODE);
            XElement scse = root.Element(csSOURCECODES);
            if (hasCudaSrc == true && scse == null) // Legacy support
            {
                km.SourceCode = root.TryGetElementBase64(csCUDASOURCECODE); 
            }
            // else if sourcecodes node
            else if (scse != null)
            {
                foreach (var sce in scse.Elements(csSOURCECODEFILE))
                {
                    SourceCodeFile scf = new SourceCodeFile();
                    scf.ID = sce.TryGetAttributeValue(csID);
                    scf.Language = sce.TryGetAttributeEnum<eLanguage>(csLANGUAGE);
                    scf.Source = UnicodeEncoding.ASCII.GetString(Convert.FromBase64String(sce.Value));
                    scf.Architecture = sce.TryGetAttributeEnum<eArchitecture>(csARCH);
                    km._sourceCodes.Add(scf);              
                }
            }
            else
            {
                km.SourceCode = string.Empty;
            }

            bool? hasDebug = root.TryGetAttributeBoolValue(csDEBUGINFO);
            km.GenerateDebug = hasDebug.HasValue && hasDebug.Value;

            // PTX
            bool? hasPtx = root.TryGetAttributeBoolValue(csHASPTX);
            if (hasPtx == true && root.Element(csPTX) != null) // legacy support V0.3 or less
            {
                string ptx = root.Element(csPTX).Value;
                byte[] ba = Convert.FromBase64String(ptx);
                km._PTXModules.Add(new PTXModule() { PTX = UnicodeEncoding.ASCII.GetString(ba), Platform = km.CurrentPlatform, Architecture = eArchitecture.Unknown });
            }
            else if (hasPtx == true)
            {
                XElement ptxsxe = root.Element(csPTXMODULES);
                foreach (XElement xe in ptxsxe.Elements(csPTXMODULE))
                {
                    string ptx = xe.Value;  //xe.Element(csPTX).Value;
                    string platformStr = xe.GetAttributeValue(csPLATFORM);
                    ePlatform platform = (ePlatform)Enum.Parse(typeof(ePlatform), platformStr);
                    string archStr = xe.TryGetAttributeValue(csARCH);
                    eArchitecture arch = eArchitecture.Unknown;
                    string sourcecodeId = xe.TryGetAttributeValue(csSOURCECODEFILE);
                    if (archStr != null)
                        arch = (eArchitecture)Enum.Parse(typeof(eArchitecture), archStr);
                    byte[] ba = Convert.FromBase64String(ptx);
                    km._PTXModules.Add(new PTXModule() { PTX = UnicodeEncoding.ASCII.GetString(ba), Platform = platform, Architecture = arch, SourceCodeID = sourcecodeId });
                }
            }

            // Binary
            bool? hasBinary = root.TryGetAttributeBoolValue(csHASBINARY);
            if (hasBinary == true)
            {
                XElement binsxe = root.Element(csBINARYMODULES);
                foreach (XElement xe in binsxe.Elements(csBINARYMODULE))
                {
                    string bin = xe.Value; //xe.Element(csBINARY).Value;
                    string platformStr = xe.GetAttributeValue(csPLATFORM);
                    string archStr = xe.GetAttributeValue(csARCH);
                    ePlatform platform = (ePlatform)Enum.Parse(typeof(ePlatform), platformStr);
                    eArchitecture arch = (eArchitecture)Enum.Parse(typeof(eArchitecture), archStr);
                    string sourcecodeId = xe.TryGetAttributeValue(csSOURCECODEFILE);
                    byte[] ba = Convert.FromBase64String(bin);
                    km._BinaryModules.Add(new BinaryModule() { Binary = ba, Platform = platform, Architecture = arch, SourceCodeID = sourcecodeId });
                }
            }

            // Functions
            XElement funcs = root.Element(csFUNCTIONS);
            if (funcs != null)
            {
                foreach (var xe in funcs.Elements(KernelMethodInfo.csCUDAFYKERNELMETHOD))
                {
                    KernelMethodInfo kmi = KernelMethodInfo.Deserialize(xe, km, path);
                    km.Functions.Add(kmi.Method.Name, kmi);
                }
            }

            // Constants
            XElement constants = root.Element(csCONSTANTS);
            if (constants != null)
            {
                foreach (var xe in constants.Elements(KernelConstantInfo.csCUDAFYCONSTANTINFO))
                {
                    KernelConstantInfo kci = KernelConstantInfo.Deserialize(xe, path);
                    km.Constants.Add(kci.Name, kci);
                }
            }

            // Types
            XElement types = root.Element(csTYPES);
            if (constants != null)
            {
                foreach (var xe in types.Elements(KernelTypeInfo.csCUDAFYTYPE))
                {
                    KernelTypeInfo kti = KernelTypeInfo.Deserialize(xe, path);
                    km.Types.Add(kti.Name, kti);
                }
            }

            return km;
        }
Пример #8
0
        public static CudafyModule Deserialize(Stream stream)
        {
            CudafyModule km = new CudafyModule();
#if NET35
            XmlReader reader = XmlReader.Create(stream);
            XDocument doc = XDocument.Load(reader);
#else
            XDocument doc = XDocument.Load(stream);
#endif
            return Deserialize(km, doc, null);
        }
Пример #9
0
        /// <summary>
        /// Deserializes the specified file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>Cudafy module.</returns>
        public static CudafyModule Deserialize(string filename)
        {
            CudafyModule km = new CudafyModule();
            if (!File.Exists(filename))
                filename += csFILEEXT;

            XDocument doc = XDocument.Load(filename);

            string path = Path.GetDirectoryName(filename);

            return Deserialize(km, doc, path);
        }
Пример #10
0
        internal static KernelMethodInfo Deserialize(XElement xe, CudafyModule parentModule, string directory = null)
        {
            string methodName = xe.GetAttributeValue(csNAME);
            string methodTypeName = xe.GetAttributeValue(csTYPE);
            eKernelMethodType methodType = (eKernelMethodType)Enum.Parse(typeof(eKernelMethodType), methodTypeName);
            bool? isDummy = xe.TryGetAttributeBoolValue(csISDUMMY);
            string behaviourStr = xe.TryGetAttributeValue(csDUMMYBEHAVIOUR);  
            string typeName = xe.Element(csTYPE).Value;
            string assemblyFullName = xe.Element(csASSEMBLY).Value;
            string assemblyName = xe.Element(csASSEMBLYNAME).Value;
            string assemblyPath = xe.TryGetElementValue(csASSEMBLYPATH);
            long checksum = XmlConvert.ToInt64(xe.Element(csCHECKSUM).Value);
            eCudafyDummyBehaviour behaviour = string.IsNullOrEmpty(behaviourStr) ? eCudafyDummyBehaviour.Default : (eCudafyDummyBehaviour)Enum.Parse(typeof(eCudafyDummyBehaviour), behaviourStr);
            MethodInfo mi = null;
            KernelMethodInfo kmi = null;

            if(!string.IsNullOrEmpty(typeName) && !string.IsNullOrEmpty(assemblyFullName))
            {
                Assembly assembly = null;
                try
                {
                    assembly = Assembly.Load(assemblyFullName);
                }
                catch (FileNotFoundException)
                {
                    directory = directory != null ? directory : string.Empty;
                    assemblyName = directory + Path.DirectorySeparatorChar + assemblyName;
                    if (File.Exists(assemblyName + ".dll"))
                    {
                        assembly = Assembly.LoadFrom(assemblyName + ".dll");
                    }
                    else if (File.Exists(assemblyName + ".exe"))
                    {
                        assembly = Assembly.LoadFrom(assemblyName + ".exe");
                    }
                    else if (!string.IsNullOrEmpty(assemblyPath))
                    {
                        assembly = Assembly.LoadFrom(assemblyPath);
                    }
                    else
                        throw;
                }
                if (assembly == null)
                    throw new CudafyException(CudafyException.csCOULD_NOT_LOAD_ASSEMBLY_X, assemblyFullName);
                Type type = assembly.GetType(typeName);
                if (type == null)
                    throw new CudafyException(CudafyException.csCOULD_NOT_FIND_TYPE_X_IN_ASSEMBLY_X, typeName, assemblyFullName);
                mi = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                if (mi == null)
                    throw new CudafyException(CudafyException.csCOULD_NOT_FIND_METHOD_X_IN_TYPE_X_IN_ASSEMBLY_X, methodName, typeName, assemblyFullName);
                kmi = new KernelMethodInfo(type, mi, methodType, isDummy == true ? true : false, behaviour, parentModule);                
            }
            kmi.DeserializedChecksum = checksum;
            return kmi;
        }
Пример #11
0
        private static CudafyModule DoCudafy(CudafyModule cm, params Type[] types)
        {
            MemoryStream output = new MemoryStream();
            var outputSw = new StreamWriter(output);
            
            MemoryStream structs = new MemoryStream();
            var structsSw = new StreamWriter(structs);
            var structsPto = new PlainTextOutput(structsSw);
            
            MemoryStream declarations = new MemoryStream();
            var declarationsSw = new StreamWriter(declarations);
            var declarationsPto = new PlainTextOutput(declarationsSw);

            MemoryStream code = new MemoryStream();
            var codeSw = new StreamWriter(code);
            var codePto = new PlainTextOutput(codeSw);

            bool isDummy = false;
            eCudafyDummyBehaviour behaviour = eCudafyDummyBehaviour.Default;

            Dictionary<string, ModuleDefinition> modules = new Dictionary<string,ModuleDefinition>();

            var compOpts = new DecompilationOptions { FullDecompilation = true };

            CUDALanguage.Reset();
            bool firstPass = true;
            if(cm == null)
                cm = new CudafyModule();// #######!!!
            else
                firstPass = false;
            
            // Test structs
            //foreach (var strct in types.Where(t => !t.IsClass))
            //    if (strct.GetCustomAttributes(typeof(CudafyAttribute), false).Length == 0)
            //        throw new CudafyLanguageException(CudafyLanguageException.csCUDAFY_ATTRIBUTE_IS_MISSING_ON_X, strct.Name);

            IEnumerable<Type> typeList = GetWithNestedTypes(types);
            foreach (var type in typeList)
            {
                if(!modules.ContainsKey(type.Assembly.Location))
                    modules.Add(type.Assembly.Location, ModuleDefinition.ReadModule(type.Assembly.Location));                
            }
            
            // Additional loop to compile in order
            foreach (var requestedType in typeList)
            {
                foreach (var kvp in modules)
                {
                    foreach (var td in kvp.Value.Types)
                    {
                        List<TypeDefinition> tdList = new List<TypeDefinition>();
                        tdList.Add(td);
                        tdList.AddRange(td.NestedTypes);

                        Type type = null;
                        foreach (var t in tdList)
                        {
                            //type = typeList.Where(tt => tt.FullName.Replace("+", "") == t.FullName.Replace("/", "")).FirstOrDefault();
                            // Only select type if this matches the requested type (to ensure order is maintained).
                            type = requestedType.FullName.Replace("+", "") == t.FullName.Replace("/", "") ? requestedType : null;

                            if (type == null)
                                continue;
                            Debug.WriteLine(t.FullName);
                            // Types                      
                            var attr = t.GetCudafyType(out isDummy, out behaviour);
                            if (attr != null)
                            {
                                _cl.DecompileType(t, structsPto, compOpts);
                                if (firstPass)
                                    cm.Types.Add(type.FullName.Replace("+", ""), new KernelTypeInfo(type, isDummy, behaviour));// #######!!!
                            }
                            else if (t.Name == td.Name)
                            {
                                // Fields
                                foreach (var fi in td.Fields)
                                {
                                    attr = fi.GetCudafyType(out isDummy, out behaviour);
                                    if (attr != null)
                                    {
                                        VerifyMemberName(fi.Name);
                                        System.Reflection.FieldInfo fieldInfo = type.GetField(fi.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                                        if (fieldInfo == null)
                                            throw new CudafyLanguageException(CudafyLanguageException.csX_ARE_NOT_SUPPORTED, "Non-static fields");
                                        int[] dims = _cl.GetFieldInfoDimensions(fieldInfo);
                                        _cl.DecompileCUDAConstantField(fi, dims, codePto, compOpts);
                                        var kci = new KernelConstantInfo(fi.Name, fieldInfo, isDummy);
                                        if (firstPass)
                                            cm.Constants.Add(fi.Name, kci);// #######!!!
                                        CUDALanguage.AddConstant(kci);
                                    }
                                }
#warning TODO Only Global Methods can be called from host
#warning TODO For OpenCL may need to do Methods once all Constants have been handled
                                // Methods
                                foreach (var med in td.Methods)
                                {
                                    attr = med.GetCudafyType(out isDummy, out behaviour);
                                    if (attr != null)
                                    {
                                        if (!med.IsStatic)
                                            throw new CudafyLanguageException(CudafyLanguageException.csX_ARE_NOT_SUPPORTED, "Non-static methods");
                                        _cl.DecompileMethodDeclaration(med, declarationsPto, new DecompilationOptions { FullDecompilation = false });
                                        _cl.DecompileMethod(med, codePto, compOpts);
                                        MethodInfo mi = type.GetMethod(med.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                                        if (mi == null)
                                            continue;
                                        VerifyMemberName(med.Name);
                                        eKernelMethodType kmt = eKernelMethodType.Device;
                                        kmt = GetKernelMethodType(attr, mi);
                                        if (firstPass)
                                            cm.Functions.Add(med.Name, new KernelMethodInfo(type, mi, kmt, isDummy, behaviour, cm));// #######!!!
                                    }
                                }
                            }
                        }
                    }
                }
            }

            codeSw.Flush();

            if (CudafyTranslator.Language == eLanguage.OpenCL)
            {
                outputSw.WriteLine("#if defined(cl_khr_fp64)");
                outputSw.WriteLine("#pragma OPENCL EXTENSION cl_khr_fp64: enable");
                outputSw.WriteLine("#elif defined(cl_amd_fp64)");
                outputSw.WriteLine("#pragma OPENCL EXTENSION cl_amd_fp64: enable");
                outputSw.WriteLine("#endif");
            }

            foreach (var oh in CUDALanguage.OptionalHeaders)
            {
                if (oh.Used && !oh.AsResource)
                    outputSw.WriteLine(oh.IncludeLine);
                else if (oh.Used)
                    outputSw.WriteLine(GetResourceString(oh.IncludeLine));
            }
            foreach (var oh in CUDALanguage.OptionalFunctions)
            {
                if (oh.Used)
                    outputSw.WriteLine(oh.Code);
            }

            declarationsSw.WriteLine();
            declarationsSw.Flush();

            structsSw.WriteLine();
            structsSw.Flush();

            foreach (var def in cm.GetDummyDefines())
                outputSw.WriteLine(def);
            foreach (var inc in cm.GetDummyStructIncludes())
                outputSw.WriteLine(inc);
            foreach (var inc in cm.GetDummyIncludes())
                outputSw.WriteLine(inc);
            outputSw.Flush();

            output.Write(structs.GetBuffer(), 0, (int)structs.Length);
            output.Write(declarations.GetBuffer(), 0, (int)declarations.Length);
            output.Write(code.GetBuffer(), 0, (int)code.Length);
            outputSw.Flush();
#if DEBUG
            using (FileStream fs = new FileStream("output.cu", FileMode.Create))
            {
                fs.Write(output.GetBuffer(), 0, (int)output.Length);
            }
#endif
            String s = Encoding.UTF8.GetString(output.GetBuffer(), 0, (int)output.Length);
            //cm.SourceCode = s;// #######!!!
            var scf = new SourceCodeFile(s, Language, _architecture);
            cm.AddSourceCodeFile(scf);
            return cm;
        }
Пример #12
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);
            }
        }