Skip to content

Commit 2e898ae

Browse files
committed
Hello World example
0 parents  commit 2e898ae

24 files changed

+658
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

0_HelloWorld/.DS_Store

10 KB
Binary file not shown.

0_HelloWorld/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2020 | Parallall
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

0_HelloWorld/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
SUBDIRS = kernel-driver
2+
3+
.PHONY: all clean
4+
5+
all: check-pfaces-sdk
6+
for dir in $(SUBDIRS); do $(MAKE) all -C $$dir $@; done
7+
8+
clean:
9+
rm -f ./kernel-pack/*.log
10+
rm -f ./kernel-pack/*.render
11+
rm -f ./kernel-pack/*.driver
12+
rm -f ./kernel-pack/*.dll
13+
rm -f ./kernel-pack/*.so
14+
rm -f ./kernel-pack/*.exp
15+
rm -f ./kernel-pack/*.ilk
16+
rm -f ./kernel-pack/*.lib
17+
rm -f ./kernel-pack/*.pdb
18+
rm -f ./kernel-pack/*.ipdb
19+
rm -f ./kernel-pack/*.iobj
20+
rm -rf x64
21+
rm -rf Release
22+
rm -rf Debug
23+
find . -name "*.raw" -type f -delete
24+
find . -name "*.asv" -type f -delete
25+
find . -name "*.exe" -type f -delete
26+
find . -name "*.prism" -type f -delete
27+
for dir in $(SUBDIRS); do $(MAKE) clean -C $$dir $@; done
28+
29+
30+
check-pfaces-sdk:
31+
ifeq ($(PFACES_SDK_ROOT),)
32+
$(error PFACES_SDK_ROOT was not found. Please assign PFACES_SDK_ROOT to the root directory of pFaces SDK)
33+
endif

0_HelloWorld/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# pFaces-Example: Hello World

0_HelloWorld/example.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#-----------------------------------------------------------------------
2+
# File: example.cfg
3+
# Date: 01.04.2020
4+
# Athr: M. Khaled
5+
# Desc: This file gives one confiigurartion file for the kernel.
6+
#-----------------------------------------------------------------------
7+
8+
config_message = "Hello from a cfg file !";

0_HelloWorld/kernel-driver/.DS_Store

6 KB
Binary file not shown.

0_HelloWorld/kernel-driver/Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Compiler
3+
#
4+
CC = g++
5+
CXXFLAGS = -Wall -Wextra -std=c++11 -O3 -DNDEBUG -Wno-deprecated -Wno-ignored-attributes
6+
7+
8+
#
9+
# pfaces-sdk
10+
#
11+
PFACES_SDK_INC = -I$(PFACES_SDK_ROOT)/include
12+
EXTRA_LNK=-L/usr/local/opt/openssl/lib -lcpprest -lssl -lcrypto -lboost_system -ldl -lpthread
13+
14+
OS := $(shell uname)
15+
ifeq ($(OS),Darwin)
16+
PFACES_SDK_LNK = -L$(PFACES_SDK_ROOT)/lib -Wl,-all_load -lpfaces-sdk -Wl,-noall_load -framework OpenCL -L/usr/local/opt/openssl/lib -lcpprest -lssl -lcrypto -lboost_system -ldl -lpthread
17+
EXTRA_LNK += -lboost_thread-mt -lboost_chrono-mt
18+
CXXFLAGS += -Wno-ignored-attributes
19+
else
20+
PFACES_SDK_LNK = -L$(PFACES_SDK_ROOT)/lib -Wl,--whole-archive -lpfaces-sdk -Wl,--no-whole-archive
21+
EXTRA_LNK += -lstdc++fs
22+
endif
23+
24+
#
25+
# example code
26+
#
27+
KERNEL_NAME = example
28+
KERNEL_SRC = $(KERNEL_NAME).cpp
29+
30+
31+
.PHONY: kernel
32+
33+
TARGET = kernel
34+
35+
all: $(TARGET)
36+
37+
kernel:
38+
$(CC) -c -o $(KERNEL_NAME).o -fPIC $(CXXFLAGS) $(PFACES_SDK_INC) $(KERNEL_SRC)
39+
$(CC) -shared -o kernel.so $(PFACES_SDK_LNK) $(EXTRA_LNK) *.o
40+
mv kernel.so ../kernel-pack/$(KERNEL_NAME).driver
41+
42+
clean:
43+
rm -f -r Debug
44+
rm -f -r Release
45+
rm -f -r x64
46+
rm -f -r .vs
47+
rm -f *.o
48+
rm -f *.so
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "example.h"
2+
3+
namespace pFacesExamples{
4+
5+
/* some constants */
6+
const std::string kernel_source = "example";
7+
const std::string mem_cfg_file = "example.mem";
8+
const size_t thread_grid_x = 1;
9+
const size_t thread_grid_y = 1;
10+
11+
/* the driver constructor */
12+
pFacesExample::pFacesExample(
13+
const std::shared_ptr<pfacesKernelLaunchState>& spLaunchState,
14+
const std::shared_ptr<pfacesConfigurationReader>& spCfg) :
15+
pfaces2DKernel(
16+
spLaunchState->getDefaultSourceFilePath(
17+
kernel_source,
18+
spLaunchState->kernelScope,
19+
spLaunchState->kernelPackPath),
20+
thread_grid_x,
21+
thread_grid_y,
22+
spCfg), m_spCfg(spCfg){
23+
24+
25+
// Create a kernel function and load its memory fingerprints
26+
std::string memFile = spLaunchState->kernelPackPath + mem_cfg_file;
27+
std::string funcName = "exampleKernelFunction";
28+
std::vector<std::string> argNames = {"data_in"};
29+
auto funcArgs = pfacesKernelFunctionArguments::loadFromFile(
30+
memFile, /* memory config file with the function memory fingerprint */
31+
funcName, /* name of the function to add */
32+
argNames /* list of the names of its args */
33+
);
34+
pfacesKernelFunction theFunction(funcName, funcArgs);
35+
addKernelFunction(theFunction);
36+
37+
// update params in the kernel source
38+
updatePrameters({"@@param_message@@"}, {"\"Hello from the driver !\""});
39+
}
40+
41+
/* provide an implementation of the virtual method: configureParallelProgram*/
42+
void pFacesExample::configureParallelProgram(pfacesParallelProgram& parallelProgram){
43+
44+
/* check this is one device */
45+
bool multiple_devices = parallelProgram.countTargetDevices() > 1;
46+
if(multiple_devices)
47+
throw std::runtime_error("This example is supposed to work only within one device.");
48+
49+
auto thisMachine = parallelProgram.getMachine();
50+
auto thisDevice = parallelProgram.getTargetDevices()[0];
51+
52+
/* the range/offset of this kernel : we just need one thread */
53+
const cl::NDRange ndKernelRange(1,1);
54+
const cl::NDRange ndKernelOffset(0,0);
55+
56+
/* allocate memory */
57+
std::vector<std::pair<char*, size_t>> dataPool;
58+
pFacesMemoryAllocationReport memReport;
59+
memReport = allocateMemory(dataPool, thisMachine, parallelProgram.getTargetDevicesIndicies(), 1, false);
60+
memReport.PrintReport();
61+
62+
63+
/* the parallel program needs a alist of instructionss */
64+
std::vector<std::shared_ptr<pfacesInstruction>> instrList;
65+
66+
/* an instruction to show a simple message from the CPU side */
67+
std::shared_ptr<pfacesInstruction> instrMsg_start = std::make_shared<pfacesInstruction>();
68+
instrMsg_start->setAsMessage("Hello World from host side !");
69+
70+
/* an instruction to lauch parallel threadss in the device */
71+
std::shared_ptr<pfacesDeviceExecuteTask> singleTask = std::make_shared<pfacesDeviceExecuteTask>(ndKernelOffset, ndKernelRange, ndKernelRange);
72+
std::shared_ptr<pfacesDeviceExecuteJob> exampleFunctionJob = std::make_shared<pfacesDeviceExecuteJob>(thisDevice);
73+
exampleFunctionJob->addTask(singleTask);
74+
exampleFunctionJob->setKernelFunctionIdx(
75+
0, /* index of the kernel function we're to write data to */
76+
1 /* how many arguments it has */
77+
);
78+
std::shared_ptr<pfacesInstruction> devFunctionInstruction = std::make_shared<pfacesInstruction>();
79+
devFunctionInstruction->setAsDeviceExecute(exampleFunctionJob);
80+
81+
/* a synchronization instruction */
82+
std::shared_ptr<pfacesInstruction> instrSyncPoint = std::make_shared<pfacesInstruction>();
83+
instrSyncPoint->setAsBlockingSyncPoint();
84+
85+
/* build the parallel program */
86+
instrList.push_back(instrMsg_start);
87+
instrList.push_back(devFunctionInstruction);
88+
instrList.push_back(instrSyncPoint);
89+
90+
/* complete the required settings in the parallel program */
91+
parallelProgram.m_Universal_globalNDRange = ndKernelRange;
92+
parallelProgram.m_Universal_offsetNDRange = ndKernelOffset;
93+
parallelProgram.m_dataPool = dataPool;
94+
parallelProgram.m_spInstructionList = instrList;
95+
}
96+
97+
/* provide an implementation of the virtual method: configureTuneParallelProgram*/
98+
void pFacesExample::configureTuneParallelProgram(pfacesParallelProgram& tuneParallelProgram, size_t targetFunctionIdx) {
99+
(void)tuneParallelProgram;
100+
(void)targetFunctionIdx;
101+
}
102+
}
103+
104+
/* register the kernel */
105+
PFACES_REGISTER_LOADABLE_KERNEL(pFacesExamples::pFacesExample)

