Last active
October 27, 2015 15:02
-
-
Save harkalygergo/914f6c42fe223202906f to your computer and use it in GitHub Desktop.
PEP Feladat 1. OMP
This file contains hidden or 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
/* | |
Function: 1. Készítsen programot, amely rögzített, 1024 × 1024 méretű, egészeket tartalmazó mátrixok sor és oszlopvektorát állítja elő! Hasonlítsa össze a program futási idejét -O3 optimalizálással, auto-vektorizálással és annak kikapcsolása esetén. A -O3 optimalizálás alapértelmezett módon tartalmazza az auto-vektorizálást, amely azonban kikapcsolható a -fno-tree-vectorize kapcsolóval. - OMP változat | |
Version: 2015.10.27. | |
Copyright: Harkály Gergő & Lehoczky Tamás | Miskolci Egyetem | |
Install: | |
- CMakeLists.txt: | |
CMAKE_MINIMUM_REQUIRED(VERSION 2.8) | |
PROJECT(feladat1OMP C) | |
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}) | |
SET(TARGET_NAME feladat1OMP) | |
AUX_SOURCE_DIRECTORY(. SRC) | |
ADD_EXECUTABLE(${TARGET_NAME} ${SRC}) | |
SET(CMAKE_C_FLAGS "-fopenmp") | |
- figure.R: | |
library('Cairo') | |
library('ggplot2') | |
library('reshape') | |
library("grid") | |
plottitle <- "mátrix sor- és oszlopösszeg - OPM változat" | |
plotlabels <- c("Az egyes tömbméretek futásideje") | |
xlabel <- "Tömb méret (32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024)" | |
ylabel <- "Idő" | |
data0 <- read.table('results.dat') | |
d <- data.frame(data0$V2, data0$V1) | |
colnames(d) <- c('alpha', 'runtime1') | |
d <- melt(d, id='alpha', variable_name='series') | |
CairoPDF("stat.pdf", width=30, height=12) | |
p<-ggplot(d, | |
aes_string(x=names(d)[1], y=names(d)[3], colour=names(d)[2]), | |
labeller=label_parsed) + | |
geom_point(size=4) + | |
geom_line(size=1.5) + | |
labs(title=plottitle) + | |
xlab(xlabel) + | |
ylab(ylabel) + | |
scale_colour_manual(values=c("red", "green", "blue", "orange", "gray"), name="", labels=plotlabels, guide=guide_legend(keyheight=unit(2, "line"), keywidth=unit(5, "line"))) + | |
theme_gray(24) + | |
scale_x_continuous(breaks=round(seq(1.0, 10.0, by=1), 1)) + | |
scale_y_continuous(breaks=sort(c(round(seq(0, max(d$value)+1, by=20), 1)))) + | |
theme(legend.position="bottom") | |
print(p) | |
dev.off() | |
- results.dat | |
Run: cmake .; make clean; make; ./feladat1OMP; Rscript figure.R | |
*/ | |
#include <stdio.h> | |
#include <stdarg.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#include <omp.h> | |
/* STOPPER */ | |
// stopper.h | |
#ifndef _STOPPER_H_ | |
#define _STOPPER_H_ | |
typedef struct stopper | |
{ | |
clock_t begin; | |
clock_t end; | |
} stopper; | |
void startS(stopper* st); | |
void stopS(stopper* st); | |
int tprintf(stopper* st, const char* fmt, ...); | |
#endif | |
// stooper.c | |
void startS(stopper *st) | |
{ | |
st->begin= clock(); | |
} | |
void stopS(stopper *st) | |
{ | |
st->end= clock(); | |
} | |
int tprintf(stopper *st, const char* fmt, ...) | |
{ | |
float d= st->end - st->begin; | |
long ms, s; | |
d*=1000; | |
d/=CLOCKS_PER_SEC; | |
ms= d; | |
s= ms / 100; | |
ms= ms % 100; | |
va_list arg; | |
va_start(arg, fmt); | |
printf("%02ld ", ms); | |
vprintf(fmt, arg); | |
va_end(arg); | |
fflush(stdout); | |
return ms; | |
} | |
int getstoppertime(stopper *st) | |
{ | |
float d = st->end - st->begin; | |
long ms; | |
d *= 1000; | |
d /= CLOCKS_PER_SEC; | |
ms = d; | |
ms = ms % 100; | |
return ms; | |
} | |
/* MAIN */ | |
int main(int argc, char** argv[]) | |
{ | |
FILE *file; | |
file = fopen("results.dat", "w"); | |
if(file == NULL) | |
{ | |
printf("Error opening file!\n"); | |
} | |
else | |
{ | |
int matrixarray[6] = {32, 64, 128, 256, 512, 1024}; | |
int r, m, i, j, k, N, sum = 0; | |
stopper st; | |
#pragma omp parallel num_threads(6) | |
{ | |
#pragma omp for firstprivate(i, j, m, N) | |
for(r=0; r<6; r++) | |
{ | |
N = matrixarray[r]; | |
int matrix[N][N], sorosszeg[N], oszloposszeg[N]; | |
for(m=0; m<100; m++) | |
{ | |
startS(&st); // start stopper | |
for(i=0; i<N; i++) | |
{ | |
for(j=0; j<N; j++) | |
{ | |
//matrix[i][j] = 5; | |
matrix[i][j] = rand()%100; | |
} | |
} | |
for(j=0; j<N; j++) | |
{ | |
oszloposszeg[j] = 0; | |
for(i=0; i<N; i++) | |
{ | |
oszloposszeg[j] += matrix[i][j]; | |
} | |
} | |
for(i=0; i<N; i++) | |
{ | |
sorosszeg[i] = 0; | |
for(j=0; j<N; j++) | |
{ | |
sorosszeg[i] += matrix[i][j]; | |
} | |
} | |
for(i=0; i<N; i++) | |
{ | |
for(j=0; j<N; j++) | |
{ | |
printf("%d\t", matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
printf("\nOszlopvektor:\t"); | |
for(j=0; j<N; j++) | |
{ | |
printf("%d\t", oszloposszeg[j]); | |
} | |
printf("\nSorvektor:\t"); | |
for(j=0; j<N; j++) | |
{ | |
printf("%d\t", sorosszeg[j]); | |
} | |
*/ | |
stopS(&st); // stop stopper | |
sum += getstoppertime(&st); // add current runtime to sum variable | |
printf("%d ", omp_get_thread_num()); | |
//tprintf(&st, "\n"); // echo current runtime | |
} | |
sum = sum/100; | |
fprintf(file, "%d\t%d\n", sum, r+1); // write summary to results file | |
} | |
} | |
fclose(file); // close results file | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment