-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelDataEntry_random.cpp
More file actions
201 lines (178 loc) · 4.4 KB
/
Copy pathparallelDataEntry_random.cpp
File metadata and controls
201 lines (178 loc) · 4.4 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <string>
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <assert.h>
#include <omp.h>
#include <time.h>
#include "timer.c"
// Use on-disk DB option from sqlite3 for parallel data-entry
// Standard libraries to communicate with sqlite3 interface are used
// comparison performed
//
// Pointer to the sqlite3 databse handle
//
sqlite3 *db;
// Global Vars
//
int MAX_THREADS = 0;
int NUM_OF_QUERIES = 0;
// Run experiments on 6 tables
//
int NUM_OF_TABLES = 6;
FILE *fp;
int count = 1;
long double e1;
long double e2;
// Call-back function from sql query
//
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
// Initalize NUM_OF_TABLES tables in the databse
//
void initTables()
{
for(int i = 0; i < NUM_OF_TABLES; i++)
{
char sql[60];
char *zErrMsg = 0;
sprintf(sql, "CREATE TABLE T%d(ID INT, ATTR1 INT, ATTR2 INT);", i);
// Execute query
//
int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
printf("Table [%d] created successfully\n", i);
}
}
}
// Run all queries on one Table
//
void runQueriesTables1()
{
int i;
for(int j = 0; j< NUM_OF_QUERIES*NUM_OF_TABLES; j++)
{
char sql[60];
char *zErrMsg = 0;
// Create INSERT Query
//
char part1[50];
sprintf(sql, "INSERT INTO T%d VALUES(%d, %d, %d)", 0, rand()%100, rand()%200, rand()%250);
int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(rc != SQLITE_OK)
{
fprintf(stdout, "Insert Error at : %s\n", zErrMsg);
}
}
}
// Run Queries divided equally among the numTables
//
void runQueriesTables(int numTables)
{
int i;
#pragma omp parallel for private(i)
for(i = 0; i < numTables; i++)
{
for(int j = 0; j < (NUM_OF_QUERIES+MAX_THREADS)*NUM_OF_TABLES/(numTables*(i+1)); j++)
{
char sql[60];
char *zErrMsg = 0;
sprintf(sql, "INSERT INTO T%d VALUES(%d, %d, %d)", rand()%6, rand()%100, rand()%200, rand()%250);
int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(rc != SQLITE_OK)
{
fprintf(stderr, "Insert error : %s\n", zErrMsg);
}
}
}
}
void runDriver()
{
printf("\nRUN CONFIGURATION:\n=== Number of INSERT queries/thread = [%d]\nTotal INSERT queries = [%d];\n=== MAX_THREADS [%d]\n\n", NUM_OF_QUERIES
, NUM_OF_TABLES * NUM_OF_QUERIES,MAX_THREADS);
initTables();
// initializing timer
//
stopwatch_init();
struct stopwatch_t *timer = stopwatch_create();
assert(timer);
// Run queries on single table
//
printf("\n===> STARTING: Sequential Module to Insert %d queries in NUM_OF_TABLES = 1 \n", NUM_OF_QUERIES*NUM_OF_TABLES);
stopwatch_start(timer);
runQueriesTables1();
long double timeTables1 = stopwatch_stop(timer);
printf("\nTIME: [%Lg] secs\n", timeTables1);
fprintf(fp, "%d %Lg\n",count, timeTables1);
// Queries divided among multiple tables
//
for(int i = 1; i<=NUM_OF_TABLES; i++)
{
printf("\n===> STARTING: Parallel Module to Insert %d queries divided among NUM_OF_TABLES = %d \n", NUM_OF_QUERIES*NUM_OF_TABLES, i);
stopwatch_start(timer);
runQueriesTables(i);
long double timeTables = stopwatch_stop(timer);
printf("\nTIME: [%Lg] secs\n", timeTables);
if(count == 1) e1= timeTables;
if(count == 6) e2 = timeTables;
fprintf(fp, "%d %Lg\n",count, timeTables);
count += 1;
}
}
int main(int argc, char* argv[])
{
// Opening handle to sqlite3 test database
//
if (argc < 2 || argc > 4)
{
printf("Usage: ./parallelDriver [# of Queries/Thread] [Optional: Max number of threads] [Optional: isRandom]");
return 0;
}
/* initialize random seed: */
srand (time(NULL));
fp = fopen("plot.txt", "w");
system("lscpu");
printf("\n");
// Initializing global params
//
NUM_OF_QUERIES = atoi(argv[1]);
if(argc == 3) MAX_THREADS = atoi(argv[2]);
else
{
if(omp_get_max_threads() < 16) MAX_THREADS = omp_get_max_threads();
else MAX_THREADS = 16;
}
// Create a in-memory databse
//
int result = sqlite3_open("test.db", &db);
if(result != SQLITE_OK)
{
printf("\n>> Error loading handle to Database");
}
else
{
printf("\n===== Optimized Parallel Data Entry Driver ======\n ");
}
runDriver();
sqlite3_close(db);
//Remove test.db to create a fresh instance the next time
//
system("rm test.db");
printf("\n\n");
fclose(fp);
return 0;
}