Exemplo n.º 1
1
Arquivo: Device.cs Projeto: lu4/ManOCL
        internal Device(CLDeviceID id)
        {
            this.CLDeviceID = id;

            this.PlatformID = GetDeviceInfo <CLPlatformID>(this, CLDeviceInfo.Platform);

            this.AddressBits            = GetDeviceInfo <Int32>(this, CLDeviceInfo.AddressBits);
            this.Available              = GetDeviceInfo <Boolean>(this, CLDeviceInfo.Available);
            this.CompilerAvailable      = GetDeviceInfo <Boolean>(this, CLDeviceInfo.CompilerAvailable);
            this.DriverVersion          = GetDeviceInfoString(this, CLDeviceInfo.DriverVersion);
            this.EndianLittle           = GetDeviceInfo <Boolean>(this, CLDeviceInfo.EndianLittle);
            this.ErrorCorrectionSupport = GetDeviceInfo <Boolean>(this, CLDeviceInfo.ErrorCorrectionSupport);
            this.ExecutionCapabilities  = (DeviceExecCapabilities)GetDeviceInfo <ulong>(this, CLDeviceInfo.ExecutionCapabilities);

            this.Extensions = GetDeviceInfoString(this, CLDeviceInfo.Extensions);

            this.GlobalMemCacheLineSize = GetDeviceInfo <Int32>(this, CLDeviceInfo.GlobalMemCacheLineSize);
            this.GlobalMemCacheSize     = GetDeviceInfo <Int64>(this, CLDeviceInfo.GlobalMemCacheSize);
            this.GlobalMemCacheType     = (DeviceMemCacheType)GetDeviceInfo <uint>(this, CLDeviceInfo.GlobalMemCacheType);
            this.GlobalMemSize          = GetDeviceInfo <Int64>(this, CLDeviceInfo.GlobalMemSize);
            this.Image2DMaxHeight       = GetDeviceInfo <SizeT>(this, CLDeviceInfo.Image2DMaxHeight);
            this.Image2DMaxWidth        = GetDeviceInfo <SizeT>(this, CLDeviceInfo.Image2DMaxWidth);
            this.Image3DMaxDepth        = GetDeviceInfo <SizeT>(this, CLDeviceInfo.Image3DMaxDepth);
            this.Image3DMaxHeight       = GetDeviceInfo <SizeT>(this, CLDeviceInfo.Image3DMaxHeight);
            this.Image3DMaxWidth        = GetDeviceInfo <SizeT>(this, CLDeviceInfo.Image3DMaxWidth);
            this.ImageSupport           = GetDeviceInfo <Boolean>(this, CLDeviceInfo.ImageSupport);
            this.LocalMemSize           = GetDeviceInfo <Int64>(this, CLDeviceInfo.LocalMemSize);
            this.LocalMemType           = (DeviceLocalMemType)GetDeviceInfo <uint>(this, CLDeviceInfo.LocalMemType);
            this.MaxClockFrequency      = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxClockFrequency);
            this.MaxComputeUnits        = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxComputeUnits);
            this.MaxConstantArgs        = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxConstantArgs);
            this.MaxConstantBufferSize  = GetDeviceInfo <Int64>(this, CLDeviceInfo.MaxConstantBufferSize);
            this.MaxMemAllocSize        = GetDeviceInfo <Int64>(this, CLDeviceInfo.MaxMemAllocSize);
            this.MaxParameterSize       = GetDeviceInfo <SizeT>(this, CLDeviceInfo.MaxParameterSize);
            this.MaxReadImageArgs       = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxReadImageArgs);
            this.MaxSamplers            = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxSamplers);
            this.MaxWorkGroupSize       = GetDeviceInfo <SizeT>(this, CLDeviceInfo.MaxWorkGroupSize);
            this.MaxWorkItemDimensions  = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxWorkItemDimensions);
            this.MaxWorkItemSizes       = new SizeT[this.MaxWorkItemDimensions];

            GCHandle bufferHandle = GCHandle.Alloc(this.MaxWorkItemSizes, GCHandleType.Pinned);

            try
            {
                SizeT param_value_size_ret = SizeT.Zero;

                OpenCLError.Validate(OpenCLDriver.clGetDeviceInfo(CLDeviceID, CLDeviceInfo.MaxWorkItemSizes, new SizeT(MaxWorkItemSizes.Length * IntPtr.Size), bufferHandle.AddrOfPinnedObject(), ref param_value_size_ret));
            }
            finally
            {
                bufferHandle.Free();
            }

            this.MaxWriteImageArgs    = GetDeviceInfo <Int32>(this, CLDeviceInfo.MaxWriteImageArgs);
            this.MemBaseAddrAlign     = GetDeviceInfo <Int32>(this, CLDeviceInfo.MemBaseAddrAlign);
            this.MinDataTypeAlignSize = GetDeviceInfo <Int32>(this, CLDeviceInfo.MinDataTypeAlignSize);
            this.Name = GetDeviceInfoString(this, CLDeviceInfo.Name);
            this.PreferredVectorWidthChar   = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthChar);
            this.PreferredVectorWidthDouble = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthDouble);
            this.PreferredVectorWidthFloat  = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthFloat);
            this.PreferredVectorWidthInt    = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthInt);
            this.PreferredVectorWidthLong   = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthLong);
            this.PreferredVectorWidthShort  = GetDeviceInfo <Int32>(this, CLDeviceInfo.PreferredVectorWidthShort);
            this.Profile = GetDeviceInfoString(this, CLDeviceInfo.Profile);
            this.ProfilingTimerResolution = GetDeviceInfo <SizeT>(this, CLDeviceInfo.ProfilingTimerResolution);
            this.QueueProperties          = GetDeviceInfo <Int64>(this, CLDeviceInfo.QueueProperties);
            this.SingleFPConfig           = (DeviceFPConfig)GetDeviceInfo <ulong>(this, CLDeviceInfo.SingleFPConfig);
            this.CLDeviceType             = (CLDeviceType)GetDeviceInfo <ulong>(this, CLDeviceInfo.Type);
            this.Vendor   = GetDeviceInfoString(this, CLDeviceInfo.Vendor);
            this.VendorID = GetDeviceInfo <Int32>(this, CLDeviceInfo.VendorID);
            this.Version  = GetDeviceInfoString(this, CLDeviceInfo.Version);
        }
