From: Dheeraj Khatri on
these are 2 programs for matrix multiplication. :-

1st one is implemented using multiple threading and 2nd using single
threading. Process' Cpu time for both comes out to be same ..... but
should be less for multi threaded process. Plzz help why Cpu time is
same.... ?

program1 using multiple threading :-

#include<signal.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#include<time.h>

// decleared globally so can be used by all threads through resorce
shareing and dont get destroyed on exit of threads............

int PMatrix[500][500]; //final matrix ie. product of other
marices
int Matrix1[500][500],Matrix2[500][500];

void *thread_function(void *arg) // here argument recieved is
pointer to set of row no.s to do multiplication
{
int i,j,k,a;
int* b = (int*) arg;
a = (*b)*50; // determines range

for(i=a;i<a+50;i++) // Each thread
computes for 50 rows
for(j=0;j<500;j++)
for(k=0;k<500;k++)
PMatrix[i][j]+=Matrix1[i][k]*Matrix2[k][j];
return NULL;
}

int main()
{
int c[10]={0,1,2,3,4,5,6,7,8,9};
int i,j;
void * exit_status;
pthread_t tid[10]; //thread ids 10 as ten therads are created
clock_t t1,t2; //to mesure no of ticks
double t;

t1=clock();
for(i=0;i<500;i++)
for(j=0;j<500;j++)
Matrix1[i][j] = rand()%10;

for(i=0;i<500;i++)
for(j=0;j<500;j++)
Matrix2[i][j] = rand()%10;

for(i=0;i<10;i++)
pthread_create(&tid[i],NULL,thread_function,&c[i]);

printf("Matrix 1 is \n");
for(i=0;i<5;i++) //prints Matrix1 up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",Matrix1[i][j]);
}
printf("\n");
}

printf("\n Matrix2 is \n");
for(i=0;i<5;i++) //prints Matrix2 up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",Matrix2[i][j]);
}
printf("\n");
}

for(i=0;i<10;i++) //waiting for all threads to terminate
pthread_join(tid[i],&exit_status);


printf("\n PMatrix is \n");
for(i=0;i<5;i++) //prints PMatrix up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",PMatrix[i][j]);
}
printf("\n");
}

t2 = clock();
t = ((double)(t2 - t1))/CLOCKS_PER_SEC ; // cpu time =
NO of tics / No of tics per sec
printf("\nTime taken by multi threading is -> %lf \n",t);

return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
program2 using single thread :-

#include<signal.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#include<time.h>

int PMatrix[500][500];
int Matrix1[500][500],Matrix2[500][500];


void *thread_function(void *arg)
{
int i,j,k;

for(i=0;i<500;i++) // single thread
computes for all 500 rows
for(j=0;j<500;j++)
for(k=0;k<500;k++)
PMatrix[i][j]+=Matrix1[i][k]*Matrix2[k][j];
return NULL;
}

int main()
{
int i,j;
void * exit_status;
pthread_t tid;
clock_t t1,t2;
double t;
t1=clock();
for(i=0;i<500;i++)
for(j=0;j<500;j++)
Matrix1[i][j] = rand()%10;

for(i=0;i<500;i++)
for(j=0;j<500;j++)
Matrix2[i][j] = rand()%10;

pthread_create(&tid,NULL,thread_function,NULL); // no value passed

printf("Matrix 1 is \n");
for(i=0;i<5;i++) //prints Matrix1 up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",Matrix1[i][j]);
}
printf("\n");
}

printf("\n Matrix2 is \n");
for(i=0;i<5;i++) //prints Matrix2 up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",Matrix2[i][j]);
}
printf("\n");
}

pthread_join(tid,&exit_status); //waiting for thread to terminate

printf("\n PMatrix is \n");
for(i=0;i<5;i++) //prints PMatrix up to 5 rows and
colums
{
for(j=0;j<5;j++)
{
printf(" %d",PMatrix[i][j]);
}
printf("\n");
}

t2 = clock();
t = ((double)(t2 - t1))/CLOCKS_PER_SEC ; // cpu time =
NO of tics / No of tics per sec
printf("\nTime taken by single thread is -> %lf \n",t);

return 0;
}


From: Ben Bacarisse on
Dheeraj Khatri <dheerajkhatri1990(a)gmail.com> writes:

> these are 2 programs for matrix multiplication. :-
>
> 1st one is implemented using multiple threading and 2nd using single
> threading. Process' Cpu time for both comes out to be same ..... but
> should be less for multi threaded process. Plzz help why Cpu time is
> same.... ?

Because they both use the same amount of CPU time!

On my two-code laptop, the threaded one runs in about half the real
time, but uses that same amount of CPU time. You need something that
measures "wall time" rather than CPU time. Maybe gettimeofday(2),
clock_gettime(2) or the time(1) command.

<snip>
--
Ben.