File: main.cpp

package info (click to toggle)
bullet 2.87%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,272 kB
  • sloc: cpp: 204,241; ansic: 12,100; lisp: 12,017; python: 593; makefile: 136; sh: 8
file content (221 lines) | stat: -rw-r--r-- 6,130 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:

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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

///original author: Erwin Coumans

#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
#include <stdio.h>
#include <string.h>

#include "Bullet3Common/shared/b3Float4.h"

//typedef b3Vector3 b3Float4;
typedef  struct b3Contact4Data b3Contact4Data_t;
struct b3Contact4Data
{
	b3Float4	m_worldPos[4];
	b3Float4	m_localPosA[4];
	b3Float4	m_localPosB[4];
	b3Float4	m_worldNormal;	//	w: m_nPoints
	unsigned short  m_restituitionCoeffCmp;
	unsigned short  m_frictionCoeffCmp;
	int m_batchIdx;
	int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr
	int m_bodyBPtrAndSignBit;
	int	m_childIndexA;
	int	m_childIndexB;
	int m_unused1;
	int m_unused2;

};


#define MSTRINGIFY(A) #A

static const char* s_testKernelString= MSTRINGIFY(

struct MyTest
{
	int bla;
};

typedef float4	b3Float4;
typedef  struct b3Contact4Data b3Contact4Data_t;
struct b3Contact4Data
{
	b3Float4	m_worldPos[4];
	b3Float4	m_localPosA[4];
	b3Float4	m_localPosB[4];
	b3Float4	m_worldNormal;	//	w: m_nPoints
	unsigned short  m_restituitionCoeffCmp;
	unsigned short  m_frictionCoeffCmp;
	int m_batchIdx;
	int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr
	int m_bodyBPtrAndSignBit;
	int	m_childIndexA;
	int	m_childIndexB;
	int m_unused1;
	int m_unused2;

};
inline int b3Contact4Data_getNumPoints(const struct b3Contact4Data* contact)
{
	return (int)contact->m_worldNormal.w;
};
inline void b3Contact4Data_setNumPoints(struct b3Contact4Data* contact, int numPoints)
{
	contact->m_worldNormal.w = (float)numPoints;
};

typedef volatile __global int* my_counter32_t;


__kernel void   testKernel( __global int* testData, __global b3Contact4Data_t* contactData, my_counter32_t numElements)
{
	int id = get_local_id(0);
	int sz = sizeof(b3Contact4Data_t);
	testData[id]=sz;

	__private b3Contact4Data_t tmp;
	if (id==0)
	{
		tmp = contactData[1];
		contactData[1] = contactData[0];
		contactData[0] = tmp;
	}
}



);



#include "Bullet3Common/b3Logging.h"


void myprintf(const char* msg)
{
	//OutputDebugStringA(msg);
	printf("%s",msg);
}

int main(int argc, char* argv[])
{
	b3SetCustomPrintfFunc(myprintf);
	//b3SetCustomWarningMessageFunc(myprintf);
	//b3SetCustomErrorMessageFunc(myprintf);

	b3Printf("test b3Printf\n");
	b3Warning("test warning\n");
	b3Error("test error\n");

	int ciErrNum = 0;

	cl_device_type deviceType = CL_DEVICE_TYPE_GPU;
	const char* vendorSDK = b3OpenCLUtils::getSdkVendorName();

	b3Printf("This program was compiled using the %s OpenCL SDK\n",vendorSDK);
	int numPlatforms = b3OpenCLUtils::getNumPlatforms();
	b3Printf("Num Platforms = %d\n", numPlatforms);

	for (int i=0;i<numPlatforms;i++)
	{
		cl_platform_id platform = b3OpenCLUtils::getPlatform(i);
		b3OpenCLPlatformInfo platformInfo;
		b3OpenCLUtils::getPlatformInfo(platform,&platformInfo);
		b3Printf("--------------------------------\n");
		b3Printf("Platform info for platform nr %d:\n",i);
		b3Printf("  CL_PLATFORM_VENDOR: \t\t\t%s\n",platformInfo.m_platformVendor);
		b3Printf("  CL_PLATFORM_NAME: \t\t\t%s\n",platformInfo.m_platformName);
		b3Printf("  CL_PLATFORM_VERSION: \t\t\t%s\n",platformInfo.m_platformVersion);

		cl_context context = b3OpenCLUtils::createContextFromPlatform(platform,deviceType,&ciErrNum);
		if (context)
		{
			int numDevices = b3OpenCLUtils::getNumDevices(context);
			b3Printf("Num Devices = %d\n", numDevices);
			for (int j=0;j<numDevices;j++)
			{
				cl_device_id dev = b3OpenCLUtils::getDevice(context,j);
				b3OpenCLDeviceInfo devInfo;
				b3OpenCLUtils::getDeviceInfo(dev,&devInfo);
				b3OpenCLUtils::printDeviceInfo(dev);

				int errNum;

				cl_command_queue queue = clCreateCommandQueue(context, dev, 0, &errNum);


				cl_program pairBenchProg=0;

				cl_kernel testKernel = b3OpenCLUtils::compileCLKernelFromString(context,dev,s_testKernelString,"testKernel",&errNum,pairBenchProg);
				if (testKernel)
				{
					printf("kernel compiled ok\n");

					int numWorkItems = 64;
					b3OpenCLArray<int> deviceElements(context,queue);
					b3OpenCLArray<int> atomicCounter(context,queue);
					b3OpenCLArray<b3Contact4Data> deviceContacts(context,queue);
					b3AlignedObjectArray<b3Contact4Data> hostContacts;

					b3Contact4Data tmp;
					int sz = sizeof(b3Contact4Data);
					memset(&tmp,1,sz);
					deviceContacts.push_back(tmp);
					b3Contact4Data tmp2 = tmp;
					memset(&tmp,2,sz);
					deviceContacts.push_back(tmp);
					b3Contact4Data tmp3 = tmp;


					atomicCounter.push_back(0);
					deviceElements.resize(numWorkItems);
					b3LauncherCL run(queue,testKernel,"testKernel");
					run.setBuffer(deviceElements.getBufferCL());
					run.setBuffer(deviceContacts.getBufferCL());
					run.setBuffer(atomicCounter.getBufferCL());

					run.launch1D(numWorkItems);

					b3AlignedObjectArray<int> hostElements;
					deviceElements.copyToHost(hostElements);
					deviceContacts.copyToHost(hostContacts);
					tmp2 = hostContacts[0];
					tmp3 = hostContacts[1];


					printf("...\n");

				} else
				{
					printf("kernel failed to compile\n");
				}



			}
		}

		clReleaseContext(context);
	}

	b3Printf("\npress <Enter>\n");
	getchar();
	return 0;
}