Exemplo n.º 2
0
        public static Context Share(IntPtr openclContext)
        {
            SizeT devicesSize = SizeT.Zero;

            CLContext clContext = new CLContext {
                Value = openclContext
            };

            OpenCLError.Validate(OpenCLDriver.clGetContextInfo(clContext, CLContextInfo.Devices, SizeT.Zero, IntPtr.Zero, ref devicesSize));

            CLDeviceID[] devices = new CLDeviceID[((Int64)(devicesSize)) / IntPtr.Size];

            GCHandle devicesHandle = GCHandle.Alloc(devices, GCHandleType.Pinned);

            try
            {
                OpenCLError.Validate(OpenCLDriver.clGetContextInfo(clContext, CLContextInfo.Devices, devicesSize, devicesHandle.AddrOfPinnedObject(), ref devicesSize));

                Dictionary <CLPlatformID, CLPlatformID> platformsDictionary = new Dictionary <CLPlatformID, CLPlatformID>();

                CLPlatformID[] platforms = null;

                foreach (CLDeviceID device in devices)
                {
                    SizeT platformsSize = SizeT.Zero;

                    OpenCLError.Validate(OpenCLDriver.clGetDeviceInfo(device, CLDeviceInfo.Platform, SizeT.Zero, platforms, ref platformsSize));

                    platforms = new CLPlatformID[((Int64)(platformsSize)) / IntPtr.Size];

                    OpenCLError.Validate(OpenCLDriver.clGetDeviceInfo(device, CLDeviceInfo.Platform, platformsSize, platforms, ref platformsSize));

                    foreach (CLPlatformID platform in platforms)
                    {
                        if (!platformsDictionary.ContainsKey(platform))
                        {
                            platformsDictionary.Add(platform, platform);
                        }
                    }
                }

                platforms = new CLPlatformID[platformsDictionary.Count];

                Int32 index = 0;

                foreach (var platform in platformsDictionary.Keys)
                {
                    platforms[index++] = platform;
                }

                return(new Context(clContext, new Platforms(platforms), new Devices(devices)));
            }
            finally
            {
                devicesHandle.Free();
            }
        }
