-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.cpp
170 lines (126 loc) · 4.06 KB
/
particles.cpp
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
/*
* Copyright 1993-2014 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
/*
Particle system example with collisions using uniform grid
CUDA 2.1 SDK release 12/2008
- removed atomic grid method, some optimization, added demo mode.
CUDA 2.2 release 3/2009
- replaced sort function with latest radix sort, now disables v-sync.
- added support for automated testing and comparison to a reference value.
*/
// CUDA runtime
#include <cuda_runtime.h>
// CUDA utilities and system includes
#include <helper_functions.h>
#include <helper_cuda.h> // includes cuda.h and cuda_runtime_api.h
// Includes
#include <stdlib.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include "particleSystem.h"
#define MAX_EPSILON_ERROR 5.00f
#define THRESHOLD 0.30f
#define GRID_SIZE 64
#define NUM_PARTICLES 16384
const uint width = 640, height = 480;
uint numParticles = 0;
uint3 gridSize;
int numIterations = 0; // run until exit
FILE *fpout;
// simulation parameters
float timestep = 0.000007f;
int iterations = 1;
ParticleSystem *psystem = 0;
// fps
static int fpsCount = 0;
static int fpsLimit = 1;
StopWatchInterface *timer = NULL;
float modelView[16];
// Auto-Verification Code
const int frameCheckNumber = 4;
unsigned int frameCount = 0;
unsigned int g_TotalErrors = 0;
char *g_refFile = NULL;
const char *sSDKsample = "CUDA Particle Dynamics Simulation";
extern "C" void cudaInit(int argc, char **argv);
// initialize particle system
void initParticleSystem(int numParticles, uint3 gridSize)
{
psystem = new ParticleSystem(numParticles, gridSize);
psystem->reset(ParticleSystem::CONFIG_GRID);
sdkCreateTimer(&timer);
}
void cleanup()
{
sdkDeleteTimer(&timer);
}
void runBenchmark(int iterations, char *exec_path)
{
int file_count=0, iterationsPerFrame = (int)(1.0/(30.0*timestep));
printf("Run %u particles simulation for %d iterations...\n\n", numParticles, iterations);
//abb58: 1. what are you trying to sync???
cudaDeviceSynchronize();
sdkStartTimer(&timer);
for (int i = 0; i < iterations; ++i){
psystem->update(timestep);
if (i % iterationsPerFrame == 0) {
psystem->writeParticles(fpout, 0, numParticles, file_count);
//psystem->dumpParticles(0, numParticles, file_count);
file_count++;
}
}
cudaDeviceSynchronize();
sdkStopTimer(&timer);
float fAvgSeconds = ((float)1.0e-3 * (float)sdkGetTimerValue(&timer)/(float)iterations);
}
inline float frand()
{
return rand() / (float) RAND_MAX; // value between 0 to 1
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv)
{
// Write the xyz file
char outfile[256];
sprintf(outfile, "outfile.xyz");
if ((fpout = fopen(outfile, "w")) == NULL) {
printf("Cannot Open File\n");
exit(1);
}
printf("%s Starting...\n\n", sSDKsample);
numParticles = NUM_PARTICLES;
uint gridDim = GRID_SIZE;
numIterations = 0;
gridSize.x = gridSize.y = gridSize.z = gridDim;
printf("grid: %d x %d x %d = %d cells\n", gridSize.x, gridSize.y, gridSize.z, gridSize.x*gridSize.y*gridSize.z);
printf("particles: %d\n", numParticles);
cudaInit(argc, argv);
initParticleSystem(numParticles, gridSize);
printf("%e\n", timestep);
//psystem->dumpParticles(0, numParticles-1, 0);
if (numIterations <= 0)
{
numIterations = (int)(2.0/timestep);
}
std::cout << "1. I am here \n";
runBenchmark(numIterations, argv[0]);
std::cout << "2. I am here \n";
if (psystem){
delete psystem;
cleanup();
}
cudaDeviceReset();
exit(g_TotalErrors > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}