-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivectorComputation.c
636 lines (587 loc) · 22 KB
/
ivectorComputation.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "ivectorComputation.h"
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <EGL/egl.h>
#include <GLES3/gl31.h>
static const char *gComputeShaderIntRes =
"#version 320 es\n"
"layout(local_size_x = 150, local_size_y = 2) in;\n"
"layout(std430, binding = 0) readonly buffer MatA {\n"
" double data[];\n"
"} matA;\n"
"layout(std430, binding = 1) readonly buffer MatB {\n"
" double data[];\n"
"} matB;\n"
"layout(std430, binding = 2) writeonly buffer MatC {\n"
" double data[];\n"
"} matC;\n"
"shared uint M = 150u;\n"
"shared uint N = 38722u;\n"
"shared uint K = 150u;\n"
"void main(){\n"
" uint globalRow = gl_GlobalInvocationID.x;\n"
" uint globalCol = gl_GlobalInvocationID.y;\n"
" double acc = double(0.0);\n"
" for (uint k = 0u; k < K; k++)\n"
" acc += matA.data[k*M + globalRow] * matB.data[globalCol * K + k];\n"
"matC.data[globalRow + globalCol * M] = acc;\n"
"}\n";
static const char *gComputeShaderIVector =
"#version 320 es\n"
"layout(local_size_x = 150,local_size_y = 2) in;\n"
"layout(std430, binding = 0) readonly buffer MatA {\n"
" double data[];\n"
"} matA;\n"
"layout(std430, binding = 1) readonly buffer MatB {\n"
" double data[];\n"
"} matB;\n"
"layout(std430, binding = 2) writeonly buffer MatC {\n"
" double data[];\n"
"} matC;\n"
"shared uint M = 150u;\n"
"shared uint N = 1u;\n"
"shared uint K = 38722u;\n"
"void main(){\n"
" uint globalRow = gl_GlobalInvocationID.x;\n"
" uint globalCol = gl_GlobalInvocationID.y;\n"
" double acc = double(0.0);\n"
" for (uint k = 0u; k < K; k++)\n"
" acc += matA.data[k*M + globalRow] * matB.data[globalCol * K + k];\n"
"matC.data[globalRow + globalCol * M] = acc;\n"
"}\n";
static const char *gComputeShaderPosteriorPrecMatrix =
"#version 320 es\n"
"layout(local_size_x = 30,local_size_y = 30) in;\n"
"layout(std430, binding = 0) readonly buffer MatA {\n"
" double data[];\n"
"} matA;\n"
"layout(std430, binding = 1) readonly buffer MatB {\n"
" double data[];\n"
"} matB;\n"
"layout(std430, binding = 2) writeonly buffer MatC {\n"
" double data[];\n"
"} matC;\n"
"shared uint nCol = 150u;\n"
"shared uint NF = 38u;\n"
"shared uint NC = 1019u;\n"
"shared uint nRow = 38722u;\n"
"void main(){\n"
" uint globalRow = gl_GlobalInvocationID.x;\n"
" uint globalCol = gl_GlobalInvocationID.y;\n"
" double acc = double(0.0);\n"
" uint index = 0u;\n"
" double result = double(0.0);\n"
" if (globalRow == globalCol){\n"
" result = double(1.0);\n"
" }\n"
" for (uint nc = 0u; nc < NC; nc++){\n"
" acc = double(0.0);\n"
" index = nc * NF;\n"
" for (uint nf = 0u; nf < NF; nf++){\n"
" acc += double(matA.data[(globalRow * nRow) + index + nf] * matA.data[(globalCol * nRow) + index + nf]);\n"
" }\n"
" result += double(acc * matB.data[nc]);\n"
" }\n"
" matC.data[globalRow + globalCol * nCol] = result;\n"
"}\n";
//verified
void initOccuProb(OccuProb *occuprob){
double **p = (double**) malloc(sizeof(double*) * occuprob->NbSamples);
if (!p){
fprintf(stderr, "Failed to allocate %d number of double* memory blocks.\n", occuprob->NbSamples);
exit(1);
} else {
occuprob->resp = p;
}
double *q;
for (int i = 0; i < occuprob->NbSamples; i++){
q = (double*) malloc(sizeof(double) * occuprob->NC);
if (!q){
fprintf(stderr, "Failed to allocate %d numer of double memory blocks.\n", occuprob->NC);
exit(1);
} else {
occuprob->resp[i] = q;
}
}
}
//verified
void clearOccuProb(OccuProb *occuprob){
for (int i = 0; i < occuprob->NbSamples; i++)
free(occuprob->resp[i]);
free(occuprob->resp);
}
//verified
void logsumexp(OccuProb *prob, double *LSE){
// LSE should be the size of number of samples in the given prob i.e NbSamples
long double intRes = 0.0f;
for (int i = 0; i < prob->NbSamples; i++){
intRes = 0.0f;
for (int j = 0; j < prob->NC; j++){
intRes += (long double) expl((long double) prob->resp[i][j]);
}
LSE[i] = (double) logl(intRes);
}
}
//verified
void computeLogWeightProb(MFCCFeatures *mfcc, OccuProb *occuLWprob, double *glbLogDet, double **glbPrec, double **glbMeanPrecProd, double *glbMeanPrecProdSum, double *glbLogWeight){
long double intRes;
double normlogPi = ((double) -0.5 * nFeatures * log((2.0f * 3.14159265358979323846f)));
for (int ns = 0; ns < occuLWprob->NbSamples; ns++){
for (int nc = 0; nc < occuLWprob->NC; nc++){
intRes = glbMeanPrecProdSum[nc];
for (int nf = 0; nf < nFeatures; nf++){
intRes -= ((long double) 2.0f * mfcc->Features[ns][nf] * glbMeanPrecProd[nf][nc]);
intRes += ((long double) mfcc->Features[ns][nf] * mfcc->Features[ns][nf] * glbPrec[nc][nf]);
}
occuLWprob->resp[ns][nc] = normlogPi - 0.5 * (double) intRes + glbLogDet[nc] + glbLogWeight[nc];
}
}
}
//verified
void computeNormWeightProb(OccuProb *occuLWprob){
//this will calculate the normalized weighted probability of each sample with respect to various components
int nSamples = occuLWprob->NbSamples;
double *LSE = (double*) malloc(sizeof(double) * nSamples);
if (!LSE){
printf("Could not allocate %d number of double type memory blocks.\n", nSamples);
exit(1);
}
logsumexp(occuLWprob, LSE);
double norm, x;
for (int ns = 0; ns < nSamples; ns++){
norm = LSE[ns];
for (int nc = 0; nc < occuLWprob->NC; nc++){
x = occuLWprob->resp[ns][nc] - norm;
occuLWprob->resp[ns][nc] = exp(x);
}
}
free(LSE);
}
//verified
void computeZeroStat(OccuProb *occuProb, double *zeroStat){
//zeroStat is the size of number of components
//can be done by gpu stuffs
double constant = 2.220446049250313e-15f;
for (int nc = 0; nc < occuProb->NC; nc++){
zeroStat[nc] = constant;
for (int ns = 0; ns < occuProb->NbSamples; ns++){
zeroStat[nc] += occuProb->resp[ns][nc];
}
}
}
//verified
void computeFirstStat(OccuProb *occuProb, double *zeroStat, double **glbSqrtPrec, double **glbMean, double **firstStat, MFCCFeatures *mfcc){
double intRes;
// this is the kind of matrix multiplication with specialized norm can be done using gpu stuff
for (int nc = 0; nc < occuProb->NC; nc++){
for (int nf = 0; nf < nFeatures; nf++){
intRes = 0.0f;
for (int ns = 0; ns < occuProb->NbSamples; ns++){
intRes += (occuProb->resp[ns][nc] * mfcc->Features[ns][nf]);
}
intRes -= (glbMean[nc][nf] * zeroStat[nc]);
firstStat[nc][nf] = intRes * glbSqrtPrec[nc][nf];
}
}
}
//verified
void computePosteriorPrecMat(double *zeroStat, double **precMatrixL, TVMatrix *tvMat){
//double intRes;
int NC = tvMat->NC;
int nCol = tvMat->TVCol;
//int NF = tvMat->nFeatures;
//double intRes;
//int index;
int nRow = tvMat->TVrow;
double *MTVGPU = (double*) malloc(sizeof(double) * nRow * nCol);
if (!MTVGPU){
printf("Could not allocate %d number of double type memory blocks.\n", nCol * nRow);
exit(1);
} else {
int d;
for (int i = 0; i < nCol; i++){
d = i * nRow;
for (int r = 0; r < nRow; r++){
MTVGPU[r + d] = tvMat->TV[r][i];
}
}
}
enum Consts {INFOLOG_LEN = 512};
GLchar infoLog[INFOLOG_LEN];
//set context for initializing EGL and openGL stuffs here
EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (dpy == EGL_NO_DISPLAY) {
printf("eglGetDisplay returns EGL_NO_DISPLAY.\n");
exit(1);
}
EGLint majorVersion;
EGLint minorVersion;
EGLBoolean returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
if (returnValue != EGL_TRUE) {
printf("eglInitialize failed\n");
exit(1);
}
EGLConfig cfg;
EGLint count;
EGLint s_configAttribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_NONE };
if (eglChooseConfig(dpy, s_configAttribs, &cfg, 1, &count) == EGL_FALSE) {
printf("eglChooseConfig failed\n");
exit(1);
}
EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE };
EGLContext context = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, context_attribs);
if (context == EGL_NO_CONTEXT) {
printf("eglCreateContext failed\n");
exit(1);
}
returnValue = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
if (returnValue != EGL_TRUE) {
printf("eglMakeCurrent failed returned %d\n", returnValue);
exit(1);
}
//creating compute shader
GLint success;
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
if (computeShader == 0){
printf("Could not create compute shader.\n");
exit(1);
}
glShaderSource(computeShader, 1, &gComputeShaderPosteriorPrecMatrix, NULL);
glCompileShader(computeShader);
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(computeShader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::COMPUTE::COMPILATION_FAILED\n%s\n", infoLog);
}
//creating the shader program
GLuint shaderProgram = glCreateProgram();
if(!shaderProgram){
printf("Failed to create a shader program.\n");
exit(1);
} else {
glAttachShader(shaderProgram, computeShader);
glLinkProgram(shaderProgram);
GLint linkStatus = GL_FALSE;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE){
glGetProgramInfoLog(shaderProgram, INFOLOG_LEN, NULL, infoLog);
fprintf(stderr, "Could not link program:\n%s\n", infoLog);
}
}
GLuint matASSbo;
GLuint matBSSbo;
GLuint matCSSbo;
GLuint sizeA, sizeB, sizeC;
sizeA = (GLuint) nRow * nCol;
sizeB = (GLuint) NC;
sizeC = (GLuint) nCol * nCol;
glGenBuffers(1, &matASSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matASSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeA * sizeof(double), MTVGPU, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, matASSbo);
glGenBuffers(1, &matBSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matBSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeB * sizeof(double), zeroStat, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, matBSSbo);
glGenBuffers(1, &matCSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matCSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeC * sizeof(double), NULL, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, matCSSbo);
GLenum err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glUseProgram(shaderProgram);
glDispatchCompute(5, 5, 1); // sizeA/local_size_x, sizeB/local_size_y, sizeC/local_size_z
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matCSSbo);
double* pOut = (double*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeC * sizeof(double), GL_MAP_READ_BIT);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
for (int r = 0; r < nCol; r++){
for (int c = 0; c < nCol; c++){
precMatrixL[r][c] = pOut[c * nCol + r];
}
}
/*for (int i = 0; i < nCol; i++){
printf("%lf\t",precMatrixL[0][i]);
}
printf("\n");
*/
glDeleteShader(computeShader);
glDeleteBuffers(1, &matCSSbo);
glDeleteBuffers(1, &matBSSbo);
glDeleteBuffers(1, &matASSbo);
glDeleteProgram(shaderProgram);
eglDestroyContext(dpy, context);
eglTerminate(dpy);
/*for (int r = 0; r < 1; r++){
for (int c = 0; c < nCol; c++){
printf("%lf\t",precMatrixL[r][c]);
}
printf("\n");
}
*/
free(MTVGPU);
//free(pOut);
}
//verified
void computeInversePosteriorPrecMat(double **precMatrixL, double **precInvMatrixL, int len){
SqMat sqMat;
sqMat.Row = len;
initSqMat(&sqMat);
SqMat invsqMat;
invsqMat.Row = len;
initSqMat(&invsqMat);
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
sqMat.matrix[i][j] = precMatrixL[i][j];
}
}
//printf("I am going to compute inverse precision matrix %d.\n",len);
inverseMatrixLU(&sqMat, &invsqMat);
//printf("I came after computing inverseMatrix.\n");
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
precInvMatrixL[i][j] = invsqMat.matrix[i][j];
}
}
clearSqMat(&sqMat);
clearSqMat(&invsqMat);
}
//verified
void computeWVector(double **precInvMatrixL, TVMatrix *tvMat, double **firstStat, double *wVector){
int nCol = tvMat->TVCol;
int NF = tvMat->nFeatures;
int NC = tvMat->NC;
int nRow = tvMat->TVrow;
//for doing the computation in gpu, we are creating the array to transfer the data into the buffer data objects
double *precInvMatrixLGPU = (double*) malloc(sizeof(double) * nCol * nCol);
if (!precInvMatrixLGPU){
printf("Could not allocate %d number of double type memory blocks.\n",nCol * nCol);
exit(1);
} else {
int d;
for (int i = 0; i < nCol; i++){
d = i * nCol;
for (int r = 0; r < nCol; r++){
precInvMatrixLGPU[r + d] = precInvMatrixL[r][i];
}
}
}
double *MatrixTVGPU = (double*) malloc(sizeof(double) * nRow * nCol);
if (!MatrixTVGPU){\
printf("Could not allocate %d number of double type memory blocks.\n", nCol * nRow);
exit(1);
} else {
int d;
for (int c = 0; c < nRow; c++){
d = c * nCol;
for (int i = 0; i < nCol; i++){
MatrixTVGPU[i + d] = tvMat->TV[c][i];
}
}
}
enum Consts {INFOLOG_LEN = 512};
GLchar infoLog[INFOLOG_LEN];
//set context for initializing EGL and openGL stuffs here
EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (dpy == EGL_NO_DISPLAY) {
printf("eglGetDisplay returns EGL_NO_DISPLAY.\n");
exit(1);
}
EGLint majorVersion;
EGLint minorVersion;
EGLBoolean returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
if (returnValue != EGL_TRUE) {
printf("eglInitialize failed\n");
exit(1);
}
EGLConfig cfg;
EGLint count;
EGLint s_configAttribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_NONE };
if (eglChooseConfig(dpy, s_configAttribs, &cfg, 1, &count) == EGL_FALSE) {
printf("eglChooseConfig failed\n");
exit(1);
}
EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE };
EGLContext context = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, context_attribs);
if (context == EGL_NO_CONTEXT) {
printf("eglCreateContext failed\n");
exit(1);
}
returnValue = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
if (returnValue != EGL_TRUE) {
printf("eglMakeCurrent failed returned %d\n", returnValue);
exit(1);
}
//creating compute shader
GLint success;
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
if (computeShader == 0){
printf("Could not create compute shader.\n");
exit(1);
}
glShaderSource(computeShader, 1, &gComputeShaderIntRes, NULL);
glCompileShader(computeShader);
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(computeShader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::COMPUTE::COMPILATION_FAILED\n%s\n", infoLog);
}
//creating the shader program
GLuint shaderProgram = glCreateProgram();
if(!shaderProgram){
printf("Failed to create a shader program.\n");
exit(1);
} else {
glAttachShader(shaderProgram, computeShader);
glLinkProgram(shaderProgram);
GLint linkStatus = GL_FALSE;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE){
glGetProgramInfoLog(shaderProgram, INFOLOG_LEN, NULL, infoLog);
fprintf(stderr, "Could not link program:\n%s\n", infoLog);
}
}
//arranging the matrix as arrays
GLuint matASSbo;
GLuint matBSSbo;
GLuint matCSSbo;
GLuint matDSSbo;
GLuint sizeA, sizeB, sizeC, sizeD;
sizeA = (GLuint) nCol * nCol;
sizeB = (GLuint) nRow * nCol;
sizeC = (GLuint) nCol * nRow;
glGenBuffers(1, &matASSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matASSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeA * sizeof(double), precInvMatrixLGPU, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, matASSbo);
glGenBuffers(1, &matBSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matBSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeB * sizeof(double), MatrixTVGPU, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, matBSSbo);
glGenBuffers(1, &matCSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matCSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeC * sizeof(double), NULL, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, matCSSbo);
GLenum err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glUseProgram(shaderProgram);
glDispatchCompute(1,19361,1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matCSSbo);
//glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
double* interMatrixGPU = (double*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeC * sizeof(double), GL_MAP_READ_BIT);
free(precInvMatrixLGPU);
free(MatrixTVGPU);
double *flattenFirstStat = (double*) malloc(sizeof(double) * nRow);
if (!flattenFirstStat){
printf("Could not allocate %d number of double type memory blocks.\n", nRow);
exit(1);
}
for (int nc = 0; nc < NC; nc++){
for (int f = 0; f < NF; f++){
flattenFirstStat[NF * nc + f] = firstStat[nc][f];
}
}
// can be done by gpu processors
glDeleteShader(computeShader);
glDeleteBuffers(1, &matBSSbo);
glDeleteBuffers(1, &matASSbo);
computeShader = glCreateShader(GL_COMPUTE_SHADER);
if (computeShader == 0){
printf("Could not create compute shader.\n");
exit(1);
}
glShaderSource(computeShader, 1, &gComputeShaderIVector, NULL);
glCompileShader(computeShader);
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(computeShader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::COMPUTE::COMPILATION_FAILED\n%s\n", infoLog);
}
//creating the shader program
shaderProgram = glCreateProgram();
if(!shaderProgram){
printf("Failed to create a shader program.\n");
exit(1);
} else {
glAttachShader(shaderProgram, computeShader);
glLinkProgram(shaderProgram);
GLint linkStatus = GL_FALSE;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE){
glGetProgramInfoLog(shaderProgram, INFOLOG_LEN, NULL, infoLog);
fprintf(stderr, "Could not link program:\n%s\n", infoLog);
}
}
sizeA = (GLuint) nCol * nRow;
sizeB = (GLuint) nRow;
sizeD = (GLuint) nCol;
glGenBuffers(1, &matASSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matASSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeA * sizeof(double), interMatrixGPU, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, matASSbo);
glGenBuffers(1, &matBSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matBSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeB * sizeof(double), flattenFirstStat, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, matBSSbo);
glGenBuffers(1, &matDSSbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matDSSbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeD * sizeof(double), NULL, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, matDSSbo);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glUseProgram(shaderProgram);
glDispatchCompute(1,1,1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, matDSSbo);
double* flattenFirstStatGPU = (double*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeD * sizeof(double), GL_MAP_READ_BIT);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
err = glGetError();
if (err != GL_NO_ERROR)
printf("glGetError returns %d\n", err);
for (int k = 0; k < nCol; k++){
wVector[k] = flattenFirstStatGPU[k];
//printf("%lf\t",wVector[k]);
}
//printf("\n");
glDeleteShader(computeShader);
glDeleteBuffers(1, &matCSSbo);
glDeleteBuffers(1, &matBSSbo);
glDeleteBuffers(1, &matASSbo);
glDeleteBuffers(1, &matDSSbo);
eglDestroyContext(dpy, context);
eglTerminate(dpy);
free(flattenFirstStat);
}
//verified
double computeCosineSimilarityScore(double *wTargVector, double *wTestVector, int Len){
double score = (double) 0.0f;
double wTargNorm = 2.220446049250313e-15f;
double wTestNorm = 2.220446049250313e-15f;
for (int i = 0; i < Len; i++){
score += wTargVector[i] * wTestVector[i];
wTargNorm += wTargVector[i] * wTargVector[i];
wTestNorm += wTestVector[i] * wTestVector[i];
}
return score / (wTargNorm * wTestNorm);
}