Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
hydra-image-processor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenSource
hydra-image-processor
Commits
f749c920
Commit
f749c920
authored
10 years ago
by
Eric Wait
Browse files
Options
Downloads
Patches
Plain Diff
Fixed Sum array for all types
parent
dc8f9587
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/c/Common/CudaSum.cuh
+23
-23
23 additions, 23 deletions
src/c/Common/CudaSum.cuh
src/c/CudaMex/MexSumArray.cpp
+5
-5
5 additions, 5 deletions
src/c/CudaMex/MexSumArray.cpp
with
28 additions
and
28 deletions
src/c/Common/CudaSum.cuh
+
23
−
23
View file @
f749c920
...
...
@@ -3,20 +3,21 @@
#include
"CudaUtilities.cuh"
#include
"cuda_runtime.h"
template
<
class
PixelType
>
__global__
void
cudaSum
(
PixelType
*
arrayIn
,
doubl
e
*
arrayOut
,
size_t
n
)
template
<
class
PixelType
In
,
class
OutType
>
__global__
void
cudaSum
(
PixelType
In
*
arrayIn
,
OutTyp
e
*
arrayOut
,
size_t
n
)
{
extern
__shared__
double
sums
[];
extern
__shared__
unsigned
char
sharedMem
[];
OutType
*
sums
=
(
OutType
*
)
sharedMem
;
size_t
i
=
threadIdx
.
x
+
blockIdx
.
x
*
blockDim
.
x
;
size_t
imStride
=
blockDim
.
x
*
gridDim
.
x
;
if
(
i
<
n
)
{
sums
[
threadIdx
.
x
]
=
(
doubl
e
)(
arrayIn
[
i
]);
sums
[
threadIdx
.
x
]
=
(
OutTyp
e
)(
arrayIn
[
i
]);
while
(
i
+
imStride
<
n
)
{
sums
[
threadIdx
.
x
]
+=
(
doubl
e
)(
arrayIn
[
i
+
imStride
]);
sums
[
threadIdx
.
x
]
+=
(
OutTyp
e
)(
arrayIn
[
i
+
imStride
]);
i
+=
imStride
;
}
}
...
...
@@ -31,8 +32,7 @@ __global__ void cudaSum(PixelType* arrayIn, double* arrayOut, size_t n)
{
if
(
threadIdx
.
x
<
localStride
)
sums
[
threadIdx
.
x
]
+=
sums
[
threadIdx
.
x
+
localStride
];
else
break
;
__syncthreads
();
}
...
...
@@ -43,13 +43,13 @@ __global__ void cudaSum(PixelType* arrayIn, double* arrayOut, size_t n)
__syncthreads
();
}
template
<
class
PixelType
>
doubl
e
sumArray
(
const
PixelType
*
imageIn
,
size_t
n
,
int
device
=
0
)
template
<
class
OutType
,
class
PixelType
In
>
OutTyp
e
sumArray
(
const
PixelType
In
*
imageIn
,
size_t
n
,
int
device
=
0
)
{
doubl
e
sum
=
0.
0
;
doubl
e
*
deviceSum
;
doubl
e
*
hostSum
;
PixelType
*
deviceBuffer
;
OutTyp
e
sum
=
0
;
OutTyp
e
*
deviceSum
;
OutTyp
e
*
hostSum
;
PixelType
In
*
deviceBuffer
;
cudaDeviceProp
props
;
cudaGetDeviceProperties
(
&
props
,
device
);
...
...
@@ -57,36 +57,36 @@ double sumArray(const PixelType* imageIn, size_t n, int device=0)
size_t
availMem
,
total
;
cudaMemGetInfo
(
&
availMem
,
&
total
);
size_t
numValsPerChunk
=
MIN
(
n
,(
size_t
)((
availMem
*
MAX_MEM_AVAIL
)
/
sizeof
(
PixelType
)));
size_t
numValsPerChunk
=
MIN
(
n
,(
size_t
)((
availMem
*
MAX_MEM_AVAIL
)
/
sizeof
(
PixelType
In
)));
int
maxBlocks
=
(
int
)
ceil
((
double
)
numValsPerChunk
/
(
2
*
props
.
maxThreadsPerBlock
));
int
threads
=
props
.
maxThreadsPerBlock
;
int
maxBlocks
=
(
int
)
ceil
((
double
)
numValsPerChunk
/
(
threads
*
2
));
HANDLE_ERROR
(
cudaMalloc
((
void
**
)
&
deviceBuffer
,
sizeof
(
PixelType
)
*
numValsPerChunk
));
HANDLE_ERROR
(
cudaMalloc
((
void
**
)
&
deviceSum
,
sizeof
(
doubl
e
)
*
maxBlocks
));
HANDLE_ERROR
(
cudaMalloc
((
void
**
)
&
deviceBuffer
,
sizeof
(
PixelType
In
)
*
numValsPerChunk
));
HANDLE_ERROR
(
cudaMalloc
((
void
**
)
&
deviceSum
,
sizeof
(
OutTyp
e
)
*
maxBlocks
));
hostSum
=
new
doubl
e
[
maxBlocks
];
hostSum
=
new
OutTyp
e
[
maxBlocks
];
for
(
size_t
startIdx
=
0
;
startIdx
<
n
;
startIdx
+=
numValsPerChunk
)
{
size_t
curNumVals
=
MIN
(
numValsPerChunk
,
n
-
startIdx
);
HANDLE_ERROR
(
cudaMemcpy
(
deviceBuffer
,
imageIn
+
startIdx
,
sizeof
(
PixelType
)
*
curNumVals
,
cudaMemcpyHostToDevice
));
HANDLE_ERROR
(
cudaMemcpy
(
deviceBuffer
,
imageIn
+
startIdx
,
sizeof
(
PixelType
In
)
*
curNumVals
,
cudaMemcpyHostToDevice
));
int
blocks
=
(
int
)
ceil
((
double
)
curNumVals
/
(
2
*
props
.
maxThreadsPerBlock
));
size_t
sharedMemSize
=
sizeof
(
double
)
*
props
.
maxThreadsPerBlock
;
int
blocks
=
(
int
)
ceil
((
double
)
curNumVals
/
(
threads
*
2
));
size_t
sharedMemSize
=
sizeof
(
OutType
)
*
threads
;
cudaSum
<<<
blocks
,
threads
,
sharedMemSize
>>>
(
deviceBuffer
,
deviceSum
,
curNumVals
);
DEBUG_KERNEL_CHECK
();
HANDLE_ERROR
(
cudaMemcpy
(
hostSum
,
deviceSum
,
sizeof
(
doubl
e
)
*
blocks
,
cudaMemcpyDeviceToHost
));
HANDLE_ERROR
(
cudaMemcpy
(
hostSum
,
deviceSum
,
sizeof
(
OutTyp
e
)
*
blocks
,
cudaMemcpyDeviceToHost
));
for
(
int
i
=
0
;
i
<
blocks
;
++
i
)
{
sum
+=
hostSum
[
i
];
}
memset
(
hostSum
,
0
,
sizeof
(
doubl
e
)
*
maxBlocks
);
memset
(
hostSum
,
0
,
sizeof
(
OutTyp
e
)
*
maxBlocks
);
}
HANDLE_ERROR
(
cudaFree
(
deviceSum
));
...
...
This diff is collapsed.
Click to expand it.
src/c/CudaMex/MexSumArray.cpp
+
5
−
5
View file @
f749c920
...
...
@@ -17,35 +17,35 @@ void MexSumArray::execute( int nlhs, mxArray* plhs[], int nrhs, const mxArray* p
unsigned
char
*
imageIn
;
setupImagePointers
(
prhs
[
0
],
&
imageIn
,
&
imageDims
);
sm
=
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
sm
=
(
double
)
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
}
else
if
(
mxIsUint16
(
prhs
[
0
]))
{
unsigned
short
*
imageIn
;
setupImagePointers
(
prhs
[
0
],
&
imageIn
,
&
imageDims
);
sm
=
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
sm
=
(
double
)
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
}
else
if
(
mxIsInt16
(
prhs
[
0
]))
{
short
*
imageIn
;
setupImagePointers
(
prhs
[
0
],
&
imageIn
,
&
imageDims
);
sm
=
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
sm
=
(
double
)
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
}
else
if
(
mxIsUint32
(
prhs
[
0
]))
{
unsigned
int
*
imageIn
;
setupImagePointers
(
prhs
[
0
],
&
imageIn
,
&
imageDims
);
sm
=
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
sm
=
(
double
)
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
}
else
if
(
mxIsInt32
(
prhs
[
0
]))
{
int
*
imageIn
;
setupImagePointers
(
prhs
[
0
],
&
imageIn
,
&
imageDims
);
sm
=
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
sm
=
(
double
)
cSumArray
(
imageIn
,
imageDims
.
product
(),
device
);
}
else
if
(
mxIsSingle
(
prhs
[
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