Skip to content

Commit 655c6be

Browse files
author
Sami Hatna
committed
Merge branch 'phase4' into 'main'
Phase4 Code Review See merge request sami.hatna/crowd-simulation!5
2 parents 4e07622 + a140c99 commit 655c6be

26 files changed

+1322
-602
lines changed

CMakeLists.txt

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,44 @@ project(crowd-simulation)
88
set(CMAKE_C_COMPILER /home/sami/sycl_workspace/llvm/build/bin/clang)
99
set(CMAKE_CXX_COMPILER /home/sami/sycl_workspace/llvm/build/bin/clang++)
1010

11-
find_package(SDL2 REQUIRED)
12-
include_directories(${SDL2_INCLUDE_DIRS})
11+
OPTION(PROFILING_MODE "Enable profiling" off)
12+
if(PROFILING_MODE)
13+
add_definitions(-DPROFILING_MODE)
14+
else()
15+
find_package(SDL2 REQUIRED)
16+
include_directories(${SDL2_INCLUDE_DIRS})
17+
endif()
18+
19+
OPTION(STATS "Run in stats mode" off)
20+
if (STATS)
21+
add_definitions(-DSTATS)
22+
endif()
23+
24+
set(SYCL_BACKEND "SYCL_BACKEND" CACHE STRING "Backend chosen by the user at CMake configure time")
25+
set_property(CACHE SYCL_BACKEND PROPERTY STRINGS spir cuda hip)
26+
27+
if(SYCL_BACKEND STREQUAL hip)
28+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -fsycl-targets=amdgcn-amd-amdhsa")
29+
elseif(SYCL_BACKEND STREQUAL cuda)
30+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -fsycl-targets=nvptx64-nvidia-cuda")
31+
else()
32+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -fsycl-targets=spir64")
33+
endif()
34+
35+
find_package(Python QUIET)
36+
if(Python_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/scripts/InputFileGenerator.py")
37+
execute_process(COMMAND ${Python_EXECUTABLE} ../scripts/InputFileGenerator.py)
38+
else()
39+
message(WARNING "Unable to generate input configuration files")
40+
endif()
1341

1442
add_executable(crowdsim src/main.cpp)
15-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -fsycl-targets=spir64-gen")
1643

1744
add_subdirectory(external)
1845

19-
target_link_libraries(crowdsim PUBLIC ${SDL2_LIBRARIES} external)
46+
if (PROFILING_MODE)
47+
target_link_libraries(crowdsim PUBLIC external)
48+
else()
49+
target_link_libraries(crowdsim PUBLIC ${SDL2_LIBRARIES} external)
50+
endif()
2051
target_include_directories(crowdsim PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/external")

external/Actor.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, int pPathId,
66
: pos(pPos), velocity(pVelocity), desiredSpeed(pDesiredSpeed),
77
pathId(pPathId), mass(pMass), radius(pRadius),
88
atDestination(pAtDestination), color(pColor),
9-
heatmapEnabled(pHeatmapEnabled), variation({0, 0}), destinationIndex(0),
10-
bBox({0, 0}) {}
9+
heatmapEnabled(pHeatmapEnabled), destinationIndex(0), bBox({0, 0}) {}
1110

1211
SYCL_EXTERNAL vecType Actor::getPos() const { return pos; }
1312

@@ -21,8 +20,6 @@ SYCL_EXTERNAL int Actor::getDestinationIndex() const {
2120
return destinationIndex;
2221
}
2322

24-
SYCL_EXTERNAL vecType Actor::getVariation() const { return variation; }
25-
2623
SYCL_EXTERNAL float Actor::getMass() const { return mass; }
2724

2825
SYCL_EXTERNAL float Actor::getRadius() const { return radius; }
@@ -37,6 +34,8 @@ SYCL_EXTERNAL std::array<int, 2> Actor::getBBox() const { return bBox; }
3734

3835
SYCL_EXTERNAL uint Actor::getSeed() const { return seed; }
3936

37+
SYCL_EXTERNAL float Actor::getForce() const { return force; }
38+
4039
SYCL_EXTERNAL void Actor::setPos(vecType newPos) { pos = newPos; }
4140

4241
SYCL_EXTERNAL void Actor::setVelocity(vecType newVelocity) {
@@ -47,10 +46,6 @@ SYCL_EXTERNAL void Actor::setDesiredSpeed(float newDesiredSpeed) {
4746
desiredSpeed = newDesiredSpeed;
4847
}
4948

50-
SYCL_EXTERNAL void Actor::setVariation(vecType newVariation) {
51-
variation = newVariation;
52-
}
53-
5449
SYCL_EXTERNAL void Actor::setAtDestination(bool param) {
5550
atDestination = param;
5651
}
@@ -65,20 +60,13 @@ SYCL_EXTERNAL void Actor::setBBox(std::array<int, 2> newBBox) {
6560

6661
SYCL_EXTERNAL void Actor::setSeed(uint newSeed) { seed = newSeed; }
6762

68-
SYCL_EXTERNAL void Actor::refreshVariations() {
69-
// Previous RNG output is used as next seed
70-
seed = randXorShift(seed);
71-
float randX = (float(seed) * (2.0f / 4294967296.0f)) - 1.0f;
72-
seed = randXorShift(seed);
73-
float randY = (float(seed) * (2.0f / 4294967296.0f)) - 1.0f;
74-
this->setVariation({randX, randY});
75-
}
63+
SYCL_EXTERNAL void Actor::setForce(float newForce) { force = newForce; }
7664

77-
SYCL_EXTERNAL void Actor::checkAtDestination(std::array<vecType, 4> destination,
65+
SYCL_EXTERNAL void Actor::checkAtDestination(std::array<vecType, 2> destination,
7866
int pathSize) {
7967
// Destinations are defined as rectangular regions
80-
if (pos[0] >= destination[0][0] && pos[0] <= destination[2][0] &&
81-
pos[1] >= destination[0][1] && pos[1] <= destination[2][1]) {
68+
if (pos[0] >= destination[0][0] && pos[0] <= destination[1][0] &&
69+
pos[1] >= destination[0][1] && pos[1] <= destination[1][1]) {
8270
if (destinationIndex >= PATHALLOCATIONSIZE - 1 ||
8371
destinationIndex >= pathSize - 1) {
8472
this->setAtDestination(true);

external/Actor.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#define Actor_hpp
33

44
#include "Path.hpp"
5-
#include "VectorMaths.hpp"
65
#include "RandomNumber.hpp"
6+
#include "VectorMaths.hpp"
77
#include <array>
88
#include <iostream>
99
#include <random>
@@ -17,14 +17,14 @@ class Actor {
1717
float desiredSpeed;
1818
int pathId;
1919
int destinationIndex;
20-
vecType variation;
2120
float mass;
2221
float radius;
2322
bool atDestination;
2423
std::array<int, 3> color;
2524
bool heatmapEnabled;
2625
std::array<int, 2> bBox;
2726
uint seed;
27+
float force;
2828

2929
public:
3030
Actor(vecType pPos, vecType pVelocity, float pdesiredSpeed, int pPathId,
@@ -36,27 +36,25 @@ class Actor {
3636
SYCL_EXTERNAL float getDesiredSpeed() const;
3737
SYCL_EXTERNAL int getPathId() const;
3838
SYCL_EXTERNAL int getDestinationIndex() const;
39-
SYCL_EXTERNAL vecType getVariation() const;
4039
SYCL_EXTERNAL float getMass() const;
4140
SYCL_EXTERNAL float getRadius() const;
4241
SYCL_EXTERNAL bool getAtDestination() const;
4342
SYCL_EXTERNAL std::array<int, 3> getColor() const;
4443
SYCL_EXTERNAL bool getHeatmapEnabled() const;
4544
SYCL_EXTERNAL std::array<int, 2> getBBox() const;
4645
SYCL_EXTERNAL uint getSeed() const;
46+
SYCL_EXTERNAL float getForce() const;
4747

4848
SYCL_EXTERNAL void setPos(vecType newPos);
4949
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
5050
SYCL_EXTERNAL void setDesiredSpeed(float newDesiredSpeed);
51-
SYCL_EXTERNAL void setVariation(vecType newVariation);
5251
SYCL_EXTERNAL void setAtDestination(bool param);
5352
SYCL_EXTERNAL void setColor(std::array<int, 3> newColor);
5453
SYCL_EXTERNAL void setBBox(std::array<int, 2> newBBox);
5554
SYCL_EXTERNAL void setSeed(uint newSeed);
55+
SYCL_EXTERNAL void setForce(float newForce);
5656

57-
SYCL_EXTERNAL void refreshVariations();
58-
59-
SYCL_EXTERNAL void checkAtDestination(std::array<vecType, 4> destination,
57+
SYCL_EXTERNAL void checkAtDestination(std::array<vecType, 2> destination,
6058
int pathSize);
6159
};
6260

external/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
add_library(external Actor.cpp Room.cpp Path.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp Heatmap.cpp ParseInputFile.cpp RandomNumber.cpp)
1+
add_library(external Actor.cpp
2+
Room.cpp
3+
Path.cpp
4+
MathHelper.cpp
5+
DifferentialEq.cpp
6+
VectorMaths.cpp
7+
Heatmap.cpp
8+
ParseInputFile.cpp
9+
RandomNumber.cpp
10+
Stats.cpp
11+
)

external/DifferentialEq.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ SYCL_EXTERNAL void differentialEq(
44
int actorIndex,
55
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
66
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
7-
sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out) {
7+
sycl::accessor<Path, 1, sycl::access::mode::read> paths) {
88
Actor *currentActor = &actors[actorIndex];
99

1010
vecType pos = currentActor->getPos();
1111

1212
// Calculate personal impulse
1313
float mi = currentActor->getMass();
1414
float v0i = currentActor->getDesiredSpeed();
15-
std::array<vecType, 4> destination =
15+
std::array<vecType, 2> destination =
1616
paths[currentActor->getPathId()]
1717
.getCheckpoints()[currentActor->getDestinationIndex()];
1818

1919
// Find direction vector to nearest point in destination region
2020
std::pair<float, vecType> minRegionDistance;
21+
std::array<vecType, 4> destinationRect = {
22+
destination[0],
23+
{destination[1][0], destination[0][1]},
24+
destination[1],
25+
{destination[0][0], destination[1][1]}};
2126
for (int x = 0; x < 4; x++) {
2227
int endIndex = x == 3 ? 0 : x + 1;
23-
auto dniw = getDistanceAndNiw(currentActor->getPos(),
24-
{destination[x], destination[endIndex]});
28+
auto dniw =
29+
getDistanceAndNiw(currentActor->getPos(),
30+
{destinationRect[x], destinationRect[endIndex]});
2531
if (dniw.first < minRegionDistance.first ||
2632
minRegionDistance.first == 0) {
2733
minRegionDistance = dniw;
@@ -93,8 +99,18 @@ SYCL_EXTERNAL void differentialEq(
9399

94100
vecType forceSum = personalImpulse + peopleForces + wallForces;
95101

102+
#ifdef STATS
103+
if (!currentActor->getAtDestination()) {
104+
currentActor->setForce(magnitude(forceSum));
105+
}
106+
#endif
107+
96108
// Apply random force variations
97-
forceSum += currentActor->getVariation();
109+
float seed = currentActor->getSeed();
110+
float randX = rngMinusOneToOne(seed);
111+
float randY = rngMinusOneToOne(randXorShift(seed));
112+
forceSum += {randX, randY};
113+
currentActor->setSeed(randXorShift(seed));
98114

99115
// Color actor according to heatmap
100116
if (currentActor->getHeatmapEnabled()) {
@@ -111,6 +127,7 @@ SYCL_EXTERNAL void differentialEq(
111127
currentActor->setColor({int(color[0]), int(color[1]), int(color[2])});
112128
}
113129

130+
// Perform integration
114131
vecType acceleration = forceSum / mi;
115132
currentActor->setVelocity(vi + acceleration * TIMESTEP);
116133
currentActor->setPos(pos + currentActor->getVelocity() * TIMESTEP);

external/DifferentialEq.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <sycl/sycl.hpp>
1313

1414
// Constant for scaling inter-actor repulsive force
15-
constexpr float PEOPLEAi = 500;
15+
constexpr float PEOPLEAi = 800;
1616
// Constant for scaling actor-wall repulsive force
1717
constexpr float WALLAi = 1300;
1818
// Reproduces distance kept at normal desired velocities
@@ -31,6 +31,6 @@ SYCL_EXTERNAL void differentialEq(
3131
int actorIndex,
3232
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
3333
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
34-
sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out);
34+
sycl::accessor<Path, 1, sycl::access::mode::read> paths);
3535

3636
#endif

external/Heatmap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Heatmap.hpp"
22

3+
// Required because SDL only takes RGB colors
34
SYCL_EXTERNAL std::array<float, 3> HSVtoRGB(float h, float s, float v) {
45
float c = v * s;
56
double scaledH = sycl::fmod(h / 60.0, 6.0);

external/MathHelper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ SYCL_EXTERNAL float magnitude(vecType inp) {
1717
return sycl::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
1818
}
1919

20+
// Fast inverse square root optimisation
21+
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
2022
SYCL_EXTERNAL float inverseMagnitude(vecType inp) {
2123
return sycl::rsqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
2224
}
@@ -33,6 +35,7 @@ SYCL_EXTERNAL vecType normalize(vecType inp) {
3335
return inp * inverseMagnitude(inp);
3436
}
3537

38+
// Returns pair of distance to line and normalised direction from wall to point
3639
SYCL_EXTERNAL std::pair<float, vecType>
3740
getDistanceAndNiw(vecType point, std::array<vecType, 2> wall) {
3841
vecType AB = getDirectionVector(wall[0], wall[1]);

external/ParseInputFile.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ParseInputFile.hpp"
22

3+
// Checks that input json file contains required members
34
void validateParameters(rapidjson::Document &jsonDoc) {
45
std::string missingParameters = "";
56

@@ -16,7 +17,8 @@ void validateParameters(rapidjson::Document &jsonDoc) {
1617
throw JSONException("Missing these parameters: " + missingParameters);
1718
} else {
1819
auto &config = jsonDoc["config"];
19-
auto configParams = {"width", "height", "scale", "delay"};
20+
auto configParams = {"width", "height", "scale",
21+
"delay", "wallColor", "bgColor"};
2022
for (auto p : configParams) {
2123
if (!config.HasMember(p))
2224
missingParameters += std::string(p) + " ";
@@ -51,7 +53,9 @@ void validateParameters(rapidjson::Document &jsonDoc) {
5153

5254
void parseInputFile(std::string filename, std::vector<Actor> &actors,
5355
Room &room, std::vector<Path> &paths, int &WIDTH,
54-
int &HEIGHT, int &SCALE, int &DELAY) {
56+
int &HEIGHT, int &SCALE, int &DELAY,
57+
std::array<int, 3> &BGCOLOR,
58+
std::array<int, 3> &WALLCOLOR) {
5559
std::ifstream jsonFile(filename);
5660
if (!jsonFile.is_open()) {
5761
throw JSONException("Error opening file " + filename);
@@ -71,6 +75,11 @@ void parseInputFile(std::string filename, std::vector<Actor> &actors,
7175
HEIGHT = config["height"].GetInt();
7276
SCALE = config["scale"].GetInt();
7377
DELAY = config["delay"].GetInt();
78+
WALLCOLOR = {config["wallColor"][0].GetInt(),
79+
config["wallColor"][1].GetInt(),
80+
config["wallColor"][2].GetInt()};
81+
BGCOLOR = {config["bgColor"][0].GetInt(), config["bgColor"][1].GetInt(),
82+
config["bgColor"][2].GetInt()};
7483

7584
// Room
7685
auto jsonWalls = jsonDoc["room"]["walls"].GetArray();
@@ -86,18 +95,18 @@ void parseInputFile(std::string filename, std::vector<Actor> &actors,
8695
for (auto &p : jsonPaths) {
8796
int id = p["id"].GetInt();
8897
auto jsonCheckpoints = p["checkpoints"].GetArray();
89-
std::array<std::array<vecType, 4>, PATHALLOCATIONSIZE> checkpoints;
98+
std::array<std::array<vecType, 2>, PATHALLOCATIONSIZE> checkpoints;
9099
if (jsonCheckpoints.Size() > PATHALLOCATIONSIZE) {
91100
throw JSONException(
92101
"Path Size exceeds amount allocated in memory\nEither reduce "
93102
"path size or increase PATHALLOCATIONSIZE in 'Path.hpp'");
94103
}
95104
for (int i = 0; i < jsonCheckpoints.Size(); i++) {
96-
std::array<vecType, 4> region;
97-
for (int j = 0; j < 4; j++) {
98-
region[j] = vecType({jsonCheckpoints[i][j][0].GetFloat(),
99-
jsonCheckpoints[i][j][1].GetFloat()});
100-
}
105+
std::array<vecType, 2> region;
106+
region[0] = vecType({jsonCheckpoints[i][0][0].GetFloat(),
107+
jsonCheckpoints[i][0][1].GetFloat()});
108+
region[1] = vecType({jsonCheckpoints[i][1][0].GetFloat(),
109+
jsonCheckpoints[i][1][1].GetFloat()});
101110
checkpoints[i] = region;
102111
}
103112
int pathSize = jsonCheckpoints.Size();

external/ParseInputFile.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void validateParameters(rapidjson::Document &jsonDoc);
2424

2525
void parseInputFile(std::string filename, std::vector<Actor> &actors,
2626
Room &room, std::vector<Path> &paths, int &WIDTH,
27-
int &HEIGHT, int &SCALE, int &DELAY);
27+
int &HEIGHT, int &SCALE, int &DELAY,
28+
std::array<int, 3> &BGCOLOR, std::array<int, 3> &WALLCOLOR);
2829

2930
#endif

0 commit comments

Comments
 (0)