aboutsummaryrefslogtreecommitdiff
path: root/opencl-sqlite.h
blob: e7c2f1db30b6f6d88247de08f0ce9c7a2817d79d (plain)
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
/* Tom Gall (tom.gall@linaro.org / tom_gall@mac.com)
 * Copyright Linaro 2014
 * Released under the terms and conditions of the license documented in the LICENSE file 
 **/

#ifndef OPENCL_SQLITE_H
#define OPENCL_SQLITE_H

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <linux/types.h>

#include <CL/cl.h>

#define OPENCL_SUCCESS			0
#define OPENCL_ERR_MEM			1
#define OPENCL_ERR_STMT			2
#define OPENCL_ERR_DEVICE		3
#define OPENCL_ERR_KERNEL		4
#define OPENCL_ERR_TYPE			5
#define OPENCL_ERR_TEXTURE		6

#define OPENCL_AGG_COUNT	0
#define OPENCL_AGG_MIN		1
#define OPENCL_AGG_MAX		2
#define OPENCL_AGG_SUM		3
#define OPENCL_AGG_AVG		4

#define OPENCL_MAX_COLUMNS		16 

/* more or less cover the base sqlite types */
#define OPENCL_INT			0
#define OPENCL_INT64		1
#define OPENCL_FLOAT		2
#define OPENCL_DOUBLE		3
#define OPENCL_TEXT			4
#define OPENCL_BLOB			5
#define OPENCL_NULL			6

/*
 * useful data structures
 **********************************************************/

struct opencl_data_shard {
	int type;
	int rows;
	cl_mem d;
};
typedef struct opencl_data_shard opencl_data_shard;

struct opencl_data {
	int rows;
	int columns;
	int stride;
	int types[OPENCL_MAX_COLUMNS];
	int offsets[OPENCL_MAX_COLUMNS];
	void *d;
};
typedef struct opencl_data opencl_data;


struct opencl_data_gpu {
	cl_mem d;
};
typedef struct opencl_data_gpu opencl_data_gpu;


struct opencl_results {
	int rows;
	int columns;
	int stride;
	int types[OPENCL_MAX_COLUMNS];
	int offsets[OPENCL_MAX_COLUMNS];
	void *r;
};
typedef struct opencl_results opencl_results;

struct opencl_gpu_results {
	int rows;
	int columns;
	int stride;
	int types[OPENCL_MAX_COLUMNS];
	int offsets[OPENCL_MAX_COLUMNS];
	cl_mem r;
	cl_mem roffsetResults_cl;
};

typedef struct opencl_gpu_results opencl_gpu_results;

struct opencl_mem {
	union {
		int i;
		long long li;
		unsigned long long int ulli;
		float f;
		double d;
		struct {
			int hi;
			int lo;
		} segment;
	} mem;
	int type;
};
typedef struct opencl_mem opencl_mem;


struct opencl_sqlite_context {
	sqlite3 *db;
	cl_context context;
	cl_command_queue queue;
	cl_kernel kernel;
	cl_program program;
	opencl_data *data_cpu;				// cpu accessable input
	opencl_data_gpu *data_gpu;			// gpu data handle(s)
	opencl_results *results_cpu;		// cpu accessable results 
	opencl_gpu_results *results_gpu;	// pointer to results block on gpu
	size_t data_size;				// size of data block
	size_t results_size;			// size of results block
};
typedef struct opencl_sqlite_context opencl_sqlite_context;



/*************************************************************************** 
 * OpenCL sqlite API 
 */

int opencl_init(opencl_sqlite_context *s, sqlite3 *db, size_t data_size, size_t results_size,
	char *filename);

int opencl_cleanup(opencl_sqlite_context *s);

int opencl_prepare_data(opencl_sqlite_context *s, const char* sql_stmt, int * columnSettings);
int opencl_set_data_map(opencl_sqlite_context *s, const char* sql_stmt, int * columnSettings);
int opencl_shard_data(opencl_sqlite_context *s, opencl_data_shard *shard, int column);


int opencl_map_input_data(opencl_sqlite_context *s);
int opencl_map_output_data(opencl_sqlite_context *s, int work_units_per_kernel);
int opencl_map_output_summary_data(opencl_sqlite_context *s, int work_units_per_kernel, int columns);
int opencl_map_output_xsummary_data(opencl_sqlite_context *s, int work_units_per_kernel, int columns);

int opencl_map_output_vector_data(opencl_sqlite_context *s);

int opencl_transfer_results(opencl_sqlite_context *s, int work_units_per_kernel, int result_stride);

float opencl_transfer_fsummary_results(opencl_sqlite_context *s, int work_units_per_kernel);
int opencl_transfer_isummary_results(opencl_sqlite_context *s, int work_units_per_kernel, int columns);
int opencl_transfer_ixsummary_results(opencl_sqlite_context *s, int work_units_per_kernel, int columns);
int opencl_transfer_vector_results(opencl_sqlite_context *s);

void *opencl_result_vector_to_result_buffer(opencl_sqlite_context *s);

int opencl_select(opencl_sqlite_context *s, int work_units_per_kernel);
int opencl_rowcolumn_select(opencl_sqlite_context *s, int work_units_per_kernel);
int opencl_vector_select(opencl_sqlite_context *s, opencl_data_shard *col1_shard, opencl_data_shard *col2_shard,
         int work_units_per_kernel);

int opencl_collect_timestamp(struct timespec *timestamp);

void opencl_print_time_interval(struct timespec *start, struct timespec *end);


#endif /* OPENCL_SQLITE_H */