Skip to content
Snippets Groups Projects
Commit c929ec4f authored by Jan Hönig's avatar Jan Hönig
Browse files

WIP. TODO get rid of C++. Compile with C99

parent 12a86893
No related branches found
No related tags found
No related merge requests found
Showing
with 183 additions and 168 deletions
...@@ -37,6 +37,29 @@ def generate_benchmark(kernel_ast: KernelFunction, ...@@ -37,6 +37,29 @@ def generate_benchmark(kernel_ast: KernelFunction,
with open(src_path / 'main.c', 'w+') as f: with open(src_path / 'main.c', 'w+') as f:
f.write(kernel_main(kernel_ast)) f.write(kernel_main(kernel_ast))
copy_static_files(path)
def copy_static_files(path: Path) -> None:
src_path = path / 'src'
src_path.mkdir(parents=True, exist_ok=True)
include_path = path / 'include'
include_path.mkdir(parents=True, exist_ok=True)
env = Environment(loader=PackageLoader('pystencils_benchmark'), undefined=StrictUndefined)
files = ['Makefile', 'aligned_malloc.h', 'timing.h', 'timing.c', 'include_Clang.mk', 'include_GCC.mk',
'include_ICC.mk']
for file_name in files:
template = env.get_template(file_name).render()
if file_name[-1] == 'h':
target_path = include_path / file_name
elif file_name[-1] == 'c':
target_path = src_path / file_name
else:
target_path = path / file_name
with open(target_path, 'w+') as f:
f.write(template)
def kernel_main(kernel: KernelFunction, timing: bool = False): def kernel_main(kernel: KernelFunction, timing: bool = False):
""" """
...@@ -86,7 +109,7 @@ def kernel_main(kernel: KernelFunction, timing: bool = False): ...@@ -86,7 +109,7 @@ def kernel_main(kernel: KernelFunction, timing: bool = False):
fields.append((p.field_name, dtype, elements, size, 0, 0)) fields.append((p.field_name, dtype, elements, size, 0, 0))
call_parameters.append(p.field_name) call_parameters.append(p.field_name)
includes = f'#include "{kernel_name}"\n' includes = f'#include "{kernel_name}.h"\n'
jinja_context = { jinja_context = {
'kernel_code': generate_c(kernel, dialect=Backend.C), 'kernel_code': generate_c(kernel, dialect=Backend.C),
'kernel_name': kernel_name, 'kernel_name': kernel_name,
...@@ -104,8 +127,10 @@ def kernel_main(kernel: KernelFunction, timing: bool = False): ...@@ -104,8 +127,10 @@ def kernel_main(kernel: KernelFunction, timing: bool = False):
def kernel_header(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str: def kernel_header(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> str:
function_signature = generate_c(kernel_ast, dialect=dialect, signature_only=True) function_signature = generate_c(kernel_ast, dialect=dialect, signature_only=True)
header_guard = f'_{kernel_ast.function_name.upper()}_H'
jinja_context = { jinja_context = {
'header_guard': header_guard,
'function_signature': function_signature, 'function_signature': function_signature,
} }
...@@ -118,7 +143,11 @@ def kernel_source(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> s ...@@ -118,7 +143,11 @@ def kernel_source(kernel_ast: KernelFunction, dialect: Backend = Backend.C) -> s
kernel_name = kernel_ast.function_name kernel_name = kernel_ast.function_name
function_source = generate_c(kernel_ast, dialect=dialect) function_source = generate_c(kernel_ast, dialect=dialect)
headers = get_headers(kernel_ast) headers = get_headers(kernel_ast)
headers.add(f'"{kernel_name}.h"') # TODO remove this after pystencils update
if isinstance(headers, list):
headers.append(f'"{kernel_name}.h"')
else:
headers.add(f'"{kernel_name}.h"')
jinja_context = { jinja_context = {
'function_source': function_source, 'function_source': function_source,
......
void dummy(void* a) {}
int var_false = 0;
# mark_description "Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.0.074 Build 2013071";
# mark_description "6";
# mark_description "-std=c99 -S -xHost -O3 -fno-alias -restrict";
.file "dummy.c"
.section __TEXT, __text
L_TXTST0:
# -- Begin _dummy
# mark_begin;
.align 4
.globl _dummy
_dummy:
# parameter 1: %rdi
L_B1.1: # Preds L_B1.0
L____tag_value__dummy.1: #1.23
ret #1.24
.align 4
L____tag_value__dummy.3: #
# LOE
# mark_end;
.section __DATA, __data
# -- End _dummy
.section __DATA, __data
.globl _dummy.eh
// -- Begin SEGMENT __eh_frame
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
__eh_frame_seg:
L.__eh_frame_seg:
EH_frame0:
L_fde_cie_0:
.long 0x00000014
.long 0x00000000
.long 0x00527a01
.long 0x01107801
.long 0x08070c10
.long 0x01900190
_dummy.eh:
.long 0x0000001c
.long _dummy.eh-L_fde_cie_0+0x4
.quad L____tag_value__dummy.1-_dummy.eh-0x8
.set L_Qlab1,L____tag_value__dummy.3-L____tag_value__dummy.1
.quad L_Qlab1
.long 0x00000000
.long 0x00000000
# End
.subsections_via_symbols
TAG = GCC
#CONFIGURE BUILD SYSTEM
TARGET = benchmark-$(TAG)
BUILD_DIR = ./$(TAG)
SRC_DIR = ./src
MAKE_DIR = ./
Q ?= @
#DO NOT EDIT BELOW
include $(MAKE_DIR)/include_$(TAG).mk
INCLUDES += -I./include
VPATH = $(SRC_DIR)
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s,$(wildcard $(SRC_DIR)/*.c))
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o,$(wildcard $(SRC_DIR)/*.c))
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(INCLUDES)
${TARGET}: $(BUILD_DIR) $(OBJ)
@echo "===> LINKING $(TARGET)"
$(Q)${LINKER} ${LFLAGS} -o $(TARGET) $(OBJ) $(LIBS)
asm: $(BUILD_DIR) $(ASM)
$(BUILD_DIR)/%.o: %.c
@echo "===> COMPILE $@"
$(Q)$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
$(Q)$(CC) $(CPPFLAGS) -MT $(@:.d=.o) -MM $< > $(BUILD_DIR)/$*.d
$(BUILD_DIR)/%.s: %.c
@echo "===> GENERATE ASM $@"
$(Q)$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
tags:
@echo "===> GENERATE TAGS"
$(Q)ctags -R
$(BUILD_DIR):
@mkdir $(BUILD_DIR)
ifeq ($(findstring $(MAKECMDGOALS),clean),)
-include $(OBJ:.o=.d)
endif
.PHONY: clean distclean debug debug-clean debug-distclean
debug:
@make TAG=GCCdebug
debug-clean:
@make clean TAG=GCCdebug
debug-distclean:
@make distclean TAG=GCCdebug
clean:
@echo "===> CLEAN"
@rm -rf $(BUILD_DIR)
@rm -f tags
distclean: clean
@echo "===> DIST CLEAN"
@rm -f $(TARGET)
@rm -f tags
#ifndef _KERNCRAFT_H #ifndef _ALIGNED_MALLOC_H
#define _KERNCRAFT_H #define _ALIGNED_MALLOC_H
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#define swap(a, b) {void* t = a; a = b; b = t;}
inline void* aligned_malloc(size_t size, size_t align) { inline void* aligned_malloc(size_t size, size_t align) {
// Based on http://stackoverflow.com/q/16376942 // Based on http://stackoverflow.com/q/16376942
void *result; void *result;
......
CC = clang
CXX = clang++
FC = gfortran
LINKER = $(CXX)
ANSI_CFLAGS = -ansi
ANSI_CFLAGS += -std=c++0x
ANSI_CFLAGS += -pedantic
ANSI_CFLAGS += -Wextra
CFLAGS = -O3 -Wno-format -Wall $(ANSI_CFLAGS)
# More warning pls
CFLAGS += -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wwrite-strings -Waggregate-return
# Maybe too much warnings
CFLAGS += -Wcast-qual -Wswitch-default -Wconversion -Wunreachable-code
CXXFLAGS := $(CFLAGS)
# Specific C flags
CFLAGS := $(CFLAGS) -Wstrict-prototypes
FCFLAGS =
CPPFLAGS = -std=c++0x
LFLAGS = #-fopenmp
DEFINES = -D_GNU_SOURCE -DNDEBUG
INCLUDES = #-I/usr/lib/gcc/x86_64-linux-gnu/4.8/include/
LIBS = #-L/usr/lib/x86_64-linux-gnu/libomp.so.5
CC = gcc
CXX = g++
FC = gfortran
LINKER = $(CXX)
ANSI_CFLAGS = -ansi
ANSI_CFLAGS += -std=c++0x
ANSI_CFLAGS += -pedantic
ANSI_CFLAGS += -Wextra
CFLAGS = -O3 -Wno-format -Wall $(ANSI_CFLAGS) -fopenmp
# More warning pls
#CFLAGS += -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wwrite-strings -Waggregate-return
# Maybe too much warnings
#CFLAGS += -Wcast-qual -Wswitch-default -Wconversion -Wunreachable-code
CXXFLAGS := $(CFLAGS)
# Specific C flags
CFLAGS := $(CFLAGS) -Wstrict-prototypes
FCFLAGS =
CPPFLAGS = -std=c++0x
LFLAGS = -fopenmp
DEFINES = -D_GNU_SOURCE -DNDEBUG
INCLUDES =
LIBS =
CC = icc
CXX = icpc
FC = ifort
LINKER = $(FC)
CFLAGS = -O3 -std=c99 -xHost
CXXFLAGS = $(CFLAGS)
FCFLAGS = -O3 -xHost
LFLAGS =
DEFINES = -D_GNU_SOURCE
INCLUDES =
LIBS =
#ifndef {{header_guard}}
#define {{header_guard}}
#define RESTRICT __restrict__ #define RESTRICT __restrict__
#define FUNC_PREFIX #define FUNC_PREFIX
{{function_signature}}; {{function_signature}};
#endif
#include "kerncraft.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
{{ includes }}
#define RESTRICT __restrict__
#define FUNC_PREFIX
void dummy(void *);
void timing(double* wcTime, double* cpuTime);
extern int var_false;
{{kernel_code}}
\ No newline at end of file
#define FUNC_PREFIX
{{function_signature}}
\ No newline at end of file
#include "kerncraft.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -10,47 +9,7 @@ ...@@ -10,47 +9,7 @@
#define RESTRICT __restrict__ #define RESTRICT __restrict__
#define FUNC_PREFIX #define FUNC_PREFIX
void dummy(void *);
void timing(double* wcTime, double* cpuTime); void timing(double* wcTime, double* cpuTime);
extern int var_false;
/* see waLBerla src/field/allocation/AlignedMalloc */
void *aligned_malloc_with_offset( uint64_t size, uint64_t alignment, uint64_t offset )
{
// With 0 alignment this function makes no sense
// use normal malloc instead
assert( alignment > 0 );
// Tests if alignment is power of two (assuming alignment>0)
assert( !(alignment & (alignment - 1)) );
assert( offset < alignment );
void *pa; // pointer to allocated memory
void *ptr; // pointer to usable aligned memory
pa=std::malloc( (size+2*alignment-1 )+sizeof(void *));
if(!pa)
return nullptr;
// Find next aligned position, starting at pa+sizeof(void*)-1
ptr=(void*)( ((size_t)pa+sizeof(void *)+alignment-1) & ~(alignment-1));
ptr=(void*) ( (char*)(ptr) + alignment - offset);
// Store pointer to real allocated chunk just before usable chunk
*((void **)ptr-1)=pa;
assert( ((size_t)ptr+offset) % alignment == 0 );
return ptr;
}
void aligned_free( void *ptr )
{
// assume that pointer to real allocated chunk is stored just before
// chunk that was given to user
if(ptr)
std::free(*((void **)ptr-1));
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
...@@ -63,24 +22,12 @@ int main(int argc, char **argv) ...@@ -63,24 +22,12 @@ int main(int argc, char **argv)
{%- endif %} {%- endif %}
for (unsigned long long i = 0; i < {{elements}}; ++i) for (unsigned long long i = 0; i < {{elements}}; ++i)
{{field_name}}[i] = 0.23; {{field_name}}[i] = 0.23;
if(var_false)
{
dummy({{field_name}});
}
{%- endfor %} {%- endfor %}
{%- for constantName, dataType in constants %} {%- for constantName, dataType in constants %}
// Constant {{constantName}} // Constant {{constantName}}
{{dataType}} {{constantName}}; {{dataType}} {{constantName}};
{{constantName}} = 0.23; {{constantName}} = 0.23;
if(var_false)
dummy(& {{constantName}});
{%- endfor %} {%- endfor %}
for(int warmup = 1; warmup >= 0; --warmup) { for(int warmup = 1; warmup >= 0; --warmup) {
...@@ -97,14 +44,6 @@ int main(int argc, char **argv) ...@@ -97,14 +44,6 @@ int main(int argc, char **argv)
for (; repeat > 0; --repeat) for (; repeat > 0; --repeat)
{ {
{{kernel_name}}({{call_argument_list}}); {{kernel_name}}({{call_argument_list}});
// Dummy calls
{%- for field_name, dataType, elements, size, offset, alignment in fields %}
if(var_false) dummy((void*){{field_name}});
{%- endfor %}
{%- for constantName, dataType in constants %}
if(var_false) dummy((void*)&{{constantName}});
{%- endfor %}
} }
{%- if timing %} {%- if timing %}
timing(&wcEndTime, &cpuEndTime); timing(&wcEndTime, &cpuEndTime);
...@@ -115,11 +54,6 @@ int main(int argc, char **argv) ...@@ -115,11 +54,6 @@ int main(int argc, char **argv)
} }
{%- for field_name, dataType, elements, size, offset, alignment in fields %} {%- for field_name, dataType, elements, size, offset, alignment in fields %}
{%- if alignment > 0 %}
aligned_free({{field_name}});
{%- else %}
delete[] {{field_name}}; delete[] {{field_name}};
{%- endif %}
{%- endfor %} {%- endfor %}
} }
#ifndef _TIMING_H
#define _TIMING_H
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/resource.h> #include <sys/resource.h>
void timing(double* wcTime, double* cpuTime); void timing(double* wcTime, double* cpuTime);
#endif
\ No newline at end of file
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
import pystencils as ps import pystencils as ps
from pystencils_benchmark import kernel_header, kernel_source, generate_benchmark from pystencils_benchmark import generate_benchmark
from pathlib import Path from pathlib import Path
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
config = ps.CreateKernelConfig() config = ps.CreateKernelConfig(function_name='vadd')
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
a, b, c = ps.fields(a=np.ones(4000000), b=np.ones(4000000), c=np.ones(4000000)) a, b, c = ps.fields(a=np.ones(4000000), b=np.ones(4000000), c=np.ones(4000000))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
@ps.kernel @ps.kernel
def vadd(): def vadd():
a[0] @= b[0] + c[0] a[0] @= b[0] + c[0]
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
kernel_vadd = ps.create_kernel(vadd, config=config) kernel_vadd = ps.create_kernel(vadd, config=config)
ps.show_code(kernel_vadd) ps.show_code(kernel_vadd)
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
example_path = Path.cwd() / 'example' example_path = Path.cwd() / 'example'
generate_benchmark(kernel_vadd, example_path) generate_benchmark(kernel_vadd, example_path)
``` ```
%% Output
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_28091/2748650420.py in <module>
1 example_path = Path.cwd() / 'example'
----> 2 generate_benchmark(kernel_vadd, example_path)
~/git/pystencils-benchmark/pystencils_benchmark/benchmark.py in generate_benchmark(kernel_ast, path, dialect)
31 f.write(header)
32
---> 33 source = kernel_source(kernel_ast, dialect)
34 with open(src_path / f'{kernel_name}.c', 'w+') as f:
35 f.write(source)
~/git/pystencils-benchmark/pystencils_benchmark/benchmark.py in kernel_source(kernel_ast, dialect)
119 function_source = generate_c(kernel_ast, dialect=dialect)
120 headers = get_headers(kernel_ast)
--> 121 headers.add(f'"{kernel_name}.h"')
122
123 jinja_context = {
AttributeError: 'list' object has no attribute 'add'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment