Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatakamanbou/2713ba9a783d95d248f7 to your computer and use it in GitHub Desktop.
Save takatakamanbou/2713ba9a783d95d248f7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
//#define PRINT
int main(int argc, char **argv)
{
double *Abuf, *Bbuf, *Cbuf;
double **A, **B, **C;
double acc;
int n;
int i, j, k, t;
if(argc != 2){
fprintf(stderr, "usage: %s n\n", argv[0]);
exit(EXIT_FAILURE);
}
n = atoi(argv[1]);
/*** 行列 A,B,C のメモリを確保,A,Bを乱数で初期化 ***/
Abuf = malloc(n*n*sizeof(double));
Bbuf = malloc(n*n*sizeof(double));
Cbuf = malloc(n*n*sizeof(double));
srand(1);
A = malloc(n*sizeof(double *));
B = malloc(n*sizeof(double *));
C = malloc(n*sizeof(double *));
for(i = 0; i < n; i++){
A[i] = Abuf + i*n;
B[i] = Bbuf + i*n;
C[i] = Cbuf + i*n;
for(j = 0; j < n; j++){
A[i][j] = drand48(); // [0.0, 1.0) pseudo-random numbers
B[i][j] = drand48();
}
}
/***** C = A x B *****/
for(t = 0; t < 10; t++){
printf("t = %d\n", t);
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
acc = 0.0;
for(k = 0; k < n; k++){
acc += A[i][k] * B[k][j];
}
C[i][j] = acc;
}
}
}
/***** 出力 *****/
#ifdef PRINT
printf("A = \n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf(" %f", A[i][j]);
}
printf("\n");
}
printf("B = \n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf(" %f", B[i][j]);
}
printf("\n");
}
printf("C = \n");
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf(" %f", C[i][j]);
}
printf("\n");
}
#else
printf("%f\n", C[n-1][n-1]);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment