[clinfo] 02/148: Start working on device info

Andreas Beckmann anbe at moszumanska.debian.org
Mon Nov 17 14:09:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

anbe pushed a commit to branch clinfo
in repository clinfo.

commit 960edf77fd3bfb6357b5af199dd3cddabe4c10e5
Author: Giuseppe Bilotta <giuseppe.bilotta at gmail.com>
Date:   Wed Jun 5 22:35:41 2013 +0200

    Start working on device info
    
    Name only for the time being. Also refactored a lot of the code.
---
 .gitignore     |   1 +
 Makefile       |  12 +++++--
 src/cl_error.h |  18 +++++++++++
 src/cl_mem.h   |  21 ++++++++++++
 src/clinfo.c   | 100 ++++++++++++++++++++++++++++++++-------------------------
 5 files changed, 105 insertions(+), 47 deletions(-)

diff --git a/.gitignore b/.gitignore
index f7f305a..c39c623 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 clinfo
 .*.swp
+*.o
diff --git a/Makefile b/Makefile
index 79105f1..e43adee 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,14 @@
+SRCDIR=src/
+
+VPATH=$(SRCDIR)
+
+HDR=$(wildcard $(SRCDIR)/*.h)
+
 LDLIBS=-lOpenCL
 
-vpath %.c src/
+clinfo:
 
-clinfo: clinfo.c
+clinfo.o: clinfo.c $(HDR)
 
 clean:
-	$(RM) clinfo
+	$(RM) clinfo.o clinfo
diff --git a/src/cl_error.h b/src/cl_error.h
new file mode 100644
index 0000000..0b0a057
--- /dev/null
+++ b/src/cl_error.h
@@ -0,0 +1,18 @@
+/* OpenCL error handling */
+
+#include <stdio.h>
+
+cl_int error;
+
+void
+check_ocl_error(cl_int err, const char *what, const char *func, int line)
+{
+	if (err != CL_SUCCESS) {
+		fprintf(stderr, "%s:%s: %s : error %d\n",
+			func, line, what, err);
+		exit(1);
+	}
+}
+
+#define CHECK_ERROR(what) check_ocl_error(error, what, __func__, __LINE__)
+
diff --git a/src/cl_mem.h b/src/cl_mem.h
new file mode 100644
index 0000000..fcf8632
--- /dev/null
+++ b/src/cl_mem.h
@@ -0,0 +1,21 @@
+/* Memory handling */
+
+#include <stdlib.h>
+
+#define CHECK_MEM(var, what) do { \
+	if (!var) { \
+		fprintf(stderr, "%s:%s: %s : Out of memory\n", \
+			__func__, __LINE__, what); \
+		exit(1); \
+	} \
+} while (0)
+
+#define ALLOC(var, num, what) do { \
+	var = malloc(num*sizeof(*var)); \
+	CHECK_MEM(var, what); \
+} while (0)
+
+#define REALLOC(var, num, what) do { \
+	var = realloc(var, num*sizeof(*var)); \
+	CHECK_MEM(var, what); \
+} while (0)
diff --git a/src/clinfo.c b/src/clinfo.c
index 2315a61..398ddf0 100644
--- a/src/clinfo.c
+++ b/src/clinfo.c
@@ -2,45 +2,24 @@
  * on all available OpenCL platforms present in the system
  */
 
-#include <stdio.h>
-
 #ifdef __APPLE__
 #include <OpenCL/cl.h>
 #else
 #include <CL/cl.h>
 #endif
 
-void
-check_ocl_error(cl_int err, const char *what, const char *func, int line)
-{
-	if (err != CL_SUCCESS) {
-		fprintf(stderr, "%s:%s: %s : error %d\n",
-			func, line, what, err);
-		exit(1);
-	}
-}
+#include <string.h>
 
-cl_int error;
-#define CHECK_ERROR(what) check_ocl_error(error, what, __func__, __LINE__)
-
-
-#define ALLOC(var, num, what) do { \
-	var = malloc(num*sizeof(*var)); \
-	if (!var) { \
-		fprintf(stderr, "%s:%s: %s : Out of memory\n", \
-			__func__, __LINE__, what); \
-		exit(1); \
-	} \
-} while (0);
+#include "cl_error.h"
+#include "cl_mem.h"
 
