Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Multicore GPU programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ylva Selling
Multicore GPU programming
Commits
19e864d6
Commit
19e864d6
authored
4 years ago
by
ylvse560
Browse files
Options
Downloads
Patches
Plain Diff
Lab 3 average and gaussian done
parent
36952f9d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Lab3/average.cpp
+45
-21
45 additions, 21 deletions
Lab3/average.cpp
Lab3/median.cpp
+62
-12
62 additions, 12 deletions
Lab3/median.cpp
with
107 additions
and
33 deletions
Lab3/average.cpp
+
45
−
21
View file @
19e864d6
...
...
@@ -26,25 +26,47 @@ unsigned char average_kernel(skepu::Region2D<unsigned char> m, size_t elemPerPx)
return
res
*
scaling
;
}
unsigned
char
average_kernel_1d
(
skepu
::
Region1D
<
unsigned
char
>
m
,
size_t
elemPerPx
)
unsigned
char
average_kernel_1d
_col
(
skepu
::
Region1D
<
unsigned
char
>
m
,
size_t
elemPerPx
)
{
float
scaling
=
1.0
/
(
m
.
oi
*
2
+
1
);
float
res
=
0
;
for
(
int
y
=
-
m
.
oi
;
y
<=
m
.
oi
;
++
y
)
for
(
int
y
=
-
m
.
oi
;
y
<=
m
.
oi
;
y
++
)
res
+=
m
(
y
);
return
res
*
scaling
;
}
unsigned
char
average_kernel_1d_row
(
skepu
::
Region1D
<
unsigned
char
>
m
,
size_t
elemPerPx
)
{
float
scaling
=
1.0
/
(
m
.
oi
/
elemPerPx
*
2
+
1
);
float
res
=
0
;
for
(
int
y
=
-
m
.
oi
;
y
<=
m
.
oi
;
y
+=
elemPerPx
)
res
+=
m
(
y
);
return
res
*
scaling
;
}
unsigned
char
gaussian_kernel
(
skepu
::
Region1D
<
unsigned
char
>
m
,
const
skepu
::
Vec
<
float
>
stencil
,
size_t
elemPerPx
)
{
// your code here
return
m
(
0
);
}
int
stepsize
=
1
;
int
stencilstep
=
0
;
// If the kernel is row wise
if
(
m
.
oi
*
2
+
1
!=
(
int
)
stencil
.
size
)
{
printf
(
"%i
\n
"
,
(
int
)
stencil
.
size
);
stepsize
=
(
int
)
elemPerPx
;
}
else
{
printf
(
"%s
\n
"
,
"col"
);
}
float
res
=
0
;
for
(
int
y
=
-
m
.
oi
;
y
<=
m
.
oi
;
y
+=
stepsize
)
{
res
+=
stencil
(
stencilstep
)
*
m
(
y
);
//printf("%f\n", stencil(stencilstep));
stencilstep
++
;
}
return
res
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -71,6 +93,7 @@ int main(int argc, char* argv[])
ImageInfo
imageInfo
;
skepu
::
Matrix
<
unsigned
char
>
inputMatrixPad
=
ReadAndPadPngFileToMatrix
(
inputFileName
,
radius
,
colorType
,
imageInfo
);
skepu
::
Matrix
<
unsigned
char
>
inputMatrix
=
ReadPngFileToMatrix
(
inputFileName
,
colorType
,
imageInfo
);
skepu
::
Matrix
<
unsigned
char
>
outputMatrixFirstPass
(
imageInfo
.
height
,
imageInfo
.
width
*
imageInfo
.
elementsPerPixel
,
120
);
skepu
::
Matrix
<
unsigned
char
>
outputMatrix
(
imageInfo
.
height
,
imageInfo
.
width
*
imageInfo
.
elementsPerPixel
,
120
);
// more containers...?
...
...
@@ -93,20 +116,17 @@ int main(int argc, char* argv[])
// use conv.setOverlapMode(skepu::Overlap::[ColWise RowWise]);
// and conv.setOverlap(<integer>)
{
auto
conv1
=
skepu
::
MapOverlap
(
average_kernel_1d
);
//auto conv2 = skepu::MapOverlap(average_kernel_1d);
conv1
.
setOverlapMode
(
skepu
::
Overlap
::
ColWise
);
conv1
.
setOverlap
(
radius
*
imageInfo
.
elementsPerPixel
);
//conv2.setOverlapMode(skepu::Overlap::RowWise);
//conv2.setOverlap(radius*imageInfo.elementsPerPixel);
auto
conv_row
=
skepu
::
MapOverlap
(
average_kernel_1d_row
);
conv_row
.
setOverlapMode
(
skepu
::
Overlap
::
RowWise
);
conv_row
.
setOverlap
(
radius
*
imageInfo
.
elementsPerPixel
);
auto
conv_col
=
skepu
::
MapOverlap
(
average_kernel_1d_col
);
conv_col
.
setOverlapMode
(
skepu
::
Overlap
::
ColWise
);
conv_col
.
setOverlap
(
radius
);
auto
timeTaken
=
skepu
::
benchmark
::
measureExecTime
([
&
]
{
// your code here
conv1
(
outputMatrix
,
inputMatrixPad
,
imageInfo
.
elementsPerPixel
);
//conv2(outputMatrix, inputMatrixPad, imageInfo.elementsPerPixel);
conv_row
(
outputMatrixFirstPass
,
inputMatrix
,
imageInfo
.
elementsPerPixel
);
conv_col
(
outputMatrix
,
outputMatrixFirstPass
,
imageInfo
.
elementsPerPixel
);
});
WritePngFileMatrix
(
outputMatrix
,
outputFile
+
"-separable.png"
,
colorType
,
imageInfo
);
...
...
@@ -117,15 +137,19 @@ int main(int argc, char* argv[])
// Separable gaussian
{
skepu
::
Vector
<
float
>
stencil
=
sampleGaussian
(
radius
);
// skeleton instance, etc here (remember to set backend)
auto
conv
=
skepu
::
MapOverlap
(
gaussian_kernel
);
conv
.
setOverlapMode
(
skepu
::
Overlap
::
ColWise
);
conv
.
setOverlap
(
radius
);
auto
timeTaken
=
skepu
::
benchmark
::
measureExecTime
([
&
]
{
// your code here
conv
(
outputMatrixFirstPass
,
inputMatrix
,
stencil
,
imageInfo
.
elementsPerPixel
);
conv
.
setOverlapMode
(
skepu
::
Overlap
::
RowWise
);
conv
.
setOverlap
(
radius
*
imageInfo
.
elementsPerPixel
);
conv
(
outputMatrix
,
outputMatrixFirstPass
,
stencil
,
imageInfo
.
elementsPerPixel
);
});
//
WritePngFileMatrix(outputMatrix, outputFile + "-gaussian.png", colorType, imageInfo);
WritePngFileMatrix
(
outputMatrix
,
outputFile
+
"-gaussian.png"
,
colorType
,
imageInfo
);
std
::
cout
<<
"Time for gaussian: "
<<
(
timeTaken
.
count
()
/
10E6
)
<<
"
\n
"
;
}
...
...
This diff is collapsed.
Click to expand it.
Lab3/median.cpp
+
62
−
12
View file @
19e864d6
...
...
@@ -19,56 +19,106 @@
unsigned
char
median_kernel
(
skepu
::
Region2D
<
unsigned
char
>
image
,
size_t
elemPerPx
)
{
// your code here
int
row_size
=
image
.
oi
*
2
+
1
;
int
col_size
=
image
.
oj
*
2
+
1
;
int
n
=
row_size
*
col_size
;
int
i
,
j
,
x
,
y
,
x_next
,
y_next
;
unsigned
char
temp
;
for
(
i
=
0
;
i
<
n
-
1
;
i
++
){
// Last i elements are already in place
for
(
j
=
0
;
j
<
n
-
i
-
1
;
j
+=
elemPerPx
)
{
x
=
j
%
row_size
;
y
=
(
j
-
x
)
/
row_size
;
x_next
=
(
j
+
elemPerPx
)
%
row_size
;
y_next
=
(
j
+
elemPerPx
-
x_next
)
/
row_size
;
if
(
image
(
x
,
y
)
>
image
(
x_next
,
y_next
))
{
temp
=
image
(
x
,
y
);
printf
(
"%uc
\n
"
,
temp
);
//image.data[x,y] = image(x_next, y_next);
//image.data[x_next, y_next] = temp;
}
}
}
return
image
(
0
,
0
);
}
/*
unsigned char median_kernel_vector(skepu::Region2D<unsigned char> image, skepu::Vector<unsigned char> sortingVec, size_t elemPerPx)
{
int row_size = image.oi*2+1;
int col_size = image.oj*2+1;
int n = row_size*col_size;
int i, j, x, y, x_next, y_next;
int k = 0;
unsigned char temp;
for(i = -image.oj; i <= image.oi; i++)
for(j = -image.oj; j <= image.oj; j++)
sortingVec(k) = image(i,j);
for (i = 0; i < n-1; i++){
// Last i elements are already in place
for (j = 0; j < n-i-1; j += elemPerPx) {
if (sortingVec(j) > sortingVec(j+1)) {
temp = sortingVec(j);
sortingVec(j) = sortingVec(j+1);
sortingVec(j+1) = temp;
}
}
}
return image(0,0);
}
*/
int
main
(
int
argc
,
char
*
argv
[])
{
LodePNGColorType
colorType
=
LCT_RGB
;
if
(
argc
<
5
)
{
std
::
cout
<<
"Usage: "
<<
argv
[
0
]
<<
" input output radius [backend]
\n
"
;
exit
(
1
);
}
std
::
string
inputFileName
=
argv
[
1
];
std
::
string
outputFileName
=
argv
[
2
];
const
int
radius
=
atoi
(
argv
[
3
]);
auto
spec
=
skepu
::
BackendSpec
{
argv
[
4
]};
skepu
::
setGlobalBackendSpec
(
spec
);
// Create the full path for writing the image.
std
::
stringstream
ss
;
ss
<<
(
2
*
radius
+
1
)
<<
"x"
<<
(
2
*
radius
+
1
);
std
::
string
outputFileNamePad
=
outputFileName
+
ss
.
str
()
+
"-median.png"
;
// Read the padded image into a matrix. Create the output matrix without padding.
ImageInfo
imageInfo
;
skepu
::
Matrix
<
unsigned
char
>
inputMatrix
=
ReadAndPadPngFileToMatrix
(
inputFileName
,
radius
,
colorType
,
imageInfo
);
std
::
cout
<<
imageInfo
.
height
<<
", "
<<
imageInfo
.
width
<<
std
::
endl
;
std
::
cout
<<
inputMatrix
.
total_cols
()
<<
", "
<<
inputMatrix
.
total_rows
()
<<
std
::
endl
;
skepu
::
Matrix
<
unsigned
char
>
outputMatrix
(
imageInfo
.
height
,
imageInfo
.
width
*
imageInfo
.
elementsPerPixel
,
120
);
std
::
cout
<<
outputMatrix
.
total_cols
()
<<
", "
<<
outputMatrix
.
total_rows
()
<<
std
::
endl
;
// Skeleton instance
auto
calculateMedian
=
skepu
::
MapOverlap
(
median_kernel
);
calculateMedian
.
setOverlap
(
radius
,
radius
*
imageInfo
.
elementsPerPixel
);
skepu
::
Vector
<
unsigned
char
>
sortingVec
((
radius
*
2
+
1
)
*
(
radius
*
2
+
1
)
*
imageInfo
.
elementsPerPixel
);
auto
timeTaken
=
skepu
::
benchmark
::
measureExecTime
([
&
]
{
calculateMedian
(
outputMatrix
,
inputMatrix
,
imageInfo
.
elementsPerPixel
);
});
WritePngFileMatrix
(
outputMatrix
,
outputFileNamePad
,
colorType
,
imageInfo
);
std
::
cout
<<
"Time: "
<<
(
timeTaken
.
count
()
/
10E6
)
<<
"
\n
"
;
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment