[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58160] branches/soc-2013-vse/source/ blender: Adding OpenCL Device Manager (CLDM)

Alexander Kuznetsov kuzsasha at gmail.com
Wed Jul 10 22:29:35 CEST 2013


Revision: 58160
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58160
Author:   alexk
Date:     2013-07-10 20:29:34 +0000 (Wed, 10 Jul 2013)
Log Message:
-----------
Adding OpenCL Device Manager (CLDM)

For now it just list OpenCL platforms and their devices
Also, it stores devices' parameters

Later, it should provide an interface for managing the devices

Added Paths:
-----------
    branches/soc-2013-vse/source/blender/cldm/
    branches/soc-2013-vse/source/blender/cldm/CLDM_init.c
    branches/soc-2013-vse/source/blender/cldm/CLDM_init.h
    branches/soc-2013-vse/source/blender/cldm/CMakeLists.txt

Added: branches/soc-2013-vse/source/blender/cldm/CLDM_init.c
===================================================================
--- branches/soc-2013-vse/source/blender/cldm/CLDM_init.c	                        (rev 0)
+++ branches/soc-2013-vse/source/blender/cldm/CLDM_init.c	2013-07-10 20:29:34 UTC (rev 58160)
@@ -0,0 +1,320 @@
+#include "CLDM_init.h"
+#include "MEM_guardedalloc.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+
+#define _XTYPEDEF_MASK
+#include <GL/glx.h>
+
+static  char* ksource[] ={
+"__kernel void square(                                                       \n"
+"   __global uchar* in,                                              \n"
+	""
+//"   __global uchar* out)                                             \n"
+	"__write_only image2d_t out,"
+	"   const unsigned int sizex, "
+"   const unsigned int sizey) "
+"{                                                                      \n"
+"   int i = get_global_id(0);                                           \n"
+	"   int j = get_global_id(1);                                           \n"
+/*"       out[i] = 255-in[i];                                \n"
+	"i++; out[i] = in[i];"
+	"i++; out[i] = in[i];"
+	"i++; out[i] = in[i];"*/
+	"if(i>=sizex || j>=sizey)return;\n"
+	"int2 pos = (int2)(i, j);\n"
+	"float4 val = (float4)(pow(1.0/255.0*in[(j*sizex+i)*4],2.2), 0.0, 0, 1.0);\n"
+	"write_imagef(out, pos, val);\n"
+
+
+
+"}                                                                      \n"
+"\n"};
+
+
+typedef unsigned int uint;
+
+cl_context cl_main_context;
+cl_command_queue cl_main_queue;
+cl_kernel kernel_square;
+cldm_device *main_cl_device;
+
+
+
+struct cldm_main
+{
+	cldm_platform * platforms;
+	uint num_platforms;
+	uint ready;
+
+} cldmmain = {0};
+
+static char * CLDM_get_cl_platform_name(cl_platform_id cl_platform)
+{
+	char * r = NULL;
+	size_t size = 0;
+
+	if(CL_SUCCESS ==
+			clGetPlatformInfo(cl_platform, CL_PLATFORM_NAME, 0, NULL, &size))
+	{
+		r = MEM_mallocN((sizeof(*r)*(size+1)),"cl_string");
+
+
+		if(CL_SUCCESS ==
+				clGetPlatformInfo(cl_platform, CL_PLATFORM_NAME, size, r, &size))
+		{
+
+			r[size] = '\0';
+		}
+		else
+		{
+			MEM_freeN(r);
+			r = NULL;
+		}
+
+
+
+	}
+	return r;
+
+}
+
+
+static char * CLDM_get_cl_device_name(cl_device_id cl_device)
+{
+	char * r = NULL;
+	size_t size = 0;
+
+	if(CL_SUCCESS ==
+			clGetDeviceInfo(cl_device, CL_DEVICE_NAME, 0, NULL, &size))
+	{
+		r = MEM_mallocN((sizeof(*r)*(size+1)),"cl_string");
+
+
+		if(CL_SUCCESS ==
+				clGetDeviceInfo(cl_device, CL_DEVICE_NAME, size, r, &size))
+		{
+
+			r[size] = '\0';
+		}
+		else
+		{
+			MEM_freeN(r);
+			r = NULL;
+		}
+
+
+
+	}
+	return r;
+
+}
+
+
+
+void cldm_mark_gl_devies(cl_context_properties gl_context)
+{
+	if(clGetGLContextInfoKHR)
+	{
+		size_t  num_devices = 0;
+		cl_device_id * devices;
+
+		clGetGLContextInfoKHR(&gl_context, CL_DEVICES_FOR_GL_CONTEXT_KHR,
+							  0, NULL, &num_devices);
+
+		devices = (cl_device_id*)MEM_mallocN(num_devices*sizeof(*devices), "cl_gl_devices");
+
+		clGetGLContextInfoKHR(&gl_context, CL_DEVICES_FOR_GL_CONTEXT_KHR,
+							  num_devices*sizeof(*devices), devices, &num_devices);
+
+
+
+
+
+		MEM_freeN(devices);
+
+	}
+
+
+
+}
+
+
+
+void CLDM_init(void)
+{
+	static int hasCLDM = 0;
+
+	if(!cldmmain.ready)
+	{
+		cl_uint cl_num_platform = 0;
+		cl_platform_id *cl_platforms = NULL;
+		uint i;
+
+		OCL_init();
+
+
+
+		clGetPlatformIDs(0, NULL, &cl_num_platform);
+
+		cl_platforms = (cl_platform_id *)MEM_mallocN((sizeof(*cl_platforms)*cl_num_platform), "cl_platforms");
+		clGetPlatformIDs(cl_num_platform, cl_platforms, &cl_num_platform);
+
+
+		cldmmain.platforms = (cldm_platform *)MEM_mallocN((sizeof(*cldmmain.platforms)*cl_num_platform), "CLDM_platforms");
+		cldmmain.num_platforms = cl_num_platform;
+
+
+
+		for(i = 0; i < cl_num_platform; i++)
+		{
+			cl_uint cl_num_devices = 0;
+			cl_device_id *cl_devices = NULL;
+			uint j;
+
+
+			cldmmain.platforms[i].pid = cl_platforms[i];
+			cldmmain.platforms[i].name = CLDM_get_cl_platform_name(cldmmain.platforms[i].pid);
+
+			printf("%s\n", cldmmain.platforms[i].name);
+
+
+
+			clGetDeviceIDs(cldmmain.platforms[i].pid, CL_DEVICE_TYPE_ALL, 0, NULL, &cl_num_devices);
+			cl_devices = (cl_device_id*)MEM_mallocN((sizeof(*cl_devices)*cl_num_devices), "cl_devices");
+
+			clGetDeviceIDs(cldmmain.platforms[i].pid, CL_DEVICE_TYPE_ALL, cl_num_devices, cl_devices, &cl_num_devices);
+			cldmmain.platforms[i].devices = (cldm_device*)MEM_mallocN((sizeof(*cldmmain.platforms[0].devices)*cl_num_devices), "CLDM_devices");
+
+			cldmmain.platforms[i].num_devices = cl_num_devices;
+
+			for(j = 0; j < cl_num_devices; j++)
+			{
+				cl_bool booltest;
+				size_t ret_size = 0;
+				cl_int ret = 0;
+
+				cldmmain.platforms[i].devices[j].did = cl_devices[j];
+				cldmmain.platforms[i].devices[j].name =CLDM_get_cl_device_name(cldmmain.platforms[i].devices[j].did);
+				cldmmain.platforms[i].devices[j].flags = 0;
+				cldmmain.platforms[i].devices[j].platform = &cldmmain.platforms[i];
+
+				booltest = 0;
+				ret = clGetDeviceInfo(cldmmain.platforms[i].devices[j].did,
+										CL_DEVICE_HOST_UNIFIED_MEMORY,
+										sizeof(booltest), &booltest, &ret_size);
+
+				if(ret == CL_SUCCESS && ret_size == sizeof(booltest) &&
+						booltest == CL_TRUE)
+				{
+					cldmmain.platforms[i].devices[j].flags |= CLDM_FLAG_MAIN_MEMORY;
+
+				}
+
+				printf("\t%s\n", cldmmain.platforms[i].devices[j].name);
+
+			}
+
+			MEM_freeN(cl_devices);
+
+
+		}
+
+
+		MEM_freeN(cl_platforms);
+
+
+		cldm_mark_gl_devies(glXGetCurrentContext());
+
+		main_cl_device = &cldmmain.platforms[0].devices[0];
+
+
+		{
+			cl_context_properties properties[] = {
+			   CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
+			   CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
+			   CL_CONTEXT_PLATFORM, (cl_context_properties) cldmmain.platforms[0].pid,
+			   0, 0};
+
+			cl_int err = 0;
+
+			cl_program cl_program;
+
+
+
+			cl_main_context = clCreateContext(properties, 1, &(cldmmain.platforms[0].devices[0].did), NULL, NULL, &err);
+			cl_program = clCreateProgramWithSource(cl_main_context, 1,  ksource, 0, &err);
+
+
+			if (!cl_program)
+			{
+				printf("Error cannnot create program\n");
+			}
+
+			err = clBuildProgram(cl_program, 0, NULL, NULL, NULL, NULL);
+			  if (err != CL_SUCCESS)
+			  {
+				  size_t len;
+				  char buffer[2048];
+
+				  printf("Error: Failed to build program executable!\n");
+				  clGetProgramBuildInfo(cl_program, cldmmain.platforms[0].devices[0].did, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
+				  printf("%s\n", buffer);
+				  exit(1);
+			  }
+
+
+			  kernel_square = clCreateKernel(cl_program, "square", &err);
+
+			cl_main_queue = clCreateCommandQueue(cl_main_context, cldmmain.platforms[0].devices[0].did, 0, &err);
+
+
+		}
+
+
+
+		cldmmain.ready = 1;
+	}
+
+
+
+
+
+}
+
+
+void CLDM_exit(void)
+{
+	if(cldmmain.ready)
+	{
+		uint i ;
+		for(i=0; i < cldmmain.num_platforms; i++)
+		{
+			uint j;
+
+			for(j = 0; j < cldmmain.platforms[i].num_devices; j++)
+			{
+				MEM_freeN(cldmmain.platforms[i].devices[j].name);
+
+			}
+
+			MEM_freeN(cldmmain.platforms[i].devices);
+
+			MEM_freeN(cldmmain.platforms[i].name);
+		}
+
+		MEM_freeN(cldmmain.platforms);
+
+
+
+
+	}
+
+
+
+
+
+
+}
+

Added: branches/soc-2013-vse/source/blender/cldm/CLDM_init.h
===================================================================
--- branches/soc-2013-vse/source/blender/cldm/CLDM_init.h	                        (rev 0)
+++ branches/soc-2013-vse/source/blender/cldm/CLDM_init.h	2013-07-10 20:29:34 UTC (rev 58160)
@@ -0,0 +1,52 @@
+#ifndef __CLDM_INIT_H__
+#define __CLDM_INIT_H__
+
+
+#include "OCL_opencl.h"
+
+typedef unsigned int uint;
+
+struct cldm_platform;
+
+typedef struct cldm_device
+{
+	char * name;
+	cl_device_id did;
+	uint flags;
+
+	struct cldm_platform *platform;
+
+} cldm_device;
+
+typedef struct cldm_platform
+{
+	char * name;
+	cl_platform_id pid;
+	uint num_devices;
+	cldm_device  * devices;
+
+} cldm_platform;
+
+
+typedef struct cldm_i_dparam
+{
+	uint num_i_dev;
+	cldm_device **dev;
+
+
+} cldm_i_dparam;
+
+extern cl_context cl_main_context;
+extern cl_command_queue cl_main_queue;
+extern cl_kernel kernel_square;
+extern cldm_device *main_cl_device;
+
+#define CLDM_FLAG_MAIN_MEMORY (0<<0)
+#define CLDM_FLAG_GL_MEMORY (0<<1)
+
+
+void CLDM_init(void);
+void CLDM_exit(void);
+
+
+#endif /*__CLDM_INIT_H__*/

Added: branches/soc-2013-vse/source/blender/cldm/CMakeLists.txt
===================================================================
--- branches/soc-2013-vse/source/blender/cldm/CMakeLists.txt	                        (rev 0)
+++ branches/soc-2013-vse/source/blender/cldm/CMakeLists.txt	2013-07-10 20:29:34 UTC (rev 58160)
@@ -0,0 +1,44 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# The Original Code is Copyright (C) 2013, Blender Foundation
+# All rights reserved.
+#
+# Contributor(s): Alexandr Kuznetsov.
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+	../../../intern/opencl
+	../../../intern/guardedalloc
+)
+
+set(INC_SYS
+	${GLEW_INCLUDE_PATH}
+)
+
+set(SRC
+	CLDM_init.c
+
+	CLDM_init.h
+)
+
+
+if(WITH_INTERNATIONAL)
+	add_definitions(-DWITH_INTERNATIONAL)
+endif()
+
+blender_add_lib(bf_cldm "${SRC}" "${INC}" "${INC_SYS}")




More information about the Bf-blender-cvs mailing list