1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 ///original author: Erwin Coumans
17 
18 #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
19 #include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
20 
21 #include <stdio.h>
22 
23 cl_context g_cxMainContext;
24 cl_command_queue g_cqCommandQue;
25 
26 #include "Bullet3Common/b3Logging.h"
27 
myerrorwarningprintf(const char * msg)28 void myerrorwarningprintf(const char* msg)
29 {
30 	//OutputDebugStringA(msg);
31 	//printf(msg);
32 }
33 
myprintf(const char * msg)34 void myprintf(const char* msg)
35 {
36 	//OutputDebugStringA(msg);
37 	printf(msg);
38 }
39 
main(int argc,char * argv[])40 int main(int argc, char* argv[])
41 {
42 	b3SetCustomPrintfFunc(myprintf);
43 	b3SetCustomWarningMessageFunc(myerrorwarningprintf);
44 	b3SetCustomErrorMessageFunc(myerrorwarningprintf);
45 
46 	b3Printf("test b3Printf\n");
47 	b3Warning("test warning\n");
48 	b3Error("test error\n");
49 
50 	int ciErrNum = 0;
51 
52 	cl_device_type deviceType = CL_DEVICE_TYPE_ALL;
53 	const char* vendorSDK = b3OpenCLUtils::getSdkVendorName();
54 
55 	b3Printf("This program was compiled using the %s OpenCL SDK\n", vendorSDK);
56 	int numPlatforms = b3OpenCLUtils::getNumPlatforms();
57 	b3Printf("Num Platforms = %d\n", numPlatforms);
58 
59 	for (int i = 0; i < numPlatforms; i++)
60 	{
61 		cl_platform_id platform = b3OpenCLUtils::getPlatform(i);
62 		b3OpenCLPlatformInfo platformInfo;
63 		b3OpenCLUtils::getPlatformInfo(platform, &platformInfo);
64 		b3Printf("--------------------------------\n");
65 		b3Printf("Platform info for platform nr %d:\n", i);
66 		b3Printf("  CL_PLATFORM_VENDOR: \t\t\t%s\n", platformInfo.m_platformVendor);
67 		b3Printf("  CL_PLATFORM_NAME: \t\t\t%s\n", platformInfo.m_platformName);
68 		b3Printf("  CL_PLATFORM_VERSION: \t\t\t%s\n", platformInfo.m_platformVersion);
69 
70 		g_cxMainContext = b3OpenCLUtils::createContextFromPlatform(platform, deviceType, &ciErrNum);
71 
72 		int numDevices = b3OpenCLUtils::getNumDevices(g_cxMainContext);
73 		b3Printf("Num Devices = %d\n", numDevices);
74 		for (int j = 0; j < numDevices; j++)
75 		{
76 			cl_device_id device = b3OpenCLUtils::getDevice(g_cxMainContext, j);
77 			b3OpenCLDeviceInfo devInfo;
78 			b3OpenCLUtils::getDeviceInfo(device, &devInfo);
79 			b3OpenCLUtils::printDeviceInfo(device);
80 			g_cqCommandQue = clCreateCommandQueue(g_cxMainContext, device, 0, &ciErrNum);
81 
82 			b3OpenCLArray<char> memTester(g_cxMainContext, g_cqCommandQue, 0, true);
83 			int maxMem = 0;
84 			bool result = true;
85 			for (size_t i = 1; result; i++)
86 			{
87 				size_t numBytes = i * 1024 * 1024;
88 				result = memTester.resize(numBytes, false);
89 
90 				if (result)
91 				{
92 					maxMem = numBytes;
93 				}
94 				else
95 				{
96 					break;
97 				}
98 			}
99 			printf("allocated %d MB successfully\n", maxMem / (1024 * 1024));
100 			clReleaseCommandQueue(g_cqCommandQue);
101 			g_cqCommandQue = 0;
102 		}
103 
104 		clReleaseContext(g_cxMainContext);
105 		g_cxMainContext = 0;
106 	}
107 
108 	///Easier method to initialize OpenCL using createContextFromType for a GPU
109 	deviceType = CL_DEVICE_TYPE_GPU;
110 
111 	void* glCtx = 0;
112 	void* glDC = 0;
113 	b3Printf("Initialize OpenCL using b3OpenCLUtils::createContextFromType for CL_DEVICE_TYPE_GPU\n");
114 	g_cxMainContext = b3OpenCLUtils::createContextFromType(deviceType, &ciErrNum, glCtx, glDC);
115 	oclCHECKERROR(ciErrNum, CL_SUCCESS);
116 
117 	if (g_cxMainContext)
118 	{
119 		int numDev = b3OpenCLUtils::getNumDevices(g_cxMainContext);
120 
121 		for (int i = 0; i < numDev; i++)
122 		{
123 			cl_device_id device;
124 			device = b3OpenCLUtils::getDevice(g_cxMainContext, i);
125 			b3OpenCLDeviceInfo clInfo;
126 			b3OpenCLUtils::getDeviceInfo(device, &clInfo);
127 			b3OpenCLUtils::printDeviceInfo(device);
128 			// create a command-queue
129 			g_cqCommandQue = clCreateCommandQueue(g_cxMainContext, device, 0, &ciErrNum);
130 			oclCHECKERROR(ciErrNum, CL_SUCCESS);
131 			//normally you would create and execute kernels using this command queue
132 
133 			int maxMem = 0;
134 			{
135 				b3OpenCLArray<char> memTester(g_cxMainContext, g_cqCommandQue, 0, true);
136 
137 				bool result = true;
138 				for (size_t i = 1; result; i++)
139 				{
140 					size_t numBytes = i * 1024 * 1024;
141 					result = memTester.resize(numBytes, false);
142 
143 					if (result)
144 					{
145 						maxMem = numBytes;
146 					}
147 					else
148 					{
149 						break;
150 					}
151 				}
152 				printf("allocated %d MB successfully\n", maxMem / (1024 * 1024));
153 			}
154 
155 			clReleaseCommandQueue(g_cqCommandQue);
156 		}
157 
158 		clReleaseContext(g_cxMainContext);
159 	}
160 	else
161 	{
162 		b3Printf("No OpenCL capable GPU found!");
163 	}
164 	b3Printf("press <Enter>\n");
165 	getchar();
166 	return 0;
167 }
168