i want to make my program faster by changing algorithm or using pthreads, but i couldn't figure out how to use pthreads and what algorithm to apply. can anyone help me out? Any algorithm to make matrix addition sum of matrix find min and max etc faster? g_height is row of matrix and g_width is column of matrix.
/**
* Returns new matrix with scalar added to each element
*/
uint32_t* scalar_add(const uint32_t* matrix, uint32_t scalar) {
uint32_t* result = new_matrix();
/*
to do
1 0 2 1
0 1 + 1 => 1 2
1 2 5 6
3 4 + 4 => 7 8
*/
for (ssize_t y = 0; y < g_height; y++) {
for (ssize_t x = 0; x < g_width; x++) {
result[y * g_width + x]=matrix[y * g_width + x]+scalar;
}
}
return result;
}
/**
* Returns new matrix with scalar multiplied to each element
*/
uint32_t* scalar_mul(const uint32_t* matrix, uint32_t scalar) {
uint32_t* result = new_matrix();
/*
to do
1 0 2 0
0 1 x 2 => 0 2
1 2 2 4
3 4 x 2 => 6 8
*/
for (ssize_t y = 0; y < g_height; y++) {
for (ssize_t x = 0; x < g_width; x++) {
result[y * g_width + x]=matrix[y * g_width + x]*scalar;
}
}
return result;
}
/**
* Returns new matrix with elements added at the same index
*/
uint32_t* matrix_add(const uint32_t* matrix_a, const uint32_t* matrix_b) {
uint32_t* result = new_matrix();
/*
to do
1 0 0 1 1 1
0 1 + 1 0 => 1 1
1 2 4 4 5 6
3 4 + 4 4 => 7 8
*/
for (ssize_t y = 0; y < g_height; y++) {
for (ssize_t x = 0; x < g_width; x++){
result[y * g_width + x]=matrix_a[y * g_width + x]+matrix_b[y * g_width + x];
}
}
return result;
}
/**
* Returns new matrix, multiplying the two matrices together
*/
uint32_t* matrix_mul(const uint32_t* matrix_a, const uint32_t* matrix_b) {
uint32_t* result = new_matrix();
/*
to do
1 2 1 0 1 2
3 4 x 0 1 => 3 4
1 2 5 6 19 22
3 4 x 7 8 => 43 50
*/
uint32_t* tempmatrix_a=cloned(matrix_a);
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
for(int i=0; i<g_width; i++)
result[y*g_width + x]+=tempmatrix_a[y*g_width + i]*matrix_b[i*g_width + x ];
}
}
return result;
}
/**
* Returns new matrix, powering the matrix to the exponent
*/
uint32_t* matrix_pow(const uint32_t* matrix, uint32_t exponent) {
uint32_t* result = new_matrix();
/*
to do
1 2 1 0
3 4 ^ 0 => 0 1
1 2 1 2
3 4 ^ 1 => 3 4
1 2 199 290
3 4 ^ 4 => 435 634
*/
uint32_t* tempresult=identity_matrix();
ssize_t i;
if (exponent == 0)
result=identity_matrix();
for (i = 0; i < exponent; i++)
tempresult = matrix_mul(tempresult, matrix);
result=tempresult;
return result;
}
////////////////////////////////
/// COMPUTATIONS //
////////////////////////////////
/**
* Returns the sum of all elements
*/
uint32_t get_sum(const uint32_t* matrix) {
/*
to do
1 2
2 1 => 6
1 1
1 1 => 4
*/
int sum=0;
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
sum+=matrix[y*g_width + x];
}
}
return sum;
return 0;
}
/**
* Returns the trace of the matrix
*/
uint32_t get_trace(const uint32_t* matrix) {
/*
to do
1 0
0 1 => 2
2 1
1 2 => 4
*/
int trace=0;
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
if(y==x)
trace+=matrix[y*g_width + x];
}
}
return trace;
return 0;
}
/**
* Returns the smallest value in the matrix
*/
uint32_t get_minimum(const uint32_t* matrix) {
/*
to do
1 2
3 4 => 1
4 3
2 1 => 1
*/
int min=matrix[0];
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
if(min>matrix[y*g_width + x])
min=matrix[y*g_width + x];
}
}
return min;
return 0;
}
/**
* Returns the largest value in the matrix
*/
uint32_t get_maximum(const uint32_t* matrix) {
/*
to do
1 2
3 4 => 4
4 3
2 1 => 4
*/
int max=matrix[0];
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
if(max<matrix[y*g_width + x])
max=matrix[y*g_width + x];
}
}
return max;
return 0;
}
/**
* Returns the frequency of the value in the matrix
*/
uint32_t get_frequency(const uint32_t* matrix, uint32_t value) {
/*
to do
1 1
1 1 :: 1 => 4
1 0
0 1 :: 2 => 0
*/
int frequency=0;
for(ssize_t y=0; y<g_height; y++){
for(ssize_t x=0; x<g_width; x++){
if(matrix[y*g_width + x]==value)
frequency++;
}
}
return frequency;
return 0;
}