#include
#include
#define N1 5000
#define N2 5000
void gen( int** a, int** b)
{
int i, j;
for (i = 0; i < N1; i++) {
for (j = 0; j < N2; j++) {
srand( 0 );
a[i][j] = rand() % 5 + 1;
srand( 1 );
b[i][j] = rand() % 5 + 1;
}
//printf("i: %d\n", i);
}
pthread_exit(NULL);
}
void mul( int** a, int** b)
{
int i, j, k;
int c[N1][N2];
for (i = 0; i < N1; i++) {
for (j = 0;j < N2; j++) {
c[i][j] = 0;
for (k = 0; k < N1; k++)
c[i][j] += a[i][k] * b[k][j];
//printf("%d\t", c[i][j]);
}
//printf("\n");
}
}
int main(int argc, char *argv[])
{
int** a = NULL;
int** b = NULL;
int** c = NULL;
a = (int**) malloc( sizeof(int) * N1 * N2);
b = (int**) malloc( sizeof(int) * N1 * N2);
c = (int**) malloc( sizeof(int) * N1 * N2);
pthread_t* thread = NULL;
thread = (pthread_t*) malloc( sizeof(pthread_t) * N1);
int result;
int i;
for (i = 0; i < N1; i++) {
a[i] = (int *) malloc( sizeof(int) * N2);
b[i] = (int *) malloc( sizeof(int) * N2);
c[i] = (int *) malloc( sizeof(int) * N2);
}
gen(a, b);
return 0;
}
$ gcc m1.c && time ./a.out
real 3m55.660s
user 3m55.391s
sys 0m0.184s
This result without threading.
沒有留言:
張貼留言