Skip to content

Commit d80856a

Browse files
committed
Kernel experimentation and DiffEqu creation
1 parent aac8ec8 commit d80856a

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

include/Actor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Actor.hpp"
22
#include <iostream>
33
#include <array>
4+
#include <sycl/sycl.hpp>
45

56
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius) {
67
pos = pPos;
@@ -15,7 +16,7 @@ std::array<float, 2> Actor::getPos() {
1516
return pos;
1617
}
1718

18-
std::array<float, 2> Actor::getVelocity() {
19+
SYCL_EXTERNAL std::array<float, 2> Actor::getVelocity() {
1920
return velocity;
2021
}
2122

include/Actor.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <iostream>
55
#include <array>
6+
#include <sycl/sycl.hpp>
67

78
class Actor {
89
private:
@@ -15,14 +16,14 @@ class Actor {
1516
public:
1617
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius);
1718

18-
std::array<float, 2> getPos();
19-
std::array<float, 2> getVelocity();
19+
SYCL_EXTERNAL std::array<float, 2> getPos();
20+
SYCL_EXTERNAL std::array<float, 2> getVelocity();
2021
std::array<float, 2> getDesiredVelocity();
2122
std::array<float, 2> getDestination();
2223
float getMass();
2324
float getRadius();
2425

25-
void setPos(std::array<float, 2> newPos);
26+
SYCL_EXTERNAL void setPos(std::array<float, 2> newPos);
2627
void setVelocity(std::array<float, 2> newVelocity);
2728
void setDesiredVelocity(std::array<float, 2> newDesiredVelocity);
2829
void setDestination(std::array<float, 2> newDestination);

include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_library(include Actor.cpp Room.cpp MathHelper.cpp)
1+
add_library(include Actor.cpp Room.cpp MathHelper.cpp DifferentialEq.cpp)

include/DifferentialEq.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <array>
4+
#include "DifferentialEq.hpp"
5+
#include "Actor.hpp"
6+
#include "Room.hpp"
7+
#include "MathHelper.hpp"
8+
9+
void DifferentialEqu(Actor actor);

include/DifferentialEq.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef DifferentialEqu_hpp
2+
#define DifferentialEqu_hpp
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <array>
7+
#include "Actor.hpp"
8+
#include "Room.hpp"
9+
#include "MathHelper.hpp"
10+
11+
const float Ai = 2000;
12+
const float Bi = 0.08;
13+
const float k1 = 125000;
14+
const float k2 = 240000;
15+
const float ti = 0.5;
16+
17+
void DifferentialEqu(Actor actor);
18+
19+
#endif

src/main.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#include <CL/sycl.hpp>
1+
#include <sycl/sycl.hpp>
22
#include <SDL2/SDL.h>
33
#include <iostream>
44

55
#include "Actor.hpp"
66
#include "Room.hpp"
77
#include "MathHelper.hpp"
8+
#include "DifferentialEq.hpp"
89

910
const int WIDTH = 8; // metres
1011
const int HEIGHT = 6; // metres
@@ -34,10 +35,19 @@ void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color c
3435
}
3536

3637
void update(std::vector<Actor> &actors) {
37-
for (auto &actor : actors) {
38-
actor.setPos({actor.getPos()[0] + actor.getVelocity()[0],
39-
actor.getPos()[1] + actor.getVelocity()[1]});
40-
}
38+
sycl::queue myQueue{sycl::gpu_selector()};
39+
40+
auto actorBuf = sycl::buffer<Actor>(actors.data(), actors.size());
41+
42+
myQueue.submit([&](sycl::handler& cgh) {
43+
auto actorAcc = actorBuf.get_access<sycl::access::mode::read_write>(cgh);
44+
45+
cgh.parallel_for(sycl::range<1>{actors.size()}, [=](sycl::id<1> index) {
46+
Actor current = actorAcc[index];
47+
actorAcc[index].setPos({current.getPos()[0] + current.getVelocity()[0],
48+
current.getPos()[1] + current.getVelocity()[1]});
49+
});
50+
}).wait();
4151
}
4252

4353
void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
@@ -55,9 +65,6 @@ void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
5565
SDL_RenderDrawLine(render, wall[0] * SCALE, wall[1] * SCALE, wall[2] * SCALE, wall[3] * SCALE);
5666
}
5767

58-
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
59-
SDL_RenderDrawPoint(render, 100, 200);
60-
6168
SDL_RenderPresent(render);
6269
}
6370

@@ -77,7 +84,7 @@ int main() {
7784
init(win, render, actors);
7885

7986
// Make actor move towards destination
80-
actors[0].setVelocity(velToPoint(0.01, actors[0].getPos(), actors[0].getDestination()));
87+
actors[0].setVelocity(velToPoint(0.008, actors[0].getPos(), actors[0].getDestination()));
8188

8289
draw(render, actors, room);
8390

0 commit comments

Comments
 (0)