Processo dove è presente clock_gettime
codice:
case 1: clock_gettime(CLOCK_REALTIME, &ts1); /* E' qui che si verifica l'errore! */
a=(short*)malloc(n_a*m_a*sizeof(short));
b=(short*)malloc(n_b*m_b*sizeof(short));
c=(short*)malloc(n_a*m_b*sizeof(short));
s_main(a,b,c,n_a,m_a,n_b,m_b);
clock_gettime(CLOCK_REALTIME, &ts2);
eTime0 = (double)(ts2.tv_sec - ts1.tv_sec);
eTime1 = (double)(ts2.tv_nsec - ts1.tv_nsec);
eTime = eTime0 + eTime1*1.0e-9;
printf("\ngettime(): \t\t\tElapsed Time = %e sec", eTime);
break;
Main.c
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
short *a,*b,*c,i,j,n_a,n_b,m_a,m_b,x;
struct timespec ts1, ts2; double eTime, eTime0, eTime1;
do
{
puts("Inserire numero di righe della matrice A:");
scanf("%hd",&n_a);
__fpurge(stdin);
puts("Inserire numero di colonne della matrice A:");
scanf("%hd",&m_a);
__fpurge(stdin);
puts("Inserire numero di righe della matrice B:");
scanf("%hd",&n_b);
__fpurge(stdin);
puts("Inserire numero di colonne della matrice B:");
printf("(Il numero di colonne della matrice B deve essere uguale al numero di righe della matrice A(%hd)):\n",n_a);
scanf("%hd",&m_b);
__fpurge(stdin);}while(n_a!=m_b);
while(x!=3)
{
puts("1.Processo con malloc.");
puts("2.Processo con calloc.");
puts("3.Fine.");
puts("Inserire scelta:");
scanf("%hd",&x);
__fpurge(stdin);
switch(x)
{
case 1: clock_gettime(CLOCK_REALTIME, &ts1); /* E' qui che si verifica l'errore! */
a=(short*)malloc(n_a*m_a*sizeof(short));
b=(short*)malloc(n_b*m_b*sizeof(short));
c=(short*)malloc(n_a*m_b*sizeof(short));
s_main(a,b,c,n_a,m_a,n_b,m_b);
clock_gettime(CLOCK_REALTIME, &ts2);
eTime0 = (double)(ts2.tv_sec - ts1.tv_sec);
eTime1 = (double)(ts2.tv_nsec - ts1.tv_nsec);
eTime = eTime0 + eTime1*1.0e-9;
printf("\ngettime(): \t\t\tElapsed Time = %e sec", eTime);
break;
case 2: clock_gettime(CLOCK_REALTIME, &ts1);
a=(short*)calloc(n_a*m_a,sizeof(short));
b=(short*)calloc(n_b*m_b,sizeof(short));
c=(short*)calloc(n_a*m_b,sizeof(short));
s_main(a,b,c,n_a,m_a,n_b,m_b);
clock_gettime(CLOCK_REALTIME, &ts2);
eTime0 = (double)(ts2.tv_sec - ts1.tv_sec); // sec
eTime1 = (double)(ts2.tv_nsec - ts1.tv_nsec); // nano sec
eTime = eTime0 + eTime1*1.0e-9; // sec
printf("\ngettime(): \t\t\tElapsed Time = %e sec", eTime);
break;
case 3: puts("Uscita");
break;
default: puts("Inserire un valore tra 1 e 3.");
break;
}
}
}
s_main.c
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void s_main(short *a,short *b,short *c,short n_a,short m_a,short n_b,short m_b)
{
comp(n_a,m_a,a);
comp(n_b,m_b,b);
stamp(n_a,m_a,a,'A');
stamp(n_b,m_b,b,'B');
prod(n_a,m_a,n_b,m_b,a,b,c);
stamp(n_a,m_b,c,'C');
}
comp.c
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void comp(short n_x,short m_x, short *x)
{
srand(time (NULL));
short i,j;
for(i=0;i<n_x;i++)
for(j=0;j<m_x;j++)
*(x+i*n_x+j)=rand()%10;
}
stamp.c
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void stamp(short n_x,short m_x,short *x,char y)
{
short i,j;
printf("\n\nMatrice %c:\n",y);
for(i=0;i<n_x;i++)
{
printf("\n");
for(j=0;j<m_x;j++)
printf("%hd ",*(x+i*n_x+j));
}
}
prod.c
codice:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void prod(short n_x,short m_x,short n_y,short m_y,short *a,short *b,short *c)
{
short i,j,k,x,sum;
for (i=0;i<n_x;i++)
for (j=0;j<m_y;j++)
{
sum=0;
for (k=0;k<m_x;k++)
{
x=(*(a+(i*n_x)+k))*(*(b+k*n_y+j));
sum=sum+x;
}
*(c+i*n_x+j)=sum;
}
}
Grazie!