-
Notifications
You must be signed in to change notification settings - Fork 1
Added Matrix Multiplication #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
84f730e
2e1d073
b0a8355
90bab0b
bec81ae
b1d177f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.7) | ||
project(GPGPUGLESMatrixMultiplyInt VERSION 1.0) | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | ||
|
||
find_package(GPGPU_GLES REQUIRED) | ||
|
||
add_executable(GPGPUGLESMatrixMultiplyInt | ||
${CMAKE_CURRENT_LIST_DIR}/mult_mat_int.c) | ||
|
||
target_link_libraries(GPGPUGLESMatrixMultiplyInt PRIVATE GPGPU_GLES::GPGPU_GLES) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <stdio.h> | ||
#include "gpgpu_gles.h" | ||
|
||
#define HEIGHT 4 | ||
#define WIDTH HEIGHT | ||
|
||
int main() | ||
{ | ||
if (gpgpu_init(HEIGHT, WIDTH) != 0) | ||
{ | ||
printf("Could not initialize the API\n"); | ||
return 0; | ||
} | ||
|
||
// create two float arrays | ||
int* a = malloc(WIDTH * HEIGHT * sizeof(float)); | ||
JDuchniewicz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int* b = malloc(WIDTH * HEIGHT * sizeof(float)); | ||
int* res = malloc(WIDTH * HEIGHT * sizeof(float)); | ||
|
||
for (int i = 0; i < WIDTH * HEIGHT; ++i) | ||
{ | ||
a[i] = b[i] = i; | ||
} | ||
|
||
printf("Data before computation: \n"); | ||
for (int i = 0; i < WIDTH * HEIGHT; ++i) | ||
{ | ||
printf("%d ", a[i]); | ||
if ((i + 1) % WIDTH == 0) | ||
printf("\n"); | ||
} | ||
printf("\n"); | ||
|
||
if (gpgpu_matrixMultiplication(a, b, 4, res) != 0) | ||
printf("Could not do the matrix multiplication\n"); | ||
|
||
printf("Contents after multiplication: \n"); | ||
for (int i = 0; i < WIDTH * HEIGHT; ++i) | ||
{ | ||
printf("%d ", res[i]); | ||
if ((i + 1) % WIDTH == 0) | ||
printf("\n"); | ||
} | ||
printf("\n"); | ||
|
||
gpgpu_deinit(); | ||
free(a); | ||
free(b); | ||
free(res); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#ifdef GL_FRAGMENT_PRECISION_HIGH | ||
precision highp float; | ||
#else | ||
precision mediump float; | ||
#endif | ||
|
||
uniform sampler2D texture0; | ||
uniform sampler2D texture1; | ||
|
||
varying vec2 vTexCoord; | ||
|
||
vec4 pack(float value) | ||
{ | ||
if (value == 0.0) return vec4(0); | ||
|
||
float exponent; | ||
float mantissa; | ||
vec4 result; | ||
float sgn; | ||
|
||
sgn = step(0.0, -value); | ||
value = abs(value); | ||
|
||
exponent = floor(log2(value)); | ||
mantissa = value * pow(2.0, -exponent) - 1.0; | ||
exponent = exponent + 127.0; | ||
result = vec4(0); | ||
|
||
result.a = floor(exponent / 2.0); | ||
exponent = exponent - result.a * 2.0; | ||
result.a = result.a + 128.0 * sgn; | ||
|
||
result.b = floor(mantissa * 128.0); | ||
mantissa = mantissa - result.b / 128.0; | ||
result.b = result.b + exponent * 128.0; | ||
|
||
result.g = floor(mantissa * 32768.0); | ||
mantissa = mantissa - result.g / 32768.0; | ||
|
||
result.r = floor(mantissa * 8388608.0); | ||
|
||
return result / 255.0; | ||
} | ||
|
||
float unpack(vec4 texel) | ||
{ | ||
float exponent; | ||
float mantissa; | ||
float value; | ||
float sgn; | ||
|
||
sgn = -step(128.0, texel.a); | ||
texel.a += 128.0 * sgn; | ||
|
||
exponent = step(128.0, texel.b); | ||
texel.b -= exponent * 128.0; | ||
exponent += 2.0 * texel.a - 127.0; | ||
|
||
mantissa = texel.b * 65536.0 + texel.g * 256.0 + texel.r; | ||
value = pow(-1.0, sgn) * exp2(exponent) * (1.0 + mantissa * exp2(-23.0)); | ||
|
||
return value; | ||
} | ||
|
||
void main (void) | ||
{ | ||
float i = vTexCoord.s; | ||
float j = vTexCoord.t; | ||
float result = 0.0; | ||
for (float k = 0.0; k < 4.0; ++k) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why 4? why k is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To reduce the value of k to lie between 0 to 1, we are dividing it by 4. This is to make sure that the coordinates of the texture element we are multiplying lies in the range 0 to 1 in the texture coordinate system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @JDuchniewicz any update on merging this inclusion of matrix multiplication to the upstream!? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, sorry got bogged down by other work. I still do not understand why 4.0 and if 4.0 is necessary (I presume the matrix size?). If this is indeed the matrix size, it should be configurable as a uniform and not hard-coded. Please change that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Jakub, yes its for size. |
||
{ | ||
result += unpack(texture2D(texture0, vec2(i, k / 4.0)) * 255.0) * unpack(texture2D(texture1, vec2(k / 4.0, j)) * 255.0); | ||
} | ||
gl_FragColor = pack(result); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not floats? ints make little sense here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have changed the entire operation on ints to that on floats
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so change the name to float please :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, done.