0_HelloWorld/kernel-driver/example.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* example.h
3+
*
4+
* Created on: 01.04.2020
5+
* Author: M. Khaled
6+
*/
7+
8+
#ifndef PFACES_KERNEL_EXAMPLE_H_
9+
#define PFACES_KERNEL_EXAMPLE_H_
10+
11+
#include <pfaces-sdk.h>
12+
13+
namespace pFacesExamples{
14+
15+
// class: pFacesExample
16+
class pFacesExample : public pfaces2DKernel {
17+
private:
18+
const std::shared_ptr<pfacesConfigurationReader> m_spCfg;
19+
20+
public:
21+
pFacesExample(
22+
const std::shared_ptr<pfacesKernelLaunchState>& spLaunchState,
23+
const std::shared_ptr<pfacesConfigurationReader>& spCfg);
24+
25+
~pFacesExample() = default;
26+
27+
void configureParallelProgram(
28+
pfacesParallelProgram& parallelProgram);
29+
30+
void configureTuneParallelProgram(
31+
pfacesParallelProgram& tuneParallelProgram,
32+
size_t targetFunctionIdx);
33+
34+
};
35+
36+
}
37+
38+
#endif /* PFACES_KERNEL_EXAMPLE_H_ */

0_HelloWorld/kernel-pack/.DS_Store

6 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#-----------------------------------------------------------------------
2+
# File: example.mem
3+
# Date: 01.04.2020
4+
# Athr: M. Khaled
5+
# Desc: This file describes the defaults of any config file that
6+
# will be loaded later to the kernel.
7+
#-----------------------------------------------------------------------
8+
9+
config_message = "no message";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#-----------------------------------------------------------------------
2+
# File: example.mem
3+
# Date: 01.04.2020
4+
# Athr: M. Khaled
5+
# Desc: This file describes the template of any later config file to be loaded to the kernel.
6+
#-----------------------------------------------------------------------
7+
8+
config_message = string
9+

0_HelloWorld/kernel-pack/example.cl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* example.cl
3+
*
4+
* date : 01.04.2020
5+
* author : M. Khaled
6+
*/
7+
8+
#define CFG_MSG @pfaces-configValueString:"config_message"
9+
#define PARAM_MSG @@param_message@@
10+
11+
12+
kernel void exampleKernelFunction(global char* data_in){
13+
printf("Hello World from the device side!\n");
14+
printf("The value of the config item (config_message) is: %s\n", CFG_MSG);
15+
printf("The value of the kernel param (param_message) is: %s\n", PARAM_MSG);
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* example.cpu.cl
3+
*
4+
* date : 01.04.2020
5+
* author : M. Khaled
6+
*/
7+
8+
#define CPU_VERSION
9+
@pfaces-include:"example.cl"
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* example.gpu.cl
3+
*
4+
* date : 01.04.2020
5+
* author : M. Khaled
6+
*/
7+
8+
#define GPU_VERSION
9+
@pfaces-include:"example.cl"
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* example.hwa.cl
3+
*
4+
* date : 01.04.2020
5+
* author : M. Khaled
6+
*/
7+
8+
#define HWA_VERSION
9+
@pfaces-include:"example.cl"
10+

0_HelloWorld/kernel-pack/example.mem

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#-----------------------------------------------------------------------
2+
# File: example.mem
3+
# Date: 01.04.2020
4+
# Athr: M. Khaled
5+
# Desc: This file describes the memory buffers needed by the kernel.
6+
#-----------------------------------------------------------------------
7+
8+
# function 0: exampleKernelFunction
9+
exampleKernelFunction{
10+
11+
# argument 0.0: data_in
12+
data_in{
13+
MemConfig = "pfaces_rw_global";
14+
isNewOrResident = "true";
15+
}
16+
}

0 commit comments

Comments
 (0)