source: trunk/CrypPlugins/Libs/OpenCLNet/Platform.cs @ 2040

Last change on this file since 2040 was 2040, checked in by Sven Rech, 11 years ago
  • added OpenCL.Net project
  • added OpenCL settings in KeySearcher
  • changed rereservation status message behavior
File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 2009 Olav Kalgraf(olav.kalgraf@gmail.com)
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26using System;
27using System.Collections.Generic;
28using System.ComponentModel;
29using System.Runtime.InteropServices;
30using System.Runtime.InteropServices.ComTypes;
31using System.Text;
32using System.Text.RegularExpressions;
33
34namespace OpenCLNet
35{
36
37    unsafe public class Platform : InteropTools.IPropertyContainer
38    {
39        public IntPtr PlatformID { get; protected set; }
40        /// <summary>
41        /// Equal to "FULL_PROFILE" if the implementation supports the OpenCL specification or
42        /// "EMBEDDED_PROFILE" if the implementation supports the OpenCL embedded profile.
43        /// </summary>
44        public string Profile { get { return InteropTools.ReadString( this, (uint)PlatformInfo.PROFILE ); } }
45        /// <summary>
46        /// OpenCL version string. Returns the OpenCL version supported by the implementation. This version string
47        /// has the following format: OpenCL&lt;space&gt;&lt;major_version.minor_version&gt;&lt;space&gt;&lt;platform specific information&gt;
48        /// </summary>
49        public string Version { get { return InteropTools.ReadString( this, (uint)PlatformInfo.VERSION ); } }
50        /// <summary>
51        /// Platform name string
52        /// </summary>
53        public string Name { get { return InteropTools.ReadString( this, (uint)PlatformInfo.NAME ); } }
54        /// <summary>
55        /// Platform Vendor string
56        /// </summary>
57        public string Vendor { get { return InteropTools.ReadString( this, (uint)PlatformInfo.VENDOR ); } }
58        /// <summary>
59        /// Space separated string of extension names.
60        /// Note that this class has some support functions to help query extension capbilities.
61        /// This property is only present for completeness.
62        /// </summary>
63        public string Extensions { get { return InteropTools.ReadString( this, (uint)PlatformInfo.EXTENSIONS ); } }
64        /// <summary>
65        /// Convenience method to get at the major_version field in the Version string
66        /// </summary>
67        public int OpenCLMajorVersion { get; protected set; }
68        /// <summary>
69        /// Convenience method to get at the minor_version field in the Version string
70        /// </summary>
71        public int OpenCLMinorVersion { get; protected set; }
72
73        Regex VersionStringRegex = new Regex("OpenCL (?<Major>[0-9]+)\\.(?<Minor>[0-9]+)");
74        protected Dictionary<IntPtr, Device> _Devices = new Dictionary<IntPtr, Device>();
75        Device[] DeviceList;
76        IntPtr[] DeviceIDs;
77
78        protected HashSet<string> ExtensionHashSet = new HashSet<string>();
79
80        public Platform( IntPtr platformID )
81        {
82            PlatformID = platformID;
83
84            // Create a local representation of all devices
85            DeviceIDs = QueryDeviceIntPtr( DeviceType.ALL );
86            for( int i=0; i<DeviceIDs.Length; i++ )
87                _Devices[DeviceIDs[i]] = new Device( this, DeviceIDs[i] );
88            DeviceList = InteropTools.ConvertDeviceIDsToDevices( this, DeviceIDs );
89
90            InitializeExtensionHashSet();
91
92            Match m = VersionStringRegex.Match(Version);
93            if (m.Success)
94            {
95                OpenCLMajorVersion = int.Parse(m.Groups["Major"].Value);
96                OpenCLMinorVersion = int.Parse(m.Groups["Minor"].Value);
97            }
98            else
99            {
100                OpenCLMajorVersion = 1;
101                OpenCLMinorVersion = 0;
102            }
103        }
104
105        public Context CreateDefaultContext()
106        {
107            return CreateDefaultContext(null, IntPtr.Zero);
108        }
109
110        public Context CreateDefaultContext( ContextNotify notify, IntPtr userData )
111        {
112            IntPtr[] properties = new IntPtr[]
113            {
114                new IntPtr((long)ContextProperties.PLATFORM), PlatformID,
115                IntPtr.Zero,
116            };
117
118            IntPtr contextID;
119            ErrorCode result;
120
121            contextID = (IntPtr)OpenCL.CreateContext( properties,
122                (uint)DeviceIDs.Length,
123                DeviceIDs,
124                notify,
125                userData,
126                out result );
127            if( result!=ErrorCode.SUCCESS )
128                throw new OpenCLException( "CreateContext failed with error code: "+result, result);
129            return new Context( this, contextID );
130        }
131
132        public Context CreateContext(IntPtr[] contextProperties, Device[] devices, ContextNotify notify, IntPtr userData)
133        {
134            IntPtr contextID;
135            ErrorCode result;
136
137            IntPtr[] deviceIDs = InteropTools.ConvertDevicesToDeviceIDs(devices);
138            contextID = (IntPtr)OpenCL.CreateContext(contextProperties,
139                (uint)deviceIDs.Length,
140                deviceIDs,
141                notify,
142                userData,
143                out result);
144            if (result != ErrorCode.SUCCESS)
145                throw new OpenCLException("CreateContext failed with error code: " + result, result);
146            return new Context(this, contextID);
147        }
148
149        public Context CreateContextFromType(IntPtr[] contextProperties, DeviceType deviceType, ContextNotify notify, IntPtr userData)
150        {
151            IntPtr contextID;
152            ErrorCode result;
153
154            contextID = (IntPtr)OpenCL.CreateContextFromType(contextProperties,
155                deviceType,
156                notify,
157                userData,
158                out result);
159            if (result != ErrorCode.SUCCESS)
160                throw new OpenCLException("CreateContextFromType failed with error code: " + result, result);
161            return new Context(this, contextID);
162        }
163
164        public Device GetDevice( IntPtr index )
165        {
166            return _Devices[index];
167        }
168
169        protected IntPtr[] QueryDeviceIntPtr( DeviceType deviceType )
170        {
171            ErrorCode result;
172            uint numberOfDevices;
173            IntPtr[] deviceIDs;
174
175            result = (ErrorCode)OpenCL.GetDeviceIDs( PlatformID, deviceType, 0, null, out numberOfDevices );
176            if (result == ErrorCode.DEVICE_NOT_FOUND)
177                return new IntPtr[0];
178
179            if( result!=ErrorCode.SUCCESS )
180                throw new OpenCLException( "GetDeviceIDs failed: "+((ErrorCode)result).ToString(), result);
181
182            deviceIDs = new IntPtr[numberOfDevices];
183            result = (ErrorCode)OpenCL.GetDeviceIDs( PlatformID, deviceType, numberOfDevices, deviceIDs, out numberOfDevices );
184            if (result != ErrorCode.SUCCESS)
185                throw new OpenCLException( "GetDeviceIDs failed: "+((ErrorCode)result).ToString(), result);
186
187            return deviceIDs;
188        }
189
190        /// <summary>
191        /// Find all devices of a specififc type
192        /// </summary>
193        /// <param name="deviceType"></param>
194        /// <returns>Array containing the devices</returns>
195        public Device[] QueryDevices( DeviceType deviceType )
196        {
197            IntPtr[] deviceIDs;
198
199            deviceIDs = QueryDeviceIntPtr( deviceType );
200            return InteropTools.ConvertDeviceIDsToDevices( this, deviceIDs );
201        }
202
203        protected void InitializeExtensionHashSet()
204        {
205            string[] ext = Extensions.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
206
207            foreach (string s in ext)
208                ExtensionHashSet.Add(s);
209        }
210
211        /// <summary>
212        /// Test if this platform supports a specific extension
213        /// </summary>
214        /// <param name="extension"></param>
215        /// <returns>Returns true if the extension is supported</returns>
216        public bool HasExtension(string extension)
217        {
218            return ExtensionHashSet.Contains(extension);
219        }
220
221        /// <summary>
222        /// Test if this platform supports a set of exentions
223        /// </summary>
224        /// <param name="extensions"></param>
225        /// <returns>Returns true if all the extensions are supported</returns>
226        public bool HasExtensions(string[] extensions)
227        {
228            foreach (string s in extensions)
229                if (!ExtensionHashSet.Contains(s))
230                    return false;
231            return true;
232        }
233
234        public static implicit operator IntPtr( Platform p )
235        {
236            return p.PlatformID;
237        }
238
239        #region IPropertyContainer Members
240
241        public IntPtr GetPropertySize( uint key )
242        {
243            IntPtr propertySize;
244            ErrorCode result;
245
246            result = (ErrorCode)OpenCL.GetPlatformInfo( PlatformID, key, IntPtr.Zero, null, out propertySize );
247            if( result!=ErrorCode.SUCCESS )
248                throw new OpenCLException( "Unable to get platform info for platform "+PlatformID+": "+result, result);
249            return propertySize;
250
251        }
252
253        public void ReadProperty( uint key, IntPtr keyLength, void* pBuffer )
254        {
255            IntPtr propertySize;
256            ErrorCode result;
257
258            result = (ErrorCode)OpenCL.GetPlatformInfo( PlatformID, key, keyLength, (void*)pBuffer, out propertySize );
259            if( result!=ErrorCode.SUCCESS )
260                throw new OpenCLException( "Unable to get platform info for platform "+PlatformID+": "+result, result);
261        }
262
263        #endregion
264    }
265}
Note: See TracBrowser for help on using the repository browser.