cf4ocl (C Framework for OpenCL)  v2.1.0
Object-oriented framework for developing and benchmarking OpenCL projects in C/C++
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ccl_platforms.c
Go to the documentation of this file.
1 /*
2  * This file is part of cf4ocl (C Framework for OpenCL).
3  *
4  * cf4ocl is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * cf4ocl is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with cf4ocl. If not, see
16  * <http://www.gnu.org/licenses/>.
17  * */
18 
30 #include "ccl_platforms.h"
31 #include "_ccl_defs.h"
32 
37 struct ccl_platforms {
38 
43  CCLPlatform** platfs;
44 
49  cl_uint num_platfs;
50 };
51 
67 CCL_EXPORT
69 
70  /* Make sure err is NULL or it is not set. */
71  g_return_val_if_fail(err == NULL || *err == NULL, NULL);
72 
73  /* Return status of OpenCL functions. */
74  cl_int ocl_status;
75 
76  /* Object which represents the list of OpenCL platforms available
77  * in the system. */
78  CCLPlatforms* platforms = NULL;
79 
80  /* Size in bytes of array of platform IDs. */
81  gsize platf_ids_size;
82 
83  /* Array of platform IDs. */
84  cl_platform_id* platf_ids = NULL;
85 
86  /* Allocate memory for the CCLPlatforms object. */
87  platforms = g_slice_new0(CCLPlatforms);
88 
89  /* Get number of platforms */
90  ocl_status = clGetPlatformIDs(0, NULL, &platforms->num_platfs);
92  platforms->num_platfs == 0, CCL_ERROR_DEVICE_NOT_FOUND,
93  error_handler, "%s: no OpenCL platforms found.", CCL_STRD);
95  CL_SUCCESS != ocl_status, ocl_status, error_handler,
96  "%s: get number of platforms (OpenCL error %d: %s).",
97  CCL_STRD, ocl_status, ccl_err(ocl_status));
98 
99  /* Determine size in bytes of array of platform IDs. */
100  platf_ids_size = sizeof(cl_platform_id) * platforms->num_platfs;
101 
102  /* Allocate memory for array of platform IDs. */
103  platf_ids = (cl_platform_id*) g_slice_alloc(platf_ids_size);
104 
105  /* Get existing platform IDs. */
106  ocl_status = clGetPlatformIDs(
107  platforms->num_platfs, platf_ids, NULL);
109  CL_SUCCESS != ocl_status, ocl_status, error_handler,
110  "%s: get platforms IDs (OpenCL error %d: %s).",
111  CCL_STRD, ocl_status, ccl_err(ocl_status));
112 
113  /* Allocate memory for array of platform wrapper objects. */
114  platforms->platfs =
115  g_slice_alloc(sizeof(CCLPlatform*) * platforms->num_platfs);
116 
117  /* Wrap platform IDs in platform wrapper objects. */
118  for (guint i = 0; i < platforms->num_platfs; i++) {
119  /* Add platform wrapper object to array of wrapper objects. */
120  platforms->platfs[i] = ccl_platform_new_wrap(platf_ids[i]);
121  }
122 
123  /* Free array of platform ids. */
124  g_slice_free1(platf_ids_size, platf_ids);
125 
126  /* If we got here, everything is OK. */
127  g_assert(err == NULL || *err == NULL);
128  goto finish;
129 
130 error_handler:
131 
132  /* If we got here there was an error, verify that it is so. */
133  g_assert(err == NULL || *err != NULL);
134 
135  /* Destroy the CCLPlatforms object, or what was possible to build
136  * of it. */
137  if (platforms) {
138  ccl_platforms_destroy(platforms);
139  }
140 
141  /* Set platforms to NULL, indicating an error occurred.*/
142  platforms = NULL;
143 
144 finish:
145 
146  /* Return the CCLPlatforms object. */
147  return platforms;
148 
149 }
150 
159 CCL_EXPORT
161 
162  /* Platforms object can't be NULL. */
163  g_return_if_fail(platforms != NULL);
164 
165  /* Destroy underlying platforms. */
166  for (guint i = 0; i < platforms->num_platfs; i++) {
167  ccl_platform_unref(platforms->platfs[i]);
168  }
169 
170  /* Free underlying platforms array. */
171  g_slice_free1(
172  sizeof(CCLPlatform*) * platforms->num_platfs, platforms->platfs);
173 
174  /* Free CCLPlatforms object. */
175  g_slice_free(CCLPlatforms, platforms);
176 }
177 
178 
188 CCL_EXPORT
189 cl_uint ccl_platforms_count(CCLPlatforms* platforms) {
190 
191  /* Platforms object can't be NULL. */
192  g_return_val_if_fail(platforms != NULL, 0);
193 
194  /* Return number of OpenCL platforms. */
195  return platforms->num_platfs;
196 }
197 
207 CCL_EXPORT
209  CCLPlatforms* platforms, cl_uint index) {
210 
211  /* Platforms object can't be NULL. */
212  g_return_val_if_fail(platforms != NULL, NULL);
213 
214  /* Index of platform to return must be smaller than the number of
215  * available platforms. */
216  g_return_val_if_fail(index < platforms->num_platfs, NULL);
217 
218  /* Return platform at given index. */
219  return platforms->platfs[index];
220 }
221 
#define CCL_OCL_ERROR
Resolves to error category identifying string, in this case an error in the OpenCL library...
Definition: ccl_common.h:324
Definition of a class which represents the list of OpenCL platforms available in the system and respe...
#define ccl_if_err_create_goto(err, quark, error_condition, error_code, label, msg,...)
If error is detected (error_code != no_error_code), create an error object (CCLErr) and go to the spe...
Definition: _ccl_defs.h:91
Useful definitions used internally by cf4ocl.
const char * ccl_err(int code)
Convert OpenCL error code to a readable string.
Definition: ccl_errors.c:118
void ccl_platforms_destroy(CCLPlatforms *platforms)
Destroy a CCLPlatforms* object, including all underlying platforms, devices and data.
CCLPlatforms * ccl_platforms_new(CCLErr **err)
Creates a new CCLPlatforms* object, which contains the list of OpenCL platforms available in the syst...
Definition: ccl_platforms.c:68
cl_uint ccl_platforms_count(CCLPlatforms *platforms)
Return number of OpenCL platforms found in CCLPlatforms object.
#define CCL_ERROR
Resolves to error category identifying string, in this case an error in cf4ocl.
Definition: ccl_common.h:320
The requested OpenCL device was not found.
Definition: ccl_common.h:308
#define ccl_platform_unref(platform)
Alias to ccl_platform_destroy().
Class which represents the OpenCL platforms available in the system.
Definition: ccl_platforms.c:37
CCLPlatform * ccl_platform_new_wrap(cl_platform_id platform)
Get the platform wrapper for the given OpenCL platform.
GError CCLErr
Error handling class.
Definition: ccl_common.h:291
CCLPlatform * ccl_platforms_get(CCLPlatforms *platforms, cl_uint index)
Get platform wrapper object at given index.
The platform wrapper class.