Created
December 18, 2015 14:20
-
-
Save kngwyu/fb16631f8e1ea953f0bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/time.h> | |
#include <random> | |
static double dtime(void); | |
extern int main(void); | |
const int rndmax = 32767; | |
static double dtime()/*実行時間を計る関数*/ | |
{ | |
double q; | |
struct timeval tnow; | |
gettimeofday(&tnow,NULL); | |
q = (double)tnow.tv_sec + (double)tnow.tv_usec * 1.0e-6; | |
return q; | |
} | |
int main(void){ | |
static long memsize[] = { | |
16l, 32l, 64l, 128l, 256l, 512l, 1024l, | |
2048l, 4096l | |
};/*lはlong型を表す接尾辞*/ | |
long max_n=memsize[sizeof(memsize)/sizeof(memsize[0])-1]; | |
long **matrix_1; | |
long *base_matrix_1; | |
long **matrix_2; | |
long *base_matrix_2; | |
long **matrix_3; | |
long *base_matrix_3; | |
long h,i,j,k; | |
long n = max_n; | |
long m = max_n; | |
double start_time,end_time,elapsed_time; | |
if(!(matrix_1 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_2 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_3 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_1=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_2=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_3=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
for(k=0; k<sizeof(memsize)/sizeof(memsize[0]); k++) { | |
long p = memsize[k]; | |
long q = memsize[k]; | |
double start_time,end_time,elapsed_time; | |
for (i=0;i<p;i++) { | |
matrix_1[i] = base_matrix_1 + i * q; | |
} | |
std::random_device rnd; | |
std::mt19937 mtrnd(rnd()); | |
std::uniform_int_distribution<> rangernd(0, rndmax); | |
for (i=0;i<p;i++) { | |
for (j=0;j<q;j++) { | |
matrix_1[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_2[i] = base_matrix_2 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_2[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_3[i] = base_matrix_3 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_3[i][j] = 0; | |
} | |
} | |
start_time=dtime(); | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++){ | |
for (h=0;h<q;h++){ | |
matrix_3[i][j] += matrix_1[i][h] * matrix_2[h][j]; | |
} | |
} | |
} | |
end_time=dtime(); | |
elapsed_time=end_time-start_time; | |
printf("%f\n",elapsed_time); | |
} | |
free(base_matrix_1); | |
free(matrix_1); | |
free(base_matrix_2); | |
free(matrix_2); | |
free(base_matrix_3); | |
free(matrix_3); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/time.h> | |
#include <random> | |
static double dtime(void); | |
extern int main(void); | |
const int rndmax = 32767; | |
static double dtime()/*実行時間を計る関数*/ | |
{ | |
double q; | |
struct timeval tnow; | |
gettimeofday(&tnow,NULL); | |
q = (double)tnow.tv_sec + (double)tnow.tv_usec * 1.0e-6; | |
return q; | |
} | |
int main(void){ | |
static long memsize[] = { | |
16l, 32l, 64l, 128l, 256l, 512l, 1024l, | |
2048l, 4096l | |
};/*すべて16で割り切れる*/ | |
long max_n = memsize[sizeof(memsize)/sizeof(memsize[0])-1]; | |
long **matrix_1; | |
long *base_matrix_1; | |
long **matrix_2; | |
long *base_matrix_2; | |
long **matrix_3; | |
long *base_matrix_3; | |
long h,i,j,k; | |
long n = max_n; | |
long m = max_n; | |
long ibl = 16; | |
long ib,jb,hb; | |
double start_time,end_time,elapsed_time; | |
if(!(matrix_1 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_2 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_3 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_1=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_2=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_3=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
for(k=0; k<sizeof(memsize)/sizeof(memsize[0]); k++) { | |
long p = memsize[k]; | |
long q = memsize[k]; | |
double start_time,end_time,elapsed_time; | |
for (i=0;i<p;i++) { | |
matrix_1[i] = base_matrix_1 + i * q; | |
} | |
std::random_device rnd; | |
std::mt19937 mtrnd(rnd()); | |
std::uniform_int_distribution<> rangernd(0, rndmax); | |
for (i=0;i<p;i++) { | |
for (j=0;j<q;j++) { | |
matrix_1[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_2[i] = base_matrix_2 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_2[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_3[i] = base_matrix_3 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_3[i][j] = 0; | |
} | |
} | |
start_time=dtime(); | |
for ( ib=0; ib<p; ib+=ibl ) { | |
for ( jb=0; jb<p; jb+=ibl ) { | |
for ( hb=0; hb<p; hb+=ibl ) { | |
for ( i=ib; i<ib+ibl; i++ ) { | |
for ( j=jb; j<jb+ibl; j++ ) { | |
for ( h=hb; h<hb+ibl; h++ ) { | |
matrix_3[i][j] += matrix_1[i][h] * matrix_2[h][j]; | |
} | |
} | |
} | |
} | |
} | |
} | |
end_time=dtime(); | |
elapsed_time=end_time-start_time; | |
printf("%f\n",elapsed_time); | |
} | |
free(base_matrix_1); | |
free(matrix_1); | |
free(base_matrix_2); | |
free(matrix_2); | |
free(base_matrix_3); | |
free(matrix_3); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/time.h> | |
#include <random> | |
static double dtime(void); | |
extern int main(void); | |
const int rndmax = 32767; | |
static double dtime()/*実行時間を計る関数*/ | |
{ | |
double q; | |
struct timeval tnow; | |
gettimeofday(&tnow,NULL); | |
q = (double)tnow.tv_sec + (double)tnow.tv_usec * 1.0e-6; | |
return q; | |
} | |
int main(void){ | |
static long memsize[] = { | |
16l, 32l, 64l, 128l, 256l, 512l, 1024l, | |
2048l, 4096l | |
};/*すべて16で割り切れる*/ | |
long max_n = memsize[sizeof(memsize)/sizeof(memsize[0])-1]; | |
long **matrix_1; | |
long *base_matrix_1; | |
long **matrix_2; | |
long *base_matrix_2; | |
long **matrix_3; | |
long *base_matrix_3; | |
long h,i,j,k; | |
long n = max_n; | |
long m = max_n; | |
long ibl = 64; | |
long ib,jb,hb; | |
double start_time,end_time,elapsed_time; | |
if(!(matrix_1 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_2 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(matrix_3 = (long**)malloc(sizeof(long*)*n))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_1=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_2=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
if(!(base_matrix_3=(long*)malloc(sizeof(long)*n*m))) { | |
puts("memory allocation failed"); | |
exit(EXIT_FAILURE); | |
} | |
for(k=0; k<sizeof(memsize)/sizeof(memsize[0]); k++) { | |
long p = memsize[k]; | |
long q = memsize[k]; | |
double start_time,end_time,elapsed_time; | |
for (i=0;i<p;i++) { | |
matrix_1[i] = base_matrix_1 + i * q; | |
} | |
std::random_device rnd; | |
std::mt19937 mtrnd(rnd()); | |
std::uniform_int_distribution<> rangernd(0, rndmax); | |
for (i=0;i<p;i++) { | |
for (j=0;j<q;j++) { | |
matrix_1[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_2[i] = base_matrix_2 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_2[i][j] = rangernd(mtrnd); | |
} | |
} | |
for (i=0;i<p;i++) { | |
matrix_3[i] = base_matrix_3 + i * q; | |
} | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++) { | |
matrix_3[i][j] = 0; | |
} | |
} | |
start_time=dtime(); | |
if(p<64){ | |
for (i=0;i<p;i++){ | |
for (j=0;j<q;j++){ | |
for (h=0;h<q;h++){ | |
matrix_3[i][j] += matrix_1[i][h] * matrix_2[h][j]; | |
} | |
} | |
} | |
}else{ | |
for ( ib=0; ib<p; ib+=ibl ) { | |
for ( jb=0; jb<p; jb+=ibl ) { | |
for ( hb=0; hb<p; hb+=ibl ) { | |
for ( i=ib; i<ib+ibl; i++ ) { | |
for ( j=jb; j<jb+ibl; j++ ) { | |
for ( h=hb; h<hb+ibl; h++ ) { | |
matrix_3[i][j] += matrix_1[i][h] * matrix_2[h][j]; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
end_time=dtime(); | |
elapsed_time=end_time-start_time; | |
printf("%f\n",elapsed_time); | |
} | |
free(base_matrix_1); | |
free(matrix_1); | |
free(base_matrix_2); | |
free(matrix_2); | |
free(base_matrix_3); | |
free(matrix_3); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment