Beispiel #1
0
        public static Context Create(Platforms platforms, Devices devices)
        {
            CLError error = CLError.None;

            // TODO: Add parameter pfn_notify (logging function)
            CLContext openclContext = OpenCLDriver.clCreateContext(GetContextProperties(platforms), devices.Count, devices.OpenCLDeviceArray, null, IntPtr.Zero, ref error);

            OpenCLError.Validate(error);

            return(new Context(openclContext, platforms, devices));
        }
Beispiel #2
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());
            }
        }
Beispiel #3
0
        public static IntPtr ShareForMac(IntPtr cglShareGroup)
        {
            CLError error = CLError.None;

            IntPtr[] properties =
            {
                new IntPtr(0x10000000), // CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE
                cglShareGroup,
                new IntPtr(0)
            };


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

            OpenCLError.Validate(error);

            return(openclContext.Value);
        }
Beispiel #4
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();
            }
        }