-#define REALLOC(var, num, what) do { \
-	var = realloc(var, num*sizeof(*var)); \
-	if (!var) { \
-		fprintf(stderr, "%s:%s: %s : Out of memory\n", \
-			__func__, __LINE__, what); \
-		exit(1); \
-	} \
-} while (0);
+cl_uint num_platforms;
+cl_platform_id *platform;
+char **platform_name;
+cl_uint *num_devs;
+cl_uint num_devs_all;
+cl_device_id *all_devices;
+cl_device_id *device;
 
 
 char *buffer;
@@ -58,44 +37,77 @@ size_t bufsz, nusz;
 	printf("  %-46s: %s\n", str, buffer); \
 } while (0);
 
+/* Print platform info and prepare arrays for device info */
 void
-printPlatformInfo(cl_platform_id pid)
+printPlatformInfo(cl_uint p)
 {
+	cl_platform_id pid = platform[p];
+
 #define PARAM(param, str) \
 	SHOW_STRING(clGetPlatformInfo, pid, CL_PLATFORM_##param, "Platform " str)
 
+	puts("");
 	PARAM(NAME, "Name");
+
+	/* Store name for future reference */
+	size_t len = strlen(buffer);
+	platform_name[p] = malloc(len + 1);
+	/* memcpy instead of strncpy since we already have the len
+	 * and memcpy is possibly more optimized */
+	memcpy(platform_name[p], buffer, len);
+	platform_name[p][len] = '\0';
+
 	PARAM(VENDOR, "Vendor");
 	PARAM(VERSION, "Version");
 	PARAM(PROFILE, "Profile");
 	PARAM(EXTENSIONS, "Extensions");
 #undef PARAM
+
+	error = clGetDeviceIDs(pid, CL_DEVICE_TYPE_ALL, 0, NULL, num_devs + p);
+	CHECK_ERROR("number of devices");
+	num_devs_all += num_devs[p];
 }
 
 void
-printDeviceInfo(cl_device_id *dev)
+printDeviceInfo(cl_uint d)
 {
+	cl_device_id dev = device[d];
+#define STR_PARAM(param, str) \
+	SHOW_STRING(clGetDeviceInfo, dev, CL_DEVICE_##param, "Device " str)
+	STR_PARAM(NAME, "Name");
 }
 
 int main(void)
 {
-	cl_uint num_entries;
-	cl_platform_id *platforms;
-	cl_device_id *devices;
-	uint i;
+	cl_uint p, d;
 
-	error = clGetPlatformIDs(0, NULL, &num_entries);
+	error = clGetPlatformIDs(0, NULL, &num_platforms);
 	CHECK_ERROR("number of platforms");
 
-	printf("%-48s: %u\n", "Number of platforms", num_entries);
-	if (!num_entries)
+	printf("%-48s: %u\n", "Number of platforms", num_platforms);
+	if (!num_platforms)
 		return 0;
 
-	ALLOC(platforms, num_entries, "platform IDs");
-	error = clGetPlatformIDs(num_entries, platforms, NULL);
+	ALLOC(platform, num_platforms, "platform IDs");
+	error = clGetPlatformIDs(num_platforms, platform, NULL);
 	CHECK_ERROR("platform IDs");
 
-	for (i = 0; i < num_entries; ++i)
-		printPlatformInfo(platforms[i]);
+	ALLOC(platform_name, num_platforms, "platform names");
+	ALLOC(num_devs, num_platforms, "platform devices");
+
+	for (p = 0; p < num_platforms; ++p)
+		printPlatformInfo(p);
 
+	ALLOC(all_devices, num_devs_all, "device IDs");
+
+	for (p = 0, device = all_devices;
+	     p < num_platforms;
+	     device += num_devs[p++]) {
+		error = clGetDeviceIDs(platform[p], CL_DEVICE_TYPE_ALL, num_devs[p], device, NULL);
+		printf("\n  %-46s: %s\n", "Platform Name", platform_name[p]);
+		printf("%-48s: %u\n", "Number of devices", num_devs[p]);
+		for (d = 0; d < num_devs[p]; ++d) {
+			printDeviceInfo(d);
+		}
+	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opencl/clinfo.git



More information about the Pkg-opencl-commits mailing list