-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestgame.cpp
More file actions
135 lines (111 loc) · 3.44 KB
/
testgame.cpp
File metadata and controls
135 lines (111 loc) · 3.44 KB
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
#include <string>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cuda_runtime.h>
#include "common.h"
#include "board.h"
#include "exampleplayer.h"
#include "player.h"
#include "gpuplayer.h"
using namespace std;
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
exit(code);
}
}
// timing setup code
cudaEvent_t start;
cudaEvent_t stop;
#define START_TIMER() { \
gpuErrchk(cudaEventCreate(&start)); \
gpuErrchk(cudaEventCreate(&stop)); \
gpuErrchk(cudaEventRecord(start)); \
}
#define STOP_RECORD_TIMER(name) { \
gpuErrchk(cudaEventRecord(stop)); \
gpuErrchk(cudaEventSynchronize(stop)); \
gpuErrchk(cudaEventElapsedTime(&name, start, stop)); \
gpuErrchk(cudaEventDestroy(start)); \
gpuErrchk(cudaEventDestroy(stop)); \
}
// Initialize timers for benchmarking
float cpu_ms = -1;
float gpu_ms = -1;
int main() {
Board *board = new Board();
Player *player1 = new Player(BLACK);
ExamplePlayer *player2 = new ExamplePlayer(WHITE);
Side turn = BLACK;
Move *m = NULL;
cout << "Starting CPU game..." << endl;
START_TIMER();
while (!board->isDone()) {
// get the current player's move
if (turn == BLACK) {
m = player1->doMove(m);
}
else {
m = player2->doMove(m);
}
if (!board->checkMove(m, turn)) {
cout << "Illegal move made: " << turn << " address: " << m << endl;
}
// make move once it is determiend to be legal
board->doMove(m, turn);
// switch players
if (turn == BLACK) {
turn = WHITE;
}
else {
turn = BLACK;
}
}
STOP_RECORD_TIMER(cpu_ms);
cout << "CPU Game completed." << endl;
cout << "Black score: " << board->countBlack() << endl;
cout << "White score: " << board->countWhite() << endl;
// Run game on GPU here
ExamplePlayer *player3 = new ExamplePlayer(WHITE);
GPUPlayer *player4 = new GPUPlayer(BLACK);
board = new Board();
turn = BLACK;
m = NULL;
cout << endl << "Starting GPU game..." << endl;
START_TIMER();
while (!board->isDone()) {
// get the current player's move
if (turn == WHITE) {
m = player3->doMove(m);
}
else {
m = player4->doMove(m);
}
if (!board->checkMove(m, turn)) {
cout << "Illegal move made: " << turn << " address: " << m << endl;
}
// make move once it is determiend to be legal
board->doMove(m, turn);
// switch players
if (turn == BLACK) {
turn = WHITE;
}
else {
turn = BLACK;
}
}
STOP_RECORD_TIMER(gpu_ms);
cout << "GPU Game completed." << endl;
cout << "Black score: " << board->countBlack() << endl;
cout << "White score: " << board->countWhite() << endl;
cout << endl;
cout << "CPU time: " << cpu_ms << " milliseconds" << endl;
cout << "GPU time: " << gpu_ms << " milliseconds" << endl;
cout << "Speedup factor: " << cpu_ms / gpu_ms << endl << endl;
return 0;
}