Exemplo n.º 3
0
        internal static CLDeviceID[] GetOpenCLDeviceArray(Device[] devices)
        {
            CLDeviceID[] openclDevices = new CLDeviceID[devices.Length];

            for (UInt32 i = 0; i < devices.Length; i++)
            {
                openclDevices[i] = devices[i].CLDeviceID;
            }

            return(openclDevices);
        }
Exemplo n.º 4
0
        public static Devices Create(CLDeviceType deviceType, Platform platform)
        {
            Int32 deviceCount = 0;

            CLPlatformID pfm = platform == null ? new CLPlatformID() : platform.CLPlatformID;

            OpenCLError.Validate(OpenCLDriver.clGetDeviceIDs(pfm, (ManOCL.Internal.OpenCL.CLDeviceType)deviceType, 0, null, ref deviceCount));

            CLDeviceID[] deviceIds = new CLDeviceID[deviceCount];

            OpenCLError.Validate(OpenCLDriver.clGetDeviceIDs(pfm, (ManOCL.Internal.OpenCL.CLDeviceType)deviceType, deviceCount, deviceIds, ref deviceCount));

            return(new Devices(deviceIds));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Setup OpenCL
        /// </summary>
        internal static void Setup()
        {
            try
            {
                // ADD YOUR CODE HERE
                CLError        err;
                uint           num_entries = 0;// get platform
                CLPlatformID[] platforms   = new CLPlatformID[5];
                err = OpenCLDriver.clGetPlatformIDs(5, platforms, ref num_entries);
                if (num_entries == 0)
                {
                    throw new Exception("No Platform Entries found!");
                }
                //get device ID
                CLDeviceID[] devices = new CLDeviceID[1];
                err = OpenCLDriver.clGetDeviceIDs(platforms[0], CLDeviceType.GPU, 1, devices, ref num_entries);
                // create context
                ctx = OpenCLDriver.clCreateContext(null, 1, devices, null, IntPtr.Zero, ref err);
                if (err != CLError.Success)
                {
                    throw new Exception(err.ToString());
                }

                // create command queue
                comQ = OpenCLDriver.clCreateCommandQueue(ctx, devices[0], 0, ref err);
                if (err != CLError.Success)
                {
                    throw new Exception(err.ToString());
                }

                // Compile Program
                string[] progString = new string[1];
                progString[0] = File.ReadAllText("..\\..\\prog.cl");
                program       = OpenCLDriver.clCreateProgramWithSource(ctx, 1, progString, null, ref err);
                err           = OpenCLDriver.clBuildProgram(program, 1, devices, "", null, IntPtr.Zero);

                //create kernel
                kernel_mult = OpenCLDriver.clCreateKernel(program, "multiply", ref err);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }
Exemplo n.º 6
0
        public static Context ShareWithCGL(IntPtr cglShareGroup)
        {
            IntPtr[] properties =
            {
                new IntPtr(0x10000000),                 // CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE
                cglShareGroup,
                new IntPtr(0)
            };

            CLError error = CLError.None;

            // TODO: Add parameter pfn_notify (logging function)
            CLContext openclContext = OpenCLDriver.clCreateContext(properties, 0, null, null, IntPtr.Zero, ref error);

            OpenCLError.Validate(error);

            SizeT devicesSize = SizeT.Zero;

            OpenCLError.Validate(OpenCLDriver.clGetContextInfo(openclContext, CLContextInfo.Devices, SizeT.Zero, IntPtr.Zero, ref devicesSize));

            CLDeviceID[] devices = new CLDeviceID[((Int64)(devicesSize)) / IntPtr.Size];

            GCHandle devicesHandle = GCHandle.Alloc(devices, GCHandleType.Pinned);

            try
            {
                OpenCLError.Validate(OpenCLDriver.clGetContextInfo(openclContext, CLContextInfo.Devices, devicesSize, devicesHandle.AddrOfPinnedObject(), ref devicesSize));

                Dictionary <CLPlatformID, CLPlatformID> platformsDictionary = new Dictionary <CLPlatformID, CLPlatformID>();

                CLPlatformID[] platforms = null;

                foreach (CLDeviceID device in devices)
                {
                    SizeT platformsSize = SizeT.Zero;

                    OpenCLError.Validate(OpenCLDriver.clGetDeviceInfo(device, CLDeviceInfo.Platform, SizeT.Zero, platforms, ref platformsSize));

                    platforms = new CLPlatformID[((Int64)(platformsSize)) / IntPtr.Size];

                    OpenCLError.Validate(OpenCLDriver.clGetDeviceInfo(device, CLDeviceInfo.Platform, platformsSize, platforms, ref platformsSize));

                    foreach (CLPlatformID platform in platforms)
                    {
                        if (!platformsDictionary.ContainsKey(platform))
                        {
                            platformsDictionary.Add(platform, platform);
                        }
                    }
                }

                platforms = new CLPlatformID[platformsDictionary.Count];

                Int32 index = 0;

                foreach (var platform in platformsDictionary.Keys)
                {
                    platforms[index++] = platform;
                }

                return(new Context(openclContext, new Platforms(platforms), new Devices(devices)));
            }
            finally
            {
                devicesHandle.Free();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get platform and device info
        /// </summary>
        internal static void GetInfo()
        {
            try
            {
                uint num_entries = 0;
                uint num_entries_devices = 0;
                // get device
                CLPlatformID[] platforms = new CLPlatformID[5];
                CLError err = OpenCLDriver.clGetPlatformIDs(5, platforms, ref num_entries);
                if (err != CLError.Success) throw new Exception(err.ToString());
                if (num_entries == 0) throw new Exception("No Platform Entries found!");
                // get platform properties
                byte[] buffer = new byte[1000];
                GCHandle bufferGC = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                SizeT buffSize, buffSizeOut = new SizeT();
                for (int i = 0; i < num_entries; i++)
                {
                    buffSize = new CASS.Types.SizeT(1000);
                    err = OpenCLDriver.clGetPlatformInfo(platforms[i], CLPlatformInfo.Name, buffSize,
                    bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    if (err != CLError.Success) throw new Exception(err.ToString());
                    MessageBox.Show("Platform: " + i + "\n" + System.Text.Encoding.ASCII.GetString(buffer));

                    CLDeviceID[] devices = new CLDeviceID[2];
                    err = OpenCLDriver.clGetDeviceIDs(platforms[i], CLDeviceType.All, 2, devices, ref num_entries_devices);
                    if (err != CLError.Success) throw new Exception(err.ToString());

                    string result = "";
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.Vendor, buffSize,
                    bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.Vendor.ToString() + ": " +
                    Encoding.ASCII.GetString(buffer, 0, buffSizeOut - 1) + "\n";
                    buffSize = new CASS.Types.SizeT(1000);
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.Name, buffSize,
                    bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.Name.ToString() + ": " +
                    Encoding.ASCII.GetString(buffer, 0, buffSizeOut - 1) + "\n";

                    int[] workDim = new int[1];
                    bufferGC = GCHandle.Alloc(workDim, GCHandleType.Pinned);
                    buffSize = new CASS.Types.SizeT(sizeof(int));
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxComputeUnits, buffSize,
                    bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxComputeUnits.ToString() + ": " + workDim[0] + "\n";
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxWorkItemDimensions,
                    workDim.Length * sizeof(int), bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxWorkItemDimensions.ToString() + ": " + workDim[0] + "\n";
                    SizeT[] sizeWI = new SizeT[workDim[0]];
                    bufferGC = GCHandle.Alloc(sizeWI, GCHandleType.Pinned);
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxWorkItemSizes, sizeWI.Length *
                    sizeof(int), bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxWorkItemSizes.ToString() + ": " +
                    sizeWI[0] + "x" + sizeWI[1] + "x" + sizeWI[2] + "\n";
                    MessageBox.Show(result, "Device");

                }

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Setup OpenCL
        /// </summary>
        internal static void Setup()
        {
            try
            {
                // ADD YOUR CODE HERE
                CLError err;
                uint num_entries = 0;// get platform
                CLPlatformID[] platforms = new CLPlatformID[5];
                err = OpenCLDriver.clGetPlatformIDs(5, platforms, ref num_entries);
                if (num_entries == 0) throw new Exception("No Platform Entries found!");
                //get device ID
                CLDeviceID[] devices = new CLDeviceID[1];
                err = OpenCLDriver.clGetDeviceIDs(platforms[0], CLDeviceType.GPU, 1, devices, ref num_entries);
                // create context
                ctx = OpenCLDriver.clCreateContext(null, 1, devices, null, IntPtr.Zero, ref err);
                if (err != CLError.Success) throw new Exception(err.ToString());

                // create command queue
                comQ = OpenCLDriver.clCreateCommandQueue(ctx, devices[0], 0, ref err);
                if (err != CLError.Success) throw new Exception(err.ToString());

                // Compile Program
                string[] progString = new string[1];
                progString[0] = File.ReadAllText("..\\..\\prog.cl");
                program = OpenCLDriver.clCreateProgramWithSource(ctx, 1, progString, null, ref err);
                err = OpenCLDriver.clBuildProgram(program, 1, devices, "", null, IntPtr.Zero);

                //create kernel
                kernel_mult = OpenCLDriver.clCreateKernel(program, "multiply", ref err);

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Get platform and device info
        /// </summary>
        internal static void GetInfo()
        {
            try
            {
                uint num_entries         = 0;
                uint num_entries_devices = 0;
                // get device
                CLPlatformID[] platforms = new CLPlatformID[5];
                CLError        err       = OpenCLDriver.clGetPlatformIDs(5, platforms, ref num_entries);
                if (err != CLError.Success)
                {
                    throw new Exception(err.ToString());
                }
                if (num_entries == 0)
                {
                    throw new Exception("No Platform Entries found!");
                }
                // get platform properties
                byte[]   buffer = new byte[1000];
                GCHandle bufferGC = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                SizeT    buffSize, buffSizeOut = new SizeT();
                for (int i = 0; i < num_entries; i++)
                {
                    buffSize = new CASS.Types.SizeT(1000);
                    err      = OpenCLDriver.clGetPlatformInfo(platforms[i], CLPlatformInfo.Name, buffSize,
                                                              bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    if (err != CLError.Success)
                    {
                        throw new Exception(err.ToString());
                    }
                    MessageBox.Show("Platform: " + i + "\n" + System.Text.Encoding.ASCII.GetString(buffer));

                    CLDeviceID[] devices = new CLDeviceID[2];
                    err = OpenCLDriver.clGetDeviceIDs(platforms[i], CLDeviceType.All, 2, devices, ref num_entries_devices);
                    if (err != CLError.Success)
                    {
                        throw new Exception(err.ToString());
                    }

                    string result = "";
                    err = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.Vendor, buffSize,
                                                       bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.Vendor.ToString() + ": " +
                              Encoding.ASCII.GetString(buffer, 0, buffSizeOut - 1) + "\n";
                    buffSize = new CASS.Types.SizeT(1000);
                    err      = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.Name, buffSize,
                                                            bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.Name.ToString() + ": " +
                              Encoding.ASCII.GetString(buffer, 0, buffSizeOut - 1) + "\n";

                    int[] workDim = new int[1];
                    bufferGC = GCHandle.Alloc(workDim, GCHandleType.Pinned);
                    buffSize = new CASS.Types.SizeT(sizeof(int));
                    err      = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxComputeUnits, buffSize,
                                                            bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxComputeUnits.ToString() + ": " + workDim[0] + "\n";
                    err     = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxWorkItemDimensions,
                                                           workDim.Length * sizeof(int), bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxWorkItemDimensions.ToString() + ": " + workDim[0] + "\n";
                    SizeT[] sizeWI = new SizeT[workDim[0]];
                    bufferGC = GCHandle.Alloc(sizeWI, GCHandleType.Pinned);
                    err      = OpenCLDriver.clGetDeviceInfo(devices[0], CLDeviceInfo.MaxWorkItemSizes, sizeWI.Length *
                                                            sizeof(int), bufferGC.AddrOfPinnedObject(), ref buffSizeOut);
                    result += CLDeviceInfo.MaxWorkItemSizes.ToString() + ": " +
                              sizeWI[0] + "x" + sizeWI[1] + "x" + sizeWI[2] + "\n";
                    MessageBox.Show(result, "Device");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }