Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
opencl
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mastersthesis-philipp
opencl
Commits
dd37efd6
Commit
dd37efd6
authored
Mar 01, 2017
by
Philipp Adolf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add hello world example
parent
d365cd44
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
168 additions
and
0 deletions
+168
-0
main.go
cmd/go-opencl-helloworld/main.go
+168
-0
No files found.
cmd/go-opencl-helloworld/main.go
0 → 100644
View file @
dd37efd6
package
main
import
(
"fmt"
"log"
"github.com/Patagonicus/opencl"
)
const
source
string
=
`__kernel void hello(__constant char *a, __constant char *b, __global char *c) {
size_t gid = get_global_id(0);
if (gid < get_global_size(0)) {
c[gid] = a[gid] + b[gid];
}
}`
const
kernelName
=
"hello"
var
dataA
=
[]
byte
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}
var
dataB
=
[]
byte
{
0
,
1
,
2
,
3
,
4
,
3
,
2
,
1
,
0
}
func
getFirstDevice
()
(
*
opencl
.
Device
,
error
)
{
platforms
,
err
:=
opencl
.
GetPlatforms
()
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
p
:=
range
platforms
{
devices
,
err
:=
p
.
Devices
(
opencl
.
DeviceTypeAll
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
devices
)
>
0
{
return
&
devices
[
0
],
nil
}
}
return
nil
,
fmt
.
Errorf
(
"no device found"
)
}
func
getDeviceContext
()
(
*
opencl
.
Device
,
*
opencl
.
Context
,
error
)
{
device
,
err
:=
getFirstDevice
()
if
err
!=
nil
{
return
nil
,
nil
,
err
}
context
,
err
:=
opencl
.
CreateContext
(
nil
,
[]
*
opencl
.
Device
{
device
},
nil
,
nil
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
return
device
,
context
,
nil
}
func
createBuffers
(
context
opencl
.
Context
)
(
*
opencl
.
Memory
,
*
opencl
.
Memory
,
*
opencl
.
Memory
,
error
)
{
bufA
,
err
:=
context
.
CreateBuffer
(
opencl
.
MemReadOnly
,
uintptr
(
len
(
dataA
)),
nil
)
if
err
!=
nil
{
return
nil
,
nil
,
nil
,
err
}
bufB
,
err
:=
context
.
CreateBuffer
(
opencl
.
MemReadOnly
,
uintptr
(
len
(
dataA
)),
nil
)
if
err
!=
nil
{
bufA
.
Release
()
return
nil
,
nil
,
nil
,
err
}
bufC
,
err
:=
context
.
CreateBuffer
(
opencl
.
MemWriteOnly
,
uintptr
(
len
(
dataA
)),
nil
)
if
err
!=
nil
{
bufA
.
Release
()
bufB
.
Release
()
return
nil
,
nil
,
nil
,
err
}
return
bufA
,
bufB
,
bufC
,
nil
}
func
createKernel
(
context
opencl
.
Context
,
device
opencl
.
Device
)
(
*
opencl
.
Program
,
*
opencl
.
Kernel
,
error
)
{
program
,
err
:=
context
.
CreateProgramWithSource
([]
string
{
source
})
if
err
!=
nil
{
return
nil
,
nil
,
err
}
err
=
program
.
Build
([]
*
opencl
.
Device
{
&
device
},
""
)
if
err
!=
nil
{
buildLog
,
logErr
:=
program
.
BuildLog
(
device
)
if
logErr
==
nil
{
log
.
Printf
(
"build log: %s"
,
buildLog
)
}
program
.
Release
()
return
nil
,
nil
,
err
}
kernel
,
err
:=
program
.
CreateKernel
(
kernelName
)
if
err
!=
nil
{
program
.
Release
()
return
nil
,
nil
,
err
}
return
program
,
kernel
,
nil
}
func
main
()
{
log
.
SetFlags
(
0
)
device
,
context
,
err
:=
getDeviceContext
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
context
.
Release
()
bufA
,
bufB
,
bufC
,
err
:=
createBuffers
(
*
context
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
bufA
.
Release
()
defer
bufB
.
Release
()
defer
bufC
.
Release
()
queue
,
err
:=
context
.
CreateCommandQueue
(
*
device
,
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
queue
.
Release
()
err
=
queue
.
EnqueueWriteBuffer
(
*
bufA
,
0
,
dataA
,
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
err
=
queue
.
EnqueueWriteBuffer
(
*
bufB
,
0
,
dataB
,
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
program
,
kernel
,
err
:=
createKernel
(
*
context
,
*
device
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
program
.
Release
()
defer
kernel
.
Release
()
err
=
kernel
.
SetArgBuffer
(
0
,
*
bufA
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
err
=
kernel
.
SetArgBuffer
(
1
,
*
bufB
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
err
=
kernel
.
SetArgBuffer
(
2
,
*
bufC
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
event
,
err
:=
queue
.
EnqueueNDRangeKernel
(
*
kernel
,
[]
uintptr
{
0
},
[]
uintptr
{
uintptr
(
len
(
dataA
))},
[]
uintptr
{
uintptr
(
len
(
dataA
))},
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
event
.
Release
()
data
:=
make
([]
byte
,
len
(
dataA
))
err
=
queue
.
EnqueueReadBuffer
(
*
bufC
,
0
,
data
,
[]
opencl
.
Event
{
*
event
})
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
log
.
Printf
(
"%d
\n
"
,
data
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment