magistraleinformaticanetworking:spm:samplevvcode
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Prossima revisione | Revisione precedente | ||
magistraleinformaticanetworking:spm:samplevvcode [11/11/2013 alle 15:38 (12 anni fa)] – creata Marco Danelutto | magistraleinformaticanetworking:spm:samplevvcode [11/11/2013 alle 15:41 (12 anni fa)] (versione attuale) – Marco Danelutto | ||
---|---|---|---|
Linea 2: | Linea 2: | ||
This is the code for matrix multiplication we used during the lesson. To be compiled with the **-O3** flag (Gnu compiler suite). Vectorization details can be obtained with the **-ftree-vectorizer-verbose=NN** with NN being 1 to 9 (see Gnu gcc/g++ compiler [[http:// | This is the code for matrix multiplication we used during the lesson. To be compiled with the **-O3** flag (Gnu compiler suite). Vectorization details can be obtained with the **-ftree-vectorizer-verbose=NN** with NN being 1 to 9 (see Gnu gcc/g++ compiler [[http:// | ||
+ | |||
+ | <code g++ mm.c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | float a[N][N]; | ||
+ | float b[N][N]; | ||
+ | float c[N][N]; | ||
+ | |||
+ | struct timespec diff(struct timespec t0, struct timespec t1); | ||
+ | |||
+ | int main(int argc, char * argv[]) { | ||
+ | |||
+ | // timer resolution | ||
+ | struct timespec res; | ||
+ | struct timespec t0, t1; | ||
+ | |||
+ | clock_getres(CLOCCHE, | ||
+ | printf(" | ||
+ | |||
+ | // init matrixes | ||
+ | srand(getpid()); | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int j=0; j<N; j++) { | ||
+ | a[i][j]=rand(); | ||
+ | b[i][j]=rand(); | ||
+ | c[i][j]=0.0; | ||
+ | } | ||
+ | |||
+ | clock_gettime(CLOCCHE,& | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int j=0; j<N; j++) | ||
+ | for(int k=0; k<N; k++) | ||
+ | c[i][j]+=a[i][k]*b[k][j]; | ||
+ | clock_gettime(CLOCCHE,& | ||
+ | diff(t0, | ||
+ | |||
+ | float sum = 0.0; | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int j=0; j<N; j++) | ||
+ | sum+=c[i][j]; | ||
+ | |||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | timespec diff(timespec start, timespec end) | ||
+ | { | ||
+ | timespec temp; | ||
+ | if ((end.tv_nsec-start.tv_nsec)< | ||
+ | temp.tv_sec = end.tv_sec-start.tv_sec-1; | ||
+ | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; | ||
+ | } else { | ||
+ | temp.tv_sec = end.tv_sec-start.tv_sec; | ||
+ | temp.tv_nsec = end.tv_nsec-start.tv_nsec; | ||
+ | } | ||
+ | printf(" | ||
+ | return temp; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code g++ mmkj.c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | float a[N][N]; | ||
+ | float b[N][N]; | ||
+ | float c[N][N]; | ||
+ | |||
+ | struct timespec diff(struct timespec t0, struct timespec t1); | ||
+ | |||
+ | int main(int argc, char * argv[]) { | ||
+ | |||
+ | // timer resolution | ||
+ | struct timespec res; | ||
+ | struct timespec t0, t1; | ||
+ | |||
+ | clock_getres(CLOCCHE, | ||
+ | printf(" | ||
+ | |||
+ | // init matrixes | ||
+ | srand(getpid()); | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int j=0; j<N; j++) { | ||
+ | a[i][j]=rand(); | ||
+ | b[i][j]=rand(); | ||
+ | c[i][j]=0.0; | ||
+ | } | ||
+ | |||
+ | clock_gettime(CLOCCHE,& | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int k=0; k<N; k++) { | ||
+ | float aik = a[i][k]; | ||
+ | for(int j=0; j<N; j++) | ||
+ | c[i][j] += aik*b[k][j]; | ||
+ | } | ||
+ | clock_gettime(CLOCCHE,& | ||
+ | diff(t0, | ||
+ | |||
+ | float sum = 0.0; | ||
+ | for(int i=0; i<N; i++) | ||
+ | for(int j=0; j<N; j++) | ||
+ | sum+=c[i][j]; | ||
+ | |||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | timespec diff(timespec start, timespec end) | ||
+ | { | ||
+ | timespec temp; | ||
+ | if ((end.tv_nsec-start.tv_nsec)< | ||
+ | temp.tv_sec = end.tv_sec-start.tv_sec-1; | ||
+ | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; | ||
+ | } else { | ||
+ | temp.tv_sec = end.tv_sec-start.tv_sec; | ||
+ | temp.tv_nsec = end.tv_nsec-start.tv_nsec; | ||
+ | } | ||
+ | printf(" | ||
+ | return temp; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code makefile> | ||
+ | CC = g++ | ||
+ | CFLAGS = -DN=128 -DCLOCCHE=CLOCK_THREAD_CPUTIME_ID | ||
+ | LDFLAGS = -lrt | ||
+ | OBJS = mm mmo3 mmkj mmkjo3 | ||
+ | |||
+ | all: mm mmo3 mmkj mmkjo3 | ||
+ | |||
+ | mm: mm.c | ||
+ | $(CC) $(CFLAGS) mm.c -o mm $(LDFLAGS) -ftree-vectorizer-verbose=2 | ||
+ | mmo3: mm.c | ||
+ | $(CC) $(CFLAGS) mm.c -o mmo3 $(LDFLAGS) -ftree-vectorizer-verbose=2 -O3 | ||
+ | mmkj: mm.c | ||
+ | $(CC) $(CFLAGS) mmkj.c -o mmkj $(LDFLAGS) -ftree-vectorizer-verbose=2 | ||
+ | mmkjo3: | ||
+ | $(CC) $(CFLAGS) mmkj.c -o mmkjo3 $(LDFLAGS) -ftree-vectorizer-verbose=2 -O3 | ||
+ | </ | ||
+ |
magistraleinformaticanetworking/spm/samplevvcode.1384184316.txt.gz · Ultima modifica: 11/11/2013 alle 15:38 (12 anni fa) da Marco Danelutto