I want to make a comparative analysis of the processing time of the code below in a singlethread and in a multithread, but I don't know where I should start the threads, and how to implement multithread. Should I split the code to make multithreads?
I already implemented the function clock_t to calculate the time that it is going to take, and the code is working. I just need to add threads to it, and to make a comparison to see which one is faster: singlethread or multithread.
BONUS: Can you make it faster using OpenMP?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// R = Row
// C = Column
void fill4x4( int a[][4] ) {
for( int r = 0; r < 4; r++ )
for( int c = 0; c < 4; c++ )
a[r][c] = rand() % 100 + 1;
}
void show4x4( int a[][4] ) {
for( int r = 0; r < 4; r++ )
for( int c = 0; c < 4; c++ )
printf( "%5d%c", a[r][c], " \n"[c] );
puts( "" );
}
int showPair( int a, int b, int pos ) {
printf( "%2dx%-2d%c", a, b, " \n"[pos] );
return a * b;
}
void mult4x4( int a[][4], int b[][4], int d[][4], bool x ) {
for( int r = 0; r < 4; r++ )
for( int c = 0; c < 4; c++ )
// assign (=), not accumulate (+=)
// notice the 'exchange' of row/col access of 'b[][]'
if( x )
d[r][c] = showPair( a[r][c], b[r][c], c );
else
d[r][c] = showPair( a[r][c], b[c][r], c );
puts( "" ); show4x4( d );
}
int main()
{
srand(time(0));
double time_spent = 0.0;
clock_t begin = clock();
int a[4][4]; fill4x4( a ); show4x4( a );
int b[4][4]; fill4x4( b ); show4x4( b );
int d[4][4];
mult4x4( a, b, d, true );
mult4x4( a, b, d, false );
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("Foram gastos %f segundos.", time_spent);
return 0;
}