Skip to content

Commit 58c9b81

Browse files
committed
Major debugging
Alot more stuff works now, just running into some small timing errors. MPI is untested but should hopefully work.
1 parent 82c460f commit 58c9b81

File tree

16 files changed

+570
-556
lines changed

16 files changed

+570
-556
lines changed

QPEMPI

-712 Bytes
Binary file not shown.

QPEMPI.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,58 @@
66
#include <string.h>
77
#include <strings.h> // For strcasecmp
88
#include <time.h>
9+
#include <ctype.h>
910
#include <mpi.h>
10-
#include "../include/executeEngine-serial.h"
11-
#include "../include/connectEngine.h"
11+
#include "../include/executeEngine-mpi.h"
1212
#include "../include/printHelper.h"
13+
#include "../include/sql.h"
14+
15+
// Constants
16+
#define DATA_FILE "data-generation/commands_50k.csv"
17+
#define TABLE_NAME "commands"
18+
#define MAX_TOKENS 100
19+
#define ROW_LIMIT 20
1320

1421
// Optimal indexes constant
15-
const char* optimalIndexes[] = {
22+
static const char* optimalIndexes[] = {
1623
"command_id",
1724
"user_id",
1825
"risk_level",
1926
"exit_code",
2027
"sudo_used"
2128
};
22-
const FieldType optimalIndexTypes[] = {
29+
static const FieldType optimalIndexTypes[] = {
2330
FIELD_UINT64,
2431
FIELD_INT,
2532
FIELD_INT,
2633
FIELD_INT,
2734
FIELD_BOOL
2835
};
29-
const int numOptimalIndexes = 5;
36+
static const int numOptimalIndexes = 5;
3037

3138
// ANSI color codes for pretty printing
3239
#define CYAN "\x1b[36m"
3340
#define YELLOW "\x1b[33m"
3441
#define BOLD "\x1b[1m"
3542
#define RESET "\x1b[0m"
3643

44+
// Helper to trim whitespace from a string
45+
static inline char* trim(char *s) {
46+
while (*s && isspace((unsigned char)*s)) s++;
47+
// Also trim trailing
48+
char *end = s + strlen(s) - 1;
49+
while (end > s && isspace((unsigned char)*end)) end--;
50+
*(end + 1) = 0;
51+
return s;
52+
}
53+
3754
// Helper to safely copy strings with truncation
3855
static inline void safe_copy(char *dst, size_t n, const char *src) {
3956
snprintf(dst, n, "%.*s", (int)n - 1, src);
4057
}
4158

4259
// Helper to map Parser OperatorType to string
43-
const char* get_operator_string(OperatorType op) {
60+
static const char* get_operator_string(OperatorType op) {
4461
switch (op) {
4562
case OP_EQ: return "=";
4663
case OP_NEQ: return "!=";
@@ -53,7 +70,7 @@ const char* get_operator_string(OperatorType op) {
5370
}
5471

5572
// Helper to map Parser LogicOperator to string
56-
const char* get_logic_op_string(LogicOperator op) {
73+
static const char* get_logic_op_string(LogicOperator op) {
5774
switch (op) {
5875
case LOGIC_AND: return "AND";
5976
case LOGIC_OR: return "OR";
@@ -62,7 +79,7 @@ const char* get_logic_op_string(LogicOperator op) {
6279
}
6380

6481
// Helper to convert ParsedSQL conditions to engine's whereClauseS linked list
65-
struct whereClauseS* convert_conditions(ParsedSQL *parsed) {
82+
static struct whereClauseS* convert_conditions(ParsedSQL *parsed) {
6683
if (parsed->num_conditions == 0) return NULL;
6784

6885
struct whereClauseS *head = NULL;
@@ -111,7 +128,7 @@ struct whereClauseS* convert_conditions(ParsedSQL *parsed) {
111128
}
112129

113130
// Helper to free the manually constructed where clause
114-
void free_where_clause_list(struct whereClauseS *head) {
131+
static void free_where_clause_list(struct whereClauseS *head) {
115132
while (head) {
116133
struct whereClauseS *temp = head;
117134
head = head->next;
@@ -133,7 +150,7 @@ int main(int argc, char *argv[]) {
133150
double totalStart = MPI_Wtime();
134151

135152
// Instantiate an engine object to handle the execution of the query
136-
struct engineS *engine = initializeEngineSerial(
153+
struct engineS *engine = initializeEngineMPI(
137154
numOptimalIndexes, // Number of indexes
138155
optimalIndexes, // Indexes to build B+ trees for
139156
(const int *)optimalIndexTypes, // Index types
@@ -149,7 +166,7 @@ int main(int argc, char *argv[]) {
149166
FILE *fp = fopen(query_file, "r");
150167
if (!fp) {
151168
if (rank == 0) perror("Failed to open query file");
152-
destroyEngineSerial(engine);
169+
destroyEngineMPI(engine);
153170
MPI_Finalize();
154171
return EXIT_FAILURE;
155172
}
@@ -162,7 +179,7 @@ int main(int argc, char *argv[]) {
162179
if (!buffer) {
163180
if (rank == 0) perror("Failed to allocate memory for query file");
164181
fclose(fp);
165-
destroyEngineSerial(engine);
182+
destroyEngineMPI(engine);
166183
MPI_Finalize();
167184
return EXIT_FAILURE;
168185
}
@@ -171,7 +188,7 @@ int main(int argc, char *argv[]) {
171188
if (rank == 0) perror("Failed to read query file");
172189
free(buffer);
173190
fclose(fp);
174-
destroyEngineSerial(engine);
191+
destroyEngineMPI(engine);
175192
MPI_Finalize();
176193
return EXIT_FAILURE;
177194
}
@@ -238,18 +255,18 @@ int main(int argc, char *argv[]) {
238255
safe_copy(r.host_name, sizeof(r.host_name), parsed.insert_values[10]);
239256
r.risk_level = atoi(parsed.insert_values[11]);
240257

241-
success = executeQueryInsertSerial(engine, parsed.table, &r);
258+
success = executeQueryInsertMPI(engine, parsed.table, &r);
242259
}
243260
}
244261
else if (parsed.command == CMD_DELETE) {
245262
struct whereClauseS *whereClause = convert_conditions(&parsed);
246-
result = executeQueryDeleteSerial(engine, parsed.table, whereClause);
263+
result = executeQueryDeleteMPI(engine, parsed.table, whereClause);
247264
if (result) rowsAffected = result->numRecords;
248265
free_where_clause_list(whereClause);
249266
}
250267
else if (parsed.command == CMD_SELECT) {
251268
struct whereClauseS *whereClause = convert_conditions(&parsed);
252-
result = executeQuerySelectSerial(engine, selectItems, numSelectItems, parsed.table, whereClause);
269+
result = executeQuerySelectMPI(engine, selectItems, numSelectItems, parsed.table, whereClause);
253270
free_where_clause_list(whereClause);
254271
}
255272

@@ -308,9 +325,9 @@ int main(int argc, char *argv[]) {
308325
printf("Rank %d: Freeing buffer...\n", rank);
309326
free(buffer);
310327
printf("Rank %d: Destroying engine...\n", rank);
311-
destroyEngineSerial(engine);
328+
destroyEngineMPI(engine);
312329
printf("Rank %d: Finalizing MPI...\n", rank);
313330

314331
MPI_Finalize();
315332
return EXIT_SUCCESS;
316-
}
333+
}

QPEOMP

-8.32 KB
Binary file not shown.

QPEOMP.c

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,143 @@
66
#include <string.h>
77
#include <strings.h>
88
#include <time.h>
9+
#include <ctype.h>
910
#include <omp.h>
10-
#include "../include/executeEngine-serial.h"
11-
#include "../include/connectEngine.h"
12-
#include "../include/printHelper.h"
11+
#include "../include/executeEngine-omp.h"
12+
13+
// Fix for include conflict: printHelper.h includes executeEngine-serial.h which conflicts with executeEngine-omp.h
14+
// We will manually declare printTable and NOT include printHelper.h
15+
// #include "../include/printHelper.h"
16+
void printTable(FILE *output, struct resultSetS *result, int limit);
17+
18+
#include "../include/sql.h"
19+
20+
// Constants
21+
#define DATA_FILE "data-generation/commands_50k.csv"
22+
#define TABLE_NAME "commands"
23+
#define MAX_TOKENS 100
24+
#define ROW_LIMIT 20
25+
26+
// Optimal indexes constant
27+
const char* optimalIndexes[] = {
28+
"command_id",
29+
"user_id",
30+
"risk_level",
31+
"exit_code",
32+
"sudo_used"
33+
};
34+
const FieldType optimalIndexTypes[] = {
35+
FIELD_UINT64,
36+
FIELD_INT,
37+
FIELD_INT,
38+
FIELD_INT,
39+
FIELD_BOOL
40+
};
41+
const int numOptimalIndexes = 5;
1342

1443
// ANSI color codes for pretty printing
1544
#define CYAN "\x1b[36m"
1645
#define YELLOW "\x1b[33m"
1746
#define BOLD "\x1b[1m"
1847
#define RESET "\x1b[0m"
1948

49+
// Helper to trim whitespace from a string
50+
static inline char* trim(char *s) {
51+
while (*s && isspace((unsigned char)*s)) s++;
52+
// Also trim trailing
53+
char *end = s + strlen(s) - 1;
54+
while (end > s && isspace((unsigned char)*end)) end--;
55+
*(end + 1) = 0;
56+
return s;
57+
}
58+
2059
// Helper to safely copy strings with truncation (local version)
2160
static inline void safe_copy(char *dst, size_t n, const char *src) {
2261
snprintf(dst, n, "%.*s", (int)n - 1, src);
2362
}
2463

64+
// Helper to map Parser OperatorType to string
65+
const char* get_operator_string(OperatorType op) {
66+
switch (op) {
67+
case OP_EQ: return "=";
68+
case OP_NEQ: return "!=";
69+
case OP_GT: return ">";
70+
case OP_LT: return "<";
71+
case OP_GTE: return ">=";
72+
case OP_LTE: return "<=";
73+
default: return "=";
74+
}
75+
}
76+
77+
// Helper to map Parser LogicOperator to string
78+
const char* get_logic_op_string(LogicOperator op) {
79+
switch (op) {
80+
case LOGIC_AND: return "AND";
81+
case LOGIC_OR: return "OR";
82+
default: return "AND";
83+
}
84+
}
85+
86+
// Helper to convert ParsedSQL conditions to engine's whereClauseS linked list
87+
struct whereClauseS* convert_conditions(ParsedSQL *parsed) {
88+
if (parsed->num_conditions == 0) return NULL;
89+
90+
struct whereClauseS *head = NULL;
91+
struct whereClauseS *current = NULL;
92+
93+
for (int i = 0; i < parsed->num_conditions; i++) {
94+
struct whereClauseS *node = malloc(sizeof(struct whereClauseS));
95+
96+
if (parsed->conditions[i].is_nested && parsed->conditions[i].nested_sql) {
97+
node->attribute = NULL;
98+
node->operator = NULL;
99+
node->value = NULL;
100+
node->value_type = 0;
101+
node->sub = convert_conditions(parsed->conditions[i].nested_sql);
102+
} else {
103+
node->attribute = parsed->conditions[i].column;
104+
node->operator = get_operator_string(parsed->conditions[i].op);
105+
node->value = parsed->conditions[i].value;
106+
107+
// Simple type inference for the test
108+
if (parsed->conditions[i].is_numeric) {
109+
node->value_type = 0; // Integer/Number
110+
} else {
111+
node->value_type = 1; // String
112+
}
113+
node->sub = NULL;
114+
}
115+
116+
node->next = NULL;
117+
118+
if (i < parsed->num_conditions - 1) {
119+
node->logical_op = get_logic_op_string(parsed->logic_ops[i]);
120+
} else {
121+
node->logical_op = NULL;
122+
}
123+
124+
if (head == NULL) {
125+
head = node;
126+
current = node;
127+
} else {
128+
current->next = node;
129+
current = node;
130+
}
131+
}
132+
return head;
133+
}
134+
135+
// Helper to free the manually constructed where clause
136+
void free_where_clause_list(struct whereClauseS *head) {
137+
while (head) {
138+
struct whereClauseS *temp = head;
139+
head = head->next;
140+
free(temp);
141+
}
142+
}
143+
25144
int main(int argc, char *argv[]) {
145+
printf("Starting main...\n"); fflush(stdout);
26146

27147
// Pull out number of defined threads from CLI args (default to 8)
28148
int num_threads = 8;
@@ -34,14 +154,19 @@ int main(int argc, char *argv[]) {
34154
// Start a timer for total runtime statistics
35155
double totalStart = omp_get_wtime();
36156

157+
printf("Initializing Engine...\n"); fflush(stdout);
37158
// Instantiate an engine object to handle the execution of the query
38-
struct engineS *engine = initializeEngineSerial(
159+
struct engineS *engine = initializeEngineOMP(
39160
numOptimalIndexes, // Number of indexes
40161
optimalIndexes, // Indexes to build B+ trees for
41162
(const int *)optimalIndexTypes, // Index types
42163
DATA_FILE,
43164
TABLE_NAME
44165
);
166+
printf("Engine Initialized.\n"); fflush(stdout);
167+
168+
// Wait for all threads to sync before proceeding
169+
#pragma omp barrier
45170

46171
// End timer for engine initialization
47172
double initTimeTaken = ((double)omp_get_wtime() - totalStart) / CLOCKS_PER_SEC;
@@ -51,7 +176,7 @@ int main(int argc, char *argv[]) {
51176
FILE *fp = fopen(query_file, "r");
52177
if (!fp) {
53178
perror("Failed to open query file");
54-
destroyEngineSerial(engine);
179+
destroyEngineOMP(engine);
55180
return EXIT_FAILURE;
56181
}
57182

@@ -63,15 +188,15 @@ int main(int argc, char *argv[]) {
63188
if (!buffer) {
64189
perror("Failed to allocate memory for query file");
65190
fclose(fp);
66-
destroyEngineSerial(engine);
191+
destroyEngineOMP(engine);
67192
return EXIT_FAILURE;
68193
}
69194
size_t read_size = fread(buffer, 1, fsize, fp);
70195
if (read_size != (size_t)fsize) {
71196
perror("Failed to read query file");
72197
free(buffer);
73198
fclose(fp);
74-
destroyEngineSerial(engine);
199+
destroyEngineOMP(engine);
75200
return EXIT_FAILURE;
76201
}
77202
buffer[fsize] = 0;
@@ -139,18 +264,18 @@ int main(int argc, char *argv[]) {
139264
safe_copy(r.host_name, sizeof(r.host_name), parsed.insert_values[10]);
140265
r.risk_level = atoi(parsed.insert_values[11]);
141266

142-
success = executeQueryInsertSerial(engine, parsed.table, &r);
267+
success = executeQueryInsertOMP(engine, parsed.table, &r);
143268
}
144269
}
145270
else if (parsed.command == CMD_DELETE) {
146271
struct whereClauseS *whereClause = convert_conditions(&parsed);
147-
result = executeQueryDeleteSerial(engine, parsed.table, whereClause);
272+
result = executeQueryDeleteOMP(engine, parsed.table, whereClause);
148273
if (result) rowsAffected = result->numRecords;
149274
free_where_clause_list(whereClause);
150275
}
151276
else if (parsed.command == CMD_SELECT) {
152277
struct whereClauseS *whereClause = convert_conditions(&parsed);
153-
result = executeQuerySelectSerial(engine, selectItems, numSelectItems, parsed.table, whereClause);
278+
result = executeQuerySelectOMP(engine, selectItems, numSelectItems, parsed.table, whereClause);
154279
free_where_clause_list(whereClause);
155280
}
156281

@@ -197,7 +322,7 @@ int main(int argc, char *argv[]) {
197322
}
198323

199324
free(buffer);
200-
destroyEngineSerial(engine);
325+
destroyEngineOMP(engine);
201326

202327
// Print total runtime statistics in pretty colors
203328
double totalTimeTaken = ((double)omp_get_wtime() - totalStart) / CLOCKS_PER_SEC;
@@ -209,4 +334,4 @@ int main(int argc, char *argv[]) {
209334
printf(CYAN "=================================" RESET "\n");
210335

211336
return EXIT_SUCCESS;
212-
}
337+
}

0 commit comments

Comments
 (0)